Make WordPress Core

Changeset 27163


Ignore:
Timestamp:
02/11/2014 04:49:11 PM (10 years ago)
Author:
wonderboymusic
Message:

Partially revert [27101], [27102], [27141], and [27142]. Those commits introduced new functions to sync up cache invalidation events. The current commit alters existing internals.

"The cache invalidation with static was introduced in r9102 with version 2.7. Multisite wasn't in core back then, so something like switch_to_blog() wasn't a concern, but now it breaks if you switch the blog in between calls to clean_term_cache."

This solution is simpler. All unit tests pass. Removes unnecessary tests linked to removed functions.

Props kovshenin.
Fixes #14485, #22526.

Location:
trunk
Files:
2 edited

Legend:

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

    r27142 r27163  
    18771877        $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
    18781878        do_action( 'edited_term_taxonomies', $edit_tt_ids );
    1879         set_taxonomy_last_changed( $taxonomy );
    18801879    }
    18811880
     
    19121911
    19131912    clean_term_cache($term, $taxonomy);
    1914     set_taxonomy_last_changed( $taxonomy );
    19151913
    19161914    do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
     
    21652163            $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
    21662164            do_action( 'edited_terms', $alias->term_id, $taxonomy );
    2167             set_taxonomy_last_changed( $taxonomy );
    21682165        }
    21692166    }
     
    22272224
    22282225    clean_term_cache($term_id, $taxonomy);
    2229     set_taxonomy_last_changed( $taxonomy );
    22302226
    22312227    do_action("created_term", $term_id, $tt_id, $taxonomy);
     
    23342330
    23352331    wp_cache_delete( $object_id, $taxonomy . '_relationships' );
    2336     set_taxonomy_last_changed( $taxonomy );
    23372332
    23382333    do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);
     
    24132408        do_action( 'deleted_term_relationships', $object_id, $tt_ids );
    24142409        wp_update_term_count( $tt_ids, $taxonomy );
    2415         set_taxonomy_last_changed( $taxonomy );
    24162410
    24172411        return (bool) $deleted;
     
    25722566            $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
    25732567            do_action( 'edited_terms', $alias->term_id, $taxonomy );
    2574             set_taxonomy_last_changed( $taxonomy );
    25752568        }
    25762569    }
     
    26022595    do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
    26032596
     2597    // Clean the relationship caches for all object types using this term
     2598    $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
     2599    $tax_object = get_taxonomy( $taxonomy );
     2600    foreach ( $tax_object->object_type as $object_type )
     2601        clean_object_term_cache( $objects, $object_type );
     2602
    26042603    do_action("edit_term", $term_id, $tt_id, $taxonomy);
    26052604    do_action("edit_$taxonomy", $term_id, $tt_id);
     
    26082607
    26092608    clean_term_cache($term_id, $taxonomy);
    2610     set_taxonomy_last_changed( $taxonomy );
    26112609
    26122610    do_action("edited_term", $term_id, $tt_id, $taxonomy);
     
    27502748        foreach ( $taxonomies as $taxonomy ) {
    27512749            wp_cache_delete($id, "{$taxonomy}_relationships");
    2752             set_taxonomy_last_changed( $taxonomy );
    27532750        }
    27542751    }
     
    27712768function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
    27722769    global $wpdb;
    2773     static $cleaned = array();
    27742770
    27752771    if ( !is_array($ids) )
     
    27992795
    28002796    foreach ( $taxonomies as $taxonomy ) {
    2801         if ( isset($cleaned[$taxonomy]) )
    2802             continue;
    2803         $cleaned[$taxonomy] = true;
    2804 
    28052797        if ( $clean_taxonomy ) {
    28062798            wp_cache_delete('all_ids', $taxonomy);
     
    28122804
    28132805        do_action('clean_term_cache', $ids, $taxonomy);
    2814         set_taxonomy_last_changed( $taxonomy );
    28152806    }
    28162807
     
    28322823 */
    28332824function get_object_term_cache($id, $taxonomy) {
    2834     if ( ! post_taxonomy_is_fresh( $id, $taxonomy ) ) {
    2835         return false;
    2836     }
    28372825    $cache = wp_cache_get($id, "{$taxonomy}_relationships");
    28382826    return $cache;
     
    29492937    if ( !is_taxonomy_hierarchical($taxonomy) )
    29502938        return array();
    2951     $children = false;
    2952     if ( taxonomy_hierarchy_is_fresh( $taxonomy ) ) {
    2953         $children = get_option("{$taxonomy}_children");
    2954     }
     2939    $children = get_option("{$taxonomy}_children");
    29552940
    29562941    if ( is_array($children) )
     
    31513136        do_action( 'edited_term_taxonomy', $term, $taxonomy );
    31523137    }
    3153     set_taxonomy_last_changed( $taxonomy->name );
    31543138}
    31553139
     
    31773161        do_action( 'edited_term_taxonomy', $term, $taxonomy );
    31783162    }
    3179     set_taxonomy_last_changed( $taxonomy->name );
    31803163}
    31813164
     
    35053488    return $parent;
    35063489}
    3507 
    3508 /**
    3509  * Retrieve the 'last_changed' value for the passed taxonomy.
    3510  *
    3511  * Retrieves from cache, if present.
    3512  *
    3513  * @since 3.9.0
    3514  *
    3515  * @param string $taxonomy Taxonomy slug.
    3516  * @return int Unix timestamp with microseconds of the last taxonomy change.
    3517  */
    3518 function get_taxonomy_last_changed( $taxonomy ) {
    3519     $key = $taxonomy . '_last_changed';
    3520     $last_changed = wp_cache_get( $key, 'terms' );
    3521     if ( ! $last_changed ) {
    3522         $last_changed = microtime();
    3523         wp_cache_set( $key, $last_changed, 'terms' );
    3524     }
    3525     return $last_changed;
    3526 }
    3527 
    3528 /**
    3529  * Reset 'last_changed' time for the passed taxonomy.
    3530  *
    3531  * @since 3.9.0
    3532  *
    3533  * @param string $taxonomy Taxonomy slug.
    3534  * @return int Unix timestamp with microseconds of the last taxonomy change.
    3535  */
    3536 function set_taxonomy_last_changed( $taxonomy ) {
    3537     wp_cache_delete( $taxonomy . '_last_changed', 'terms' );
    3538     return get_taxonomy_last_changed( $taxonomy );
    3539 }
    3540 
    3541 /**
    3542  * Determine if a post's cache for the passed taxonomy is in sync.
    3543  *
    3544  * 'Fresh'-ness is determined based on whether the 'last_changed'
    3545  * times of the passed post and taxonomy are in sync. Times are
    3546  * stored as Unix timestamps with microseconds.
    3547  *
    3548  * @since 3.9.0
    3549  *
    3550  * @param int    $id       Post ID.
    3551  * @param string $taxonomy Taxonomy slug.
    3552  * @return bool True if the 'last_changed' times are in sync, or 'fresh'.
    3553  *              False if they are out of sync, or not cached.
    3554  */
    3555 function post_taxonomy_is_fresh( $id, $taxonomy ) {
    3556     $last_changed = get_taxonomy_last_changed( $taxonomy );
    3557     $key = $id . '_' . $taxonomy . '_last_changed';
    3558     $post_last_changed = wp_cache_get( $key, 'terms' );
    3559     if ( ! $post_last_changed || $last_changed !== $post_last_changed ) {
    3560         wp_cache_set( $key, $last_changed, 'terms' );
    3561         return false;
    3562     }
    3563     return true;
    3564 }
    3565 
    3566 /**
    3567  * Determine if a hierarchy's cache for the passed taxonomy is in sync.
    3568  *
    3569  * 'Fresh'-ness is determined based on whether the 'last_changed' times
    3570  * of the passed taxonomy and its hierarchy are in sync. Times are
    3571  * stored as Unix timestamps with microseconds.
    3572  *
    3573  * @since 3.9.0
    3574  *
    3575  * @param int    $id       Post ID.
    3576  * @param string $taxonomy Taxonomy slug.
    3577  * @return bool True if the 'last_changed' times are in sync, or 'fresh'.
    3578  *              False if they are out of sync, or not cached.
    3579  */
    3580 function taxonomy_hierarchy_is_fresh( $taxonomy ) {
    3581     $last_changed = get_taxonomy_last_changed( $taxonomy );
    3582     $key = $taxonomy . '_hierarchy_last_changed';
    3583     $hierarchy_last_changed = wp_cache_get( $key, 'terms' );
    3584     if ( ! $hierarchy_last_changed || $last_changed !== $hierarchy_last_changed ) {
    3585         wp_cache_set( $key, $last_changed, 'terms' );
    3586         return false;
    3587     }
    3588     return true;
    3589 }
  • trunk/tests/phpunit/tests/term/cache.php

    r27142 r27163  
    3333     * @ticket 22526
    3434     */
    35     function test_get_taxonomy_last_changed() {
    36         $last_changed = get_taxonomy_last_changed( 'category' );
    37         $key = 'category_last_changed';
    38         $last_changed_cache = wp_cache_get( $key, 'terms' );
    39         $this->assertEquals( $last_changed, $last_changed_cache );
    40         wp_cache_delete( $key, 'terms' );
    41         $this->assertEquals( $last_changed, $last_changed_cache );
    42         $last_changed = get_taxonomy_last_changed( 'category' );
    43         $this->assertNotEquals( $last_changed, $last_changed_cache );
    44 
    45         $last_changed2 = get_taxonomy_last_changed( 'category' );
    46         $this->factory->category->create();
    47         $last_changed3 = get_taxonomy_last_changed( 'category' );
    48         $this->assertNotEquals( $last_changed2, $last_changed3 );
    49     }
    50 
    51     /**
    52      * @ticket 22526
    53      */
    54     function test_set_taxonomy_last_changed() {
    55         $last_changed1 = set_taxonomy_last_changed( 'category' );
    56         $last_changed2 = set_taxonomy_last_changed( 'category' );
    57         $this->assertNotEquals( $last_changed1, $last_changed2 );
    58 
    59         $last_changed3 = set_taxonomy_last_changed( 'category' );
    60         $last_changed4 = get_taxonomy_last_changed( 'category' );
    61         $this->assertEquals( $last_changed3, $last_changed4 );
    62     }
    63 
    64     /**
    65      * @ticket 22526
    66      */
    67     function test_post_taxonomy_is_fresh() {
    68         $post_id = $this->factory->post->create();
    69         $term_id = $this->factory->category->create( array( 'name' => 'Foo' ) );
    70         wp_set_post_categories( $post_id, $term_id );
    71 
    72         $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) );
    73         $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
    74         $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
    75 
    76         wp_update_term( $term_id, 'category', array( 'name' => 'Bar' ) );
    77 
    78         $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) );
    79         get_the_category( $post_id );
    80         $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
    81     }
    82 
    83     /**
    84      * @ticket 22526
    85      */
    8635    function test_category_name_change() {
    8736        $term = $this->factory->category->create_and_get( array( 'name' => 'Foo' ) );
     
    9847    }
    9948
     49    /**
     50     * @ticket 14485
     51     */
    10052    function test_hierachy_invalidation() {
    10153        $tax = 'burrito';
     
    12779            $terms = get_terms( $tax, array( 'hide_empty' => false ) );
    12880            $this->assertEquals( $i, count( $terms ) );
    129             if ( 1 < $i ) {
     81            if ( $i > 1 ) {
    13082                $hierarchy = _get_term_hierarchy( $tax );
    13183                $this->assertNotEmpty( $hierarchy );
Note: See TracChangeset for help on using the changeset viewer.