WordPress.org

Make WordPress Core

Ticket #40306: 40306.2.diff

File 40306.2.diff, 3.0 KB (added by boonebgorges, 3 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index 9ebc9d0d0b..94f36689fd 100644
    function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { 
    22822282        }
    22832283
    22842284        wp_cache_delete( $object_id, $taxonomy . '_relationships' );
     2285        wp_cache_delete( 'last_changed', 'terms' );
    22852286
    22862287        /**
    22872288         * Fires after an object's terms have been set.
    function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { 
    23762377                $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
    23772378
    23782379                wp_cache_delete( $object_id, $taxonomy . '_relationships' );
     2380                wp_cache_delete( 'last_changed', 'terms' );
    23792381
    23802382                /**
    23812383                 * Fires immediately after an object-term relationship is deleted.
  • tests/phpunit/tests/term/getTheTerms.php

    diff --git tests/phpunit/tests/term/getTheTerms.php tests/phpunit/tests/term/getTheTerms.php
    index 6a3d4ba5c1..1bffcb6a63 100644
    class Tests_Term_GetTheTerms extends WP_UnitTestCase { 
    191191                $this->assertSame( $num_queries, $wpdb->num_queries );
    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}