Changeset 17686 for trunk/wp-includes/taxonomy.php
- Timestamp:
- 04/22/2011 07:09:48 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/taxonomy.php
r17652 r17686 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. … … 549 549 * @var array 550 550 */ 551 var$queries = array();551 public $queries = array(); 552 552 553 553 /** … … 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 * 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. 564 573 * 565 574 * Parses a compact tax query and sets defaults. … … 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'; … … 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 … … 630 637 631 638 foreach ( $this->queries as $query ) { 639 $this->clean_query( $query ); 640 641 if ( is_wp_error( $query ) ) { 642 return self::$no_results; 643 } 644 632 645 extract( $query ); 633 634 if ( ! taxonomy_exists( $taxonomy ) )635 return array( 'join' => '', 'where' => ' AND 0 = 1');636 637 $terms = array_unique( (array) $terms );638 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 646 658 647 if ( 'IN' == $operator ) { … … 662 651 continue; 663 652 else 664 return array( 'join' => '', 'where' => ' AND 0 = 1' );653 return self::$no_results; 665 654 } 666 655 … … 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 $children = array(); 728 foreach ( $query['terms'] as $term ) { 729 $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) ); 730 $children[] = $term; 731 } 732 $query['terms'] = $children; 733 } 734 735 $this->transform_query( $query, 'term_taxonomy_id' ); 736 } 737 738 /** 739 * Transforms a single query, from one field to another. 740 * 741 * @since 3.2.0 742 * @access private 743 * 744 * @param array &$query The single query 725 745 * @param string $resulting_field The resulting field 726 746 */ 727 function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {747 private function transform_query( &$query, $resulting_field ) { 728 748 global $wpdb; 729 749 730 if ( empty( $ terms) )750 if ( empty( $query['terms'] ) ) 731 751 return; 732 752 733 if ( $ field== $resulting_field )753 if ( $query['field'] == $resulting_field ) 734 754 return; 735 755 736 756 $resulting_field = esc_sql( $resulting_field ); 737 757 738 switch ( $ field) {758 switch ( $query['field'] ) { 739 759 case 'slug': 740 760 case 'name': 741 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $ terms) ) . "'";761 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'"; 742 762 $terms = $wpdb->get_col( " 743 763 SELECT $wpdb->term_taxonomy.$resulting_field 744 764 FROM $wpdb->term_taxonomy 745 765 INNER JOIN $wpdb->terms USING (term_id) 746 WHERE taxonomy = ' $taxonomy'747 AND $wpdb->terms. $fieldIN ($terms)766 WHERE taxonomy = '{$query['taxonomy']}' 767 AND $wpdb->terms.{$query['field']} IN ($terms) 748 768 " ); 749 769 break; 750 770 751 771 default: 752 $terms = implode( ',', array_map( 'intval', $ terms) );772 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 753 773 $terms = $wpdb->get_col( " 754 774 SELECT $resulting_field 755 775 FROM $wpdb->term_taxonomy 756 WHERE taxonomy = ' $taxonomy'776 WHERE taxonomy = '{$query['taxonomy']}' 757 777 AND term_id IN ($terms) 758 778 " ); 759 779 } 780 781 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 782 $query = new WP_Error( 'Inexistent terms' ); 783 return; 784 } 785 786 $query['terms'] = $terms; 787 $query['field'] = $resulting_field; 760 788 } 761 789 }
Note: See TracChangeset
for help on using the changeset viewer.