Make WordPress Core

Ticket #43621: 43621.wp_hook.diff

File 43621.wp_hook.diff, 3.8 KB (added by soulseekah, 7 years ago)

Add WP_Hook::current_callback, WP_Hook::previous_callback (w/ tests)

  • 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 { 
    4242        private $current_priority = array();
    4343
    4444        /**
     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        /**
    4562         * Number of levels this hook can be recursively called.
    4663         *
    4764         * @since 4.7.0
    final class WP_Hook implements Iterator, ArrayAccess { 
    279296                                        $args[0] = $value;
    280297                                }
    281298
     299                                $this->current_callback = $the_;
     300
    282301                                // Avoid the array_slice if possible.
    283302                                if ( $the_['accepted_args'] == 0 ) {
    284303                                        $value = call_user_func_array( $the_['function'], array() );
    final class WP_Hook implements Iterator, ArrayAccess { 
    287306                                } else {
    288307                                        $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
    289308                                }
     309
     310                                $this->current_callback = null;
     311                                $this->previous_callback = $the_;
    290312                        }
    291313                } while ( false !== next( $this->iterations[ $nesting_level ] ) );
    292314
    final class WP_Hook implements Iterator, ArrayAccess { 
    353375        }
    354376
    355377        /**
     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        /**
    356400         * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
    357401         *
    358402         * @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 { 
    380380                global $wp_filter;
    381381                $this->current_priority = $wp_filter['the_content']->current_priority();
    382382        }
     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        }
    383422}