Skip to content

Commit

Permalink
Use custom tables, new db-helper, modified install tables
Browse files Browse the repository at this point in the history
  • Loading branch information
shadyvb committed Nov 26, 2013
1 parent b652c6b commit 5b809ce
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 83 deletions.
9 changes: 4 additions & 5 deletions classes/context.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ public static function action_links( $links, $stream_id, $object_id ) {
* @param array $contexts Contexts of the action
* @return void
*/
public static function log( $message, $args, $object_id, $action, $user_id = null, array $contexts = array() ) {
return WP_Stream_Log::$instance->log(
public static function log( $message, $args, $object_id, $contexts, $user_id = null ) {
return WP_Stream_Log::get_instance()->log(
get_called_class(),
$message,
$args,
$object_id,
$action,
$user_id,
$contexts
$contexts,
$user_id
);
}

Expand Down
25 changes: 20 additions & 5 deletions contexts/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public static function get_label() {
}

/**
* Return translated action term labels
* Return translated action labels
*
* @return array Action terms label translation
* @return array Action label translations
*/
public static function get_action_term_labels() {
public static function get_action_labels() {
return array(
'updated' => __( 'Updated', 'stream' ),
'created' => __( 'Created', 'stream' ),
Expand All @@ -40,6 +40,17 @@ public static function get_action_term_labels() {
);
}

/**
* Return translated context labels
*
* @return array Context label translations
*/
public static function get_context_labels() {
return array(
'posts' => __( 'Posts', 'stream' ),
);
}

/**
* Add action links to Stream drop row in admin list screen
*
Expand Down Expand Up @@ -140,7 +151,9 @@ public static function callback_transition_post_status( $new, $old, $post ) {
'revision_id' => $revision_id,
),
$post->ID,
$action
array(
'posts' => $action,
)
);
}

Expand All @@ -157,7 +170,9 @@ public static function callback_deleted_post( $post_id ) {
'post_title' => $post->post_title,
),
$post->ID,
'deleted'
array(
'posts' => 'deleted',
)
);
}

Expand Down
38 changes: 30 additions & 8 deletions contexts/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function get_label() {
*
* @return array Action terms label translation
*/
public static function get_action_term_labels() {
public static function get_action_labels() {
return array(
'updated' => __( 'Updated', 'stream' ),
'created' => __( 'Created', 'stream' ),
Expand All @@ -47,6 +47,17 @@ public static function get_action_term_labels() {
);
}

/**
* Return translated context labels
*
* @return array Context label translations
*/
public static function get_context_labels() {
return array(
'users' => __( 'Users', 'stream' ),
);
}

/**
* Add action links to Stream drop row in admin list screen
*
Expand Down Expand Up @@ -74,7 +85,9 @@ public static function callback_user_register( $user_id ) {
'email' => $user->email,
),
$user->ID,
'created',
array(
'users' => 'created',
),
$user->ID
);
}
Expand All @@ -91,8 +104,9 @@ public static function callback_profile_update( $user_id, $user ) {
'display_name' => $user->display_name,
),
$user->ID,
'updated',
$user->ID
array(
'users' => 'updated',
)
);
}

Expand All @@ -108,7 +122,9 @@ public static function callback_password_reset( $user ) {
'email' => $user->display_name,
),
$user->ID,
'password-reset',
array(
'users' => 'password-reset',
),
$user->ID
);
}
Expand All @@ -130,7 +146,9 @@ public static function callback_retrieve_password( $user_login ) {
'display_name' => $user->display_name,
),
$user->ID,
'forgot-password',
array(
'users' => 'forgot-password',
),
$user->ID
);
}
Expand All @@ -147,7 +165,9 @@ public static function callback_wp_login( $user_login, $user ) {
'display_name' => $user->display_name,
),
$user->ID,
'login',
array(
'users' => 'login',
),
$user->ID
);
}
Expand All @@ -165,7 +185,9 @@ public static function callback_clear_auth_cookie() {
'display_name' => $user->display_name,
),
$user->ID,
'logout',
array(
'users' => 'logout',
),
$user->ID
);
}
Expand Down
9 changes: 6 additions & 3 deletions includes/contexts.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ public static function load() {
$context::register();

// Add new terms to our label lookup array
self::$term_labels['stream_action'] = array_merge(
self::$term_labels['stream_action'] = array_merge(
self::$term_labels['stream_action'],
$context::get_action_term_labels()
$context::get_action_labels()
);
self::$term_labels['stream_context'] = array_merge(
self::$term_labels['stream_context'],
$context::get_context_labels()
);
self::$term_labels['stream_context'][$context::$name] = $context::get_label();
}

// Filter taxonomy names to use translated labels
Expand Down
112 changes: 112 additions & 0 deletions includes/db-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

class WP_Stream_DB {

public static $instance;

public $table;

public $table_meta;

public $table_context;

public function __construct() {
global $wpdb;
// Allow devs to alter the tables prefix, default to base_prefix
$prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix );
$this->table = $prefix . 'stream';
$this->table_meta = $prefix . 'stream_meta';
$this->table_context = $prefix . 'stream_context';

This comment has been minimized.

Copy link
@frankiejarrett

frankiejarrett Nov 27, 2013

Contributor

@shadyvb Why stream_context instead of stream_taxonomy? Seems confusing to me since there is a context column in this table too.

This comment has been minimized.

Copy link
@shadyvb

shadyvb Dec 5, 2013

Author Contributor

@fjarrett But i think that people would confuse it with the taxonomy architecture of WP ( having taxonomies AND terms ), while we only have 'terms' now ( ie: Posts, Widgets ) assigned to fixed taxonomies ( connector, context, action ). So it is more like wp_terms not wp_term_taxonomy, hence choosing the _context suffix.

}

public static function get_instance() {
if ( ! self::$instance ) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}

public function insert( $recordarr ) {
global $wpdb;

$recordarr = apply_filters( 'wp_stream_record_array', $recordarr );

// Allow extensions to handle the saving process
if ( empty( $recordarr ) ) {
return;
}

$fields = array( 'author', 'created', 'summary', 'parent', 'status' );
$data = array_intersect_key( $recordarr, array_flip( $fields ) );

$data = array_filter( $data );

// TODO Check/Validate *required* fields

$result = $wpdb->insert(
$this->table,
$data
);

if ( $result == 1 ) {
$record_id = $wpdb->insert_id;
}
else {
do_action( 'wp_stream_post_insert_error', $record_id );
return $record_id;
}


self::$instance->prev_record = $record_id;

$connector = $recordarr['connector'];

foreach ( (array) $recordarr['contexts'] as $context => $action ) {
$this->insert_context( $record_id, $connector, $context, $action );
}

foreach ( $recordarr['meta'] as $key => $vals ) {
foreach ( (array) $vals as $val ) {
$this->insert_meta( $record_id, $key, $val );
}
}

do_action( 'wp_stream_post_inserted', $record_id, $recordarr );

return $record_id;
}

public function insert_context( $record_id, $connector, $context, $action ) {
global $wpdb;

$result = $wpdb->insert(
$this->table_context,
array(
'record_id' => $record_id,
'connector' => $connector,
'context' => $context,
'action' => $action,
)
);

return $result;
}

public function insert_meta( $record_id, $key, $val ) {
global $wpdb;

$result = $wpdb->insert(
$this->table_meta,
array(
'record_id' => $record_id,
'meta_key' => $key,
'meta_value' => $val,
)
);

return $result;
}


}
31 changes: 19 additions & 12 deletions includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

class WP_Stream_Install {

public static $table_prefix;

/**
* Check db version, create/update table schema accordingly
*
* @return void
*/
public static function check() {
global $wpdb;

$current = self::get_version();

$db_version = get_option( plugin_basename( WP_STREAM_DIR ) . '_db' );

self::$table_prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->prefix );

if ( empty( $db_version ) ) {
self::install();
}
Expand Down Expand Up @@ -41,25 +46,27 @@ public static function install() {
global $wpdb;
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

$sql = "CREATE TABLE {$wpdb->base_prefix}stream (
$prefix = self::$table_prefix;

$sql = "CREATE TABLE {$prefix}stream (
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
site_id bigint(20) unsigned NOT NULL DEFAULT '1',
record_author bigint(20) unsigned NOT NULL DEFAULT '0',
record_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
record_summary longtext NOT NULL,
record_visibility varchar(20) NOT NULL DEFAULT 'publish',
record_parent bigint(20) unsigned NOT NULL DEFAULT '0',
record_type varchar(20) NOT NULL DEFAULT 'stream',
author bigint(20) unsigned NOT NULL DEFAULT '0',
summary longtext NOT NULL,
visibility varchar(20) NOT NULL DEFAULT 'publish',
parent bigint(20) unsigned NOT NULL DEFAULT '0',
type varchar(20) NOT NULL DEFAULT 'stream',
created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (ID),
KEY site_id (site_id),
KEY record_parent (record_parent),
KEY record_author (record_author),
KEY record_date (record_date)
KEY parent (parent),
KEY author (author),
KEY created (created)
);";

dbDelta( $sql );

$sql = "CREATE TABLE {$wpdb->base_prefix}stream_tax (
$sql = "CREATE TABLE {$prefix}stream_context (
meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
record_id bigint(20) unsigned NOT NULL,
context varchar(100) NOT NULL,
Expand All @@ -73,7 +80,7 @@ public static function install() {

dbDelta( $sql );

$sql = "CREATE TABLE {$wpdb->base_prefix}stream_meta (
$sql = "CREATE TABLE {$prefix}stream_meta (
meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
record_id bigint(20) unsigned NOT NULL,
meta_key varchar(200) NOT NULL,
Expand Down
Loading

0 comments on commit 5b809ce

Please sign in to comment.