From 22643b946441caca87e014e2a0b574d4beb1eee0 Mon Sep 17 00:00:00 2001 From: scribu Date: Wed, 13 Oct 2010 16:37:01 +0000 Subject: [PATCH] Sacrifice DRY for readability in WP_Query. Props nacin for initial patch. See #14997 git-svn-id: http://svn.automattic.com/wordpress/trunk@15795 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 79 ++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 53 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index d8938128a..e0409d16b 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -2183,21 +2183,23 @@ class WP_Query extends WP_Object_Query { $orderby = $q['orderby']; + $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); + // Apply post-paging filters on where and join. Only plugins that // manipulate paging queries should use these hooks. if ( !$q['suppress_filters'] ) { - $var_config = array( - 'where' => 'posts_where_paged', - 'groupby' => 'posts_groupby', - 'join' => 'posts_join_paged', - 'orderby' => 'posts_orderby', - 'distinct' => 'posts_distinct', - 'limits' => 'post_limits', - 'fields' => 'posts_fields', - ); - $var_values = compact( array_keys( $var_config ) ); - $result = $this->_filter_clause_variables( $var_config, $var_values, 'posts_clauses' ); - extract( $result ); + $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); + $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); + $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); + $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); + $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); + $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); + $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); + + // Filter all clauses at once, for convenience + $clauses = apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); + foreach ( $pieces as $piece ) + $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; } // Announce current selection parameters. For use by caching plugins. @@ -2205,18 +2207,18 @@ class WP_Query extends WP_Object_Query { // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above. if ( !$q['suppress_filters'] ) { - $var_config = array( - 'where' => 'posts_where_request', - 'groupby' => 'posts_groupby_request', - 'join' => 'posts_join_request', - 'orderby' => 'posts_orderby_request', - 'distinct' => 'posts_distinct_request', - 'fields' => 'posts_fields_request', - 'limits' => 'post_limits_request', - ); - $var_values = compact( array_keys( $var_config ) ); - $result = $this->_filter_clause_variables( $var_config, $var_values, 'posts_clauses_request' ); - extract( $result ); + $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); + $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); + $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); + $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); + $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) ); + $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) ); + $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); + + // Filter all clauses at once, for convenience + $clauses = apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); + foreach ( $pieces as $piece ) + $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; } if ( ! empty($groupby) ) @@ -2357,35 +2359,6 @@ class WP_Query extends WP_Object_Query { return $this->posts; } - - /** - * Filter clause variables based on configuration. Once per clause and - * then all clauses at once. - * - * @access private - * @param array $config filternames keyed with their variable names - * @param array $values values keyed with their variable names - * @param string $all_filter_name for the "single filter" - * @return array filtered values keyed with their names - */ - function _filter_clause_variables( $config, $values, $all_filter_name ) { - $clauses = array(); - foreach ( $config as $variable_name => $filter ) { - $clauses[$variable_name] = apply_filters_ref_array( $filter, array( $values[$variable_name], &$this ) ); - } - // run filter for all clauses at once - $clauses = apply_filters_ref_array( $all_filter_name, array( $clauses, &$this ) ); - $is_array = is_array( $clauses ); - $filtered = array(); - foreach ( $config as $variable_name => $filter ) { - if ( $is_array && isset( $clauses[$variable_name] ) ) { - $filtered[$variable_name] = $clauses[$variable_name]; - } else { - $filtered[$variable_name] = ''; - } - } - return $filtered; - } /** * Set up the next post and iterate current post index.