Ticket #17194: 17194.2.diff
| File 17194.2.diff, 5.8 KB (added by , 15 years ago) |
|---|
-
wp-includes/taxonomy.php
539 539 * Possible values: 'term_id', 'slug' or 'name' 540 540 * Default: 'term_id' 541 541 * - 'operator' string (optional) 542 * Possible values: ' IN' and'NOT IN'.542 * Possible values: 'AND', 'IN' or 'NOT IN'. 543 543 * Default: 'IN' 544 544 * - 'include_children' bool (optional) Whether to include child terms. 545 545 * Default: true … … 548 548 * @access public 549 549 * @var array 550 550 */ 551 var$queries = array();551 public $queries = array(); 552 552 553 553 /** 554 554 * The relation between the queries. Can be one of 'AND' or 'OR'. … … 557 557 * @access public 558 558 * @var string 559 559 */ 560 var$relation;560 public $relation; 561 561 562 562 /** 563 * PHP4 type constructor.563 * Standard response when the query should not return any rows. 564 564 * 565 * @since 3.2.0 566 * @access private 567 * @var string 568 */ 569 private static $no_results = array( 'join' => '', 'where' => ' AND 0 = 1' ); 570 571 /** 572 * Constructor. 573 * 565 574 * Parses a compact tax query and sets defaults. 566 575 * 567 576 * @since 3.1.0 … … 581 590 * 'field' => 'slug', 582 591 * ), 583 592 * ) 584 *585 * @return WP_Tax_Query586 593 */ 587 function WP_Tax_Query( $tax_query ) {594 public function __construct( $tax_query ) { 588 595 if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) { 589 596 $this->relation = 'OR'; 590 597 } else { … … 621 628 * @param string $primary_id_column 622 629 * @return array 623 630 */ 624 function get_sql( $primary_table, $primary_id_column ) {631 public function get_sql( $primary_table, $primary_id_column ) { 625 632 global $wpdb; 626 633 627 634 $join = ''; … … 629 636 $i = 0; 630 637 631 638 foreach ( $this->queries as $query ) { 632 extract( $query );639 $this->clean_query( $query ); 633 640 634 if ( ! taxonomy_exists( $taxonomy ) ) 635 return array( 'join' => '', 'where' => ' AND 0 = 1'); 641 if ( is_wp_error( $query ) ) { 642 return self::$no_results; 643 } 636 644 637 $terms = array_unique( (array) $terms);645 extract( $query ); 638 646 639 if ( empty( $terms ) )640 continue;641 642 if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) {643 $this->_transform_terms( $terms, $taxonomy, $field, 'term_id' );644 645 $children = array();646 foreach ( $terms as $term ) {647 $children = array_merge( $children, get_term_children( $term, $taxonomy ) );648 $children[] = $term;649 }650 $terms = $children;651 652 $this->_transform_terms( $terms, $taxonomy, 'term_id', 'term_taxonomy_id' );653 }654 else {655 $this->_transform_terms( $terms, $taxonomy, $field, 'term_taxonomy_id' );656 }657 658 647 if ( 'IN' == $operator ) { 659 648 660 649 if ( empty( $terms ) ) { 661 650 if ( 'OR' == $this->relation ) 662 651 continue; 663 652 else 664 return array( 'join' => '', 'where' => ' AND 0 = 1' );653 return self::$no_results; 665 654 } 666 655 667 656 $terms = implode( ',', $terms ); … … 714 703 } 715 704 716 705 /** 717 * Transforms a list of terms, from one field to another.706 * Validates a single query. 718 707 * 719 * @since 3. 1.0708 * @since 3.2.0 720 709 * @access private 721 710 * 722 * @param array &$terms The list of terms 723 * @param string $taxonomy The taxonomy of the terms 724 * @param string $field The initial field 711 * @param array &$query The single query 712 */ 713 private function clean_query( &$query ) { 714 if ( ! taxonomy_exists( $query['taxonomy'] ) ) { 715 $query = new WP_Error( 'Invalid taxonomy' ); 716 return; 717 } 718 719 $query['terms'] = array_unique( (array) $query['terms'] ); 720 721 if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) { 722 $this->transform_query( $query, 'term_id' ); 723 724 if ( is_wp_error( $query ) ) { 725 return; 726 } 727 728 $children = array(); 729 foreach ( $query['terms'] as $term ) { 730 $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) ); 731 $children[] = $term; 732 } 733 $query['terms'] = $children; 734 } 735 736 $this->transform_query( $query, 'term_taxonomy_id' ); 737 } 738 739 /** 740 * Transforms a single query, from one field to another. 741 * 742 * @since 3.2.0 743 * @access private 744 * 745 * @param array &$query The single query 725 746 * @param string $resulting_field The resulting field 726 747 */ 727 function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {748 private function transform_query( &$query, $resulting_field ) { 728 749 global $wpdb; 729 750 730 if ( empty( $ terms) )751 if ( empty( $query['terms'] ) ) 731 752 return; 732 753 733 if ( $ field== $resulting_field )754 if ( $query['field'] == $resulting_field ) 734 755 return; 735 756 736 757 $resulting_field = esc_sql( $resulting_field ); 737 758 738 switch ( $ field) {759 switch ( $query['field'] ) { 739 760 case 'slug': 740 761 case 'name': 741 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $ terms) ) . "'";762 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'"; 742 763 $terms = $wpdb->get_col( " 743 764 SELECT $wpdb->term_taxonomy.$resulting_field 744 765 FROM $wpdb->term_taxonomy 745 766 INNER JOIN $wpdb->terms USING (term_id) 746 WHERE taxonomy = ' $taxonomy'747 AND $wpdb->terms. $fieldIN ($terms)767 WHERE taxonomy = '{$query['taxonomy']}' 768 AND $wpdb->terms.{$query['field']} IN ($terms) 748 769 " ); 749 770 break; 750 771 751 772 default: 752 $terms = implode( ',', array_map( 'intval', $ terms) );773 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 753 774 $terms = $wpdb->get_col( " 754 775 SELECT $resulting_field 755 776 FROM $wpdb->term_taxonomy 756 WHERE taxonomy = ' $taxonomy'777 WHERE taxonomy = '{$query['taxonomy']}' 757 778 AND term_id IN ($terms) 758 779 " ); 759 780 } 781 782 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 783 $query = new WP_Error( 'Inexistent terms' ); 784 return; 785 } 786 787 $query['terms'] = $terms; 788 $query['field'] = $resulting_field; 760 789 } 761 790 } 762 791