Ticket #35213: 35213.3.diff
File 35213.3.diff, 6.2 KB (added by , 9 years ago) |
---|
-
src/wp-includes/taxonomy.php
2158 2158 // Get the term before deleting it or its term relationships so we can pass to actions below. 2159 2159 $deleted_term = get_term( $term, $taxonomy ); 2160 2160 2161 $object s =$wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );2161 $object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); 2162 2162 2163 foreach ( (array) $objects as $object) {2164 $terms = wp_get_object_terms( $object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));2163 foreach ( $object_ids as $object_id ) { 2164 $terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) ); 2165 2165 if ( 1 == count($terms) && isset($default) ) { 2166 2166 $terms = array($default); 2167 2167 } else { … … 2170 2170 $terms = array_merge($terms, array($default)); 2171 2171 } 2172 2172 $terms = array_map('intval', $terms); 2173 wp_set_object_terms( $object, $terms, $taxonomy);2173 wp_set_object_terms( $object_id, $terms, $taxonomy ); 2174 2174 } 2175 2175 2176 2176 // Clean the relationship caches for all object types using this term. 2177 2177 $tax_object = get_taxonomy( $taxonomy ); 2178 2178 foreach ( $tax_object->object_type as $object_type ) 2179 clean_object_term_cache( $object s, $object_type );2179 clean_object_term_cache( $object_ids, $object_type ); 2180 2180 2181 2181 $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) ); 2182 2182 foreach ( $term_meta_ids as $mid ) { … … 2212 2212 * Fires after a term is deleted from the database and the cache is cleaned. 2213 2213 * 2214 2214 * @since 2.5.0 2215 * @since 4.5.0 Introduced `$object_ids` argument. 2215 2216 * 2216 2217 * @param int $term Term ID. 2217 2218 * @param int $tt_id Term taxonomy ID. 2218 2219 * @param string $taxonomy Taxonomy slug. 2219 2220 * @param mixed $deleted_term Copy of the already-deleted term, in the form specified 2220 2221 * by the parent function. WP_Error otherwise. 2222 * @param array $object_ids List of term object IDs. 2221 2223 */ 2222 do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );2224 do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids ); 2223 2225 2224 2226 /** 2225 2227 * Fires after a term in a specific taxonomy is deleted. … … 2228 2230 * taxonomy the term belonged to. 2229 2231 * 2230 2232 * @since 2.3.0 2233 * @since 4.5.0 Introduced `$object_ids` argument. 2231 2234 * 2232 2235 * @param int $term Term ID. 2233 2236 * @param int $tt_id Term taxonomy ID. 2234 2237 * @param mixed $deleted_term Copy of the already-deleted term, in the form specified 2235 2238 * by the parent function. WP_Error otherwise. 2239 * @param array $object_ids List of term object IDs. 2236 2240 */ 2237 do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );2241 do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term, $object_ids ); 2238 2242 2239 2243 return true; 2240 2244 } -
tests/phpunit/tests/term/wpDeleteTerm.php
5 5 */ 6 6 class Tests_Term_WpDeleteTerm extends WP_UnitTestCase { 7 7 protected $deleted_term; 8 protected $object_ids; 8 9 9 10 /** 10 11 * @ticket 33485 12 * @ticket 35213 11 13 */ 12 14 public function test_count_property_passed_to_filters_should_reflect_pre_deleted_term() { 13 15 register_taxonomy( 'wptests_tax', 'post' ); … … 16 18 'taxonomy' => 'wptests_tax', 17 19 ) ); 18 20 19 $p = self::factory()->post->create();21 $post_id = self::factory()->post->create(); 20 22 21 wp_set_object_terms( $p , array( $terms[0] ), 'wptests_tax' );23 wp_set_object_terms( $post_id, array( $terms[0] ), 'wptests_tax' ); 22 24 23 add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 4);25 add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 5 ); 24 26 25 27 wp_delete_term( $terms[0], 'wptests_tax' ); 26 28 $this->assertEquals( 1, $this->deleted_term->count ); 29 $this->assertSame( $this->object_ids, array( "$post_id" ) ); 27 30 28 31 wp_delete_term( $terms[1], 'wptests_tax' ); 29 32 $this->assertEquals( 0, $this->deleted_term->count ); 33 $this->assertSame( $this->object_ids, array() ); 30 34 } 31 35 32 public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term ) {36 public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term, $object_ids ) { 33 37 $this->deleted_term = $deleted_term; 38 $this->object_ids = $object_ids; 34 39 } 35 40 } -
tests/phpunit/tests/term/wpInsertTerm.php
37 37 $this->assertTrue( term_exists($t['term_id']) > 0 ); 38 38 39 39 // now delete it 40 add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4);40 add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 ); 41 41 $this->assertTrue( wp_delete_term( $t['term_id'], $taxonomy ) ); 42 remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4);42 remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 ); 43 43 $this->assertNull( term_exists($term) ); 44 44 $this->assertNull( term_exists($t['term_id']) ); 45 45 $this->assertEquals( $initial_count, wp_count_terms( $taxonomy ) ); … … 644 644 645 645 /** Helpers **********************************************************/ 646 646 647 public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term ) {647 public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) { 648 648 $this->assertInternalType( 'object', $deleted_term ); 649 649 $this->assertInternalType( 'int', $term ); 650 $this->assertInternalType( 'array', $object_ids ); 650 651 // Pesky string $this->assertInternalType( 'int', $tt_id ); 651 652 $this->assertEquals( $term, $deleted_term->term_id ); 652 653 $this->assertEquals( $taxonomy, $deleted_term->taxonomy ); 653 654 $this->assertEquals( $tt_id, $deleted_term->term_taxonomy_id ); 655 $this->assertEmpty( $object_ids ); 654 656 } 655 657 656 658 public function _pre_insert_term_callback() {