Taxonomy: Pass object ids to delete_* actions.

Allows for more targeted updates to affected posts in callbacks.
Disambiguates `$objects` variable and amends unit tests.

Fixes #35213.


git-svn-id: https://develop.svn.wordpress.org/trunk@36080 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Konstantin Obenland 2015-12-23 23:43:03 +00:00
parent 9c2d3a7a4d
commit 1d68393907
3 changed files with 25 additions and 14 deletions

View File

@ -2158,10 +2158,10 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
// 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 ) );
$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
foreach ( (array) $objects as $object ) {
$terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
foreach ( $object_ids as $object_id ) {
$terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) );
if ( 1 == count($terms) && isset($default) ) {
$terms = array($default);
} else {
@ -2170,13 +2170,13 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
$terms = array_merge($terms, array($default));
}
$terms = array_map('intval', $terms);
wp_set_object_terms($object, $terms, $taxonomy);
wp_set_object_terms( $object_id, $terms, $taxonomy );
}
// Clean the relationship caches for all object types using this term.
$tax_object = get_taxonomy( $taxonomy );
foreach ( $tax_object->object_type as $object_type )
clean_object_term_cache( $objects, $object_type );
clean_object_term_cache( $object_ids, $object_type );
$term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) );
foreach ( $term_meta_ids as $mid ) {
@ -2212,14 +2212,16 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
* Fires after a term is deleted from the database and the cache is cleaned.
*
* @since 2.5.0
* @since 4.5.0 Introduced `$object_ids` argument.
*
* @param int $term Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
* @param mixed $deleted_term Copy of the already-deleted term, in the form specified
* by the parent function. WP_Error otherwise.
* @param array $object_ids List of term object IDs.
*/
do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids );
/**
* Fires after a term in a specific taxonomy is deleted.
@ -2228,13 +2230,15 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
* taxonomy the term belonged to.
*
* @since 2.3.0
* @since 4.5.0 Introduced `$object_ids` argument.
*
* @param int $term Term ID.
* @param int $tt_id Term taxonomy ID.
* @param mixed $deleted_term Copy of the already-deleted term, in the form specified
* by the parent function. WP_Error otherwise.
* @param array $object_ids List of term object IDs.
*/
do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );
do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term, $object_ids );
return true;
}

View File

@ -5,9 +5,11 @@
*/
class Tests_Term_WpDeleteTerm extends WP_UnitTestCase {
protected $deleted_term;
protected $object_ids;
/**
* @ticket 33485
* @ticket 35213
*/
public function test_count_property_passed_to_filters_should_reflect_pre_deleted_term() {
register_taxonomy( 'wptests_tax', 'post' );
@ -16,20 +18,23 @@ class Tests_Term_WpDeleteTerm extends WP_UnitTestCase {
'taxonomy' => 'wptests_tax',
) );
$p = self::factory()->post->create();
$post_id = self::factory()->post->create();
wp_set_object_terms( $p, array( $terms[0] ), 'wptests_tax' );
wp_set_object_terms( $post_id, array( $terms[0] ), 'wptests_tax' );
add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 4 );
add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 5 );
wp_delete_term( $terms[0], 'wptests_tax' );
$this->assertEquals( 1, $this->deleted_term->count );
$this->assertSame( $this->object_ids, array( "$post_id" ) );
wp_delete_term( $terms[1], 'wptests_tax' );
$this->assertEquals( 0, $this->deleted_term->count );
$this->assertSame( $this->object_ids, array() );
}
public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term ) {
public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term, $object_ids ) {
$this->deleted_term = $deleted_term;
$this->object_ids = $object_ids;
}
}

View File

@ -37,9 +37,9 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase {
$this->assertTrue( term_exists($t['term_id']) > 0 );
// now delete it
add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 );
$this->assertTrue( wp_delete_term( $t['term_id'], $taxonomy ) );
remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 );
$this->assertNull( term_exists($term) );
$this->assertNull( term_exists($t['term_id']) );
$this->assertEquals( $initial_count, wp_count_terms( $taxonomy ) );
@ -644,13 +644,15 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase {
/** Helpers **********************************************************/
public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term ) {
public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {
$this->assertInternalType( 'object', $deleted_term );
$this->assertInternalType( 'int', $term );
$this->assertInternalType( 'array', $object_ids );
// Pesky string $this->assertInternalType( 'int', $tt_id );
$this->assertEquals( $term, $deleted_term->term_id );
$this->assertEquals( $taxonomy, $deleted_term->taxonomy );
$this->assertEquals( $tt_id, $deleted_term->term_taxonomy_id );
$this->assertEmpty( $object_ids );
}
public function _pre_insert_term_callback() {