Make WordPress Core

Ticket #17165: 17165.8.diff

File 17165.8.diff, 12.1 KB (added by greuben, 14 years ago)
  • wp-includes/user.php

     
    501501                        $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
    502502                }
    503503
    504                 _parse_meta_query( $qv );
    505 
    506504                $role = trim( $qv['role'] );
    507505
    508506                if ( $blog_id && ( $role || is_multisite() ) ) {
     
    517515                        $qv['meta_query'][] = $cap_meta_query;
    518516                }
    519517
    520                 if ( !empty( $qv['meta_query'] ) ) {
    521                         $clauses = call_user_func_array( '_get_meta_sql', array( $qv['meta_query'], 'user', $wpdb->users, 'ID', &$this ) );
     518                $meta_query = new WP_Meta_Query();
     519                $meta_query->parse_query_vars( $qv );
     520
     521                if ( !empty( $meta_query->queries ) ) {
     522                        $clauses = $meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
    522523                        $this->query_from .= $clauses['join'];
    523524                        $this->query_where .= $clauses['where'];
    524525                }
  • wp-includes/query.php

     
    849849        var $tax_query;
    850850
    851851        /**
     852         * Metadata query container
     853         *
     854         * @since 3.2
     855         * @access public
     856         * @var object WP_Meta_Query
     857         */
     858        var $meta_query = false;
     859
     860        /**
    852861         * Holds the data for a single object that is queried.
    853862         *
    854863         * Holds the contents of a post, page, category, attachment.
     
    15251534                        }
    15261535                        unset( $tax_query );
    15271536
    1528                         _parse_meta_query( $qv );
    1529 
    15301537                        if ( empty($qv['author']) || ($qv['author'] == '0') ) {
    15311538                                $this->is_author = false;
    15321539                        } else {
     
    18961903                // Fill again in case pre_get_posts unset some vars.
    18971904                $q = $this->fill_query_vars($q);
    18981905
     1906                // Parse meta query
     1907                $this->meta_query = new WP_Meta_Query();
     1908                $this->meta_query->parse_query_vars( $q );
     1909
    18991910                // Set a flag if a pre_get_posts hook changed the query vars.
    19001911                $hash = md5( serialize( $this->query_vars ) );
    19011912                if ( $hash != $this->query_vars_hash ) {
     
    22312242                        }
    22322243                }
    22332244
    2234                 if ( !empty( $this->tax_query->queries ) || !empty( $q['meta_key'] ) ) {
     2245                if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    22352246                        $groupby = "{$wpdb->posts}.ID";
    22362247                }
    22372248
     
    24622473                        $where .= ')';
    24632474                }
    24642475
    2465                 // Parse the meta query again if query vars have changed.
    2466                 if ( $this->query_vars_changed ) {
    2467                         $meta_query_hash = md5( serialize( $q['meta_query'] ) );
    2468                         $_meta_query = $q['meta_query'];
    2469                         unset( $q['meta_query'] );
    2470                         _parse_meta_query( $q );
    2471                         if ( md5( serialize( $q['meta_query'] ) ) != $meta_query_hash && is_array( $_meta_query ) )
    2472                                 $q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
    2473                 }
    2474 
    2475                 if ( !empty( $q['meta_query'] ) ) {
    2476                         $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
     2476                if ( !empty( $this->meta_query->queries ) ) {
     2477                        $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    24772478                        $join .= $clauses['join'];
    24782479                        $where .= $clauses['where'];
    24792480                }
  • wp-includes/meta.php

     
    355355/**
    356356 * Given a meta query, generates SQL clauses to be appended to a main query
    357357 *
    358  * @since 3.1.0
    359  * @access private
     358 * @since 3.2.0
    360359 *
    361  * @param array $meta_query List of metadata queries. A single query is an associative array:
    362  * - 'key' string The meta key
    363  * - 'value' string|array The meta value
    364  * - 'compare' (optional) string How to compare the key to the value.
    365  *              Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
    366  *              Default: '='
    367  * - 'type' string (optional) The type of the value.
    368  *              Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
    369  *              Default: 'CHAR'
     360 * @see WP_Meta_Query
    370361 *
     362 * @param array (optional) $meta_query A meta query
    371363 * @param string $type Type of meta
    372364 * @param string $primary_table
    373365 * @param string $primary_id_column
    374366 * @param object $context (optional) The main query object
    375367 * @return array( 'join' => $join_sql, 'where' => $where_sql )
    376368 */
    377 function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
    378         global $wpdb;
     369function get_meta_sql( $meta_query = false, $type, $primary_table, $primary_id_column, $context = null ) {
     370        $meta_query_obj = new WP_Meta_Query( $meta_query );
     371        return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
     372}
    379373
    380         if ( ! $meta_table = _get_meta_table( $type ) )
    381                 return false;
     374/**
     375 * Container class for a multiple metadata query
     376 *
     377 * @since 3.2
     378 */
     379class WP_Meta_Query {
     380        /**
     381        * List of metadata queries. A single query is an associative array:
     382        * - 'key' string The meta key
     383        * - 'value' string|array The meta value
     384        * - 'compare' (optional) string How to compare the key to the value.
     385        *              Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
     386        *              Default: '='
     387        * - 'type' string (optional) The type of the value.
     388        *              Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
     389        *              Default: 'CHAR'
     390        *
     391        * @since 3.2
     392        * @access public
     393        * @var array
     394        */
     395        public $queries = array();
    382396
    383         $meta_id_column = esc_sql( $type . '_id' );
     397        /**
     398         * Constructor
     399         *
     400         * @param array (optional) $meta_query A meta query
     401         */
     402        function __construct( $meta_query = false ) {
     403                if( $meta_query )
     404                        $this->queries = $meta_query;
     405        }
    384406
    385         $join = '';
    386         $where = '';
    387         $i = 0;
    388         foreach ( $meta_query as $q ) {
    389                 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
    390                 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
    391                 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
     407        /**
     408         * Populates the $queries property by looking for 'meta_*' query variables
     409         *
     410         * @since 3.2
     411         * @access public
     412         *
     413         * @param array $qv The query variables
     414         */
     415        function parse_query_vars( $qv ) {
     416                $this->queries = array();
    392417
    393                 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
    394                         $meta_compare = '=';
     418                // Simple query needs to be first for orderby=meta_value to work correctly
     419                foreach ( array( 'key', 'compare', 'type' ) as $key ) {
     420                        if ( !empty( $qv[ "meta_$key" ] ) )
     421                                $this->queries[0][ $key ] = $qv[ "meta_$key" ];
     422                }
    395423
    396                 if ( 'NUMERIC' == $meta_type )
    397                         $meta_type = 'SIGNED';
    398                 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
    399                         $meta_type = 'CHAR';
     424                // WP_Query sets 'meta_value' = '' by default
     425                if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
     426                        $this->queries[0]['value'] = $qv[ 'meta_value' ];
    400427
    401                 if ( empty( $meta_key ) && empty( $meta_value ) )
    402                         continue;
     428                if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
     429                        $this->queries = array_merge( $this->queries, $qv['meta_query'] );
     430                }
     431        }
    403432
    404                 $alias = $i ? 'mt' . $i : $meta_table;
     433        /**
     434         * Generates SQL clauses to be appended to a main query.
     435         *
     436         * @since 3.2
     437         * @access public
     438         *
     439         * @param string $type Type of meta
     440         * @param string $primary_table
     441         * @param string $primary_id_column
     442         * @param object $context (optional) The main query object
     443         * @return array( 'join' => $join_sql, 'where' => $where_sql )
     444         */
     445        function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
     446                global $wpdb;
    405447
    406                 $join .= "\nINNER JOIN $meta_table";
    407                 $join .= $i ? " AS $alias" : '';
    408                 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
     448                if ( ! $meta_table = _get_meta_table( $type ) )
     449                        return false;
    409450
    410                 $i++;
     451                $meta_id_column = esc_sql( $type . '_id' );
    411452
    412                 if ( !empty( $meta_key ) )
    413                         $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
     453                $join = '';
     454                $where = '';
     455                $i = 0;
     456                foreach ( $this->queries as $q ) {
     457                        if( ! is_array( $q ) )
     458                                continue;
    414459
    415                 if ( !isset( $q['value'] ) )
    416                         continue;
    417                 $meta_value = $q['value'];
     460                        $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
     461                        $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
     462                        $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
    418463
    419                 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
    420                         if ( ! is_array( $meta_value ) )
    421                                 $meta_value = preg_split( '/[,\s]+/', $meta_value );
     464                        if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
     465                                $meta_compare = '=';
    422466
    423                         if ( empty( $meta_value ) )
     467                        if ( 'NUMERIC' == $meta_type )
     468                                $meta_type = 'SIGNED';
     469                        elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
     470                                $meta_type = 'CHAR';
     471
     472                        if ( empty( $meta_key ) && empty( $meta_value ) )
    424473                                continue;
    425                 } else {
    426                         $meta_value = trim( $meta_value );
    427                 }
    428474
    429                 if ( 'IN' == substr( $meta_compare, -2) ) {
    430                         $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
    431                 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
    432                         $meta_value = array_slice( $meta_value, 0, 2 );
    433                         $meta_compare_string = '%s AND %s';
    434                 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
    435                         $meta_value = '%' . like_escape( $meta_value ) . '%';
    436                         $meta_compare_string = '%s';
    437                 } else {
    438                         $meta_compare_string = '%s';
    439                 }
     475                        $alias = $i ? 'mt' . $i : $meta_table;
    440476
    441                 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
    442         }
     477                        $join .= "\nINNER JOIN $meta_table";
     478                        $join .= $i ? " AS $alias" : '';
     479                        $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
    443480
    444         return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $type, $primary_table, $primary_id_column, &$context ) );
    445 }
     481                        $i++;
    446482
    447 /**
    448  * Populates the $meta_query property
    449  *
    450  * @access private
    451  * @since 3.1.0
    452  *
    453  * @param array $qv The query variables
    454  */
    455 function _parse_meta_query( &$qv ) {
    456         $meta_query = array();
     483                        if ( !empty( $meta_key ) )
     484                                $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
    457485
    458         // Simple query needs to be first for orderby=meta_value to work correctly
    459         foreach ( array( 'key', 'compare', 'type' ) as $key ) {
    460                 if ( !empty( $qv[ "meta_$key" ] ) )
    461                         $meta_query[0][ $key ] = $qv[ "meta_$key" ];
    462         }
     486                        if ( !isset( $q['value'] ) )
     487                                continue;
     488                        $meta_value = $q['value'];
    463489
    464         // WP_Query sets 'meta_value' = '' by default
    465         if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
    466                 $meta_query[0]['value'] = $qv[ 'meta_value' ];
     490                        if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
     491                                if ( ! is_array( $meta_value ) )
     492                                        $meta_value = preg_split( '/[,\s]+/', $meta_value );
    467493
    468         if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
    469                 $meta_query = array_merge( $meta_query, $qv['meta_query'] );
     494                                if ( empty( $meta_value ) )
     495                                        continue;
     496                        } else {
     497                                $meta_value = trim( $meta_value );
     498                        }
     499
     500                        if ( 'IN' == substr( $meta_compare, -2) ) {
     501                                $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
     502                        } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
     503                                $meta_value = array_slice( $meta_value, 0, 2 );
     504                                $meta_compare_string = '%s AND %s';
     505                        } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
     506                                $meta_value = '%' . like_escape( $meta_value ) . '%';
     507                                $meta_compare_string = '%s';
     508                        } else {
     509                                $meta_compare_string = '%s';
     510                        }
     511
     512                        $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
     513                }
     514
     515                return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
    470516        }
    471517
    472         $qv['meta_query'] = $meta_query;
    473518}
    474519
    475520/**