Ticket #15032: 15032.diff
File 15032.diff, 7.6 KB (added by , 14 years ago) |
---|
-
wp-includes/functions.php
4257 4257 } 4258 4258 4259 4259 /* 4260 * Used internally to generate an SQL string for searching across multiple meta key = value pairs4261 *4262 * @access private4263 * @since 3.1.04264 *4265 * @param array $queries An array of queries4266 * @param string $primary_table4267 * @param string $primary_id_column4268 * @param string $meta_table4269 * @param string $meta_id_column4270 * @return array( $join_sql, $where_sql )4271 */4272 function _wp_meta_sql( $queries, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {4273 global $wpdb;4274 4275 $clauses = array();4276 4277 $join = '';4278 $where = '';4279 $i = 0;4280 foreach ( $queries as $q ) {4281 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';4282 $meta_value = isset( $q['value'] ) ? trim( $q['value'] ) : '';4283 $meta_compare = isset( $q['compare'] ) ? $q['compare'] : '=';4284 4285 if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) )4286 $meta_compare = '=';4287 4288 if ( empty( $meta_key ) && empty( $meta_value ) )4289 continue;4290 4291 $alias = $i ? 'mt' . $i : $meta_table;4292 4293 $join .= "\nINNER JOIN $meta_table";4294 $join .= $i ? " AS $alias" : '';4295 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";4296 4297 $i++;4298 4299 if ( !empty( $meta_key ) )4300 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );4301 4302 if ( empty( $meta_value ) )4303 continue;4304 4305 if ( 'like' == $meta_compare ) {4306 $where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' );4307 } else {4308 $where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value );4309 }4310 }4311 4312 return array( $join, $where );4313 }4314 4315 /*4316 4260 * Used internally to tidy up the search terms 4317 4261 * 4318 4262 * @access private -
wp-includes/user.php
330 330 * 331 331 * @since 3.1.0 332 332 */ 333 class WP_User_Query {333 class WP_User_Query extends WP_Object_Query { 334 334 335 335 /** 336 336 * List of found user ids … … 444 444 $this->query_where .= _wp_search_sql( $search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') ); 445 445 } 446 446 447 $this->parse_meta_query( $qv ); 448 447 449 $role = trim( $qv['role'] ); 448 450 $blog_id = absint( $qv['blog_id'] ); 449 451 450 $meta_queries = array();451 452 452 if ( $blog_id ) { 453 453 $cap_meta_query = array(); 454 454 $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; … … 458 458 $cap_meta_query['compare'] = 'like'; 459 459 } 460 460 461 $ meta_queries[] = $cap_meta_query;461 $this->meta_query[] = $cap_meta_query; 462 462 } 463 463 464 $meta_query = array(); 465 foreach ( array( 'key', 'value', 'compare' ) as $key ) { 466 if ( !empty( $qv[ "meta_$key" ] ) ) 467 $meta_query[ $key ] = $qv[ "meta_$key" ]; 468 } 469 470 if ( !empty( $meta_query ) ) { 471 $meta_queries[] = $meta_query; 472 } 473 474 list( $meta_join, $meta_where ) = _wp_meta_sql( $meta_queries, $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' ); 464 list( $meta_join, $meta_where ) = $this->get_meta_sql( $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' ); 475 465 $this->query_from .= $meta_join; 476 466 $this->query_where .= $meta_where; 477 467 -
wp-includes/query.php
641 641 * 642 642 * @since 1.5.0 643 643 */ 644 class WP_Query {644 class WP_Query extends WP_Object_Query { 645 645 646 646 /** 647 647 * Query vars set by the user … … 671 671 var $tax_query = array(); 672 672 673 673 /** 674 * Metadata query675 *676 * @since 3.1.0677 * @access public678 * @var array679 */680 var $meta_query = array();681 682 /**683 674 * Holds the data for a single object that is queried. 684 675 * 685 676 * Holds the contents of a post, page, category, attachment. … … 1389 1380 $this->is_tax = true; 1390 1381 } 1391 1382 1392 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { 1393 $this->meta_query = $qv['meta_query']; 1394 } 1383 $this->parse_meta_query( $qv ); 1395 1384 1396 $meta_query = array();1397 foreach ( array( 'key', 'value', 'compare' ) as $key ) {1398 if ( !empty( $qv[ "meta_$key" ] ) )1399 $meta_query[ $key ] = $qv[ "meta_$key" ];1400 }1401 1402 if ( !empty( $meta_query ) ) {1403 $this->meta_query[] = $meta_query;1404 }1405 1406 1385 if ( empty($qv['author']) || ($qv['author'] == '0') ) { 1407 1386 $this->is_author = false; 1408 1387 } else { … … 2211 2190 $where .= ')'; 2212 2191 } 2213 2192 2214 list( $meta_join, $meta_where ) = _wp_meta_sql( $this->meta_query,$wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' );2193 list( $meta_join, $meta_where ) = $this->get_meta_sql( $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' ); 2215 2194 $join .= $meta_join; 2216 2195 $where .= $meta_where; 2217 2196 -
wp-includes/classes.php
525 525 } 526 526 527 527 /** 528 * WordPress Query class. 529 * 530 * Abstract class for handling advanced queries 531 * 532 * @package WordPress 533 * @since 3.1.0 534 */ 535 class WP_Object_Query { 536 537 /** 538 * Metadata query 539 * 540 * @since 3.1.0 541 * @access public 542 * @var array 543 */ 544 var $meta_query = array(); 545 546 /* 547 * Populates the $meta_query property 548 * 549 * @access private 550 * @since 3.1.0 551 * 552 * @param array $qv The query variables 553 */ 554 function parse_meta_query( $qv ) { 555 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { 556 $this->meta_query = $qv['meta_query']; 557 } 558 559 $meta_query = array(); 560 foreach ( array( 'key', 'value', 'compare' ) as $key ) { 561 if ( !empty( $qv[ "meta_$key" ] ) ) 562 $meta_query[ $key ] = $qv[ "meta_$key" ]; 563 } 564 565 if ( !empty( $meta_query ) ) { 566 $this->meta_query[] = $meta_query; 567 } 568 } 569 570 /* 571 * Used internally to generate an SQL string for searching across multiple meta key = value pairs 572 * 573 * @access private 574 * @since 3.1.0 575 * 576 * @param string $primary_table 577 * @param string $primary_id_column 578 * @param string $meta_table 579 * @param string $meta_id_column 580 * @return array( $join_sql, $where_sql ) 581 */ 582 function get_meta_sql( $primary_table, $primary_id_column, $meta_table, $meta_id_column ) { 583 global $wpdb; 584 585 $clauses = array(); 586 587 $join = ''; 588 $where = ''; 589 $i = 0; 590 foreach ( $this->meta_query as $q ) { 591 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; 592 $meta_value = isset( $q['value'] ) ? trim( $q['value'] ) : ''; 593 $meta_compare = isset( $q['compare'] ) ? $q['compare'] : '='; 594 595 if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) ) 596 $meta_compare = '='; 597 598 if ( empty( $meta_key ) && empty( $meta_value ) ) 599 continue; 600 601 $alias = $i ? 'mt' . $i : $meta_table; 602 603 $join .= "\nINNER JOIN $meta_table"; 604 $join .= $i ? " AS $alias" : ''; 605 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; 606 607 $i++; 608 609 if ( !empty( $meta_key ) ) 610 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key ); 611 612 if ( empty( $meta_value ) ) 613 continue; 614 615 if ( 'like' == $meta_compare ) { 616 $where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' ); 617 } else { 618 $where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value ); 619 } 620 } 621 622 return array( $join, $where ); 623 } 624 } 625 626 /** 528 627 * WordPress Error class. 529 628 * 530 629 * Container for checking for WordPress errors and error messages. Return