WordPress.org

Make WordPress Core

Ticket #22526: 22526.4.diff

File 22526.4.diff, 10.6 KB (added by kovshenin, 4 years ago)
  • src/wp-includes/taxonomy.php

     
    18761876                do_action( 'edit_term_taxonomies', $edit_tt_ids );
    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
    18821881        $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
     
    19111910                $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
    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 );
    19171915        do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );
     
    21642162                        do_action( 'edit_terms', $alias->term_id, $taxonomy );
    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        }
    21702167
     
    22262223        $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
    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);
    22322228        do_action("created_$taxonomy", $term_id, $tt_id);
     
    23332329        }
    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);
    23392334        return $tt_ids;
     
    24122407                $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
    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;
    24182412        }
     
    25712565                        do_action( 'edit_terms', $alias->term_id, $taxonomy );
    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        }
    25772570
     
    26012594        $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
    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);
    26062605
    26072606        $term_id = apply_filters('term_id_filter', $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);
    26132611        do_action("edited_$taxonomy", $term_id, $tt_id);
     
    27492747        foreach ( $object_ids as $id ) {
    27502748                foreach ( $taxonomies as $taxonomy ) {
    27512749                        wp_cache_delete($id, "{$taxonomy}_relationships");
    2752                         set_taxonomy_last_changed( $taxonomy );
    27532750                }
    27542751        }
    27552752
     
    28112808                }
    28122809
    28132810                do_action('clean_term_cache', $ids, $taxonomy);
    2814                 set_taxonomy_last_changed( $taxonomy );
    28152811        }
    28162812
    28172813        wp_cache_set( 'last_changed', microtime( true ), 'terms' );
     
    28312827 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
    28322828 */
    28332829function get_object_term_cache($id, $taxonomy) {
    2834         if ( ! post_taxonomy_is_fresh( $id, $taxonomy ) ) {
    2835                 return false;
    2836         }
    28372830        $cache = wp_cache_get($id, "{$taxonomy}_relationships");
    28382831        return $cache;
    28392832}
     
    29482941function _get_term_hierarchy($taxonomy) {
    29492942        if ( !is_taxonomy_hierarchical($taxonomy) )
    29502943                return array();
    2951         $children = false;
    2952         if ( taxonomy_hierarchy_is_fresh( $taxonomy ) ) {
    2953                 $children = get_option("{$taxonomy}_children");
    2954         }
     2944        $children = get_option("{$taxonomy}_children");
    29552945
    29562946        if ( is_array($children) )
    29572947                return $children;
     
    31503140                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
    31513141                do_action( 'edited_term_taxonomy', $term, $taxonomy );
    31523142        }
    3153         set_taxonomy_last_changed( $taxonomy->name );
    31543143}
    31553144
    31563145/**
     
    31763165                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
    31773166                do_action( 'edited_term_taxonomy', $term, $taxonomy );
    31783167        }
    3179         set_taxonomy_last_changed( $taxonomy->name );
    31803168}
    31813169
    31823170/**
     
    35033491                wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
    35043492
    35053493        return $parent;
    3506 }
    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;
    35893494}
    3590  No newline at end of file
  • tests/phpunit/tests/term/cache.php

     
    3232        /**
    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' ) );
    8837                $post_id = $this->factory->post->create();
     
    9746                $this->assertNotEquals( $term->name, reset( $cats2 )->name );
    9847        }
    9948
     49        /**
     50         * @ticket 14485
     51         */
    10052        function test_hierachy_invalidation() {
    10153                $tax = 'burrito';
    10254                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     
    12678
    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 );
    13284                                $this->assertEquals( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );