Ticket #40685: 40685.patch
File 40685.patch, 12.3 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-hook.php
109 109 private function resort_active_iterations( $new_priority = false, $priority_existed = false ) { 110 110 $new_priorities = array_keys( $this->callbacks ); 111 111 112 // If there are no remaining hooks, clear out all running iterations.113 if ( ! $new_priorities ) {114 foreach ( $this->iterations as $index => $iteration ) {115 $this->iterations[ $index ] = $new_priorities;116 }117 return;118 }119 120 112 $min = min( $new_priorities ); 121 113 foreach ( $this->iterations as $index => &$iteration ) { 122 114 $current = current( $iteration ); … … 182 174 $exists = isset( $this->callbacks[ $priority ][ $function_key ] ); 183 175 if ( $exists ) { 184 176 unset( $this->callbacks[ $priority ][ $function_key ] ); 185 if ( ! $this->callbacks[ $priority ] ) {186 unset( $this->callbacks[ $priority ] );187 if ( $this->nesting_level > 0 ) {188 $this->resort_active_iterations();189 }190 }191 177 } 192 178 return $exists; 193 179 } … … 254 240 255 241 if ( false === $priority ) { 256 242 $this->callbacks = array(); 243 if ( $this->nesting_level > 0 ) { 244 foreach ( $this->iterations as $index => $iteration ) { 245 $this->iterations[ $index ] = array(); 246 } 247 } 257 248 } else if ( isset( $this->callbacks[ $priority ] ) ) { 258 unset( $this->callbacks[ $priority ]);249 $this->callbacks[ $priority ] = array(); 259 250 } 260 261 if ( $this->nesting_level > 0 ) {262 $this->resort_active_iterations();263 }264 251 } 265 252 266 253 /** -
tests/phpunit/tests/filters.php
380 380 global $wp_filter; 381 381 $this->current_priority = $wp_filter[ 'the_content' ]->current_priority(); 382 382 } 383 384 /** 385 * @ticket 40685 386 */ 387 public function test_add_three_filters_and_middle_self_remove() { 388 add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 389 add_filter( 'ticket_40685', array( $this, '_filter_middle_self_remove' ), 2, 1 ); 390 add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 391 392 $value = apply_filters( 'ticket_40685', '' ); 393 $this->assertSame( '1r23', $value ); 394 } 395 396 public function _filter_first( $value ) { 397 return $value . '1'; 398 } 399 400 public function _filter_middle( $value ) { 401 return $value . '2'; 402 } 403 404 public function _filter_last( $value ) { 405 return $value . '3'; 406 } 407 408 public function _filter_middle_self_remove( $value ) { 409 remove_filter( 'ticket_40685', array( $this, __FUNCTION__ ), 2 ); 410 return $value . 'r2'; 411 } 383 412 } -
tests/phpunit/tests/hooks/add_filter.php
277 277 public function _action_remove_and_add4() { 278 278 $this->action_output .= '4'; 279 279 } 280 281 /** 282 * @ticket 40685 283 */ 284 public function test_add_three_filters_and_middle_self_remove() { 285 $this->hook = new Wp_Hook(); 286 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 287 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle_self_remove' ), 2, 1 ); 288 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 289 290 $value = $this->hook->apply_filters( '', array() ); 291 $this->assertSame( '1r23', $value ); 292 } 293 294 /** 295 * @ticket 40685 296 */ 297 public function test_add_four_filters_and_middle_self_remove_and_middle2_self_remove() { 298 $this->hook = new Wp_Hook(); 299 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 300 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle_self_remove' ), 2, 1 ); 301 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle2_self_remove' ), 2, 1 ); 302 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 303 304 $value = $this->hook->apply_filters( '', array() ); 305 $this->assertSame( '1r2rr23', $value ); 306 } 307 308 /** 309 * @ticket 40685 310 */ 311 public function test_add_four_filters_and_middle_self_remove_and_middle_remove_before() { 312 $this->hook = new Wp_Hook(); 313 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 314 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle_self_remove_and_middle_remove' ), 2, 1 ); 315 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle' ), 2, 1 ); 316 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 317 318 $value = $this->hook->apply_filters( '', array() ); 319 $this->assertSame( '1rb223', $value ); 320 } 321 322 /** 323 * @ticket 40685 324 */ 325 public function test_add_four_filters_and_middle_self_remove_and_middle_remove_after() { 326 $this->hook = new Wp_Hook(); 327 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 328 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle' ), 2, 1 ); 329 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle_self_remove_and_middle_remove' ), 2, 1 ); 330 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 331 332 $value = $this->hook->apply_filters( '', array() ); 333 $this->assertSame( '12rb23', $value ); 334 } 335 336 public function _filter_first( $value ) { 337 return $value . '1'; 338 } 339 340 public function _filter_middle( $value ) { 341 return $value . '2'; 342 } 343 344 public function _filter_middle2( $value ) { 345 return $value . 'm2'; 346 } 347 348 public function _filter_last( $value ) { 349 return $value . '3'; 350 } 351 352 public function _filter_middle_self_remove( $value ) { 353 $this->hook->remove_filter( 'ticket_40685', array( $this, __FUNCTION__ ), 2 ); 354 return $value . 'r2'; 355 } 356 357 public function _filter_middle2_self_remove( $value ) { 358 $this->hook->remove_filter( 'ticket_40685', array( $this, __FUNCTION__ ), 2 ); 359 return $value . 'rr2'; 360 } 361 362 public function _filter_middle_self_remove_and_middle_remove( $value ) { 363 $this->hook->remove_filter( 'ticket_40685', array( $this, __FUNCTION__ ), 2 ); 364 $this->hook->remove_filter( 'ticket_40685', array( $this, '_filter_middle' ), 2 ); 365 return $value . 'rb2'; 366 } 280 367 } -
tests/phpunit/tests/hooks/remove_all_filters.php
7 7 */ 8 8 class Tests_WP_Hook_Remove_All_Filters extends WP_UnitTestCase { 9 9 10 public $hook; 11 10 12 public function test_remove_all_filters() { 11 13 $callback = '__return_null'; 12 14 $hook = new WP_Hook(); … … 38 40 $this->assertTrue( $hook->has_filters() ); 39 41 $this->assertEquals( $priority + 1, $hook->has_filter( $tag, $callback_two ) ); 40 42 } 43 44 /** 45 * @ticket 40685 46 */ 47 public function test_remove_all_filters_with_priority_in_filter_before_other_middles() { 48 $this->hook = new Wp_Hook(); 49 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 50 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_remove_all_priority2' ), 2, 1 ); 51 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle' ), 2, 1 ); 52 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle2' ), 2, 1 ); 53 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 54 55 $value = $this->hook->apply_filters( '', array() ); 56 $this->assertSame( '1rp22m23', $value ); // Note priority 2 hooks will still be run. 57 } 58 59 /** 60 * @ticket 40685 61 */ 62 public function test_remove_all_filters_with_priority_in_filter_in_the_middle() { 63 $this->hook = new Wp_Hook(); 64 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 65 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle' ), 2, 1 ); 66 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_remove_all_priority2' ), 2, 1 ); 67 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle2' ), 2, 1 ); 68 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 69 70 $value = $this->hook->apply_filters( '', array() ); 71 $this->assertSame( '12rp2m23', $value ); // Note priority 2 hooks will still be run. 72 } 73 74 /** 75 * @ticket 40685 76 */ 77 public function test_remove_all_filters_with_priority_in_filter_after_other_middles() { 78 $this->hook = new Wp_Hook(); 79 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_first' ), 1, 1 ); 80 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle' ), 2, 1 ); 81 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_middle2' ), 2, 1 ); 82 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_remove_all_priority2' ), 2, 1 ); 83 $this->hook->add_filter( 'ticket_40685', array( $this, '_filter_last' ), 3, 1 ); 84 85 $value = $this->hook->apply_filters( '', array() ); 86 $this->assertSame( '12m2rp23', $value ); 87 } 88 89 public function _filter_first( $value ) { 90 return $value . '1'; 91 } 92 93 public function _filter_middle( $value ) { 94 return $value . '2'; 95 } 96 97 public function _filter_middle2( $value ) { 98 return $value . 'm2'; 99 } 100 101 public function _filter_last( $value ) { 102 return $value . '3'; 103 } 104 105 public function _filter_remove_all( $value ) { 106 $this->hook->remove_all_filters(); 107 return $value . 'ra'; 108 } 109 110 public function _filter_remove_all_priority2( $value ) { 111 $this->hook->remove_all_filters( 2 ); 112 return $value . 'rp2'; 113 } 41 114 } -
tests/phpunit/tests/hooks/remove_filter.php
17 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 18 18 $hook->remove_filter( $tag, $callback, $priority ); 19 19 20 $this->assert False( isset( $hook->callbacks[ $priority ] ));20 $this->assertEmpty( $hook->callbacks[ $priority ] ); 21 21 } 22 22 23 23 public function test_remove_filter_with_object() { … … 31 31 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 32 32 $hook->remove_filter( $tag, $callback, $priority ); 33 33 34 $this->assert False( isset( $hook->callbacks[ $priority ] ));34 $this->assertEmpty( $hook->callbacks[ $priority ] ); 35 35 } 36 36 37 37 public function test_remove_filter_with_static_method() { … … 44 44 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 45 45 $hook->remove_filter( $tag, $callback, $priority ); 46 46 47 $this->assert False( isset( $hook->callbacks[ $priority ] ));47 $this->assertEmpty( $hook->callbacks[ $priority ] ); 48 48 } 49 49 50 50 public function test_remove_filters_with_another_at_same_priority() { … … 75 75 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args ); 76 76 77 77 $hook->remove_filter( $tag, $callback_one, $priority ); 78 $this->assert False( isset( $hook->callbacks[ $priority ] ));78 $this->assertEmpty( $hook->callbacks[ $priority ] ); 79 79 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); 80 80 } 81 81 } -
tests/phpunit/tests/post/types.php
425 425 426 426 $this->assertSame( 1, count( $wp_filter['future_foo'] ) ); 427 427 $this->assertTrue( unregister_post_type( 'foo' ) ); 428 $this->assert ArrayNotHasKey( 'future_foo', $wp_filter);428 $this->assertFalse( has_filter( 'future_foo' ) ); 429 429 } 430 430 431 431 /** … … 441 441 442 442 $this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo'] ) ); 443 443 $this->assertTrue( unregister_post_type( 'foo' ) ); 444 $this->assert ArrayNotHasKey( 'add_meta_boxes_foo', $wp_filter);444 $this->assertFalse( has_filter( 'add_meta_boxes_foo' ) ); 445 445 } 446 446 447 447 /** -
tests/phpunit/tests/taxonomy.php
697 697 698 698 $this->assertSame( 1, count( $wp_filter['wp_ajax_add-foo'] ) ); 699 699 $this->assertTrue( unregister_taxonomy( 'foo' ) ); 700 $this->assert ArrayNotHasKey( 'wp_ajax_add-foo', $wp_filter);700 $this->assertFalse( has_filter( 'wp_ajax_add-foo' ) ); 701 701 } 702 702 703 703 /**