Make WordPress Core

Changeset 56555


Ignore:
Timestamp:
09/11/2023 11:35:59 AM (15 months ago)
Author:
spacedmonkey
Message:

Taxonomy: Cache term objects in WP_Term_Query if query is filtered.

When utilizing the terms_clauses or get_terms_fields filters within WP_Term_Query and the selected fields are modified, the entire term object is now cached. This adjustment is necessary because filters can broaden the selected fields beyond just the term ID. Fields linked to the term object, such as the count or parent, may undergo modifications when queried. Caching the complete object ensures the accurate storage of these modified fields within the cache.

Props spacedmonkey, tnolte, peterwilsoncc.
Fixes #58116.

Location:
trunk
Files:
2 edited

Legend:

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

    r55671 r56555  
    739739        $limits   = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
    740740
     741        $fields_is_filtered = implode( ', ', $selects ) !== $fields;
     742
    741743        if ( $where ) {
    742744            $where = "WHERE $where";
     
    783785            } elseif ( 'count' !== $_fields ) {
    784786                if ( ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) )
    785                     || ( 'all' === $_fields && $args['pad_counts'] )
     787                    || ( 'all' === $_fields && $args['pad_counts'] || $fields_is_filtered )
    786788                ) {
    787789                    $term_ids = wp_list_pluck( $cache, 'term_id' );
     
    885887                $term_cache[]    = $object;
    886888            }
     889        } elseif ( $fields_is_filtered ) {
     890            $term_cache = $term_objects;
    887891        } else {
    888892            $term_cache = wp_list_pluck( $term_objects, 'term_id' );
  • trunk/tests/phpunit/tests/term/query.php

    r56421 r56555  
    697697        );
    698698        $this->assertSame( 0, $q->query( $args ) );
     699    }
     700
     701    /**
     702     * If fields have filtered, cached results should work.
     703     *
     704     * @ticket 58116
     705     * @group cache
     706     */
     707    public function test_query_filter_fields() {
     708        $post_id = self::factory()->post->create();
     709        register_taxonomy( 'wptests_tax', 'post' );
     710
     711        $term_id = self::factory()->term->create(
     712            array(
     713                'taxonomy' => 'wptests_tax',
     714            )
     715        );
     716        wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     717        $post_draft_id = self::factory()->post->create( array( 'post_type' => 'draft' ) );
     718        wp_set_object_terms( $post_draft_id, array( $term_id ), 'wptests_tax' );
     719
     720        add_filter( 'terms_clauses', array( $this, 'filter_fields_terms_clauses' ), 10, 3 );
     721
     722        $args = array(
     723            'taxonomy'    => 'wptests_tax',
     724            'hide_empty'  => false,
     725            'post_type'   => 'post',
     726            'post_status' => 'publish',
     727        );
     728
     729        $q1     = new WP_Term_Query();
     730        $terms1 = $q1->query( $args );
     731        $q2     = new WP_Term_Query();
     732        $terms2 = $q2->query( $args );
     733        $this->assertSameSets( wp_list_pluck( $terms1, 'term_id' ), wp_list_pluck( $terms2, 'term_id' ), 'Term IDs are expected to match' );
     734        $this->assertSameSets( wp_list_pluck( $terms1, 'count' ), wp_list_pluck( $terms2, 'count' ), 'Term counts are expected to match' );
     735    }
     736
     737    /**
     738     * Filter `terms_clauses` to change the field requested. The filter is from example code given in #58116.
     739     */
     740    public function filter_fields_terms_clauses( $clauses, $taxonomies, $args ) {
     741        global $wpdb;
     742
     743        // Set to query specific posts types if set.
     744        if ( ! empty( $args['post_type'] ) ) {
     745            $clauses['fields']  = 'DISTINCT t.term_id, tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent, COUNT(p.post_type) AS count';
     746            $clauses['join']   .= ' LEFT JOIN ' . $wpdb->term_relationships . ' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' AS p ON p.ID = r.object_id';
     747            $clauses['where']  .= " AND (p.post_type = '" . $args['post_type'] . "' OR p.post_type IS NULL)";
     748            $clauses['orderby'] = 'GROUP BY t.term_id ' . $clauses['orderby'];
     749        }
     750
     751        // Set to query posts with specific status.
     752        if ( ! empty( $args['post_status'] ) ) {
     753            $clauses['where'] .= " AND (p.post_status = '" . $args['post_status'] . "')";
     754        }
     755        return $clauses;
     756    }
     757
     758    /**
     759     * If fields have filtered, cached results should work.
     760     *
     761     * @ticket 58116
     762     * @group cache
     763     */
     764    public function test_query_filter_select_fields() {
     765        $post_id = self::factory()->post->create();
     766        register_taxonomy( 'wptests_tax', 'post' );
     767
     768        $term_id = self::factory()->term->create(
     769            array(
     770                'taxonomy' => 'wptests_tax',
     771            )
     772        );
     773        wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     774        $post_draft_id = self::factory()->post->create( array( 'post_type' => 'draft' ) );
     775        wp_set_object_terms( $post_draft_id, array( $term_id ), 'wptests_tax' );
     776
     777        add_filter( 'get_terms_fields', array( $this, 'filter_get_terms_fields' ) );
     778
     779        $args = array(
     780            'taxonomy'    => 'wptests_tax',
     781            'hide_empty'  => false,
     782            'post_type'   => 'post',
     783            'post_status' => 'publish',
     784        );
     785
     786        $q1     = new WP_Term_Query();
     787        $terms1 = $q1->query( $args );
     788        $q2     = new WP_Term_Query();
     789        $terms2 = $q2->query( $args );
     790        $this->assertSameSets( wp_list_pluck( $terms1, 'term_id' ), wp_list_pluck( $terms2, 'term_id' ), 'Term IDs are expected to match' );
     791        $this->assertSameSets( wp_list_pluck( $terms1, 'parent' ), wp_list_pluck( $terms2, 'parent' ), 'Term parent are expected to match' );
     792    }
     793
     794    /**
     795     * Filter `get_terms_fields` to change the field requested.
     796     */
     797    public function filter_get_terms_fields( $select ) {
     798        return array( 't.term_id', 'tt.parent' );
    699799    }
    700800
Note: See TracChangeset for help on using the changeset viewer.