Make WordPress Core


Ignore:
Timestamp:
02/05/2015 07:37:47 PM (10 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/meta.php

    r31312 r31340  
    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. When first-order clauses use strings as their array keys, they may be
     954     *     referenced in the 'orderby' parameter of the parent query.
    954955     *
    955956     *     @type string $relation Optional. The MySQL keyword used to join
     
    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     * }
     
    10171016                }
    10181017
    1019                 $clean_queries[] = $query;
     1018                $clean_queries[ $key ] = $query;
    10201019
    10211020            // Otherwise, it's a nested query, so we recurse.
     
    10241023
    10251024                if ( ! empty( $cleaned_query ) ) {
    1026                     $clean_queries[] = $cleaned_query;
     1025                    $clean_queries[ $key ] = $cleaned_query;
    10271026                }
    10281027            }
     
    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'] );
     
    13221321     * @access public
    13231322     *
    1324      * @param array $clause       Query clause, passed by reference.
    1325      * @param array $parent_query Parent query array.
     1323     * @param array  $clause       Query clause, passed by reference.
     1324     * @param array  $parent_query Parent query array.
     1325     * @param string $clause_key   Optional. The array key used to name the clause in the original `$meta_query`
     1326     *                             parameters. If not provided, a key will be generated automatically.
    13261327     * @return array {
    13271328     *     Array containing JOIN and WHERE SQL clauses to append to a first-order query.
     
    13311332     * }
    13321333     */
    1333     public function get_sql_for_clause( &$clause, $parent_query ) {
     1334    public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
    13341335        global $wpdb;
    13351336
     
    13921393        $clause['cast'] = $meta_type;
    13931394
     1395        // Fallback for clause keys is the table alias.
     1396        if ( ! $clause_key ) {
     1397            $clause_key = $clause['alias'];
     1398        }
     1399
     1400        // Ensure unique clause keys, so none are overwritten.
     1401        $iterator = 1;
     1402        $clause_key_base = $clause_key;
     1403        while ( isset( $this->clauses[ $clause_key ] ) ) {
     1404            $clause_key = $clause_key_base . '-' . $iterator;
     1405            $iterator++;
     1406        }
     1407
    13941408        // Store the clause in our flat array.
    1395         $clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
    1396         $this->clauses[ $clause_name ] =& $clause;
     1409        $this->clauses[ $clause_key ] =& $clause;
    13971410
    13981411        // Next, build the WHERE clause.
     
    14721485
    14731486    /**
    1474      * Get a flattened list of sanitized meta clauses, indexed by clause 'name'.
     1487     * Get a flattened list of sanitized meta clauses.
    14751488     *
    14761489     * This array should be used for clause lookup, as when the table alias and CAST type must be determined for
Note: See TracChangeset for help on using the changeset viewer.