Created
November 14, 2012 18:19
-
-
Save chrisjdavis/4073806 to your computer and use it in GitHub Desktop.
Complete Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Conversations extends Plugin | |
{ | |
const APP_ID = 'YOUR_APP_ID'; | |
const API_KEY = 'YOUR API_KEY'; | |
const API_SECRET = 'YOUR_API_SEKRET'; | |
public function action_init() { | |
DB::register_table( 'quickchats' ); | |
DB::register_table( 'quickchat_lines' ); | |
} | |
public function action_plugin_activation() { | |
Post::add_new_type( 'quickchat' ); | |
$this->create_quickchat_table(); | |
$this->create_quickchat_lines_table(); | |
} | |
public function action_plugin_deactivation () { | |
Post::deactivate_post_type( 'quickchat' ); | |
} | |
private function create_quickchat_table() { | |
$sql = "CREATE TABLE {\$prefix}quickchats ( | |
id int unsigned NOT NULL AUTO_INCREMENT, | |
post_id int unsigned NOT NULL, | |
sender_id int unsigned NOT NULL, | |
receiver_id int unsigned NOT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `post_id` (`post_id`), | |
KEY `sender_id` (`sender_id`), | |
KEY `receiver_id` (`receiver_id`) | |
) DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;"; | |
DB::dbdelta($sql); | |
} | |
private function create_quickchat_lines_table() { | |
$sql = "CREATE TABLE {\$prefix}quickchat_lines ( | |
id int unsigned NOT NULL AUTO_INCREMENT, | |
chat_id int unsigned NOT NULL, | |
sender_id int unsigned NOT NULL, | |
receiver_id int unsigned NOT NULL, | |
message TEXT NULL, | |
date VARCHAR(255) NULL, | |
PRIMARY KEY (`id`), | |
KEY `sender_id` (`sender_id`), | |
KEY `receiver_id` (`receiver_id`), | |
KEY `chat_id` (`chat_id`) | |
) DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;"; | |
DB::dbdelta($sql); | |
} | |
public function filter_posts_get_paramarray($paramarray) { | |
$queried_types = Posts::extract_param($paramarray, 'content_type'); | |
if($queried_types && in_array('quickchat', $queried_types)) { | |
$paramarray['post_join'][] = '{quickchats}'; | |
$default_fields = isset($paramarray['default_fields']) ? | |
$paramarray['default_fields'] : array(); | |
$default_fields['{quickchats}.sender_id'] = ''; | |
$default_fields['{quickchats}.receiver_id'] = ''; | |
$paramarray['default_fields'] = $default_fields; | |
} | |
return $paramarray; | |
} | |
public function filter_post_schema_map_quickchat($schema, $post) { | |
$schema['quickchats'] = $schema['*']; | |
$schema['quickchats']['post_id'] = '*id'; | |
return $schema; | |
} | |
private function bootstrap() { | |
return new Pusher( self::API_KEY, self::API_SECRET, self::APP_ID ); | |
} | |
public function action_auth_ajax_chat_update($data) { | |
$push = array(); | |
$text = $data->handler_vars['message']; | |
$room = $data->handler_vars['location']; | |
$message = '' . Gravatar::get( User::identify()->email ) . ''; | |
$message .= '' . $text .''; | |
$message .= ''; | |
$vars = array( | |
'user_id' => User::identify()->id, | |
'location' => $room, | |
'message' => $message, | |
'date' => HabariDateTime::date_create( date(DATE_RFC822) ), | |
); | |
DB::insert( DB::table( 'chat_activity' ), $vars ); | |
$rec = DB::last_insert_id(); | |
$return = '<li id="' . $room . '-' . $rec . '">'; | |
$return .= $message; | |
$return .= ''; | |
$pusher = $this->bootstrap(); | |
$pusher->trigger( 'chat', $location, $return ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment