From c2bea27bfdd83a9b1e9ad9b601e6c0f60eedde55 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 17 Oct 2014 20:58:48 +0000 Subject: [PATCH] Invalidate cache for child terms when parent term is deleted. Props socki03. Fixes #29911. git-svn-id: https://develop.svn.wordpress.org/trunk@29945 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 7 ++++++- tests/phpunit/tests/term.php | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index dcf1b56b37..2eecfecf7c 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2414,7 +2414,8 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { return $term_obj; $parent = $term_obj->parent; - $edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); + $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); + $edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' ); /** * Fires immediately before a term to delete's children are reassigned a parent. @@ -2426,6 +2427,10 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { do_action( 'edit_term_taxonomies', $edit_tt_ids ); $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) ); + // Clean the cache for all child terms. + $edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' ); + clean_term_cache( $edit_term_ids, $taxonomy ); + /** * Fires immediately after a term to delete's children are reassigned a parent. * diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 07d57106d7..5ec9b246cd 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -1006,6 +1006,33 @@ class Tests_Term extends WP_UnitTestCase { $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t2 ] ) ); } + /** + * @ticket 29911 + */ + public function test_wp_delete_term_should_invalidate_cache_for_child_terms() { + register_taxonomy( 'wptests_tax', 'post', array( + 'hierarchical' => true, + ) ); + + $parent = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + ) ); + + $child = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'parent' => $parent, + 'slug' => 'foo', + ) ); + + // Prime the cache. + $child_term = get_term( $child, 'wptests_tax' ); + $this->assertSame( $parent, $child_term->parent ); + + wp_delete_term( $parent, 'wptests_tax' ); + $child_term = get_term( $child, 'wptests_tax' ); + $this->assertSame( 0, $child_term->parent ); + } + /** * @ticket 5381 */