Make WordPress Core

Changeset 40918


Ignore:
Timestamp:
06/21/2017 03:56:25 AM (7 years ago)
Author:
boonebgorges
Message:

Use WP_Term_Query when transforming tax queries.

This change allows tax query transformations to be cached.

Props spacedmonkey.
Fixes #37038.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-tax-query.php

    r39662 r40918  
    596596     * Transforms a single query, from one field to another.
    597597     *
     598     * Operates on the `$query` object by reference. In the case of error,
     599     * `$query` is converted to a WP_Error object.
     600     *
    598601     * @since 3.2.0
    599602     *
     
    605608     */
    606609    public function transform_query( &$query, $resulting_field ) {
    607         global $wpdb;
    608 
    609610        if ( empty( $query['terms'] ) )
    610611            return;
     
    615616        $resulting_field = sanitize_key( $resulting_field );
    616617
     618        // Empty 'terms' always results in a null transformation.
     619        $terms = array_filter( $query['terms'] );
     620        if ( empty( $terms ) ) {
     621            $query['terms'] = array();
     622            $query['field'] = $resulting_field;
     623            return;
     624        }
     625
     626        $args = array(
     627            'get'                    => 'all',
     628            'number'                 => 0,
     629            'taxonomy'               => $query['taxonomy'],
     630            'update_term_meta_cache' => false,
     631            'orderby'                => 'none',
     632        );
     633
     634        // Term query parameter name depends on the 'field' being searched on.
    617635        switch ( $query['field'] ) {
    618636            case 'slug':
     637                $args['slug'] = $terms;
     638                break;
    619639            case 'name':
    620                 foreach ( $query['terms'] as &$term ) {
    621                     /*
    622                      * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't
    623                      * matter because `sanitize_term_field()` ignores the $term_id param when the
    624                      * context is 'db'.
    625                      */
    626                     $clean_term = sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' );
    627 
    628                     // Match sanitization in wp_insert_term().
    629                     $clean_term = wp_unslash( $clean_term );
    630 
    631                     $term = "'" . esc_sql( $clean_term ) . "'";
    632                 }
    633 
    634                 $terms = implode( ",", $query['terms'] );
    635 
    636                 $terms = $wpdb->get_col( "
    637                     SELECT $wpdb->term_taxonomy.$resulting_field
    638                     FROM $wpdb->term_taxonomy
    639                     INNER JOIN $wpdb->terms USING (term_id)
    640                     WHERE taxonomy = '{$query['taxonomy']}'
    641                     AND $wpdb->terms.{$query['field']} IN ($terms)
    642                 " );
     640                $args['name'] = $terms;
    643641                break;
    644642            case 'term_taxonomy_id':
    645                 $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
    646                 $terms = $wpdb->get_col( "
    647                     SELECT $resulting_field
    648                     FROM $wpdb->term_taxonomy
    649                     WHERE term_taxonomy_id IN ($terms)
    650                 " );
     643                $args['term_taxonomy_id'] = $terms;
    651644                break;
    652645            default:
    653                 $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
    654                 $terms = $wpdb->get_col( "
    655                     SELECT $resulting_field
    656                     FROM $wpdb->term_taxonomy
    657                     WHERE taxonomy = '{$query['taxonomy']}'
    658                     AND term_id IN ($terms)
    659                 " );
    660         }
    661 
    662         if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
     646                $args['include'] = wp_parse_id_list( $terms );
     647                break;
     648        }
     649
     650        $term_query = new WP_Term_Query();
     651        $term_list  = $term_query->query( $args );
     652
     653        if ( is_wp_error( $term_list ) ) {
     654            $query = $term_list;
     655            return;
     656        }
     657
     658        if ( 'AND' == $query['operator'] && count( $term_list ) < count( $query['terms'] ) ) {
    663659            $query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) );
    664660            return;
    665661        }
    666662
    667         $query['terms'] = $terms;
     663        $query['terms'] = wp_list_pluck( $term_list, $resulting_field );
    668664        $query['field'] = $resulting_field;
    669665    }
  • trunk/tests/phpunit/tests/term/taxQuery.php

    r39174 r40918  
    164164        $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
    165165
    166         $this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
     166        $this->assertEqualSets( $tt_ids, $tq->queries[0]['terms'] );
    167167        $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
    168168    }
     
    182182        $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
    183183
    184         $this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
     184        $this->assertEqualSets( $tt_ids, $tq->queries[0]['terms'] );
    185185        $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
    186186    }
Note: See TracChangeset for help on using the changeset viewer.