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: 9 additions & 1 deletion includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,16 @@ public static function gather_updated_items( $last_id, $query = null ) {
// Filter default
$query = wp_parse_args( $query, $default );

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

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

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

return $items;
}

/**
Expand Down
62 changes: 34 additions & 28 deletions includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,64 +129,70 @@ 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 = '(' . substr( str_repeat( ',%d', count( $record__in ) ), 1 ) . ')';
Copy link
Contributor

Choose a reason for hiding this comment

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

@faishal it might be better to use array_fill here:

$record__in_format = '(' . join( ',' array_fill( 0, count( $record__in ), '%d' ) ) . ')';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@westonruter Yes it will be better, I will do that changes.

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 = '(' . substr( str_repeat( ',%d', count( $record__not_in ) ), 1 ) . ')';
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 = '(' . substr( str_repeat( ',%d', count( $record_parent__in ) ), 1 ) . ')';
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 = '(' . substr( str_repeat( ',%d', count( $record_parent__not_in ) ), 1 ) . ')';
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 = '(' . substr( str_repeat( ',%d', count( $author__in ) ), 1 ) . ')';
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 = '(' . substr( str_repeat( ',%d', count( $author__not_in ) ), 1 ) . ')';
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 ) {
if ( ! empty( $args[ 'ip__in' ] ) ) {
$ip__in = '(' . substr( str_repeat( ',%s', count( $args[ 'ip__in' ] ) ), 1 ) . ')';
$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 ) {
if ( ! empty( $args[ 'ip__not_in' ] ) ) {
$ip__not_in = '(' . substr( str_repeat( ',%s', count( $args[ 'ip__not_in' ] ) ), 1 ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ip NOT IN {$ip__not_in}", $args[ 'ip__not_in' ] );
}
Expand Down