diff --git a/includes/admin.php b/includes/admin.php index 3122dce94..1d91dfe03 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -547,14 +547,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 ) { ?>
@@ -810,7 +804,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; } /** diff --git a/includes/feeds.php b/includes/feeds.php index de94e5efd..109a87a9c 100644 --- a/includes/feeds.php +++ b/includes/feeds.php @@ -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( diff --git a/includes/list-table.php b/includes/list-table.php index 70aa64acd..fcb3356d1 100644 --- a/includes/list-table.php +++ b/includes/list-table.php @@ -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; } diff --git a/includes/query.php b/includes/query.php index fc582b241..808103535 100644 --- a/includes/query.php +++ b/includes/query.php @@ -21,7 +21,6 @@ public static function get_instance() { */ public function query( $args ) { global $wpdb; - $defaults = array( // Pagination params 'records_per_page' => 10, @@ -60,10 +59,11 @@ public function query( $args ) { // Fields selection 'fields' => '', 'ignore_context' => null, + //Hide Excluded + 'hide_excluded' => ( WP_Stream_Settings::$options[ 'exclude_hide_previous_records' ] === 0 ) ? false : true, ); $args = wp_parse_args( $args, $defaults ); - /** * Filter allows additional arguments to query $args * @@ -72,6 +72,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 = ''; @@ -129,65 +133,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' ); + if ( ! empty( $record__in ) ) { + $record__in_format = '(' . join( ',', array_fill( 0, count( $record__in ), '%d' ) ) . ')'; + $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' ); + if ( ! empty( $record__not_in ) ) { + $record__not_in_format = '(' . join( ',', array_fill( 0, count( $record__not_in ), '%d' ) ) . ')'; + $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' ); + if ( ! empty( $record_parent__in ) ) { + $record_parent__in_format = '(' . join( ',', array_fill( 0, count( $record_parent__in ), '%d' ) ) . ')'; + $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' ); + if ( ! empty( $record_parent__not_in ) ) { + $record_parent__not_in_format = '(' . join( ',', array_fill( 0, count( $record_parent__not_in ), '%d' ) ) . ')'; + $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' ); + if ( ! empty( $author__in ) ) { + $author__in_format = '(' . join( ',', array_fill( 0, count( $author__in ), '%d' ) ) . ')'; + $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' ); + if ( ! empty( $author__not_in ) ) { + $author__not_in_format = '(' . join( ',', array_fill( 0, count( $author__not_in ), '%d' ) ) . ')'; + $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' ] ); } } @@ -305,6 +315,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() ) { @@ -360,3 +406,4 @@ function existing_records( $column, $table = '' ) { return isset( WP_Stream_Connectors::$term_labels[ $column ] ) ? WP_Stream_Connectors::$term_labels[ $column ] : array(); } } + diff --git a/includes/settings.php b/includes/settings.php index 0b909d7a3..160d84734 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -305,6 +305,16 @@ public static function get_fields() { 'default' => array(), 'nonce' => 'stream_get_ips', ), + array( + 'name' => 'hide_previous_records', + 'title' => __( 'Visibility', 'stream' ), + 'type' => 'checkbox', + 'desc' => sprintf( + __( 'When Checked, all post records that match the excluded rules above will be hidden from view', 'stream' ) + ), + 'after_field' => __( 'Hide Previous Records' ), + 'default' => 0, + ), ), ), ); @@ -775,40 +785,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; - } }