Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply filter to query args Fix #355 #362

Merged
merged 8 commits into from
Mar 27, 2014
10 changes: 3 additions & 7 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,8 @@ public static function dashboard_stream_activity_contents( $paged = 1 ) {
'paged' => $paged,
);

// Remove excluded records as per settings
add_filter( 'stream_query_args', array( 'WP_Stream_Settings', 'remove_excluded_record_filter' ), 10, 1 );

$records = stream_query( $args );

// Remove filter added before
remove_filter( 'stream_query_args', array( 'WP_Stream_Settings', 'remove_excluded_record_filter' ), 10, 1 );

if ( ! $records ) {
?>
<p class="no-records"><?php esc_html_e( 'Sorry, no activity records were found.', 'stream' ) ?></p>
Expand Down Expand Up @@ -809,7 +803,9 @@ public static function gather_updated_items( $last_id, $query = null ) {
$query = wp_parse_args( $query, $default );

// Run query
return stream_query( $query );
$items = stream_query( $query );

return $items;
}

/**
Expand Down
6 changes: 0 additions & 6 deletions includes/feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,8 @@ public static function feed_template() {
'fields' => isset( $_GET['fields'] ) ? (string) $_GET['fields'] : '',
);

// Remove excluded records as per settings
add_filter( 'stream_query_args', array( 'WP_Stream_Settings', 'remove_excluded_record_filter' ), 10, 1 );

$records = stream_query( $args );

// Remove filter added before
remove_filter( 'stream_query_args', array( 'WP_Stream_Settings', 'remove_excluded_record_filter' ), 10, 1 );

$latest_record = isset( $records[0]->created ) ? $records[0]->created : null;

$records_admin_url = add_query_arg(
Expand Down
6 changes: 0 additions & 6 deletions includes/list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,8 @@ function get_records() {
$args['records_per_page'] = $this->get_items_per_page( 'edit_stream_per_page', 20 );
}

// Remove excluded records as per settings
add_filter( 'stream_query_args', array( 'WP_Stream_Settings', 'remove_excluded_record_filter' ), 10, 1 );

$items = stream_query( $args );

// Remove filter added before
remove_filter( 'stream_query_args', array( 'WP_Stream_Settings', 'remove_excluded_record_filter' ), 10, 1 );

return $items;
}

Expand Down
110 changes: 79 additions & 31 deletions includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ public function query( $args ) {
// Fields selection
'fields' => '',
'ignore_context' => null,
//Hide Excluded
'hide_excluded' => true,
);

$args = wp_parse_args( $args, $defaults );

/**
* Filter allows additional arguments to query $args
*
Expand All @@ -72,6 +73,10 @@ public function query( $args ) {
*/
$args = apply_filters( 'stream_query_args', $args );

if ( true === $args[ 'hide_excluded' ] ) {
$args = self::add_excluded_record_args( $args );
}

$join = '';
$where = '';

Expand Down Expand Up @@ -129,65 +134,71 @@ public function query( $args ) {
/**
* PARSE __IN PARAM FAMILY
*/
if ( $args['record_greater_than'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID > %d", (int) $args['record_greater_than'] );
if ( $args[ 'record_greater_than' ] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID > %d", (int)$args[ 'record_greater_than' ] );
}

if ( $args['record__in'] ) {
$record__in = implode( ',', array_filter( (array) $args['record__in'], 'is_numeric' ) );
if ( $record__in ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID IN ($record__in)", '' );
if ( $args[ 'record__in' ] ) {
$record__in = array_filter( (array)$args[ 'record__in' ], 'is_numeric' );
$record__in_format = '(' . join( ',', array_fill( 0, count( $record__in ), '%d' ) ) . ')';
if ( ! empty( $record__in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID IN {$record__in_format}", $record__in );
}
}

if ( $args['record__not_in'] ) {
$record__not_in = implode( ',', array_filter( (array) $args['record__not_in'], 'is_numeric' ) );
if ( strlen( $record__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID NOT IN ($record__not_in)", '' );
if ( $args[ 'record__not_in' ] ) {
$record__not_in = array_filter( (array)$args[ 'record__not_in' ], 'is_numeric' );
$record__not_in_format = '(' . join( ',', array_fill( 0, count( $record__not_in ), '%d' ) ) . ')';
if ( ! empty( $record__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID NOT IN {$record__not_in_format}", $record__not_in );
}
}

if ( $args['record_parent'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent = %d", (int) $args['record_parent'] );
if ( $args[ 'record_parent' ] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent = %d", (int)$args[ 'record_parent' ] );
}

if ( $args['record_parent__in'] ) {
$record_parent__in = implode( ',', array_filter( (array) $args['record_parent__in'], 'is_numeric' ) );
if ( strlen( $record_parent__in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent IN ($record_parent__in)", '' );
if ( $args[ 'record_parent__in' ] ) {
$record_parent__in = array_filter( (array)$args[ 'record_parent__in' ], 'is_numeric' );
$record_parent__in_format = '(' . join( ',', array_fill( 0, count( $record_parent__in ), '%d' ) ) . ')';
if ( ! empty( $record_parent__in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent IN {$record_parent__in_format}", $record_parent__in );
}
}

if ( $args['record_parent__not_in'] ) {
$record_parent__not_in = implode( ',', array_filter( (array) $args['record_parent__not_in'], 'is_numeric' ) );
if ( strlen( $record_parent__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent NOT IN ($record_parent__not_in)", '' );
if ( $args[ 'record_parent__not_in' ] ) {
$record_parent__not_in = array_filter( (array)$args[ 'record_parent__not_in' ], 'is_numeric' );
$record_parent__not_in_format = '(' . join( ',', array_fill( 0, count( $record_parent__not_in ), '%d' ) ) . ')';
if ( ! empty( $record_parent__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent NOT IN {$record_parent__not_in_format}", $record_parent__not_in );
}
}

if ( $args[ 'author__in' ] ) {
$author__in = implode( ',', array_filter( (array)$args[ 'author__in' ], 'is_numeric' ) );
if ( $author__in ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author IN ($author__in)", '' );
$author__in = array_filter( (array)$args[ 'author__in' ], 'is_numeric' );
$author__in_format = '(' . join( ',', array_fill( 0, count( $author__in ), '%d' ) ) . ')';
if ( ! empty( $author__in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author IN {$author__in_format}", $author__in );
}
}

if ( $args[ 'author__not_in' ] ) {
$author__not_in = implode( ',', array_filter( (array)$args[ 'author__not_in' ], 'is_numeric' ) );
if ( strlen( $author__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author NOT IN ($author__not_in)", '' );
$author__not_in = array_filter( (array)$args[ 'author__not_in' ], 'is_numeric' );
$author__not_in_format = '(' . join( ',', array_fill( 0, count( $author__not_in ), '%d' ) ) . ')';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faishal This is causing some warnings to appear for me.

Warning: array_fill(): Number of elements must be positive in /srv/www/wordpress-trunk/wp-content/plugins/stream/includes/query.php on line 187
Warning: join(): Invalid arguments passed in /srv/www/wordpress-trunk/wp-content/plugins/stream/includes/query.php on line 187

if ( ! empty( $author__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author NOT IN {$author__not_in_format}", $author__not_in );
}
}
if ( $args[ 'ip__in' ] ) {
if ( count( $args[ 'ip__in' ] ) > 0 ) {
$ip__in = '(' . substr( str_repeat( ',%s', count( $args[ 'ip__in' ] ) ), 1 ) . ')';
if ( ! empty( $args[ 'ip__in' ] ) ) {
$ip__in = '(' . join( ',', array_fill( 0, count( $args[ 'ip__in' ] ), '%s' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ip IN {$ip__in}", $args[ 'ip__in' ] );
}
}

if ( $args[ 'ip__not_in' ] ) {
if ( count( $args[ 'ip__not_in' ] ) > 0 ) {
$ip__not_in = '(' . substr( str_repeat( ',%s', count( $args[ 'ip__not_in' ] ) ), 1 ) . ')';
if ( ! empty( $args[ 'ip__not_in' ] ) ) {
$ip__not_in = '(' . join( ',', array_fill( 0, count( $args[ 'ip__not_in' ] ), '%s' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ip NOT IN {$ip__not_in}", $args[ 'ip__not_in' ] );
}
}
Expand Down Expand Up @@ -305,6 +316,42 @@ public function query( $args ) {
return $results;
}

/**
* Function will add excluded settings args into stream query
*
* @param $args array query args passed to stream_query
*
* @return array
*/
public static function add_excluded_record_args( $args ) {
// Remove record of excluded connector
if ( empty( $args['connector'] ) ) {
$args['connector__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'connectors' );
}

// Remove record of excluded context
if ( empty( $args['context'] ) ) {
$args['context__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'contexts' );
}

// Remove record of excluded actions
if ( empty( $args['action'] ) ) {
$args['action__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'actions' );
}

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

// Remove record of excluded ip
if ( empty( $args['ip'] ) ) {
$args['ip__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'ip_addresses' );
}

return $args;
}

}

function stream_query( $args = array() ) {
Expand Down Expand Up @@ -360,3 +407,4 @@ function existing_records( $column, $table = '' ) {
return isset( WP_Stream_Connectors::$term_labels[ $column ] ) ? WP_Stream_Connectors::$term_labels[ $column ] : array();
}
}

36 changes: 0 additions & 36 deletions includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,40 +775,4 @@ public static function updated_option_ttl_remove_records( $old_value, $new_value
do_action( 'wp_stream_auto_purge' );
}
}

/**
* Function will add excluded settings args into stream query
*
* @param $args array query args passed to stream_query
*
* @return array
*/
public static function remove_excluded_record_filter( $args ) {
// Remove record of excluded connector
if ( empty( $args['connector'] ) ) {
$args['connector__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'connectors' );
}

// Remove record of excluded context
if ( empty( $args['context'] ) ) {
$args['context__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'contexts' );
}

// Remove record of excluded actions
if ( empty( $args['action'] ) ) {
$args['action__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'actions' );
}

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

// Remove record of excluded ip
if ( empty( $args['ip'] ) ) {
$args['ip__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'ip_addresses' );
}

return $args;
}
}