Skip to content

Commit

Permalink
Merge pull request #552 from x-team/fix-exclude-authors-roles
Browse files Browse the repository at this point in the history
Fix loading and exclude query issues with authors/roles
  • Loading branch information
lukecarbis committed May 30, 2014
2 parents 900159e + eb5712f commit df3edba
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 20 deletions.
26 changes: 16 additions & 10 deletions includes/connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,24 @@ public static function is_logging_enabled_for_user( $user = null ) {
$user = wp_get_current_user();
}

// If the user is not a valid user then we log action
$bool = true;
$user_roles = array_values( $user->roles );
$excluded_authors = WP_Stream_Settings::get_excluded_by_key( 'authors' );
$excluded_roles = WP_Stream_Settings::get_excluded_by_key( 'roles' );

// Don't log excluded users
if ( in_array( $user->ID, $excluded_authors ) ) {
$bool = false;
}

// Don't log excluded user roles
if ( 0 !== count( array_intersect( $user_roles, $excluded_roles ) ) ) {
$bool = false;
}

// If the user is not a valid user then we always log the action
if ( ! ( $user instanceof WP_User ) || 0 === $user->ID ) {
$bool = true;
} else {
// If a user is part of a role that we don't want to log, we disable it
$user_roles = array_values( $user->roles );
$roles_logged = WP_Stream_Settings::get_excluded_by_key( 'authors_and_roles' );
$bool = ( 0 === count( array_intersect( $user_roles, $roles_logged ) ) );
//Check user id in exclude array
if ( $bool ) {
$bool = ! ( in_array( $user->ID, $roles_logged ) );
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions includes/list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ function assemble_records( $column, $table = '' ) {
require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php';
$all_records = array();

// Short circuit and return empty array if we have more than 10 users, to use Ajax instead
// If the number of users exceeds the max authors constant value then return an empty array and use AJAX instead
$user_count = count_users();
$total_users = $user_count['total_users'];
if ( $total_users > WP_Stream_Admin::PRELOAD_AUTHORS_MAX ) {
Expand Down Expand Up @@ -751,7 +751,7 @@ function get_column_excluded_setting_key( $column ) {
$output = 'ip_addresses';
break;
case 'author':
$output = 'authors_and_roles';
$output = 'authors';
break;
default:
$output = false;
Expand Down
3 changes: 2 additions & 1 deletion includes/network.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ function get_network_admin_fields( $fields ) {
'private_feeds',
),
'exclude' => array(
'authors_and_roles',
'authors',
'roles',
'connectors',
'contexts',
'actions',
Expand Down
5 changes: 4 additions & 1 deletion includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ public static function add_excluded_record_args( $args ) {
$args['action__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'actions' );

// Remove record of excluded author
$args['author__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'authors_and_roles' );
$args['author__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'authors' );

// Remove record of excluded author role
$args['author_role__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'roles' );

// Remove record of excluded ip
$args['ip__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'ip_addresses' );
Expand Down
22 changes: 20 additions & 2 deletions includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static function get_users(){
'user_url',
),
'orderby' => 'display_name',
'number' => WP_Stream_Admin::PRELOAD_AUTHORS_MAX,
)
);

Expand Down Expand Up @@ -212,6 +213,7 @@ public static function get_option_key() {
$option_key = self::KEY;

$current_page = wp_stream_filter_input( INPUT_GET, 'page' );

if ( ! $current_page ) {
$current_page = wp_stream_filter_input( INPUT_GET, 'action' );
}
Expand Down Expand Up @@ -846,12 +848,13 @@ public static function get_active_connectors() {
}

/**
* @param $column string name of the setting key (actions|ip_addresses|contexts|connectors)
* @param $column string name of the setting key (authors|roles|actions|ip_addresses|contexts|connectors)
*
* @return array
*/
public static function get_excluded_by_key( $column ) {
$option_name = 'exclude_' . $column;
$option_name = ( 'authors' === $column || 'roles' === $column ) ? 'exclude_authors_and_roles' : 'exclude_' . $column;

$excluded_values = ( isset( self::$options[ $option_name ] ) ) ? self::$options[ $option_name ] : array();

if ( is_callable( $excluded_values ) ) {
Expand All @@ -860,6 +863,21 @@ public static function get_excluded_by_key( $column ) {

$excluded_values = wp_list_filter( $excluded_values, array( '__placeholder__' ), 'NOT' );

if ( 'exclude_authors_and_roles' === $option_name ) {
// Convert numeric strings to integers
array_walk( $excluded_values,
function ( &$value ) {
if ( is_numeric( $value ) ) {
$value = absint( $value );
}
}
);

$filter = ( 'roles' === $column ) ? 'is_string' : 'is_int'; // Author roles are always strings and author ID's are always integers

$excluded_values = array_values( array_filter( $excluded_values, $filter ) ); // Reset the array keys
}

return $excluded_values;
}

Expand Down
6 changes: 3 additions & 3 deletions stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ private function __construct() {
// Load languages
add_action( 'plugins_loaded', array( __CLASS__, 'i18n' ) );

// Load settings, enabling extensions to hook in
// Load settings at the same priority as connectors to support exclusions
require_once WP_STREAM_INC_DIR . 'settings.php';
add_action( 'init', array( 'WP_Stream_Settings', 'load' ) );
add_action( 'init', array( 'WP_Stream_Settings', 'load' ), 9 );

// Load network class
if ( is_multisite() ) {
Expand All @@ -97,7 +97,7 @@ private function __construct() {
require_once WP_STREAM_INC_DIR . 'log.php';
add_action( 'plugins_loaded', array( 'WP_Stream_Log', 'load' ) );

// Load connectors
// Load connectors after widgets_init, but before the default of 10
require_once WP_STREAM_INC_DIR . 'connectors.php';
add_action( 'init', array( 'WP_Stream_Connectors', 'load' ), 9 );

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/test-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function test_constructor() {
$this->assertTrue( defined( 'WP_STREAM_INC_DIR' ), 'WP_STREAM_INC_DIR is not defined' );

$actions_tests = array(
array( 'init', 'WP_Stream_Settings', 'load' ),
array( 'init', 'WP_Stream_Settings', 'load', 9 ),
array( 'plugins_loaded', 'WP_Stream_Log', 'load' ),
array( 'init', 'WP_Stream_Connectors', 'load', 9 ),
);
Expand Down

0 comments on commit df3edba

Please sign in to comment.