WordPress.org

Make WordPress Core

Changeset 40353


Ignore:
Timestamp:
03/30/2017 04:49:47 PM (3 years ago)
Author:
boonebgorges
Message:

Invalidate term query caches when setting or deleting term relationships.

Prior to 4.7, term relationships - as set by wp_set_object_terms() or
wp_remove_object_terms() - did not affect the term query cache. The
introduction of the 'object_ids' parameter in 4.7 means that the query
cache must be aware of object-term relationships. As such, the
'last_changed' incrementor is now invalidated when term relationships
are modified.

This bug only reared its head when delaying term counting, because term
counting performs its own term query cache invalidation.

Props mboynes.
Fixes #40306.

Location:
trunk
Files:
2 edited

Legend:

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

    r40298 r40353  
    22832283
    22842284    wp_cache_delete( $object_id, $taxonomy . '_relationships' );
     2285    wp_cache_delete( 'last_changed', 'terms' );
    22852286
    22862287    /**
     
    23772378
    23782379        wp_cache_delete( $object_id, $taxonomy . '_relationships' );
     2380        wp_cache_delete( 'last_changed', 'terms' );
    23792381
    23802382        /**
  • trunk/tests/phpunit/tests/term/getTheTerms.php

    r39174 r40353  
    192192
    193193    }
     194
     195    /**
     196     * @ticket 40306
     197     */
     198    public function test_term_cache_should_be_invalidated_on_set_object_terms() {
     199        register_taxonomy( 'wptests_tax', 'post' );
     200
     201        // Temporarily disable term counting, which performs its own cache invalidation.
     202        wp_defer_term_counting( true );
     203
     204        // Create Test Category.
     205        $term_id = self::factory()->term->create( array(
     206            'taxonomy' => 'wptests_tax',
     207        ) );
     208
     209        $post_id = self::factory()->post->create();
     210
     211        // Prime cache.
     212        get_the_terms( $post_id, 'wptests_tax' );
     213
     214        wp_set_object_terms( $post_id, $term_id, 'wptests_tax' );
     215
     216        $terms = get_the_terms( $post_id, 'wptests_tax' );
     217
     218        // Re-activate term counting so this doesn't affect other tests.
     219        wp_defer_term_counting( false );
     220
     221        $this->assertTrue( is_array( $terms ) );
     222        $this->assertSame( array( $term_id ), wp_list_pluck( $terms, 'term_id' ) );
     223    }
     224
     225    /**
     226     * @ticket 40306
     227     */
     228    public function test_term_cache_should_be_invalidated_on_remove_object_terms() {
     229        register_taxonomy( 'wptests_tax', 'post' );
     230
     231        // Create Test Category.
     232        $term_ids = self::factory()->term->create_many( 2, array(
     233            'taxonomy' => 'wptests_tax',
     234        ) );
     235
     236        $post_id = self::factory()->post->create();
     237
     238        wp_set_object_terms( $post_id, $term_ids, 'wptests_tax' );
     239
     240        // Prime cache.
     241        get_the_terms( $post_id, 'wptests_tax' );
     242
     243        // Temporarily disable term counting, which performs its own cache invalidation.
     244        wp_defer_term_counting( true );
     245
     246        wp_remove_object_terms( $post_id, $term_ids[0], 'wptests_tax' );
     247
     248        $terms = get_the_terms( $post_id, 'wptests_tax' );
     249
     250        // Re-activate term counting so this doesn't affect other tests.
     251        wp_defer_term_counting( false );
     252
     253        $this->assertTrue( is_array( $terms ) );
     254        $this->assertSame( array( $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) );
     255    }
    194256}
Note: See TracChangeset for help on using the changeset viewer.