WordPress.org

Make WordPress Core

Ticket #40306: 40306.diff

File 40306.diff, 2.1 KB (added by mboynes, 3 years ago)

Invalidate Term_Query cache on wp_set_object_terms and wp_remove_object_terms

  • src/wp-includes/taxonomy.php

     
    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.
     
    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

     
    191191                $this->assertSame( $num_queries, $wpdb->num_queries );
    192192
    193193        }
     194
     195        public function test_defer_term_counting_term_cache( $value = null ) {
     196                // Temporarily disable term counting.
     197                wp_defer_term_counting( true );
     198
     199                // Create Test Category.
     200                $term_id = self::factory()->category->create( array(
     201                        'slug' => 'uncounted-category',
     202                ) );
     203
     204                // Create the blank post object to mimic the "Write" screen.
     205                $post = get_default_post_to_edit( 'post', true );
     206
     207                // Save a draft of the post.
     208                wp_update_post( array(
     209                        'ID' => $post->ID,
     210                        'post_status' => 'draft',
     211                        'post_title' => 'some-post',
     212                        'post_type' => 'post',
     213                        'post_content' => 'some_content',
     214                        'post_category' => array( $term_id ),
     215                ) );
     216
     217                // Ensure that caches were cleared.
     218                $terms = get_the_terms( $post->ID, 'category' );
     219
     220                // Re-activate term counting so this doesn't affect other tests. This
     221                // doesn't affect the assertions because `$terms` is already populated.
     222                wp_defer_term_counting( false );
     223
     224                $this->assertTrue( is_array( $terms ) );
     225                $this->assertSame( array( $term_id ), wp_list_pluck( $terms, 'term_id' ) );
     226        }
    194227}