private function install() { global $wpdb; $sql = array(); $charset_collate = ''; //check if tables exist, //just update one table in that case require_once ABSPATH . 'wp-admin/includes/upgrade.php'; /* BuddyPress component DB schema */ if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } $bp_prefix = bp_core_get_table_prefix(); $bp_chat = bp_chat(); //install $sql_table_exists_check = "SHOW TABLES LIKE '" . $bp_prefix . "_bp_chat_%'"; if ($wpdb->get_col($sql_table_exists_check)) { ////just update the user table $sql[] = "ALTER TABLE {$bp_chat->table_name_users} MODIFY last_active_time DATETIME "; } else { //if tables do not exist $sql[] = "CREATE TABLE {$bp_chat->table_name_users} (\r\n\t\t\t\t\t\tuser_id bigint(20) NOT NULL,\r\n\t\t\t\t\t\tis_online tinyint(4) NOT NULL,\r\n\t\t\t\t\t\tlast_active_time datetime NOT NULL,\r\n\t\t\t\t\t\tstatus_message varchar(255) NOT NULL,\r\n\t\t\t\t\t\tlast_fetch_time datetime NOT NULL ,\r\n\t\t\t\t\t\tfriends_only tinyint(1) NOT NULL DEFAULT '0',\r\n\t\t\t\t\t\tPRIMARY KEY (user_id)\r\n\t\t\t\t\t ) {$charset_collate};"; $sql[] = "CREATE TABLE {$bp_chat->table_name_channels} (\r\n\t\t\t\t\t\tid bigint(20) unsigned NOT NULL AUTO_INCREMENT,\r\n\t\t\t\t\t\tlast_message_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\r\n\t\t\t\t\t\ttime_created datetime NOT NULL,\r\n\t\t\t\t\t\tstatus tinyint(3) unsigned NOT NULL,\r\n\t\t\t\t\t\tis_multichat tinyint(3) NOT NULL,\r\n\t\t\t\t\t\tis_open tinyint(4) NOT NULL,\r\n\t\t\t\t\t\tPRIMARY KEY (id)\r\n\t\t\t\t\t ) {$charset_collate};"; $sql[] = "CREATE TABLE {$bp_chat->table_name_channel_users} (\r\n\t\t\t\t\t\tchannel_id bigint(20) NOT NULL,\r\n\t\t\t\t\t\tuser_id bigint(20) NOT NULL,\r\n\t\t\t\t\t\tstatus varchar(32) NOT NULL,\r\n\t\t\t\t\t\thas_initiated tinyint(4) NOT NULL,\r\n\t\t\t\t\t\tKEY channel_id (channel_id,user_id)\r\n\t\t\t\t ) {$charset_collate};"; //meta data $sql[] = "CREATE TABLE {$bp_chat->table_name_messages} (\r\n\t\t\t\t\t\t\tid bigint(20) NOT NULL AUTO_INCREMENT,\r\n\t\t\t\t\t\t\tsender_id bigint(20) NOT NULL,\r\n\t\t\t\t\t\t\tchannel_id bigint(20) NOT NULL,\r\n\t\t\t\t\t\t\tmessage text NOT NULL,\r\n\t\t\t\t\t\t\tsent_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\r\n\t\t\t\t\t\t\tPRIMARY KEY (id),\r\n\t\t\t\t\t\t\tKEY channel_id (channel_id)\r\n\t\t\t\t\t ) {$charset_collate};"; } //execute sql dbDelta($sql); update_site_option('bp-chat-db-version', BP_CHAT_DB_VERSION); }
public static function get_messages_after_time($channel_id, $time) { global $wpdb; $bpchat = bp_chat(); $sql = "SELECT * FROM {$bpchat->table_name_messages} msg WHERE msg.channel_id = %d and msg.sent_at > FROM_UNIXTIME({$time})"; $msgs = $wpdb->get_results($wpdb->prepare($sql, $channel_id)); return $msgs; ///return array of message objects }
public function load_soundmanager_js() { if (!is_user_logged_in() || bpchat_is_disabled()) { //allow to disable for mobile browsers return; //do not bother if the user is not logged in } ?> <script type="text/javascript"> bpchat = {}; bpchat.plugin_url = "<?php echo bp_chat()->get_url(); ?> "; bpchat.current_user_id = "<?php echo get_current_user_id(); ?> "; bpchat.sound_notification_enabled = "<?php echo bpchat_is_notification_sound_enabled(get_current_user_id()); ?> "; <?php if (bpchat_is_notification_sound_enabled(get_current_user_id())) { ?> soundManager.url = bpchat.plugin_url+"assets/vendors/soundmanager/swf/soundmanager2.swf"; // directory where SM2 .SWFs live soundManager.debugMode = false; //in future will have volume control feature, currently allow site admin to set it via the php soundManager.defaultOptions.volume = <?php echo bpchat_get_notification_volume(get_current_user_id()); ?> ; // soundManager.useFlashBlock = false; <?php } ?> </script> <?php }
/** * Get the ids of other users connected to this channel * @global type $wpdb * @param type $channel_id * @return type */ function bpchat_get_other_party_ids($channel_id) { global $wpdb; $bpchat = bp_chat(); $user_id = get_current_user_id(); $query = "SELECT o.user_id FROM {$bpchat->table_name_channel_users} i, {$bpchat->table_name_channel_users} o where o.channel_id=i.channel_id AND i.user_id=%d"; $ids = $wpdb->get_results($wpdb->prepare($query, $user_id)); return $ids; }
/** check for the new chat requests, list which which we are chatting currently or the messages we have recieved for the user */ public function get_updates_for_user() { global $bp, $wpdb; $user_id = get_current_user_id(); $last_fetch_time = $_POST["fetch_time"]; //$time=gmdate("Y-m-d H:i:s", time()); $bpchat = bp_chat(); // $query = "SELECT msg.id,msg.channel_id, msg.message, msg.sender_id,msg.message,msg.sent_at FROM {$bp->chat->table_chat_messages} msg, WHERE msg.channel_id IN( SELECT channel_id FROM {$bp->chat->table_channel_users} where user_id=%d and status <> 'closed') and msg.sent_at >= '".$last_fetch_time."' ORDER BY msg.sent_at ASC "; $query = "SELECT msg.id,msg.channel_id, msg.message, msg.sender_id,msg.message,msg.sent_at FROM {$bpchat->table_name_messages} msg WHERE msg.channel_id IN( SELECT channel_id FROM {$bpchat->table_name_channel_users} where user_id=%d and status <> 'closed') and msg.sent_at >= '" . $last_fetch_time . "' ORDER BY msg.sent_at ASC "; $q = $wpdb->prepare($query, $user_id); $messages = $wpdb->get_results($q); //array of message objects $time = current_time('timestamp'); $messages = bpchat_extend_messages($messages); $query_status = "SELECT c.channel_id,c.status, c.user_id,u.is_online,IF (DATE_ADD( u.last_active_time, INTERVAL 30 SECOND ) >= NOW(), 'active','idle') as user_status FROM {$bpchat->table_name_channel_users} c,{$bpchat->table_name_users} u WHERE c.channel_id IN( SELECT channel_id FROM {$bpchat->table_name_channel_users} where user_id=%d and status <> 'closed') AND c.user_id!=%d and u.user_id=c.user_id ORDER BY channel_id DESC "; $status = $wpdb->get_results($wpdb->prepare($query_status, $user_id, $user_id)); //update last fetch time for user bpchat_update_fetch_time($user_id); //update the fetch time $response = array("messages" => $messages, "fetch_time" => $time, "status" => $status); echo json_encode($response); exit(0); }
do_action('bpchat_loaded'); } /** * URL to the bp-chat plugin directory with trailing slash * * @return string url */ public function get_url() { return $this->url; } /** * File system absolute path to the bp chat plugin directory with trailing slash * @return string */ public function get_path() { return $this->path; } } /** * Singleton instance * * @return BP_Chat_Helper */ function bp_chat() { return BP_Chat_Helper::get_instance(); } bp_chat();
public static function keep_channels_open($channels, $user_id) { global $wpdb; if (empty($channels)) { return; } $bpchat = bp_chat(); $channel_list = '(' . join(',', $channels) . ')'; $query = "UPDATE {$bpchat->table_name_channel_users} SET status='open' WHERE channel_id IN {$channel_list} AND user_id=%d"; $wpdb->query($wpdb->prepare($query, $user_id)); }