Make WordPress Core


Ignore:
Timestamp:
02/06/2014 01:40:05 AM (11 years ago)
Author:
wonderboymusic
Message:

Add cache invalidation when updating a term, example: create a category, assign it to a post, edit the category. Currently, the post's term cache is not updated. When updating terms in a given taxonomy, invalidate the object term caches linked to that taxonomy.

Introduce get_taxonomy_last_changed(), set_taxonomy_last_changed(), and post_taxonomy_is_fresh().

post_taxonomy_is_fresh() is only called in get_object_term_cache() - at which point the taxonomy's last_changed value is checked against the post's {$taxonomy}_last_changed value.

set_taxonomy_last_changed() is called whenever directory database queries are made that insert new terms or affect existing terms.

Fixes #22526.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term.php

    r27099 r27101  
    572572     * @ticket 22526
    573573     */
     574    function test_get_taxonomy_last_changed() {
     575        $last_changed = get_taxonomy_last_changed( 'category' );
     576        $last_changed_cache = wp_cache_get( 'last_changed', 'category' );
     577        $this->assertEquals( $last_changed, $last_changed_cache );
     578        wp_cache_delete( 'last_changed', 'category' );
     579        $this->assertEquals( $last_changed, $last_changed_cache );
     580        $last_changed = get_taxonomy_last_changed( 'category' );
     581        $this->assertNotEquals( $last_changed, $last_changed_cache );
     582
     583        $last_changed2 = get_taxonomy_last_changed( 'category' );
     584        $this->factory->category->create();
     585        $last_changed3 = get_taxonomy_last_changed( 'category' );
     586        $this->assertNotEquals( $last_changed2, $last_changed3 );
     587    }
     588   
     589    /**
     590     * @ticket 22526
     591     */
     592    function test_set_taxonomy_last_changed() {
     593        $last_changed1 = set_taxonomy_last_changed( 'category' );
     594        $last_changed2 = set_taxonomy_last_changed( 'category' );
     595        $this->assertNotEquals( $last_changed1, $last_changed2 );
     596
     597        $last_changed3 = set_taxonomy_last_changed( 'category' );
     598        $last_changed4 = get_taxonomy_last_changed( 'category' );
     599        $this->assertEquals( $last_changed3, $last_changed4 );
     600    }
     601
     602    /**
     603     * @ticket 22526
     604     */
     605    function test_post_taxonomy_is_fresh() {
     606        $post_id = $this->factory->post->create();
     607        $term_id = $this->factory->category->create( array( 'name' => 'Foo' ) );
     608        wp_set_post_categories( $post_id, $term_id );
     609
     610        $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) );
     611        $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
     612        $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
     613
     614        wp_update_term( $term_id, 'category', array( 'name' => 'Bar' ) );
     615
     616        $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) );
     617        get_the_category( $post_id );
     618        $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
     619    }
     620
     621    /**
     622     * @ticket 22526
     623     */
    574624    function test_category_name_change() {
    575625        $term = $this->factory->category->create_and_get( array( 'name' => 'Foo' ) );
Note: See TracChangeset for help on using the changeset viewer.