Skip to content

Commit

Permalink
Merge pull request #1 from wp-stream/connectors
Browse files Browse the repository at this point in the history
Connectors integration
  • Loading branch information
frankiejarrett committed Jul 18, 2014
2 parents 8a6530e + da69954 commit bbf37d4
Show file tree
Hide file tree
Showing 23 changed files with 4,296 additions and 23 deletions.
53 changes: 47 additions & 6 deletions classes/class-wp-stream-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function action_links( $links, $record ) {
* @param int $user_id User responsible for the event
*
* @internal param string $action Action performed (stream_action)
* @return void
* @return bool
*/
public static function log( $message, $args, $object_id, $context, $action, $user_id = null ) {
$class = get_called_class();
Expand All @@ -100,7 +100,7 @@ public static function log( $message, $args, $object_id, $context, $action, $use
);

if ( ! $data ) {
return;
return false;
} else {
$connector = $data['connector'];
$message = $data['message'];
Expand Down Expand Up @@ -146,11 +146,14 @@ public static function delayed_log_commit() {

/**
* Compare two values and return changed keys if they are arrays
* @param mixed $old_value Value before change
* @param mixed $new_value Value after change
*
* @param mixed $old_value Value before change
* @param mixed $new_value Value after change
* @param bool|int $deep Get array children changes keys as well, not just parents
*
* @return array
*/
public static function get_changed_keys( $old_value, $new_value ) {
public static function get_changed_keys( $old_value, $new_value, $deep = false ) {
if ( ! is_array( $old_value ) && ! is_array( $new_value ) ) {
return array();
}
Expand Down Expand Up @@ -188,7 +191,45 @@ function( $value ) {
}
);

return array_values( array_unique( $result ) );
$result = array_values( array_unique( $result ) );

if ( false === $deep ) {
return $result; // Return an numerical based array with changed TOP PARENT keys only
}

$result = array_fill_keys( $result, null );

foreach ( $result as $key => $val ) {
if ( in_array( $key, $unique_keys_old ) ) {
$result[ $key ] = false; // Removed
}
elseif ( in_array( $key, $unique_keys_new ) ) {
$result[ $key ] = true; // Added
}
elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level
if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
$inner = array();
$parent = $key;
$changed = self::get_changed_keys( $old_value[ $key ], $new_value[ $key ], --$deep );
foreach ( $changed as $child => $change ) {
$inner[ $parent . '::' . $child ] = $change;
}
$result[ $key ] = 0; // Changed parent which has a changed children
$result = array_merge( $result, $inner );
}
}
}

return $result;
}

/**
* Allow connectors to determine if their dependencies is satisfied or not
*
* @return bool
*/
public static function is_dependency_satisfied() {
return true;
}

}
15 changes: 12 additions & 3 deletions classes/class-wp-stream-connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public static function load() {
'taxonomies',
'users',
'widgets',
'jetpack',
'woocommerce',
'gravityforms',
'edd',
'wordpress-seo',
'buddypress',
'bbpress',
);

if ( is_network_admin() ) {
Expand All @@ -61,9 +68,11 @@ public static function load() {

$classes = array();
foreach ( $connectors as $connector ) {
include_once WP_STREAM_DIR . '/connectors/' . $connector .'.php';
$class = "WP_Stream_Connector_$connector";
$classes[] = $class;
include_once WP_STREAM_DIR . '/connectors/class-wp-stream-connector-' . $connector .'.php';
$class = sprintf( 'WP_Stream_Connector_%s', str_replace( '-', '_', $connector ) );
if ( $class::is_dependency_satisfied() ) {
$classes[] = $class;
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion classes/class-wp-stream-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static function get_instance() {
* @param string $action Action of the event
* @param int $user_id User responsible for the event
*
* @internal param string $action Action performed (stream_action)
* @return int
*/
public function log( $connector, $message, $args, $object_id, $context, $action, $user_id = null ) {
Expand Down
230 changes: 230 additions & 0 deletions connectors/class-wp-stream-connector-bbpress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

class WP_Stream_Connector_bbPress extends WP_Stream_Connector {

/**
* Connector slug
*
* @var string
*/
public static $name = 'bbpress';

/**
* Holds tracked plugin minimum version required
*
* @const string
*/
const PLUGIN_MIN_VERSION = '2.5.4';

/**
* Actions registered for this connector
*
* @var array
*/
public static $actions = array(
'bbp_toggle_topic_admin',
);

/**
* Tracked option keys
*
* @var array
*/
public static $options = array(
'bbpress' => null,
);

/**
* Flag to stop logging update logic twice
*
* @var bool
*/
public static $is_update = false;

/**
* @var bool
*/
public static $_deleted_activity = false;

/**
* @var array
*/
public static $_delete_activity_args = array();

/**
* @var bool
*/
public static $ignore_activity_bulk_deletion = false;

/**
* Check if plugin dependencies are satisfied and add an admin notice if not
*
* @return bool
*/
public static function is_dependency_satisfied() {
if ( ! class_exists( 'bbPress' ) ) {
//WP_Stream::notice(
// sprintf( __( '<strong>Stream bbPress Connector</strong> requires the <a href="%1$s" target="_blank">bbPress</a> plugin to be installed and activated.', 'stream' ), esc_url( 'https://wordpress.org/plugins/bbpress' ) ),
// true
//);
} elseif ( version_compare( bbp_get_version(), self::PLUGIN_MIN_VERSION, '<' ) ) {
//WP_Stream::notice(
// sprintf( __( 'Please <a href="%1$s" target="_blank">install bbPress</a> version %2$s or higher for the <strong>Stream bbPress Connector</strong> plugin to work properly.', 'stream' ), esc_url( 'https://wordpress.org/plugins/bbpress' ), self::PLUGIN_MIN_VERSION ),
// true
//);
} else {
return true;
}
}

/**
* Return translated connector label
*
* @return string Translated connector label
*/
public static function get_label() {
return __( 'bbPress', 'bbpress' );
}

/**
* Return translated action labels
*
* @return array Action label translations
*/
public static function get_action_labels() {
return array(
'created' => __( 'Created', 'stream' ),
'updated' => __( 'Updated', 'stream' ),
'activated' => __( 'Activated', 'stream' ),
'deactivated' => __( 'Deactivated', 'stream' ),
'deleted' => __( 'Deleted', 'stream' ),
'trashed' => __( 'Trashed', 'stream' ),
'restored' => __( 'Restored', 'stream' ),
'generated' => __( 'Generated', 'stream' ),
'imported' => __( 'Imported', 'stream' ),
'exported' => __( 'Exported', 'stream' ),

'closed' => __( 'Closed', 'stream' ),
'opened' => __( 'Opened', 'stream' ),
'sticked' => __( 'Sticked', 'stream' ),
'unsticked' => __( 'Unsticked', 'stream' ),
'spammed' => __( 'Marked as spam', 'stream' ),
'unspammed' => __( 'Unmarked as spam', 'stream' ),
);
}

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

/**
* Add action links to Stream drop row in admin list screen
*
* @filter wp_stream_action_links_{connector}
*
* @param array $links Previous links registered
* @param object $record Stream record
*
* @return array Action links
*/
public static function action_links( $links, $record ) {
if ( 'settings' === $record->context ) {
$option = wp_stream_get_meta( $record->ID, 'option', true );
$links[ __( 'Edit', 'stream' ) ] = esc_url( add_query_arg(
array(
'page' => 'bbpress',
),
admin_url( 'options-general.php' )
) . esc_url_raw( '#' . $option ) );
}
return $links;
}

public static function register() {
parent::register();

add_filter( 'wp_stream_log_data', array( __CLASS__, 'log_override' ) );
}

public static function log_override( array $data ) {
if ( 'settings' === $data['connector'] && 'bbpress' === $data['args']['context'] ) {
$settings = bbp_admin_get_settings_fields();

/* fix for missing title for this single field */
$settings['bbp_settings_features']['_bbp_allow_threaded_replies']['title'] = __( 'Reply Threading', 'bbpress' );

$option = $data['args']['option'];
foreach ( $settings as $section => $fields ) {
if ( isset( $fields[ $option ] ) ) {
$field = $fields[ $option ];
break;
}
}

if ( ! isset( $field ) ) {
return $data;
}

$data['args']['label'] = $field['title'];
$data['connector'] = self::$name;
$data['context'] = 'settings';
$data['action'] = 'updated';
}
elseif ( 'posts' === $data['connector'] && in_array( $data['context'], array( 'forum', 'topic', 'reply' ) ) ) {
if ( 'reply' === $data['context'] ) {
if ( 'updated' === $data['action'] ) {
$data['message'] = __( 'Replied on "%1$s"', 'stream' );
$data['args']['post_title'] = get_post( wp_get_post_parent_id( $data['object_id'] ) )->post_title;
}
$data['args']['post_title'] = sprintf(
__( 'Reply to: %s', 'stream' ),
get_post( wp_get_post_parent_id( $data['object_id'] ) )->post_title
);
}

$data['connector'] = self::$name;
}
elseif ( 'taxonomies' === $data['connector'] && in_array( $data['context'], array( 'topic-tag' ) ) ) {
$data['connector'] = self::$name;
}

return $data;
}

public static function callback_bbp_toggle_topic_admin( $success, $post_data, $action, $message ) {

if ( ! empty( $message['failed'] ) ) {
return;
}

$action = $message['bbp_topic_toggle_notice'];
$actions = self::get_action_labels();

if ( ! isset( $actions[ $action ] ) ) {
return;
}

$label = $actions[ $action ];
$topic = get_post( $message['topic_id'] );

self::log(
_x( '%1$s "%2$s" topic', '1: Action, 2: Topic title', 'stream' ),
array(
'action_title' => $actions[ $action ],
'topic_title' => $topic->post_title,
'action' => $action,
),
$topic->ID,
'topic',
$action
);
}

}
File renamed without changes.
Loading

0 comments on commit bbf37d4

Please sign in to comment.