Modify `meta_query orderby syntax to use array keys as clause "handles".

The implementation of `meta_query` orderby introduced in [31312] put clause
identifiers into a 'name' parameter of the clause. For greater clarity, this
changeset updates the syntax to use the associative array key used when
defining `meta_query` parameters, instead of the 'name' parameter.

Props Funkatronic, DrewAPicture.
Fixes #31045.

git-svn-id: https://develop.svn.wordpress.org/trunk@31340 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-02-05 19:37:47 +00:00
parent 9d72e1c6a2
commit 0f28011bcf
3 changed files with 75 additions and 32 deletions

View File

@ -945,12 +945,13 @@ class WP_Meta_Query {
* Constructor.
*
* @since 3.2.0
* @since 4.2.0 Introduced the `$name` parameter, for improved `$orderby` support in the parent query.
* @since 4.2.0 Introduced support for naming query clauses by associative array keys.
*
* @access public
*
* @param array $meta_query {
* Array of meta query clauses.
* Array of meta query clauses. When first-order clauses use strings as their array keys, they may be
* referenced in the 'orderby' parameter of the parent query.
*
* @type string $relation Optional. The MySQL keyword used to join
* the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
@ -967,8 +968,6 @@ class WP_Meta_Query {
* comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
* 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
* Default is 'CHAR'.
* @type string $name Optional. A unique identifier for the clause. If provided, `$name` can be
* referenced in the `$orderby` parameter of the parent query.
* }
* }
*/
@ -1016,14 +1015,14 @@ class WP_Meta_Query {
unset( $query['value'] );
}
$clean_queries[] = $query;
$clean_queries[ $key ] = $query;
// Otherwise, it's a nested query, so we recurse.
} else {
$cleaned_query = $this->sanitize_query( $query );
if ( ! empty( $cleaned_query ) ) {
$clean_queries[] = $cleaned_query;
$clean_queries[ $key ] = $cleaned_query;
}
}
}
@ -1270,7 +1269,7 @@ class WP_Meta_Query {
// This is a first-order clause.
if ( $this->is_first_order_clause( $clause ) ) {
$clause_sql = $this->get_sql_for_clause( $clause, $query );
$clause_sql = $this->get_sql_for_clause( $clause, $query, $key );
$where_count = count( $clause_sql['where'] );
if ( ! $where_count ) {
@ -1321,8 +1320,10 @@ class WP_Meta_Query {
* @since 4.1.0
* @access public
*
* @param array $clause Query clause, passed by reference.
* @param array $parent_query Parent query array.
* @param array $clause Query clause, passed by reference.
* @param array $parent_query Parent query array.
* @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query`
* parameters. If not provided, a key will be generated automatically.
* @return array {
* Array containing JOIN and WHERE SQL clauses to append to a first-order query.
*
@ -1330,7 +1331,7 @@ class WP_Meta_Query {
* @type string $where SQL fragment to append to the main WHERE clause.
* }
*/
public function get_sql_for_clause( &$clause, $parent_query ) {
public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
global $wpdb;
$sql_chunks = array(
@ -1391,9 +1392,21 @@ class WP_Meta_Query {
$meta_type = $this->get_cast_for_type( $_meta_type );
$clause['cast'] = $meta_type;
// Fallback for clause keys is the table alias.
if ( ! $clause_key ) {
$clause_key = $clause['alias'];
}
// Ensure unique clause keys, so none are overwritten.
$iterator = 1;
$clause_key_base = $clause_key;
while ( isset( $this->clauses[ $clause_key ] ) ) {
$clause_key = $clause_key_base . '-' . $iterator;
$iterator++;
}
// Store the clause in our flat array.
$clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
$this->clauses[ $clause_name ] =& $clause;
$this->clauses[ $clause_key ] =& $clause;
// Next, build the WHERE clause.
@ -1471,7 +1484,7 @@ class WP_Meta_Query {
}
/**
* Get a flattened list of sanitized meta clauses, indexed by clause 'name'.
* Get a flattened list of sanitized meta clauses.
*
* This array should be used for clause lookup, as when the table alias and CAST type must be determined for
* a value of 'orderby' corresponding to a meta clause.

View File

@ -1450,6 +1450,8 @@ class WP_Query {
* Parse a query string and set query type booleans.
*
* @since 1.5.0
* @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
* array key to `$orderby`.
* @access public
*
* @param string|array $query {
@ -1497,11 +1499,14 @@ class WP_Query {
* @type int $offset The number of posts to offset before retrieval.
* @type string $order Designates ascending or descending order of posts. Default 'DESC'.
* Accepts 'ASC', 'DESC'.
* @type string $orderby Sort retrieved posts by parameter. One or more options can be
* @type string|array $orderby Sort retrieved posts by parameter. One or more options may be
* passed. To use 'meta_value', or 'meta_value_num',
* 'meta_key=keyname' must be also be defined. Default 'date'.
* Accepts 'none', 'name', 'author', 'date', 'title', 'modified',
* 'menu_order', 'parent', 'ID', 'rand', 'comment_count'.
* 'meta_key=keyname' must be also be defined. To sort by a
* specific `$meta_query` clause, use that clause's array key.
* Default 'date'. Accepts 'none', 'name', 'author', 'date',
* 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
* 'comment_count', 'meta_value', 'meta_value_num', and the
* array keys of `$meta_query`.
* @type int $p Post ID.
* @type int $page Show the number of posts that would show up on page X of a
* static front page.

View File

@ -1562,7 +1562,7 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
/**
* @ticket 31045
*/
public function test_orderby_name() {
public function test_orderby_clause_key() {
$posts = $this->factory->post->create_many( 3 );
add_post_meta( $posts[0], 'foo', 'aaa' );
add_post_meta( $posts[1], 'foo', 'zzz' );
@ -1571,13 +1571,12 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
$q = new WP_Query( array(
'fields' => 'ids',
'meta_query' => array(
array(
'name' => 'foo_name',
'foo_key' => array(
'key' => 'foo',
'compare' => 'EXISTS',
),
),
'orderby' => 'foo_name',
'orderby' => 'foo_key',
'order' => 'DESC',
) );
@ -1587,7 +1586,7 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
/**
* @ticket 31045
*/
public function test_orderby_name_as_secondary_sort() {
public function test_orderby_clause_key_as_secondary_sort() {
$p1 = $this->factory->post->create( array(
'post_date' => '2015-01-28 03:00:00',
) );
@ -1605,15 +1604,14 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
$q = new WP_Query( array(
'fields' => 'ids',
'meta_query' => array(
array(
'name' => 'foo_name',
'foo_key' => array(
'key' => 'foo',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'post_date' => 'asc',
'foo_name' => 'asc',
'foo_key' => 'asc',
),
) );
@ -1623,7 +1621,7 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
/**
* @ticket 31045
*/
public function test_orderby_more_than_one_name() {
public function test_orderby_more_than_one_clause_key() {
$posts = $this->factory->post->create_many( 3 );
add_post_meta( $posts[0], 'foo', 'jjj' );
@ -1636,23 +1634,50 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
$q = new WP_Query( array(
'fields' => 'ids',
'meta_query' => array(
array(
'name' => 'foo_name',
'foo_key' => array(
'key' => 'foo',
'compare' => 'EXISTS',
),
array(
'name' => 'bar_name',
'bar_key' => array(
'key' => 'bar',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'foo_name' => 'asc',
'bar_name' => 'desc',
'foo_key' => 'asc',
'bar_key' => 'desc',
),
) );
$this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $q->posts );
}
/**
* @ticket 31045
*/
public function test_duplicate_clause_keys_should_be_made_unique() {
$q = new WP_Query( array(
'fields' => 'ids',
'meta_query' => array(
'foo_key' => array(
'key' => 'foo',
'compare' => 'EXISTS',
),
array(
'foo_key' => array(
'key' => 'bar',
'compare' => 'EXISTS',
),
),
array(
'foo_key' => array(
'key' => 'baz',
'compare' => 'EXISTS',
),
),
),
) );
$this->assertEqualSets( array( 'foo_key', 'foo_key-1', 'foo_key-2' ), array_keys( $q->meta_query->get_clauses() ) );
}
}