Ticket #14485: 14485.diff
File 14485.diff, 10.0 KB (added by , 11 years ago) |
---|
-
src/wp-includes/taxonomy.php
1876 1876 do_action( 'edit_term_taxonomies', $edit_tt_ids ); 1877 1877 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) ); 1878 1878 do_action( 'edited_term_taxonomies', $edit_tt_ids ); 1879 set_taxonomy_last_changed( $taxonomy ); 1879 1880 } 1880 1881 1881 1882 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); … … 1910 1911 $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); 1911 1912 1912 1913 clean_term_cache($term, $taxonomy); 1914 set_taxonomy_last_changed( $taxonomy ); 1913 1915 1914 1916 do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term ); 1915 1917 do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term ); … … 2162 2164 do_action( 'edit_terms', $alias->term_id, $taxonomy ); 2163 2165 $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) ); 2164 2166 do_action( 'edited_terms', $alias->term_id, $taxonomy ); 2167 set_taxonomy_last_changed( $taxonomy ); 2165 2168 } 2166 2169 } 2167 2170 … … 2223 2226 $term_id = apply_filters('term_id_filter', $term_id, $tt_id); 2224 2227 2225 2228 clean_term_cache($term_id, $taxonomy); 2229 set_taxonomy_last_changed( $taxonomy ); 2226 2230 2227 2231 do_action("created_term", $term_id, $tt_id, $taxonomy); 2228 2232 do_action("created_$taxonomy", $term_id, $tt_id); … … 2329 2333 } 2330 2334 2331 2335 wp_cache_delete( $object_id, $taxonomy . '_relationships' ); 2336 set_taxonomy_last_changed( $taxonomy ); 2332 2337 2333 2338 do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids); 2334 2339 return $tt_ids; … … 2407 2412 $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); 2408 2413 do_action( 'deleted_term_relationships', $object_id, $tt_ids ); 2409 2414 wp_update_term_count( $tt_ids, $taxonomy ); 2415 set_taxonomy_last_changed( $taxonomy ); 2410 2416 2411 2417 return (bool) $deleted; 2412 2418 } … … 2565 2571 do_action( 'edit_terms', $alias->term_id, $taxonomy ); 2566 2572 $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); 2567 2573 do_action( 'edited_terms', $alias->term_id, $taxonomy ); 2574 set_taxonomy_last_changed( $taxonomy ); 2568 2575 } 2569 2576 } 2570 2577 … … 2600 2607 $term_id = apply_filters('term_id_filter', $term_id, $tt_id); 2601 2608 2602 2609 clean_term_cache($term_id, $taxonomy); 2610 set_taxonomy_last_changed( $taxonomy ); 2603 2611 2604 2612 do_action("edited_term", $term_id, $tt_id, $taxonomy); 2605 2613 do_action("edited_$taxonomy", $term_id, $tt_id); … … 2738 2746 2739 2747 $taxonomies = get_object_taxonomies( $object_type ); 2740 2748 2741 foreach ( $object_ids as $id ) 2742 foreach ( $taxonomies as $taxonomy ) 2749 foreach ( $object_ids as $id ) { 2750 foreach ( $taxonomies as $taxonomy ) { 2743 2751 wp_cache_delete($id, "{$taxonomy}_relationships"); 2752 set_taxonomy_last_changed( $taxonomy ); 2753 } 2754 } 2744 2755 2745 2756 do_action('clean_object_term_cache', $object_ids, $object_type); 2746 2757 } … … 2800 2811 } 2801 2812 2802 2813 do_action('clean_term_cache', $ids, $taxonomy); 2814 set_taxonomy_last_changed( $taxonomy ); 2803 2815 } 2804 2816 2805 2817 wp_cache_set( 'last_changed', microtime(), 'terms' ); … … 2819 2831 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id. 2820 2832 */ 2821 2833 function get_object_term_cache($id, $taxonomy) { 2834 if ( ! post_taxonomy_is_fresh( $id, $taxonomy ) ) { 2835 return false; 2836 } 2822 2837 $cache = wp_cache_get($id, "{$taxonomy}_relationships"); 2823 2838 return $cache; 2824 2839 } … … 2933 2948 function _get_term_hierarchy($taxonomy) { 2934 2949 if ( !is_taxonomy_hierarchical($taxonomy) ) 2935 2950 return array(); 2936 $children = get_option("{$taxonomy}_children"); 2951 $children = false; 2952 if ( taxonomy_hierarchy_is_fresh( $taxonomy ) ) { 2953 $children = get_option( "{$taxonomy}_children" ); 2954 } 2937 2955 2938 2956 if ( is_array($children) ) 2939 2957 return $children; 2940 $children = array();2941 2958 $terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent')); 2942 2959 foreach ( $terms as $term_id => $parent ) { 2943 2960 if ( $parent > 0 ) … … 3117 3134 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 3118 3135 do_action( 'edited_term_taxonomy', $term, $taxonomy ); 3119 3136 } 3137 set_taxonomy_last_changed( $taxonomy->name ); 3120 3138 } 3121 3139 3122 3140 /** … … 3142 3160 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 3143 3161 do_action( 'edited_term_taxonomy', $term, $taxonomy ); 3144 3162 } 3163 set_taxonomy_last_changed( $taxonomy->name ); 3145 3164 } 3146 3165 3147 3166 /** … … 3469 3488 3470 3489 return $parent; 3471 3490 } 3491 3492 /** 3493 * Retrieve the 'last_changed' value for the passed taxonomy. Retrieves 3494 * from cache, if present 3495 * 3496 * @since 3.9.0 3497 * 3498 * @param string $taxonomy 3499 * @return int 3500 */ 3501 function get_taxonomy_last_changed( $taxonomy ) { 3502 $last_changed = wp_cache_get( 'last_changed', $taxonomy ); 3503 if ( ! $last_changed ) { 3504 $last_changed = microtime(); 3505 wp_cache_set( 'last_changed', $last_changed, $taxonomy ); 3506 } 3507 return $last_changed; 3508 } 3509 3510 /** 3511 * Reset 'last_changed' for the passed taxonomy 3512 * 3513 * @since 3.9.0 3514 * 3515 * @param string $taxonomy 3516 * @return int 3517 */ 3518 function set_taxonomy_last_changed( $taxonomy ) { 3519 wp_cache_delete( 'last_changed', $taxonomy ); 3520 return get_taxonomy_last_changed( $taxonomy ); 3521 } 3522 3523 /** 3524 * Determine if a post's cache for the passed taxonomy 3525 * is in sync. 3526 * 3527 * @since 3.9.0 3528 * 3529 * @param int $id 3530 * @param string $taxonomy 3531 * @return boolean 3532 */ 3533 function post_taxonomy_is_fresh( $id, $taxonomy ) { 3534 $last_changed = get_taxonomy_last_changed( $taxonomy ); 3535 $post_last_changed = wp_cache_get( $id, $taxonomy . '_last_changed' ); 3536 if ( ! $post_last_changed || $last_changed !== $post_last_changed ) { 3537 wp_cache_set( $id, $last_changed, $taxonomy . '_last_changed' ); 3538 return false; 3539 } 3540 return true; 3541 } 3542 3543 /** 3544 * Determine if a hierarchy's cache for the passed taxonomy 3545 * is in sync. 3546 * 3547 * @since 3.9.0 3548 * 3549 * @param int $id 3550 * @param string $taxonomy 3551 * @return boolean 3552 */ 3553 function taxonomy_hierarchy_is_fresh( $taxonomy ) { 3554 $last_changed = get_taxonomy_last_changed( $taxonomy ); 3555 $hierarchy_last_changed = wp_cache_get( 'hierarchy_last_changed', $taxonomy ); 3556 if ( ! $hierarchy_last_changed || $last_changed !== $hierarchy_last_changed ) { 3557 wp_cache_set( 'hierarchy_last_changed', $last_changed, $taxonomy ); 3558 return false; 3559 } 3560 return true; 3561 } 3562 No newline at end of file -
tests/phpunit/tests/term.php
568 568 $this->assertEquals( 0, $count ); 569 569 } 570 570 571 /** 572 * @ticket 22526 573 */ 571 function test_get_taxonomy_last_changed() { 572 $last_changed = get_taxonomy_last_changed( 'category' ); 573 $last_changed_cache = wp_cache_get( 'last_changed', 'category' ); 574 $this->assertEquals( $last_changed, $last_changed_cache ); 575 wp_cache_delete( 'last_changed', 'category' ); 576 $this->assertEquals( $last_changed, $last_changed_cache ); 577 $last_changed = get_taxonomy_last_changed( 'category' ); 578 $this->assertNotEquals( $last_changed, $last_changed_cache ); 579 580 $last_changed2 = get_taxonomy_last_changed( 'category' ); 581 $this->factory->category->create(); 582 $last_changed3 = get_taxonomy_last_changed( 'category' ); 583 $this->assertNotEquals( $last_changed2, $last_changed3 ); 584 } 585 586 function test_set_taxonomy_last_changed() { 587 $last_changed1 = set_taxonomy_last_changed( 'category' ); 588 $last_changed2 = set_taxonomy_last_changed( 'category' ); 589 $this->assertNotEquals( $last_changed1, $last_changed2 ); 590 591 $last_changed3 = set_taxonomy_last_changed( 'category' ); 592 $last_changed4 = get_taxonomy_last_changed( 'category' ); 593 $this->assertEquals( $last_changed3, $last_changed4 ); 594 } 595 596 function test_post_taxonomy_is_fresh() { 597 $post_id = $this->factory->post->create(); 598 $term_id = $this->factory->category->create( array( 'name' => 'Foo' ) ); 599 wp_set_post_categories( $post_id, $term_id ); 600 601 $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) ); 602 $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) ); 603 $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) ); 604 605 wp_update_term( $term_id, 'category', array( 'name' => 'Bar' ) ); 606 607 $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) ); 608 get_the_category( $post_id ); 609 $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) ); 610 } 611 574 612 function test_category_name_change() { 575 613 $term = $this->factory->category->create_and_get( array( 'name' => 'Foo' ) ); 576 614 $post_id = $this->factory->post->create(); … … 584 622 $cats2 = get_the_category( $post->ID ); 585 623 $this->assertNotEquals( $term->name, reset( $cats2 )->name ); 586 624 } 625 626 function test_hierachy_invalidation() { 627 $tax = 'burrito'; 628 register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); 629 $this->assertTrue( get_taxonomy( $tax )->hierarchical ); 630 631 $step = 1; 632 $parent_id = 0; 633 $children = 0; 634 635 foreach ( range( 1, 99 ) as $i ) { 636 switch ( $step ) { 637 case 1: 638 $parent = wp_insert_term( 'Parent' . $i, $tax ); 639 $parent_id = $parent['term_id']; 640 break; 641 case 2: 642 $parent = wp_insert_term( 'Child' . $i, $tax, array( 'parent' => $parent_id ) ); 643 $parent_id = $parent['term_id']; 644 $children++; 645 break; 646 case 3: 647 wp_insert_term( 'Grandchild' . $i, $tax, array( 'parent' => $parent_id ) ); 648 $parent_id = 0; 649 $children++; 650 break; 651 } 652 653 $terms = get_terms( $tax, array( 'hide_empty' => false ) ); 654 $this->assertEquals( $i, count( $terms ) ); 655 if ( 1 < $i ) { 656 $hierarchy = _get_term_hierarchy( $tax ); 657 $this->assertNotEmpty( $hierarchy ); 658 $this->assertEquals( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) ); 659 } 660 661 if ( $i % 3 === 0 ) { 662 $step = 1; 663 } else { 664 $step++; 665 } 666 } 667 668 _unregister_taxonomy( $tax ); 669 } 587 670 }