Make WordPress Core

Changeset 43313


Ignore:
Timestamp:
05/25/2018 01:22:44 AM (7 years ago)
Author:
boonebgorges
Message:

Taxonomy: Improve cache handling when querying for terms using all_with_object_id.

When a term query using fields=all_with_object_id hits the cache, the
cached stdClass objects must be converted to WP_Term objects. This
was overlooked when WP_Term_Query was refactored to support object
queries in [38667].

Props dlh.
Fixes #44221.

Location:
trunk
Files:
2 edited

Legend:

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

    r43308 r43313  
    678678        $cache        = wp_cache_get( $cache_key, 'terms' );
    679679        if ( false !== $cache ) {
    680             if ( 'all' === $_fields ) {
     680            if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    681681                $cache = $this->populate_terms( $cache );
    682682            }
  • trunk/tests/phpunit/tests/term/query.php

    r43049 r43313  
    316316        foreach ( $query->terms as $term ) {
    317317            $this->assertSame( $t, $term->term_id );
     318        }
     319    }
     320
     321    /**
     322     * @ticket 44221
     323     */
     324    public function test_all_with_object_id_should_return_term_objects() {
     325        register_taxonomy( 'wptests_tax_1', 'post' );
     326        $posts = self::factory()->post->create_many( 2 );
     327        $t     = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     328
     329        foreach ( $posts as $p ) {
     330            wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
     331        }
     332
     333        $query = new WP_Term_Query();
     334        $args = array(
     335            'taxonomy'   => 'wptests_tax_1',
     336            'object_ids' => $posts,
     337            'fields'     => 'all_with_object_id',
     338        );
     339
     340        $terms = $query->query( $args );
     341        $this->assertNotEmpty( $terms );
     342        foreach ( $terms as $term ) {
     343            $this->assertInstanceOf( 'WP_Term', $term );
     344            $this->assertObjectHasAttribute( 'object_id', $term );
     345        }
     346
     347        // Run again to check the cached response.
     348        $terms = $query->query( $args );
     349        $this->assertNotEmpty( $terms );
     350        foreach ( $terms as $term ) {
     351            $this->assertInstanceOf( 'WP_Term', $term );
     352            $this->assertObjectHasAttribute( 'object_id', $term );
    318353        }
    319354    }
Note: See TracChangeset for help on using the changeset viewer.