WordPress.org

Make WordPress Core

Ticket #14994: 14994.4.diff

File 14994.4.diff, 3.8 KB (added by ericmann, 6 years ago)

Use current_filter()/current_action() rather than a call to a global variable

  • src/wp-includes/plugin.php

     
    340340}
    341341
    342342/**
     343 * Retrieve the name of the current action.
     344 *
     345 * @since 3.9.0
     346 * @uses current_filter();
     347 *
     348 * @return string Hook name of the current action.
     349 */
     350function current_action() {
     351        return current_filter();
     352}
     353
     354/**
     355 * Retrieve the name of a filter currently being processed.
     356 *
     357 * The function current_filter() only returns the most recent filter or action
     358 * being executed.  did_action() returns true once the action is initially
     359 * processed.  This function allows detection for any filter currently being
     360 * executed (despite not being the most recent filter to fire, in the case of
     361 * hooks called from hook callbacks) to be verified.
     362 *
     363 * @since 3.9.0
     364 * @see did_action()
     365 * @uses current_filter()
     366 *
     367 * @param string $filter Optional. Filter to check. Defaults to null, which checks if any filter is currently being run.
     368 *
     369 * @return bool Whether the filter is currently in the stack
     370 */
     371function doing_filter( $filter = null ) {
     372        $current_filter = current_filter();
     373
     374        if ( null === $filter ) {
     375                return false === $current_filter;
     376        }
     377
     378        return $filter === $current_filter;
     379}
     380
     381/**
     382 * Retrieve the name of an action currently being processed.
     383 *
     384 * @since 3.9.0
     385 * @uses current_action()
     386 *
     387 * @param string $action Optional. Action to check. Defaults to null, which checks if any action is currently being run.
     388 *
     389 * @return bool Whether the action is currently in the stack.
     390 */
     391function doing_action( $action = null ) {
     392        $current_action = current_action();
     393
     394        if ( null === $action ) {
     395                return false === $current_action;
     396        }
     397
     398        return $action === $current_action;
     399}
     400
     401/**
    343402 * Hooks a function on to a specific action.
    344403 *
    345404 * Actions are the hooks that the WordPress core launches at specific points
  • tests/phpunit/tests/actions.php

     
    256256        function action_self_removal() {
    257257                remove_action( 'test_action_self_removal', array( $this, 'action_self_removal' ) );
    258258        }
     259
     260        /**
     261         * Make sure current_action() behaves as current_filter()
     262         *
     263         * @ticket 14994
     264         */
     265        function test_current_action() {
     266                global $wp_current_filter;
     267                $wp_current_filter[] = 'first';
     268                $wp_current_filter[] = 'second'; // Let's say a second action was invoked.
     269
     270                $this->assertEquals( 'second', current_action() );
     271        }
     272
     273        /**
     274         * @ticket 14994
     275         */
     276        function test_doing_filter() {
     277                global $wp_current_filter;
     278                $wp_current_filter = array(); // Set to an empty array first
     279
     280                $this->assertTrue( doing_filter() ); // No filter is passed in, and no filter is being processed
     281                $this->assertFalse( doing_filter( 'testing' ) ); // Filter is passed in but not being processed
     282
     283                $wp_current_filter[] = 'testing';
     284
     285                $this->assertTrue( doing_filter( 'testing') ); // Filter is passed in and is being processed
     286                $this->assertFalse( doing_filter( 'something_else' ) ); // Filter is passed in but not being processed
     287        }
     288
     289        /**
     290         * @ticket 14994
     291         */
     292        function test_doing_action() {
     293                global $wp_current_filter;
     294                $wp_current_filter = array(); // Set to an empty array first
     295
     296                $this->assertTrue( doing_action() ); // No action is passed in, and no filter is being processed
     297                $this->assertFalse( doing_action( 'testing' ) ); // Action is passed in but not being processed
     298
     299                $wp_current_filter[] = 'testing';
     300
     301                $this->assertTrue( doing_action( 'testing') ); // Action is passed in and is being processed
     302                $this->assertFalse( doing_action( 'something_else' ) ); // Action is passed in but not being processed
     303        }
    259304}