From 69a167688e4279f761fdd13a9366b2aea5e0765a Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 14 Nov 2014 21:52:23 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 17 +++++++---- tests/phpunit/tests/term/splitSharedTerm.php | 32 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 79d04ae81f..fa450e5fea 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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. diff --git a/tests/phpunit/tests/term/splitSharedTerm.php b/tests/phpunit/tests/term/splitSharedTerm.php index 50ef1c96c7..ceab947049 100644 --- a/tests/phpunit/tests/term/splitSharedTerm.php +++ b/tests/phpunit/tests/term/splitSharedTerm.php @@ -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 ); + } }