Make WordPress Core

Ticket #17165: 17165.relation.diff

File 17165.relation.diff, 12.5 KB (added by greuben, 13 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 {
     
    19001907                // Fill again in case pre_get_posts unset some vars.
    19011908                $q = $this->fill_query_vars($q);
    19021909
     1910                // Parse meta query
     1911                $this->meta_query = new WP_Meta_Query();
     1912                $this->meta_query->parse_query_vars( $q );
     1913
    19031914                // Set a flag if a pre_get_posts hook changed the query vars.
    19041915                $hash = md5( serialize( $this->query_vars ) );
    19051916                if ( $hash != $this->query_vars_hash ) {
     
    22352246                        }
    22362247                }
    22372248
    2238                 if ( !empty( $this->tax_query->queries ) || !empty( $q['meta_key'] ) ) {
     2249                if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    22392250                        $groupby = "{$wpdb->posts}.ID";
    22402251                }
    22412252
     
    24682479                        $where .= ')';
    24692480                }
    24702481
    2471                 // Parse the meta query again if query vars have changed.
    2472                 if ( $this->query_vars_changed ) {
    2473                         $meta_query_hash = md5( serialize( $q['meta_query'] ) );
    2474                         $_meta_query = $q['meta_query'];
    2475                         unset( $q['meta_query'] );
    2476                         _parse_meta_query( $q );
    2477                         if ( md5( serialize( $q['meta_query'] ) ) != $meta_query_hash && is_array( $_meta_query ) )
    2478                                 $q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
    2479                 }
    2480 
    2481                 if ( !empty( $q['meta_query'] ) ) {
    2482                         $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
     2482                if ( !empty( $this->meta_query->queries ) ) {
     2483                        $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    24832484                        $join .= $clauses['join'];
    24842485                        $where .= $clauses['where'];
    24852486                }
  • 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         * The relation between the queries. Can be one of 'AND' or 'OR'.
     399         *
     400         * @since 3.2
     401         * @access public
     402         * @var string
     403         */     
     404        public $relation;
    384405
    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';
     406        /**
     407         * Constructor
     408         *
     409         * @param array (optional) $meta_query A meta query
     410         */
     411        function __construct( $meta_query = false ) {
     412                if( $meta_query )
     413                        $this->queries = $meta_query;
     414        }
    392415
    393                 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
    394                         $meta_compare = '=';
     416        /**
     417         * Populates the $queries property by looking for 'meta_*' query variables
     418         *
     419         * @since 3.2
     420         * @access public
     421         *
     422         * @param array $qv The query variables
     423         */
     424        function parse_query_vars( $qv ) {
     425                $this->queries = array();
    395426
    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';
     427                // Simple query needs to be first for orderby=meta_value to work correctly
     428                foreach ( array( 'key', 'compare', 'type' ) as $key ) {
     429                        if ( !empty( $qv[ "meta_$key" ] ) )
     430                                $this->queries[0][ $key ] = $qv[ "meta_$key" ];
     431                }
    400432
    401                 if ( empty( $meta_key ) && empty( $meta_value ) )
    402                         continue;
     433                // WP_Query sets 'meta_value' = '' by default
     434                if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
     435                        $this->queries[0]['value'] = $qv[ 'meta_value' ];
    403436
    404                 $alias = $i ? 'mt' . $i : $meta_table;
     437                if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
     438                        $this->queries = array_merge( $this->queries, $qv['meta_query'] );
     439                }
    405440
    406                 $join .= "\nINNER JOIN $meta_table";
    407                 $join .= $i ? " AS $alias" : '';
    408                 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
     441                if( isset( $this->queries['relation'] ) && strtoupper( $this->queries['relation'] ) == 'OR' ) {
     442                        $this->relation = 'OR';
     443                } else {
     444                        $this->relation = 'AND';
     445                }
     446        }
    409447
    410                 $i++;
     448        /**
     449         * Generates SQL clauses to be appended to a main query.
     450         *
     451         * @since 3.2
     452         * @access public
     453         *
     454         * @param string $type Type of meta
     455         * @param string $primary_table
     456         * @param string $primary_id_column
     457         * @param object $context (optional) The main query object
     458         * @return array( 'join' => $join_sql, 'where' => $where_sql )
     459         */
     460        function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
     461                global $wpdb;
    411462
    412                 if ( !empty( $meta_key ) )
    413                         $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
     463                if ( ! $meta_table = _get_meta_table( $type ) )
     464                        return false;
    414465
    415                 if ( !isset( $q['value'] ) )
    416                         continue;
    417                 $meta_value = $q['value'];
     466                $meta_id_column = esc_sql( $type . '_id' );
    418467
    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 );
     468                $join = '';
     469                $where = array();
     470                $i = 0;
     471                foreach ( $this->queries as $k => $q ) {
     472                        if( ! is_array( $q ) )
     473                                continue;
    422474
    423                         if ( empty( $meta_value ) )
     475                        $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
     476                        $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
     477                        $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
     478
     479                        if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
     480                                $meta_compare = '=';
     481
     482                        if ( 'NUMERIC' == $meta_type )
     483                                $meta_type = 'SIGNED';
     484                        elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
     485                                $meta_type = 'CHAR';
     486
     487                        if ( empty( $meta_key ) && empty( $meta_value ) )
    424488                                continue;
    425                 } else {
    426                         $meta_value = trim( $meta_value );
    427                 }
    428489
    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                 }
     490                        $alias = $i ? 'mt' . $i : $meta_table;
    440491
    441                 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
    442         }
     492                        $join .= "\nINNER JOIN $meta_table";
     493                        $join .= $i ? " AS $alias" : '';
     494                        $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
    443495
    444         return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $type, $primary_table, $primary_id_column, &$context ) );
    445 }
     496                        $i++;
    446497
    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();
     498                        if ( !empty( $meta_key ) )
     499                                $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
    457500
    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         }
     501                        if ( !isset( $q['value'] ) )
     502                                continue;
     503                        $meta_value = $q['value'];
    463504
    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' ];
     505                        if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
     506                                if ( ! is_array( $meta_value ) )
     507                                        $meta_value = preg_split( '/[,\s]+/', $meta_value );
    467508
    468         if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
    469                 $meta_query = array_merge( $meta_query, $qv['meta_query'] );
     509                                if ( empty( $meta_value ) )
     510                                        continue;
     511                        } else {
     512                                $meta_value = trim( $meta_value );
     513                        }
     514
     515                        if ( 'IN' == substr( $meta_compare, -2) ) {
     516                                $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
     517                        } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
     518                                $meta_value = array_slice( $meta_value, 0, 2 );
     519                                $meta_compare_string = '%s AND %s';
     520                        } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
     521                                $meta_value = '%' . like_escape( $meta_value ) . '%';
     522                                $meta_compare_string = '%s';
     523                        } else {
     524                                $meta_compare_string = '%s';
     525                        }
     526
     527                        $where[$k] = ' (' . $where[$k] . $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );
     528                }
     529                $where = ' AND (' . implode( " $this->relation ", $where ) . ' )';
     530
     531                return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
    470532        }
    471533
    472         $qv['meta_query'] = $meta_query;
    473534}
    474535
    475536/**