Make WordPress Core

Ticket #17165: 17165.2.diff

File 17165.2.diff, 12.1 KB (added by scribu, 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 = call_user_func_array( array( $meta_query, 'get_sql' ), array( 'user', $wpdb->users, 'ID', &$this ) );
    522523                        $this->query_from .= $clauses['join'];
    523524                        $this->query_where .= $clauses['where'];
    524525                }
  • wp-includes/query.php

     
    840840        var $query_vars = array();
    841841
    842842        /**
    843          * Taxonomy query, as passed to get_tax_sql()
     843         * Taxonomy query container
    844844         *
    845845         * @since 3.1.0
    846846         * @access public
     
    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;
     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 );
     1537                        $this->meta_query = new WP_Meta_Query();
     1538                        $this->meta_query->parse_query_vars( $qv );
    15291539
    15301540                        if ( empty($qv['author']) || ($qv['author'] == '0') ) {
    15311541                                $this->is_author = false;
     
    22312241                        }
    22322242                }
    22332243
    2234                 if ( !empty( $this->tax_query->queries ) || !empty( $q['meta_key'] ) ) {
     2244                if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    22352245                        $groupby = "{$wpdb->posts}.ID";
    22362246                }
    22372247
     
    24632473                }
    24642474
    24652475                // 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                 }
     2476                $this->meta_query->parse_query_vars( $q );
    24742477
    2475                 if ( !empty( $q['meta_query'] ) ) {
    2476                         $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
     2478                if ( !empty( $this->meta_query->queries ) ) {
     2479                        $clauses = call_user_func_array( array( $this->meta_query, 'get_sql' ), array( 'post', $wpdb->posts, 'ID', &$this ) );
    24772480                        $join .= $clauses['join'];
    24782481                        $where .= $clauses['where'];
    24792482                }
  • wp-includes/meta.php

     
    353353}
    354354
    355355/**
    356  * Given a meta query, generates SQL clauses to be appended to a main query
     356 * Given a taxonomy query, generates SQL 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, $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 {
    382380
    383         $meta_id_column = esc_sql( $type . '_id' );
     381        /**
     382         * List of metadata queries. A single query is an associative array:
     383         * - 'key' string The meta key
     384         * - 'value' string|array The meta value
     385         * - 'compare' (optional) string How to compare the key to the value.
     386         *              Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
     387         *              Default: '='
     388         * - 'type' string (optional) The type of the value.
     389         *              Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
     390         *              Default: 'CHAR'
     391         *
     392         * @since 3.2
     393         * @access public
     394         * @var array
     395         */
     396        public $queries = array();
    384397
    385         $join = '';
    386         $where = '';
    387         $i = 0;
    388         foreach ( $meta_query as $q ) {
    389                 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
    390                 $meta_value = isset( $q['value'] ) ? $q['value'] : '';
    391                 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
    392                 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
     398        /**
     399         * Constructor
     400         *
     401         * @param array (optional) $meta_query A meta query
     402         */
     403        function __construct( $meta_query = false ) {
     404                if ( $meta_query )
     405                        $this->queries = $meta_query;
     406        }
    393407
    394                 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
    395                         $meta_compare = '=';
     408        /**
     409         * Populates the $queries property by looking for 'meta_*' query variables
     410         *
     411         * @since 3.2
     412         * @access public
     413         *
     414         * @param array $qv The query variables
     415         */
     416        function parse_query_vars( $qv ) {
     417                $this->queries = array();
    396418
    397                 if ( 'NUMERIC' == $meta_type )
    398                         $meta_type = 'SIGNED';
    399                 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
    400                         $meta_type = 'CHAR';
     419                // Simple query needs to be first for orderby=meta_value to work correctly
     420                foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {
     421                        if ( !empty( $qv[ "meta_$key" ] ) )
     422                                $this->queries[0][ $key ] = $qv[ "meta_$key" ];
     423                }
    401424
    402                 if ( empty( $meta_key ) && empty( $meta_value ) )
    403                         continue;
     425                if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
     426                        $this->queries = array_merge( $this->queries, $qv['meta_query'] );
     427                }
     428        }
    404429
    405                 $alias = $i ? 'mt' . $i : $meta_table;
     430        /**
     431         * Generates SQL clauses to be appended to a main query.
     432         *
     433         * @since 3.2
     434         * @access public
     435         *
     436         * @param string $type Type of meta
     437         * @param string $primary_table
     438         * @param string $primary_id_column
     439         * @param object $context (optional) The main query object
     440         * @return array( 'join' => $join_sql, 'where' => $where_sql )
     441         */
     442        function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
     443                global $wpdb;
    406444
    407                 $join .= "\nINNER JOIN $meta_table";
    408                 $join .= $i ? " AS $alias" : '';
    409                 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
     445                if ( ! $meta_table = _get_meta_table( $type ) )
     446                        return false;
    410447
    411                 $i++;
     448                $meta_id_column = esc_sql( $type . '_id' );
    412449
    413                 if ( !empty( $meta_key ) )
    414                         $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
     450                $join = '';
     451                $where = '';
     452                $i = 0;
     453                foreach ( $this->queries as $q ) {
     454                        $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
     455                        $meta_value = isset( $q['value'] ) ? $q['value'] : '';
     456                        $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
     457                        $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
    415458
    416                 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
    417                         if ( ! is_array( $meta_value ) )
    418                                 $meta_value = preg_split( '/[,\s]+/', $meta_value );
    419                 } else {
    420                         $meta_value = trim( $meta_value );
    421                 }
     459                        if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
     460                                $meta_compare = '=';
    422461
    423                 if ( empty( $meta_value ) )
    424                         continue;
     462                        if ( 'NUMERIC' == $meta_type )
     463                                $meta_type = 'SIGNED';
     464                        elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
     465                                $meta_type = 'CHAR';
    425466
    426                 if ( 'IN' == substr( $meta_compare, -2) ) {
    427                         $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
    428                 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
    429                         $meta_value = array_slice( $meta_value, 0, 2 );
    430                         $meta_compare_string = '%s AND %s';
    431                 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
    432                         $meta_value = '%' . like_escape( $meta_value ) . '%';
    433                         $meta_compare_string = '%s';
    434                 } else {
    435                         $meta_compare_string = '%s';
    436                 }
     467                        if ( empty( $meta_key ) && empty( $meta_value ) )
     468                                continue;
    437469
    438                 // @todo Temporary hack to support empty values. Do not use outside of core.
    439                 if ( '_wp_zero_value' == $meta_value )
    440                         $meta_value = 0;
     470                        $alias = $i ? 'mt' . $i : $meta_table;
    441471
    442                 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
    443         }
     472                        $join .= "\nINNER JOIN $meta_table";
     473                        $join .= $i ? " AS $alias" : '';
     474                        $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
    444475
    445         return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $type, $primary_table, $primary_id_column, &$context ) );
    446 }
     476                        $i++;
    447477
    448 /**
    449  * Populates the $meta_query property
    450  *
    451  * @access private
    452  * @since 3.1.0
    453  *
    454  * @param array $qv The query variables
    455  */
    456 function _parse_meta_query( &$qv ) {
    457         $meta_query = array();
     478                        if ( !empty( $meta_key ) )
     479                                $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
    458480
    459         // Simple query needs to be first for orderby=meta_value to work correctly
    460         foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {
    461                 if ( !empty( $qv[ "meta_$key" ] ) )
    462                         $meta_query[0][ $key ] = $qv[ "meta_$key" ];
    463         }
     481                        if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
     482                                if ( ! is_array( $meta_value ) )
     483                                        $meta_value = preg_split( '/[,\s]+/', $meta_value );
     484                        } else {
     485                                $meta_value = trim( $meta_value );
     486                        }
    464487
    465         if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
    466                 $meta_query = array_merge( $meta_query, $qv['meta_query'] );
     488                        if ( empty( $meta_value ) )
     489                                continue;
     490
     491                        if ( 'IN' == substr( $meta_compare, -2) ) {
     492                                $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
     493                        } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
     494                                $meta_value = array_slice( $meta_value, 0, 2 );
     495                                $meta_compare_string = '%s AND %s';
     496                        } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
     497                                $meta_value = '%' . like_escape( $meta_value ) . '%';
     498                                $meta_compare_string = '%s';
     499                        } else {
     500                                $meta_compare_string = '%s';
     501                        }
     502
     503                        // @todo Temporary hack to support empty values. Do not use outside of core.
     504                        if ( '_wp_zero_value' == $meta_value )
     505                                $meta_value = 0;
     506
     507                        $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
     508                }
     509
     510                return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
    467511        }
    468 
    469         $qv['meta_query'] = $meta_query;
    470512}
    471513
    472514/**
     
    488530
    489531        return $wpdb->$table_name;
    490532}
     533
    491534?>