Ticket #43621: 43621.wp_hook.diff
File 43621.wp_hook.diff, 3.8 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-hook.php
diff --git src/wp-includes/class-wp-hook.php src/wp-includes/class-wp-hook.php index efb1360..ebdcecc 100644
final class WP_Hook implements Iterator, ArrayAccess { 42 42 private $current_priority = array(); 43 43 44 44 /** 45 * The current callback that is running. 46 * 47 * @since 5.0 48 * @var callable 49 */ 50 private $current_callback = null; 51 /** 52 53 /** 54 * The previous callback that ran before the current one. 55 * 56 * @since 5.0 57 * @var callable 58 */ 59 private $previous_callback = null; 60 61 /** 45 62 * Number of levels this hook can be recursively called. 46 63 * 47 64 * @since 4.7.0 … … final class WP_Hook implements Iterator, ArrayAccess { 279 296 $args[0] = $value; 280 297 } 281 298 299 $this->current_callback = $the_; 300 282 301 // Avoid the array_slice if possible. 283 302 if ( $the_['accepted_args'] == 0 ) { 284 303 $value = call_user_func_array( $the_['function'], array() ); … … final class WP_Hook implements Iterator, ArrayAccess { 287 306 } else { 288 307 $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); 289 308 } 309 310 $this->current_callback = null; 311 $this->previous_callback = $the_; 290 312 } 291 313 } while ( false !== next( $this->iterations[ $nesting_level ] ) ); 292 314 … … final class WP_Hook implements Iterator, ArrayAccess { 353 375 } 354 376 355 377 /** 378 * Return the current callback that is running. 379 * 380 * @since 5.0 381 * 382 * @return callable|null Callback that is running or null if no callback running. 383 */ 384 public function current_callback() { 385 return $this->current_callback; 386 } 387 388 /** 389 * Return the callback that has just run, or ran before the current callback. 390 * 391 * @since 5.0 392 * 393 * @return callable|null Callback that ran previously or null if no callback ran previously. 394 */ 395 public function previous_callback() { 396 return $this->previous_callback; 397 } 398 399 /** 356 400 * Normalizes filters set up before WordPress has initialized to WP_Hook objects. 357 401 * 358 402 * @since 4.7.0 -
tests/phpunit/tests/filters.php
diff --git tests/phpunit/tests/filters.php tests/phpunit/tests/filters.php index 03af7e5..3e8a251 100644
class Tests_Filters extends WP_UnitTestCase { 380 380 global $wp_filter; 381 381 $this->current_priority = $wp_filter['the_content']->current_priority(); 382 382 } 383 384 /** 385 * @ticket 43621 386 */ 387 public function test_current_filter_current_previous_callback() { 388 global $_current_previous_callbacks; 389 390 $_current_previous_callbacks = array( 391 array( $this, '_current_previous_callback_one' ), 392 array( $this, '_current_previous_callback_two' ), 393 ); 394 395 add_action( 'test_current_previous_callback', $_current_previous_callbacks[0] ); 396 add_action( 'test_current_previous_callback', $_current_previous_callbacks[1] ); 397 398 do_action( 'test_current_previous_callback' ); 399 } 400 401 public function _current_previous_callback_one() { 402 global $wp_filter, $_current_previous_callbacks; 403 404 $current_filter = $wp_filter[ current_filter() ]; 405 $current_callback = $current_filter->current_callback(); 406 $previous_callback = $current_filter->previous_callback(); 407 408 $this->assertEquals( $_current_previous_callbacks[0], $current_callback['function'] ); 409 $this->assertNull( $previous_callback ); 410 } 411 412 public function _current_previous_callback_two() { 413 global $wp_filter, $_current_previous_callbacks; 414 415 $current_filter = $wp_filter[ current_filter() ]; 416 $current_callback = $current_filter->current_callback(); 417 $previous_callback = $current_filter->previous_callback(); 418 419 $this->assertEquals( $_current_previous_callbacks[1], $current_callback['function'] ); 420 $this->assertEquals( $_current_previous_callbacks[0], $previous_callback['function'] ); 421 } 383 422 }