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. Built from https://develop.svn.wordpress.org/trunk@27294 git-svn-id: http://core.svn.wordpress.org/trunk@27150 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
22b4567550
commit
076077383d
|
@ -324,6 +324,61 @@ function current_filter() {
|
||||||
return end( $wp_current_filter );
|
return end( $wp_current_filter );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the name of the current action.
|
||||||
|
*
|
||||||
|
* @since 3.9.0
|
||||||
|
* @uses current_filter()
|
||||||
|
*
|
||||||
|
* @return string Hook name of the current action.
|
||||||
|
*/
|
||||||
|
function current_action() {
|
||||||
|
return current_filter();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the name of a filter currently being processed.
|
||||||
|
*
|
||||||
|
* The function current_filter() only returns the most recent filter or action
|
||||||
|
* being executed. did_action() returns true once the action is initially
|
||||||
|
* processed. This function allows detection for any filter currently being
|
||||||
|
* executed (despite not being the most recent filter to fire, in the case of
|
||||||
|
* hooks called from hook callbacks) to be verified.
|
||||||
|
*
|
||||||
|
* @since 3.9.0
|
||||||
|
* @see current_filter()
|
||||||
|
* @see did_action()
|
||||||
|
*
|
||||||
|
* @param string $filter Optional. Filter to check. Defaults to null, which checks if any filter is currently being run.
|
||||||
|
*
|
||||||
|
* @global array $wp_current_filter
|
||||||
|
*
|
||||||
|
* @return bool Whether the filter is currently in the stack
|
||||||
|
*/
|
||||||
|
function doing_filter( $filter = null ) {
|
||||||
|
global $wp_current_filter;
|
||||||
|
|
||||||
|
if ( null === $filter ) {
|
||||||
|
return ! empty( $wp_current_filter );
|
||||||
|
}
|
||||||
|
|
||||||
|
return in_array( $filter, $wp_current_filter );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the name of an action currently being processed.
|
||||||
|
*
|
||||||
|
* @since 3.9.0
|
||||||
|
* @uses doing_filter()
|
||||||
|
*
|
||||||
|
* @param string $action Optional. Action to check. Defaults to null, which checks if any action is currently being run.
|
||||||
|
*
|
||||||
|
* @return bool Whether the action is currently in the stack.
|
||||||
|
*/
|
||||||
|
function doing_action( $action = null ) {
|
||||||
|
return doing_filter( $action );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hooks a function on to a specific action.
|
* Hooks a function on to a specific action.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue