Regenerate the term hierarchy cache ({taxonomy}_children) when it is out of sync with the passed taxonomy's last_changed value.

Introduces `taxonomy_hierarchy_is_fresh()`, which is only called in `_get_term_hierarchy()`. The taxonomy's `last_changed` value is checked against the value of `wp_cache_get( 'hierarchy_last_changed', $taxonomy )`.

Adds a unit test - `Tests_Term:test_hierachy_invalidation()`.

See [27101], which makes this type of cache invalidation possible.
Fixes #14485.



git-svn-id: https://develop.svn.wordpress.org/trunk@27102 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2014-02-06 01:58:01 +00:00
parent e33ef0c6ac
commit 50a96f165e
2 changed files with 71 additions and 2 deletions
+46 -1
View File
@@ -585,7 +585,7 @@ class Tests_Term extends WP_UnitTestCase {
$last_changed3 = get_taxonomy_last_changed( 'category' );
$this->assertNotEquals( $last_changed2, $last_changed3 );
}
/**
* @ticket 22526
*/
@@ -634,4 +634,49 @@ class Tests_Term extends WP_UnitTestCase {
$cats2 = get_the_category( $post->ID );
$this->assertNotEquals( $term->name, reset( $cats2 )->name );
}
function test_hierachy_invalidation() {
$tax = 'burrito';
register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
$this->assertTrue( get_taxonomy( $tax )->hierarchical );
$step = 1;
$parent_id = 0;
$children = 0;
foreach ( range( 1, 99 ) as $i ) {
switch ( $step ) {
case 1:
$parent = wp_insert_term( 'Parent' . $i, $tax );
$parent_id = $parent['term_id'];
break;
case 2:
$parent = wp_insert_term( 'Child' . $i, $tax, array( 'parent' => $parent_id ) );
$parent_id = $parent['term_id'];
$children++;
break;
case 3:
wp_insert_term( 'Grandchild' . $i, $tax, array( 'parent' => $parent_id ) );
$parent_id = 0;
$children++;
break;
}
$terms = get_terms( $tax, array( 'hide_empty' => false ) );
$this->assertEquals( $i, count( $terms ) );
if ( 1 < $i ) {
$hierarchy = _get_term_hierarchy( $tax );
$this->assertNotEmpty( $hierarchy );
$this->assertEquals( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );
}
if ( $i % 3 === 0 ) {
$step = 1;
} else {
$step++;
}
}
_unregister_taxonomy( $tax );
}
}