Make WordPress Core

Ticket #14994: 14994.3.diff

File 14994.3.diff, 3.7 KB (added by ericmann, 11 years ago)

Refresh patch and add unit tests

  • 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 current_filter()
     365 * @see did_action()
     366 *
     367 * @param string $filter Optional. Filter to check. Defaults to null, which checks if any filter is currently being run.
     368 *
     369 * @global array $wp_current_filter
     370 *
     371 * @return bool Whether the filter is currently in the stack
     372 */
     373function doing_filter( $filter = null ) {
     374        global $wp_current_filter;
     375
     376        if ( null === $filter ) {
     377                return empty( $wp_current_filter );
     378        }
     379
     380        return in_array( $filter, $wp_current_filter );
     381}
     382
     383/**
     384 * Retrieve the name of an action currently being processed.
     385 *
     386 * @since 3.9.0
     387 * @uses doing_filter()
     388 *
     389 * @param string $action Optional. Action to check. Defaults to null, which checks if any action is currently being run.
     390 *
     391 * @return bool Whether the action is currently in the stack.
     392 */
     393function doing_action( $action = null ) {
     394        return doing_filter( $action );
     395}
     396
     397/**
    343398 * Hooks a function on to a specific action.
    344399 *
    345400 * 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}