Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r18021 r18254  
    3737        'rewrite' => did_action( 'init' ) ? array(
    3838                    'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
    39                     'with_front' => ( get_option('category_base') && ! $wp_rewrite->using_index_permalinks() ) ? false : true ) : false,
     39                    'with_front' => ( get_option('tag_base') && ! $wp_rewrite->using_index_permalinks() ) ? false : true ) : false,
    4040        'public' => true,
    4141        'show_ui' => true,
     
    355355
    356356    $args['name'] = $taxonomy;
    357     $args['object_type'] = (array) $object_type;
     357    $args['object_type'] =  array_unique( (array)$object_type );
    358358
    359359    $args['labels'] = get_taxonomy_labels( (object) $args );
     
    405405        'parent_item_colon' => array( null, __( 'Parent Category:' ) ),
    406406        'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
     407        'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),
    407408        'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
    408409        'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
     
    438439        return false;
    439440
    440     $wp_taxonomies[$taxonomy]->object_type[] = $object_type;
     441    if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )
     442        $wp_taxonomies[$taxonomy]->object_type[] = $object_type;
    441443
    442444    return true;
     
    540542     *      Default: 'term_id'
    541543     * - 'operator' string (optional)
    542      *      Possible values: 'IN' and 'NOT IN'.
     544     *      Possible values: 'AND', 'IN' or 'NOT IN'.
    543545     *      Default: 'IN'
    544546     * - 'include_children' bool (optional) Whether to include child terms.
     
    549551     * @var array
    550552     */
    551     var $queries = array();
     553    public $queries = array();
    552554
    553555    /**
     
    558560     * @var string
    559561     */
    560     var $relation;
     562    public $relation;
    561563
    562564    /**
    563      * PHP4 type constructor.
     565     * Standard response when the query should not return any rows.
     566     *
     567     * @since 3.2.0
     568     * @access private
     569     * @var string
     570     */
     571    private static $no_results = array( 'join' => '', 'where' => ' AND 0 = 1' );
     572
     573    /**
     574     * Constructor.
    564575     *
    565576     * Parses a compact tax query and sets defaults.
     
    582593     *    ),
    583594     *  )
    584      *
    585      * @return WP_Tax_Query
    586595     */
    587     function WP_Tax_Query( $tax_query ) {
     596    public function __construct( $tax_query ) {
    588597        if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
    589598            $this->relation = 'OR';
     
    622631     * @return array
    623632     */
    624     function get_sql( $primary_table, $primary_id_column ) {
     633    public function get_sql( $primary_table, $primary_id_column ) {
    625634        global $wpdb;
    626635
     
    630639
    631640        foreach ( $this->queries as $query ) {
     641            $this->clean_query( $query );
     642
     643            if ( is_wp_error( $query ) ) {
     644                return self::$no_results;
     645            }
     646
    632647            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             }
    657648
    658649            if ( 'IN' == $operator ) {
     
    662653                        continue;
    663654                    else
    664                         return array( 'join' => '', 'where' => ' AND 0 = 1' );
     655                        return self::$no_results;
    665656                }
    666657
     
    695686                $terms = implode( ',', $terms );
    696687
    697                 $where[] = "$primary_table.$primary_id_column IN (
    698                     SELECT object_id
     688                $where[] = "(
     689                    SELECT COUNT(1)
    699690                    FROM $wpdb->term_relationships
    700691                    WHERE term_taxonomy_id IN ($terms)
    701                     GROUP BY object_id HAVING COUNT(object_id) = $num_terms
    702                 )";
     692                    AND object_id = $primary_table.$primary_id_column
     693                ) = $num_terms";
    703694            }
    704695
     
    715706
    716707    /**
    717      * Transforms a list of terms, from one field to another.
     708     * Validates a single query.
    718709     *
    719      * @since 3.1.0
     710     * @since 3.2.0
    720711     * @access private
    721712     *
    722      * @param array &$terms The list of terms
    723      * @param string $taxonomy The taxonomy of the terms
    724      * @param string $field The initial field
     713     * @param array &$query The single query
     714     */
     715    private function clean_query( &$query ) {
     716        if ( ! taxonomy_exists( $query['taxonomy'] ) ) {
     717            $query = new WP_Error( 'Invalid taxonomy' );
     718            return;
     719        }
     720
     721        $query['terms'] = array_unique( (array) $query['terms'] );
     722
     723        if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {
     724            $this->transform_query( $query, 'term_id' );
     725
     726            if ( is_wp_error( $query ) )
     727                return;
     728
     729            $children = array();
     730            foreach ( $query['terms'] as $term ) {
     731                $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) );
     732                $children[] = $term;
     733            }
     734            $query['terms'] = $children;
     735        }
     736
     737        $this->transform_query( $query, 'term_taxonomy_id' );
     738    }
     739
     740    /**
     741     * Transforms a single query, from one field to another.
     742     *
     743     * @since 3.2.0
     744     * @access private
     745     *
     746     * @param array &$query The single query
    725747     * @param string $resulting_field The resulting field
    726748     */
    727     function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {
     749    private function transform_query( &$query, $resulting_field ) {
    728750        global $wpdb;
    729751
    730         if ( empty( $terms ) )
     752        if ( empty( $query['terms'] ) )
    731753            return;
    732754
    733         if ( $field == $resulting_field )
     755        if ( $query['field'] == $resulting_field )
    734756            return;
    735757
    736758        $resulting_field = esc_sql( $resulting_field );
    737759
    738         switch ( $field ) {
     760        switch ( $query['field'] ) {
    739761            case 'slug':
    740762            case 'name':
    741                 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $terms ) ) . "'";
     763                $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'";
    742764                $terms = $wpdb->get_col( "
    743765                    SELECT $wpdb->term_taxonomy.$resulting_field
    744766                    FROM $wpdb->term_taxonomy
    745767                    INNER JOIN $wpdb->terms USING (term_id)
    746                     WHERE taxonomy = '$taxonomy'
    747                     AND $wpdb->terms.$field IN ($terms)
     768                    WHERE taxonomy = '{$query['taxonomy']}'
     769                    AND $wpdb->terms.{$query['field']} IN ($terms)
    748770                " );
    749771                break;
    750772
    751773            default:
    752                 $terms = implode( ',', array_map( 'intval', $terms ) );
     774                $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
    753775                $terms = $wpdb->get_col( "
    754776                    SELECT $resulting_field
    755777                    FROM $wpdb->term_taxonomy
    756                     WHERE taxonomy = '$taxonomy'
     778                    WHERE taxonomy = '{$query['taxonomy']}'
    757779                    AND term_id IN ($terms)
    758780                " );
    759781        }
     782
     783        if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
     784            $query = new WP_Error( 'Inexistent terms' );
     785            return;
     786        }
     787
     788        $query['terms'] = $terms;
     789        $query['field'] = $resulting_field;
    760790    }
    761791}
     
    11011131 * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
    11021132 *
     1133 * The 'cache_domain' argument enables a unique cache key to be produced when this query is stored
     1134 * in object cache. For instance, if you are using one of this function's filters to modify the
     1135 * query (such as 'terms_clauses'), setting 'cache_domain' to a unique value will not overwrite
     1136 * the cache for similar queries. Default value is 'core'.
     1137 *
    11031138 * @package WordPress
    11041139 * @subpackage Taxonomy
     
    11331168        'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
    11341169        'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
    1135         'pad_counts' => false, 'offset' => '', 'search' => '');
     1170        'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
    11361171    $args = wp_parse_args( $args, $defaults );
    11371172    $args['number'] = absint( $args['number'] );
Note: See TracChangeset for help on using the changeset viewer.