diff --git a/classes/context.php b/classes/context.php index 6dd7f7328..8a99297cb 100644 --- a/classes/context.php +++ b/classes/context.php @@ -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 ); } diff --git a/contexts/post.php b/contexts/post.php index 5b3c853f3..8d0a793f0 100644 --- a/contexts/post.php +++ b/contexts/post.php @@ -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' ), @@ -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 * @@ -140,7 +151,9 @@ public static function callback_transition_post_status( $new, $old, $post ) { 'revision_id' => $revision_id, ), $post->ID, - $action + array( + 'posts' => $action, + ) ); } @@ -157,7 +170,9 @@ public static function callback_deleted_post( $post_id ) { 'post_title' => $post->post_title, ), $post->ID, - 'deleted' + array( + 'posts' => 'deleted', + ) ); } diff --git a/contexts/user.php b/contexts/user.php index 69186ddbe..40c1db9e7 100644 --- a/contexts/user.php +++ b/contexts/user.php @@ -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' ), @@ -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 * @@ -74,7 +85,9 @@ public static function callback_user_register( $user_id ) { 'email' => $user->email, ), $user->ID, - 'created', + array( + 'users' => 'created', + ), $user->ID ); } @@ -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', + ) ); } @@ -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 ); } @@ -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 ); } @@ -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 ); } @@ -165,7 +185,9 @@ public static function callback_clear_auth_cookie() { 'display_name' => $user->display_name, ), $user->ID, - 'logout', + array( + 'users' => 'logout', + ), $user->ID ); } diff --git a/includes/contexts.php b/includes/contexts.php index dce883a9d..dce95b045 100644 --- a/includes/contexts.php +++ b/includes/contexts.php @@ -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 diff --git a/includes/db-actions.php b/includes/db-actions.php new file mode 100644 index 000000000..fe072b76a --- /dev/null +++ b/includes/db-actions.php @@ -0,0 +1,112 @@ +base_prefix ); + $this->table = $prefix . 'stream'; + $this->table_meta = $prefix . 'stream_meta'; + $this->table_context = $prefix . 'stream_context'; + } + + 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; + } + + +} \ No newline at end of file diff --git a/includes/install.php b/includes/install.php index 65abcd66a..30b58151a 100644 --- a/includes/install.php +++ b/includes/install.php @@ -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(); } @@ -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, @@ -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, diff --git a/includes/log.php b/includes/log.php index 9e1622ed5..2c04d6d8e 100644 --- a/includes/log.php +++ b/includes/log.php @@ -12,7 +12,7 @@ class WP_Stream_Log { * Previous Stream record ID, used for chaining same-session records * @var int */ - public $prev_stream; + public $prev_record; /** * Load log handler class, filterable by extensions @@ -24,6 +24,18 @@ public static function load() { self::$instance = new $log_handler; } + /** + * Return active instance of this class + * @return WP_Stream_Log + */ + public static function get_instance() { + if ( ! self::$instance ) { + $class = __CLASS__; + self::$instance = new $class; + } + return self::$instance; + } + /** * Log handler * @param string $message sprintf-ready error message string @@ -34,64 +46,30 @@ public static function load() { * @param array $contexts Contexts of the action * @return void */ - public function log( $connector, $message, $args, $object_id, $action, $user_id = null, array $contexts = array() ) { + public function log( $connector, $message, $args, $object_id, $contexts, $user_id = null ) { if ( is_null( $user_id ) ) { $user_id = get_current_user_id(); } - // Allow extensions to define more contexts - $contexts[] = $connector::$name; - - $arg_keys = array_keys( $args ); - foreach ( $arg_keys as $i => $key ) { - $arg_keys[$i] = '_arg_' . $key; - } - $args = array_combine( $arg_keys, $args ); - - $postarr = array( - 'post_type' => 'stream', - 'post_status' => 'publish', - 'post_title' => vsprintf( $message, $args ), - 'post_author' => $user_id, - 'post_parent' => self::$instance->prev_stream, - 'post_tax' => array( // tax_input uses current_user_can which fails on user context! - 'stream_context' => $contexts, - 'stream_action' => $action, - ), - 'post_meta' => array_merge( + $recordarr = array( + 'author' => $user_id, + 'created' => current_time( 'mysql' ), // TODO: use GMT (get_gmt_from_date) + 'summary' => vsprintf( $message, $args ), + 'parent' => self::$instance->prev_record, + 'connector' => $connector, + 'contexts' => $contexts, + 'meta' => array_merge( $args, array( - '_object_id' => $object_id, - '_ip_address' => filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP ), + 'object_id' => $object_id, + 'ip_address' => filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP ), ) ), ); - $post_id = wp_insert_post( - apply_filters( - 'wp_stream_post_array', - $postarr - ) - ); - - if ( is_a( $post_id, 'WP_Error' ) ) { - // TODO:: Log error - do_action( 'wp_stream_post_insert_error', $post_id, $postarr ); - } else { - self::$instance->prev_stream = $post_id; - - foreach ( $postarr['post_meta'] as $key => $vals ) { - foreach ( (array) $vals as $val ) { - add_post_meta( $post_id, $key, $val ); - } - } - - foreach ( $postarr['post_tax'] as $key => $vals ) { - wp_set_post_terms( $post_id, (array) $vals, $key ); - } - - do_action( 'wp_stream_post_inserted', $post_id, $postarr ); - } + $record_id = WP_Stream_DB::get_instance()->insert( $recordarr ); + + return $record_id; } - + } \ No newline at end of file diff --git a/stream.php b/stream.php index bb4a3546d..6726782d2 100644 --- a/stream.php +++ b/stream.php @@ -64,6 +64,9 @@ public function __construct() { // Load admin scripts and styles add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); + // Load DB helper class + require_once WP_STREAM_INC_DIR . 'db-actions.php'; + } /**