From b01b3be23e7f3e895ef2302b252e61756c7a9426 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 22 Aug 2015 19:42:30 +0000 Subject: [PATCH] In `wp_delete_term()`, the `$deleted_term` object passed to filters should be generated before term relationships are deleted. This allows the `count` property to reflect the pre-delete state of affairs, rather than always being 0. Props nicholas_io. Fixes #33485. git-svn-id: https://develop.svn.wordpress.org/trunk@33711 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 6 ++-- tests/phpunit/tests/term/wpDeleteTerm.php | 35 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/tests/term/wpDeleteTerm.php diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 46a25274dd..63c3c844c1 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2551,6 +2551,9 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { do_action( 'edited_term_taxonomies', $edit_tt_ids ); } + // Get the term before deleting it or its term relationships so we can pass to actions below. + $deleted_term = get_term( $term, $taxonomy ); + $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); foreach ( (array) $objects as $object ) { @@ -2571,9 +2574,6 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { foreach ( $tax_object->object_type as $object_type ) clean_object_term_cache( $objects, $object_type ); - // Get the object before deletion so we can pass to actions below - $deleted_term = get_term( $term, $taxonomy ); - /** * Fires immediately before a term taxonomy ID is deleted. * diff --git a/tests/phpunit/tests/term/wpDeleteTerm.php b/tests/phpunit/tests/term/wpDeleteTerm.php new file mode 100644 index 0000000000..064528f281 --- /dev/null +++ b/tests/phpunit/tests/term/wpDeleteTerm.php @@ -0,0 +1,35 @@ +factory->term->create_many( 2, array( + 'taxonomy' => 'wptests_tax', + ) ); + + $p = $this->factory->post->create(); + + wp_set_object_terms( $p, array( $terms[0] ), 'wptests_tax' ); + + add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 4 ); + + wp_delete_term( $terms[0], 'wptests_tax' ); + $this->assertEquals( 1, $this->deleted_term->count ); + + wp_delete_term( $terms[1], 'wptests_tax' ); + $this->assertEquals( 0, $this->deleted_term->count ); + } + + public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term ) { + $this->deleted_term = $deleted_term; + } +}