Ticket #17165: 17165.diff
File 17165.diff, 11.3 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 = call_user_func_array( array( $meta_query, 'get_sql' ), array( '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
840 840 var $query_vars = array(); 841 841 842 842 /** 843 * Taxonomy query , as passed to get_tax_sql()843 * Taxonomy query container 844 844 * 845 845 * @since 3.1.0 846 846 * @access public … … 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; 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 ); 1537 $this->meta_query = new WP_Meta_Query(); 1538 $this->meta_query->parse_query_vars( $qv ); 1529 1539 1530 1540 if ( empty($qv['author']) || ($qv['author'] == '0') ) { 1531 1541 $this->is_author = false; … … 2231 2241 } 2232 2242 } 2233 2243 2234 if ( !empty( $this->tax_query->queries ) || !empty( $ q['meta_key']) ) {2244 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) { 2235 2245 $groupby = "{$wpdb->posts}.ID"; 2236 2246 } 2237 2247 … … 2474 2484 $q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] ); 2475 2485 } 2476 2486 2477 if ( !empty( $q['meta_query'] ) ) { 2478 $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) ); 2487 $this->meta_query->parse_query_vars( $q ); 2488 2489 if ( !empty( $this->meta_query->queries ) ) { 2490 $clauses = call_user_func_array( array( $this->meta_query, 'get_sql' ), array( 'post', $wpdb->posts, 'ID', &$this ) ); 2479 2491 $join .= $clauses['join']; 2480 2492 $where .= $clauses['where']; 2481 2493 } -
wp-includes/meta.php
353 353 } 354 354 355 355 /** 356 * Given a meta query, generates SQL clauses to be appended to a main query356 * Container class for a multiple metadata query. 357 357 * 358 * @since 3.1.0 359 * @access private 360 * 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' 370 * 371 * @param string $type Type of meta 372 * @param string $primary_table 373 * @param string $primary_id_column 374 * @param object $context (optional) The main query object 375 * @return array( 'join' => $join_sql, 'where' => $where_sql ) 358 * @since 3.2 376 359 */ 377 function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { 378 global $wpdb; 360 class WP_Meta_Query { 379 361 380 if ( ! $meta_table = _get_meta_table( $type ) ) 381 return false; 362 /** 363 * List of metadata queries. A single query is an associative array: 364 * - 'key' string The meta key 365 * - 'value' string|array The meta value 366 * - 'compare' (optional) string How to compare the key to the value. 367 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 368 * Default: '=' 369 * - 'type' string (optional) The type of the value. 370 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. 371 * Default: 'CHAR' 372 * 373 * @since 3.2 374 * @access public 375 * @var array 376 */ 377 public $queries = array(); 382 378 383 $meta_id_column = esc_sql( $type . '_id' ); 379 /** 380 * Constructor 381 * 382 * @param array (optional) $meta_query A meta query 383 */ 384 function __construct( $meta_query = false ) { 385 if ( $meta_query ) 386 $this->queries = $meta_query; 387 } 384 388 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'; 389 /** 390 * Populates the $queries property by looking for 'meta_*' query variables 391 * 392 * @since 3.2 393 * @access public 394 * 395 * @param array $qv The query variables 396 */ 397 function parse_query_vars( $qv ) { 398 $this->queries = array(); 393 399 394 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) 395 $meta_compare = '='; 400 // Simple query needs to be first for orderby=meta_value to work correctly 401 foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) { 402 if ( !empty( $qv[ "meta_$key" ] ) ) 403 $this->queries[0][ $key ] = $qv[ "meta_$key" ]; 404 } 396 405 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';406 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { 407 $this->queries = array_merge( $this->queries, $qv['meta_query'] ); 408 } 409 } 401 410 402 if ( empty( $meta_key ) && empty( $meta_value ) ) 403 continue; 411 /** 412 * Generates SQL clauses to be appended to a main query. 413 * 414 * @since 3.2 415 * @access public 416 * 417 * @param string $type Type of meta 418 * @param string $primary_table 419 * @param string $primary_id_column 420 * @param object $context (optional) The main query object 421 * @return array( 'join' => $join_sql, 'where' => $where_sql ) 422 */ 423 function get_sql( $type, $primary_table, $primary_id_column, $context = null ) { 424 global $wpdb; 404 425 405 $alias = $i ? 'mt' . $i : $meta_table; 426 if ( ! $meta_table = _get_meta_table( $type ) ) 427 return false; 406 428 407 $join .= "\nINNER JOIN $meta_table"; 408 $join .= $i ? " AS $alias" : ''; 409 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; 429 $meta_id_column = esc_sql( $type . '_id' ); 410 430 411 $i++; 431 $join = ''; 432 $where = ''; 433 $i = 0; 434 foreach ( $this->queries as $q ) { 435 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; 436 $meta_value = isset( $q['value'] ) ? $q['value'] : ''; 437 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '='; 438 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR'; 412 439 413 if ( !empty( $meta_key) )414 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );440 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) 441 $meta_compare = '='; 415 442 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 } 443 if ( 'NUMERIC' == $meta_type ) 444 $meta_type = 'SIGNED'; 445 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) 446 $meta_type = 'CHAR'; 422 447 423 if (empty( $meta_value ) )424 continue;448 if ( empty( $meta_key ) && empty( $meta_value ) ) 449 continue; 425 450 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 } 451 $alias = $i ? 'mt' . $i : $meta_table; 437 452 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;453 $join .= "\nINNER JOIN $meta_table"; 454 $join .= $i ? " AS $alias" : ''; 455 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; 441 456 442 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value ); 443 } 457 $i++; 444 458 445 return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $type, $primary_table, $primary_id_column, &$context ) );446 } 459 if ( !empty( $meta_key ) ) 460 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key ); 447 461 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(); 462 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 463 if ( ! is_array( $meta_value ) ) 464 $meta_value = preg_split( '/[,\s]+/', $meta_value ); 465 } else { 466 $meta_value = trim( $meta_value ); 467 } 458 468 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 } 469 if ( empty( $meta_value ) ) 470 continue; 464 471 465 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { 466 $meta_query = array_merge( $meta_query, $qv['meta_query'] ); 472 if ( 'IN' == substr( $meta_compare, -2) ) { 473 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')'; 474 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { 475 $meta_value = array_slice( $meta_value, 0, 2 ); 476 $meta_compare_string = '%s AND %s'; 477 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) { 478 $meta_value = '%' . like_escape( $meta_value ) . '%'; 479 $meta_compare_string = '%s'; 480 } else { 481 $meta_compare_string = '%s'; 482 } 483 484 // @todo Temporary hack to support empty values. Do not use outside of core. 485 if ( '_wp_zero_value' == $meta_value ) 486 $meta_value = 0; 487 488 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value ); 489 } 490 491 return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) ); 467 492 } 468 469 $qv['meta_query'] = $meta_query;470 493 } 471 494 472 495 /** … … 488 511 489 512 return $wpdb->$table_name; 490 513 } 514 491 515 ?>