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
This commit is contained in:
Boone Gorges 2015-08-22 19:42:30 +00:00
parent 6c3449b710
commit b01b3be23e
2 changed files with 38 additions and 3 deletions

View File

@ -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.
*

View File

@ -0,0 +1,35 @@
<?php
/**
* @group taxonomy
*/
class Tests_Term_WpDeleteTerm extends WP_UnitTestCase {
protected $deleted_term;
/**
* @ticket 33485
*/
public function test_count_property_passed_to_filters_should_reflect_pre_deleted_term() {
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->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;
}
}