Ticket #17165: 17165.8.diff
File 17165.8.diff, 12.1 KB (added by , 14 years ago) |
---|
-
wp-includes/user.php
501 501 $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query 502 502 } 503 503 504 _parse_meta_query( $qv );505 506 504 $role = trim( $qv['role'] ); 507 505 508 506 if ( $blog_id && ( $role || is_multisite() ) ) { … … 517 515 $qv['meta_query'][] = $cap_meta_query; 518 516 } 519 517 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 ); 522 523 $this->query_from .= $clauses['join']; 523 524 $this->query_where .= $clauses['where']; 524 525 } -
wp-includes/query.php
849 849 var $tax_query; 850 850 851 851 /** 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 /** 852 861 * Holds the data for a single object that is queried. 853 862 * 854 863 * Holds the contents of a post, page, category, attachment. … … 1525 1534 } 1526 1535 unset( $tax_query ); 1527 1536 1528 _parse_meta_query( $qv );1529 1530 1537 if ( empty($qv['author']) || ($qv['author'] == '0') ) { 1531 1538 $this->is_author = false; 1532 1539 } else { … … 1896 1903 // Fill again in case pre_get_posts unset some vars. 1897 1904 $q = $this->fill_query_vars($q); 1898 1905 1906 // Parse meta query 1907 $this->meta_query = new WP_Meta_Query(); 1908 $this->meta_query->parse_query_vars( $q ); 1909 1899 1910 // Set a flag if a pre_get_posts hook changed the query vars. 1900 1911 $hash = md5( serialize( $this->query_vars ) ); 1901 1912 if ( $hash != $this->query_vars_hash ) { … … 2231 2242 } 2232 2243 } 2233 2244 2234 if ( !empty( $this->tax_query->queries ) || !empty( $ q['meta_key']) ) {2245 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) { 2235 2246 $groupby = "{$wpdb->posts}.ID"; 2236 2247 } 2237 2248 … … 2462 2473 $where .= ')'; 2463 2474 } 2464 2475 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 ); 2477 2478 $join .= $clauses['join']; 2478 2479 $where .= $clauses['where']; 2479 2480 } -
wp-includes/meta.php
355 355 /** 356 356 * Given a meta query, generates SQL clauses to be appended to a main query 357 357 * 358 * @since 3.1.0 359 * @access private 358 * @since 3.2.0 360 359 * 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 370 361 * 362 * @param array (optional) $meta_query A meta query 371 363 * @param string $type Type of meta 372 364 * @param string $primary_table 373 365 * @param string $primary_id_column 374 366 * @param object $context (optional) The main query object 375 367 * @return array( 'join' => $join_sql, 'where' => $where_sql ) 376 368 */ 377 function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { 378 global $wpdb; 369 function 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 } 379 373 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 */ 379 class 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(); 382 396 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 } 384 406 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(); 392 417 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 } 395 423 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' ]; 400 427 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 } 403 432 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; 405 447 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; 409 450 410 $ i++;451 $meta_id_column = esc_sql( $type . '_id' ); 411 452 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; 414 459 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'; 418 463 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 = '='; 422 466 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 ) ) 424 473 continue; 425 } else {426 $meta_value = trim( $meta_value );427 }428 474 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; 440 476 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)"; 443 480 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++; 446 482 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 ); 457 485 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']; 463 489 464 // WP_Query sets 'meta_value' = '' by default465 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 ); 467 493 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 ) ); 470 516 } 471 517 472 $qv['meta_query'] = $meta_query;473 518 } 474 519 475 520 /**