Changeset 42343 for trunk/src/wp-includes/class-wp-meta-query.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/class-wp-meta-query.php (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-meta-query.php
r41688 r42343 101 101 * @since 4.2.0 Introduced support for naming query clauses by associative array keys. 102 102 * 103 *104 103 * @param array $meta_query { 105 104 * Array of meta query clauses. When first-order clauses or sub-clauses use strings as … … 126 125 */ 127 126 public function __construct( $meta_query = false ) { 128 if ( ! $meta_query )127 if ( ! $meta_query ) { 129 128 return; 129 } 130 130 131 131 if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) { … … 162 162 continue; 163 163 164 // First-order clause.164 // First-order clause. 165 165 } elseif ( $this->is_first_order_clause( $query ) ) { 166 166 if ( isset( $query['value'] ) && array() === $query['value'] ) { … … 170 170 $clean_queries[ $key ] = $query; 171 171 172 // Otherwise, it's a nested query, so we recurse.172 // Otherwise, it's a nested query, so we recurse. 173 173 } else { 174 174 $cleaned_query = $this->sanitize_query( $query ); … … 187 187 if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) { 188 188 $clean_queries['relation'] = 'OR'; 189 $this->has_or_relation = true;190 191 /*192 * If there is only a single clause, call the relation 'OR'.193 * This value will not actually be used to join clauses, but it194 * simplifies the logic around combining key-only queries.195 */189 $this->has_or_relation = true; 190 191 /* 192 * If there is only a single clause, call the relation 'OR'. 193 * This value will not actually be used to join clauses, but it 194 * simplifies the logic around combining key-only queries. 195 */ 196 196 } elseif ( 1 === count( $clean_queries ) ) { 197 197 $clean_queries['relation'] = 'OR'; 198 198 199 // Default to AND.199 // Default to AND. 200 200 } else { 201 201 $clean_queries['relation'] = 'AND'; … … 276 276 */ 277 277 public function get_cast_for_type( $type = '' ) { 278 if ( empty( $type ) ) 278 if ( empty( $type ) ) { 279 279 return 'CHAR'; 280 } 280 281 281 282 $meta_type = strtoupper( $type ); 282 283 283 if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) ) 284 if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) ) { 284 285 return 'CHAR'; 285 286 if ( 'NUMERIC' == $meta_type ) 286 } 287 288 if ( 'NUMERIC' == $meta_type ) { 287 289 $meta_type = 'SIGNED'; 290 } 288 291 289 292 return $meta_type; … … 365 368 */ 366 369 $queries = $this->queries; 367 $sql = $this->get_sql_for_query( $queries );370 $sql = $this->get_sql_for_query( $queries ); 368 371 369 372 if ( ! empty( $sql['where'] ) ) { … … 405 408 $indent = ''; 406 409 for ( $i = 0; $i < $depth; $i++ ) { 407 $indent .= " ";410 $indent .= ' '; 408 411 } 409 412 … … 427 430 428 431 $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] ); 429 // This is a subquery, so we recurse.432 // This is a subquery, so we recurse. 430 433 } else { 431 434 $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 ); … … 483 486 $sql_chunks = array( 484 487 'where' => array(), 485 'join' => array(),488 'join' => array(), 486 489 ); 487 490 … … 492 495 } 493 496 494 if ( ! in_array( $clause['compare'], array( 495 '=', '!=', '>', '>=', '<', '<=', 496 'LIKE', 'NOT LIKE', 497 'IN', 'NOT IN', 498 'BETWEEN', 'NOT BETWEEN', 499 'EXISTS', 'NOT EXISTS', 500 'REGEXP', 'NOT REGEXP', 'RLIKE' 501 ) ) ) { 497 if ( ! in_array( 498 $clause['compare'], array( 499 '=', 500 '!=', 501 '>', 502 '>=', 503 '<', 504 '<=', 505 'LIKE', 506 'NOT LIKE', 507 'IN', 508 'NOT IN', 509 'BETWEEN', 510 'NOT BETWEEN', 511 'EXISTS', 512 'NOT EXISTS', 513 'REGEXP', 514 'NOT REGEXP', 515 'RLIKE', 516 ) 517 ) ) { 502 518 $clause['compare'] = '='; 503 519 } … … 511 527 $alias = $this->find_compatible_table_alias( $clause, $parent_query ); 512 528 if ( false === $alias ) { 513 $i = count( $this->table_aliases );529 $i = count( $this->table_aliases ); 514 530 $alias = $i ? 'mt' . $i : $this->meta_table; 515 531 … … 520 536 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] ); 521 537 522 // All other JOIN clauses.538 // All other JOIN clauses. 523 539 } else { 524 540 $join .= " INNER JOIN $this->meta_table"; … … 528 544 529 545 $this->table_aliases[] = $alias; 530 $sql_chunks['join'][] = $join;546 $sql_chunks['join'][] = $join; 531 547 } 532 548 … … 535 551 536 552 // Determine the data type. 537 $_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';538 $meta_type = $this->get_cast_for_type( $_meta_type );553 $_meta_type = isset( $clause['type'] ) ? $clause['type'] : ''; 554 $meta_type = $this->get_cast_for_type( $_meta_type ); 539 555 $clause['cast'] = $meta_type; 540 556 … … 545 561 546 562 // Ensure unique clause keys, so none are overwritten. 547 $iterator = 1;563 $iterator = 1; 548 564 $clause_key_base = $clause_key; 549 565 while ( isset( $this->clauses[ $clause_key ] ) ) { … … 579 595 580 596 switch ( $meta_compare ) { 581 case 'IN' :582 case 'NOT IN' :597 case 'IN': 598 case 'NOT IN': 583 599 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')'; 584 $where = $wpdb->prepare( $meta_compare_string, $meta_value );600 $where = $wpdb->prepare( $meta_compare_string, $meta_value ); 585 601 break; 586 602 587 case 'BETWEEN' :588 case 'NOT BETWEEN' :603 case 'BETWEEN': 604 case 'NOT BETWEEN': 589 605 $meta_value = array_slice( $meta_value, 0, 2 ); 590 $where = $wpdb->prepare( '%s AND %s', $meta_value );606 $where = $wpdb->prepare( '%s AND %s', $meta_value ); 591 607 break; 592 608 593 case 'LIKE' :594 case 'NOT LIKE' :609 case 'LIKE': 610 case 'NOT LIKE': 595 611 $meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%'; 596 $where = $wpdb->prepare( '%s', $meta_value );612 $where = $wpdb->prepare( '%s', $meta_value ); 597 613 break; 598 614 599 615 // EXISTS with a value is interpreted as '='. 600 case 'EXISTS' :616 case 'EXISTS': 601 617 $meta_compare = '='; 602 $where = $wpdb->prepare( '%s', $meta_value );618 $where = $wpdb->prepare( '%s', $meta_value ); 603 619 break; 604 620 605 621 // 'value' is ignored for NOT EXISTS. 606 case 'NOT EXISTS' :622 case 'NOT EXISTS': 607 623 $where = ''; 608 624 break; 609 625 610 default :626 default: 611 627 $where = $wpdb->prepare( '%s', $meta_value ); 612 628 break; … … 688 704 $compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' ); 689 705 690 // Clauses joined by AND with "negative" operators share a join only if they also share a key.706 // Clauses joined by AND with "negative" operators share a join only if they also share a key. 691 707 } elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) { 692 708 $compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' ); … … 711 727 * @param object $this WP_Meta_Query object. 712 728 */ 713 return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ;729 return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ); 714 730 } 715 731
Note: See TracChangeset
for help on using the changeset viewer.