Make WordPress Core

Ticket #31045: 31045.2.diff

File 31045.2.diff, 7.7 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/meta.php

    diff --git src/wp-includes/meta.php src/wp-includes/meta.php
    index c0430f7..36d6d1d 100644
    class WP_Meta_Query { 
    945945         * Constructor.
    946946         *
    947947         * @since 3.2.0
    948          * @since 4.2.0 Introduced the `$name` parameter, for improved `$orderby` support in the parent query.
     948         * @since 4.2.0 Introduced support for naming query clauses by associative array keys.
    949949         *
    950950         * @access public
    951951         *
    952952         * @param array $meta_query {
    953          *     Array of meta query clauses.
     953         *     Array of meta query clauses. First-order clauses may use strings for array keys, in order to reference
     954         *     the clause in the 'orderby' parameter of the parent query.
    954955         *
    955956         *     @type string $relation Optional. The MySQL keyword used to join
    956957         *                            the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
    class WP_Meta_Query { 
    967968         *                               comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
    968969         *                               'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
    969970         *                               Default is 'CHAR'.
    970          *         @type string $name    Optional. A unique identifier for the clause. If provided, `$name` can be
    971          *                               referenced in the `$orderby` parameter of the parent query.
    972971         *     }
    973972         * }
    974973         */
    class WP_Meta_Query { 
    10161015                                        unset( $query['value'] );
    10171016                                }
    10181017
    1019                                 $clean_queries[] = $query;
     1018                                $clean_queries[ $key ] = $query;
    10201019
    10211020                        // Otherwise, it's a nested query, so we recurse.
    10221021                        } else {
    10231022                                $cleaned_query = $this->sanitize_query( $query );
    10241023
    10251024                                if ( ! empty( $cleaned_query ) ) {
    1026                                         $clean_queries[] = $cleaned_query;
     1025                                        $clean_queries[ $key ] = $cleaned_query;
    10271026                                }
    10281027                        }
    10291028                }
    class WP_Meta_Query { 
    12701269
    12711270                                // This is a first-order clause.
    12721271                                if ( $this->is_first_order_clause( $clause ) ) {
    1273                                         $clause_sql = $this->get_sql_for_clause( $clause, $query );
     1272                                        $clause_sql = $this->get_sql_for_clause( $clause, $query, $key );
    12741273
    12751274                                        $where_count = count( $clause_sql['where'] );
    12761275                                        if ( ! $where_count ) {
    class WP_Meta_Query { 
    13301329         *     @type string $where SQL fragment to append to the main WHERE clause.
    13311330         * }
    13321331         */
    1333         public function get_sql_for_clause( &$clause, $parent_query ) {
     1332        public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
    13341333                global $wpdb;
    13351334
    13361335                $sql_chunks = array(
    class WP_Meta_Query { 
    13921391                $clause['cast'] = $meta_type;
    13931392
    13941393                // Store the clause in our flat array.
    1395                 $clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
    1396                 $this->clauses[ $clause_name ] =& $clause;
     1394                if ( ! $clause_key ) {
     1395                        $clause_key = $clause['alias'];
     1396                }
     1397
     1398                $this->clauses[ $clause_key ] =& $clause;
    13971399
    13981400                // Next, build the WHERE clause.
    13991401
    class WP_Meta_Query { 
    14711473        }
    14721474
    14731475        /**
    1474          * Get a flattened list of sanitized meta clauses, indexed by clause 'name'.
     1476         * Get a flattened list of sanitized meta clauses.
    14751477         *
    14761478         * This array should be used for clause lookup, as when the table alias and CAST type must be determined for
    14771479         * a value of 'orderby' corresponding to a meta clause.
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 3c1b0c2..c65152e 100644
    class WP_Query { 
    14971497         *     @type int          $offset                  The number of posts to offset before retrieval.
    14981498         *     @type string       $order                   Designates ascending or descending order of posts. Default 'DESC'.
    14991499         *                                                 Accepts 'ASC', 'DESC'.
    1500          *     @type string      $orderby                 Sort retrieved posts by parameter. One or more options can be
     1500         *     @type string|array $orderby                 Sort retrieved posts by parameter. One or more options can be
    15011501         *                                                 passed. To use 'meta_value', or 'meta_value_num',
    1502          *                                                 'meta_key=keyname' must be also be defined. Default 'date'.
    1503          *                                                 Accepts 'none', 'name', 'author', 'date', 'title', 'modified',
    1504          *                                                 'menu_order', 'parent', 'ID', 'rand', 'comment_count'.
     1502         *                                                 'meta_key=keyname' must be also be defined. To sort by a
     1503         *                                                 specific `$meta_query` clause, use that clause's array key.
     1504         *                                                 Default 'date'. Accepts 'none', 'name', 'author', 'date',
     1505         *                                                 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
     1506         *                                                 'comment_count', 'meta_value', 'meta_value_num', and the
     1507         *                                                 array keys of `$meta_query`.
    15051508         *     @type int          $p                       Post ID.
    15061509         *     @type int          $page                    Show the number of posts that would show up on page X of a
    15071510         *                                                 static front page.
  • tests/phpunit/tests/query/metaQuery.php

    diff --git tests/phpunit/tests/query/metaQuery.php tests/phpunit/tests/query/metaQuery.php
    index 2eff8eb..d34dc01 100644
    class Tests_Query_MetaQuery extends WP_UnitTestCase { 
    15621562        /**
    15631563         * @ticket 31045
    15641564         */
    1565         public function test_orderby_name() {
     1565        public function test_orderby_clause_key() {
    15661566                $posts = $this->factory->post->create_many( 3 );
    15671567                add_post_meta( $posts[0], 'foo', 'aaa' );
    15681568                add_post_meta( $posts[1], 'foo', 'zzz' );
    class Tests_Query_MetaQuery extends WP_UnitTestCase { 
    15711571                $q = new WP_Query( array(
    15721572                        'fields' => 'ids',
    15731573                        'meta_query' => array(
    1574                                 array(
    1575                                         'name' => 'foo_name',
     1574                                'foo_key' => array(
    15761575                                        'key' => 'foo',
    15771576                                        'compare' => 'EXISTS',
    15781577                                ),
    15791578                        ),
    1580                         'orderby' => 'foo_name',
     1579                        'orderby' => 'foo_key',
    15811580                        'order' => 'DESC',
    15821581                ) );
    15831582
    class Tests_Query_MetaQuery extends WP_UnitTestCase { 
    15871586        /**
    15881587         * @ticket 31045
    15891588         */
    1590         public function test_orderby_name_as_secondary_sort() {
     1589        public function test_orderby_clause_key_as_secondary_sort() {
    15911590                $p1 = $this->factory->post->create( array(
    15921591                        'post_date' => '2015-01-28 03:00:00',
    15931592                ) );
    class Tests_Query_MetaQuery extends WP_UnitTestCase { 
    16051604                $q = new WP_Query( array(
    16061605                        'fields' => 'ids',
    16071606                        'meta_query' => array(
    1608                                 array(
    1609                                         'name' => 'foo_name',
     1607                                'foo_key' => array(
    16101608                                        'key' => 'foo',
    16111609                                        'compare' => 'EXISTS',
    16121610                                ),
    16131611                        ),
    16141612                        'orderby' => array(
    16151613                                'post_date' => 'asc',
    1616                                 'foo_name' => 'asc',
     1614                                'foo_key' => 'asc',
    16171615                        ),
    16181616                ) );
    16191617
    class Tests_Query_MetaQuery extends WP_UnitTestCase { 
    16231621        /**
    16241622         * @ticket 31045
    16251623         */
    1626         public function test_orderby_more_than_one_name() {
     1624        public function test_orderby_more_than_one_clause_key() {
    16271625                $posts = $this->factory->post->create_many( 3 );
    16281626
    16291627                add_post_meta( $posts[0], 'foo', 'jjj' );
    class Tests_Query_MetaQuery extends WP_UnitTestCase { 
    16361634                $q = new WP_Query( array(
    16371635                        'fields' => 'ids',
    16381636                        'meta_query' => array(
    1639                                 array(
    1640                                         'name' => 'foo_name',
     1637                                'foo_key' => array(
    16411638                                        'key' => 'foo',
    16421639                                        'compare' => 'EXISTS',
    16431640                                ),
    1644                                 array(
    1645                                         'name' => 'bar_name',
     1641                                'bar_key' => array(
    16461642                                        'key' => 'bar',
    16471643                                        'compare' => 'EXISTS',
    16481644                                ),
    16491645                        ),
    16501646                        'orderby' => array(
    1651                                 'foo_name' => 'asc',
    1652                                 'bar_name' => 'desc',
     1647                                'foo_key' => 'asc',
     1648                                'bar_key' => 'desc',
    16531649                        ),
    16541650                ) );
    16551651