Flush cache for newly created term in _split_shared_term().

The term itself does not have any cached values yet, but in some cases the new
term's taxonomy may need its cached hierarchy to be refreshed as a result of
the term splitting.

Props jorbin.
See #30335.

git-svn-id: https://develop.svn.wordpress.org/trunk@30347 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-11-14 21:52:23 +00:00
parent e1ca159011
commit 69a167688e
2 changed files with 43 additions and 6 deletions

View File

@ -4096,12 +4096,17 @@ function _split_shared_term( $term_id, $term_taxonomy_id ) {
$term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
$children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent = %d", $term_taxonomy->taxonomy, $term_id ) );
foreach ( $children_tt_ids as $child_tt_id ) {
$wpdb->update( $wpdb->term_taxonomy,
array( 'parent' => $new_term_id ),
array( 'term_taxonomy_id' => $child_tt_id )
);
clean_term_cache( $term_id, $term_taxonomy->taxonomy );
if ( ! empty( $children_tt_ids ) ) {
foreach ( $children_tt_ids as $child_tt_id ) {
$wpdb->update( $wpdb->term_taxonomy,
array( 'parent' => $new_term_id ),
array( 'term_taxonomy_id' => $child_tt_id )
);
clean_term_cache( $term_id, $term_taxonomy->taxonomy );
}
} else {
// If the term has no children, we must force its taxonomy cache to be rebuilt separately.
clean_term_cache( $new_term_id, $term_taxonomy->taxonomy );
}
// Clean the cache for term taxonomies formerly shared with the current term.

View File

@ -89,4 +89,36 @@ class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
$this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
}
/**
* @ticket 30335
*/
public function test_should_rebuild_split_term_taxonomy_hierarchy() {
global $wpdb;
register_taxonomy( 'wptests_tax_3', 'post' );
register_taxonomy( 'wptests_tax_4', 'post', array(
'hierarchical' => true,
) );
$t1 = wp_insert_term( 'Foo1', 'wptests_tax_3' );
$t2 = wp_insert_term( 'Foo1 Parent', 'wptests_tax_4' );
$t3 = wp_insert_term( 'Foo1', 'wptests_tax_4', array(
'parent' => $t2['term_id'],
) );
// Manually modify because split terms shouldn't naturally occur.
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $t1['term_id'] ),
array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
array( '%d' ),
array( '%d' )
);
$th = _get_term_hierarchy( 'wptests_tax_4' );
$new_term_id = _split_shared_term( $t1['term_id'], $t3['term_taxonomy_id'] );
$t2_children = get_term_children( $t2['term_id'], 'wptests_tax_4' );
$this->assertEquals( array( $new_term_id ), $t2_children );
}
}