WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/26/2014 06:43:59 PM (6 years ago)
Author:
nacin
Message:

Introduce doing_filter() and doing_action() to identify hooks in progress.

did_action() returns true the moment a hook is initially run, leaving you no way to tell if the hook is still in progress. Hooks can be nested and this checks the entire stack, versus current_filter() which only identifies the final hook in the stack. This commit also introduces current_action() for parity.

To tell if a hook has completed, one can use did_action() and ! doing_action() together.

The functions do not require an argument. In that situation, they indicate whether the stack is empty.

props ericmann for the initial unit tests.
fixes #14994.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/plugin.php

    r27262 r27294  
    323323    global $wp_current_filter;
    324324    return end( $wp_current_filter );
     325}
     326
     327/**
     328 * Retrieve the name of the current action.
     329 *
     330 * @since 3.9.0
     331 * @uses current_filter()
     332 *
     333 * @return string Hook name of the current action.
     334 */
     335function current_action() {
     336    return current_filter();
     337}
     338
     339/**
     340 * Retrieve the name of a filter currently being processed.
     341 *
     342 * The function current_filter() only returns the most recent filter or action
     343 * being executed.  did_action() returns true once the action is initially
     344 * processed.  This function allows detection for any filter currently being
     345 * executed (despite not being the most recent filter to fire, in the case of
     346 * hooks called from hook callbacks) to be verified.
     347 *
     348 * @since 3.9.0
     349 * @see current_filter()
     350 * @see did_action()
     351 *
     352 * @param string $filter Optional. Filter to check. Defaults to null, which checks if any filter is currently being run.
     353 *
     354 * @global array $wp_current_filter
     355 *
     356 * @return bool Whether the filter is currently in the stack
     357 */
     358function doing_filter( $filter = null ) {
     359    global $wp_current_filter;
     360
     361    if ( null === $filter ) {
     362        return ! empty( $wp_current_filter );
     363    }
     364
     365    return in_array( $filter, $wp_current_filter );
     366}
     367
     368/**
     369 * Retrieve the name of an action currently being processed.
     370 *
     371 * @since 3.9.0
     372 * @uses doing_filter()
     373 *
     374 * @param string $action Optional. Action to check. Defaults to null, which checks if any action is currently being run.
     375 *
     376 * @return bool Whether the action is currently in the stack.
     377 */
     378function doing_action( $action = null ) {
     379    return doing_filter( $action );
    325380}
    326381
Note: See TracChangeset for help on using the changeset viewer.