Skip to content

Instantly share code, notes, and snippets.

@chrisjdavis
Created October 22, 2012 23:16
Show Gist options
  • Save chrisjdavis/3935377 to your computer and use it in GitHub Desktop.
Save chrisjdavis/3935377 to your computer and use it in GitHub Desktop.
Conversations Plugin: Phase 1
<?php
class Conversations extends Plugin
{
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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment