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
This commit is contained in:
scribu 2010-10-13 16:37:01 +00:00
parent a9e671a75c
commit 22643b9464
1 changed files with 26 additions and 53 deletions

View File

@ -2183,21 +2183,23 @@ class WP_Query extends WP_Object_Query {
$orderby = $q['orderby']; $orderby = $q['orderby'];
$pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
// Apply post-paging filters on where and join. Only plugins that // Apply post-paging filters on where and join. Only plugins that
// manipulate paging queries should use these hooks. // manipulate paging queries should use these hooks.
if ( !$q['suppress_filters'] ) { if ( !$q['suppress_filters'] ) {
$var_config = array( $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
'where' => 'posts_where_paged', $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
'groupby' => 'posts_groupby', $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
'join' => 'posts_join_paged', $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
'orderby' => 'posts_orderby', $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
'distinct' => 'posts_distinct', $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
'limits' => 'post_limits', $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
'fields' => 'posts_fields',
); // Filter all clauses at once, for convenience
$var_values = compact( array_keys( $var_config ) ); $clauses = apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
$result = $this->_filter_clause_variables( $var_config, $var_values, 'posts_clauses' ); foreach ( $pieces as $piece )
extract( $result ); $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
} }
// Announce current selection parameters. For use by caching plugins. // 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. // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
if ( !$q['suppress_filters'] ) { if ( !$q['suppress_filters'] ) {
$var_config = array( $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
'where' => 'posts_where_request', $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
'groupby' => 'posts_groupby_request', $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
'join' => 'posts_join_request', $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
'orderby' => 'posts_orderby_request', $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
'distinct' => 'posts_distinct_request', $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
'fields' => 'posts_fields_request', $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
'limits' => 'post_limits_request',
); // Filter all clauses at once, for convenience
$var_values = compact( array_keys( $var_config ) ); $clauses = apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
$result = $this->_filter_clause_variables( $var_config, $var_values, 'posts_clauses_request' ); foreach ( $pieces as $piece )
extract( $result ); $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
} }
if ( ! empty($groupby) ) if ( ! empty($groupby) )
@ -2358,35 +2360,6 @@ class WP_Query extends WP_Object_Query {
return $this->posts; 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. * Set up the next post and iterate current post index.
* *