Make WordPress Core

Changeset 43314


Ignore:
Timestamp:
05/25/2018 01:25:55 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].

Merges [43313] to the 4.9 branch.

Props dlh.
Fixes #44221.

Location:
branches/4.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/class-wp-term-query.php

    r41881 r43314  
    672672        $cache = wp_cache_get( $cache_key, 'terms' );
    673673        if ( false !== $cache ) {
    674             if ( 'all' === $_fields ) {
     674            if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    675675                $cache = array_map( 'get_term', $cache );
    676676            }
  • branches/4.9/tests/phpunit/tests/term/query.php

    r41880 r43314  
    288288        foreach ( $query->terms as $term ) {
    289289            $this->assertSame( $t, $term->term_id );
     290        }
     291    }
     292
     293    /**
     294     * @ticket 44221
     295     */
     296    public function test_all_with_object_id_should_return_term_objects() {
     297        register_taxonomy( 'wptests_tax_1', 'post' );
     298        $posts = self::factory()->post->create_many( 2 );
     299        $t     = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     300
     301        foreach ( $posts as $p ) {
     302            wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
     303        }
     304
     305        $query = new WP_Term_Query();
     306        $args = array(
     307            'taxonomy'   => 'wptests_tax_1',
     308            'object_ids' => $posts,
     309            'fields'     => 'all_with_object_id',
     310        );
     311
     312        $terms = $query->query( $args );
     313        $this->assertNotEmpty( $terms );
     314        foreach ( $terms as $term ) {
     315            $this->assertInstanceOf( 'WP_Term', $term );
     316            $this->assertObjectHasAttribute( 'object_id', $term );
     317        }
     318
     319        // Run again to check the cached response.
     320        $terms = $query->query( $args );
     321        $this->assertNotEmpty( $terms );
     322        foreach ( $terms as $term ) {
     323            $this->assertInstanceOf( 'WP_Term', $term );
     324            $this->assertObjectHasAttribute( 'object_id', $term );
    290325        }
    291326    }
Note: See TracChangeset for help on using the changeset viewer.