Changeset 27163
- Timestamp:
- 02/11/2014 04:49:11 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r27142 r27163 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 );1880 1879 } 1881 1880 … … 1912 1911 1913 1912 clean_term_cache($term, $taxonomy); 1914 set_taxonomy_last_changed( $taxonomy );1915 1913 1916 1914 do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term ); … … 2165 2163 $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) ); 2166 2164 do_action( 'edited_terms', $alias->term_id, $taxonomy ); 2167 set_taxonomy_last_changed( $taxonomy );2168 2165 } 2169 2166 } … … 2227 2224 2228 2225 clean_term_cache($term_id, $taxonomy); 2229 set_taxonomy_last_changed( $taxonomy );2230 2226 2231 2227 do_action("created_term", $term_id, $tt_id, $taxonomy); … … 2334 2330 2335 2331 wp_cache_delete( $object_id, $taxonomy . '_relationships' ); 2336 set_taxonomy_last_changed( $taxonomy );2337 2332 2338 2333 do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids); … … 2413 2408 do_action( 'deleted_term_relationships', $object_id, $tt_ids ); 2414 2409 wp_update_term_count( $tt_ids, $taxonomy ); 2415 set_taxonomy_last_changed( $taxonomy );2416 2410 2417 2411 return (bool) $deleted; … … 2572 2566 $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); 2573 2567 do_action( 'edited_terms', $alias->term_id, $taxonomy ); 2574 set_taxonomy_last_changed( $taxonomy );2575 2568 } 2576 2569 } … … 2602 2595 do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); 2603 2596 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 2604 2603 do_action("edit_term", $term_id, $tt_id, $taxonomy); 2605 2604 do_action("edit_$taxonomy", $term_id, $tt_id); … … 2608 2607 2609 2608 clean_term_cache($term_id, $taxonomy); 2610 set_taxonomy_last_changed( $taxonomy );2611 2609 2612 2610 do_action("edited_term", $term_id, $tt_id, $taxonomy); … … 2750 2748 foreach ( $taxonomies as $taxonomy ) { 2751 2749 wp_cache_delete($id, "{$taxonomy}_relationships"); 2752 set_taxonomy_last_changed( $taxonomy );2753 2750 } 2754 2751 } … … 2771 2768 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 2772 2769 global $wpdb; 2773 static $cleaned = array();2774 2770 2775 2771 if ( !is_array($ids) ) … … 2799 2795 2800 2796 foreach ( $taxonomies as $taxonomy ) { 2801 if ( isset($cleaned[$taxonomy]) )2802 continue;2803 $cleaned[$taxonomy] = true;2804 2805 2797 if ( $clean_taxonomy ) { 2806 2798 wp_cache_delete('all_ids', $taxonomy); … … 2812 2804 2813 2805 do_action('clean_term_cache', $ids, $taxonomy); 2814 set_taxonomy_last_changed( $taxonomy );2815 2806 } 2816 2807 … … 2832 2823 */ 2833 2824 function get_object_term_cache($id, $taxonomy) { 2834 if ( ! post_taxonomy_is_fresh( $id, $taxonomy ) ) {2835 return false;2836 }2837 2825 $cache = wp_cache_get($id, "{$taxonomy}_relationships"); 2838 2826 return $cache; … … 2949 2937 if ( !is_taxonomy_hierarchical($taxonomy) ) 2950 2938 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"); 2955 2940 2956 2941 if ( is_array($children) ) … … 3151 3136 do_action( 'edited_term_taxonomy', $term, $taxonomy ); 3152 3137 } 3153 set_taxonomy_last_changed( $taxonomy->name );3154 3138 } 3155 3139 … … 3177 3161 do_action( 'edited_term_taxonomy', $term, $taxonomy ); 3178 3162 } 3179 set_taxonomy_last_changed( $taxonomy->name );3180 3163 } 3181 3164 … … 3505 3488 return $parent; 3506 3489 } 3507 3508 /**3509 * Retrieve the 'last_changed' value for the passed taxonomy.3510 *3511 * Retrieves from cache, if present.3512 *3513 * @since 3.9.03514 *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.03532 *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 are3546 * stored as Unix timestamps with microseconds.3547 *3548 * @since 3.9.03549 *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' times3570 * of the passed taxonomy and its hierarchy are in sync. Times are3571 * stored as Unix timestamps with microseconds.3572 *3573 * @since 3.9.03574 *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 33 33 * @ticket 22526 34 34 */ 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 2252653 */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 2252666 */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 2252685 */86 35 function test_category_name_change() { 87 36 $term = $this->factory->category->create_and_get( array( 'name' => 'Foo' ) ); … … 98 47 } 99 48 49 /** 50 * @ticket 14485 51 */ 100 52 function test_hierachy_invalidation() { 101 53 $tax = 'burrito'; … … 127 79 $terms = get_terms( $tax, array( 'hide_empty' => false ) ); 128 80 $this->assertEquals( $i, count( $terms ) ); 129 if ( 1 < $i) {81 if ( $i > 1 ) { 130 82 $hierarchy = _get_term_hierarchy( $tax ); 131 83 $this->assertNotEmpty( $hierarchy );
Note: See TracChangeset
for help on using the changeset viewer.