Plugins: Standardize the terminology used for actions, filters, and callback functions.

Use `$hook_name` when referring to a filter or action hook name, and `$callback` when referring to a callback function.

This brings more consistency to parameter names in Plugin API functions.

Includes minor code layout fixes for better readability and reordering some functions in a more logical order.

Props johnbillion, jrf, SergeyBiryukov.
Fixes #50531.
Built from https://develop.svn.wordpress.org/trunk@50807


git-svn-id: http://core.svn.wordpress.org/trunk@50416 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-05-04 10:48:03 +00:00
parent 6f2fa9ef42
commit ef28363c7b
3 changed files with 293 additions and 260 deletions

View File

@ -58,25 +58,25 @@ final class WP_Hook implements Iterator, ArrayAccess {
private $doing_action = false;
/**
* Hooks a function or method to a specific filter action.
* Adds a callback function to the specified filter hook.
*
* @since 4.7.0
*
* @param string $tag The name of the filter to hook the $function_to_add callback to.
* @param callable $function_to_add The callback to be run when the filter is applied.
* @param int $priority The order in which the functions associated with a particular action
* are executed. Lower numbers correspond with earlier execution,
* and functions with the same priority are executed in the order
* in which they were added to the action.
* @param int $accepted_args The number of arguments the function accepts.
* @param string $hook_name The name of the filter to add the callback to.
* @param callable $callback The callback to be run when the filter is applied.
* @param int $priority The order in which the functions associated with a particular action
* are executed. Lower numbers correspond with earlier execution,
* and functions with the same priority are executed in the order
* in which they were added to the action.
* @param int $accepted_args The number of arguments the function accepts.
*/
public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
$idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
public function add_filter( $hook_name, $callback, $priority, $accepted_args ) {
$idx = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
$priority_existed = isset( $this->callbacks[ $priority ] );
$this->callbacks[ $priority ][ $idx ] = array(
'function' => $function_to_add,
'function' => $callback,
'accepted_args' => $accepted_args,
);
@ -108,12 +108,15 @@ final class WP_Hook implements Iterator, ArrayAccess {
foreach ( $this->iterations as $index => $iteration ) {
$this->iterations[ $index ] = $new_priorities;
}
return;
}
$min = min( $new_priorities );
foreach ( $this->iterations as $index => &$iteration ) {
$current = current( $iteration );
// If we're already at the end of this iteration, just leave the array pointer where it is.
if ( false === $current ) {
continue;
@ -146,6 +149,7 @@ final class WP_Hook implements Iterator, ArrayAccess {
// Otherwise, just go back to the previous element.
$prev = prev( $iteration );
}
if ( false === $prev ) {
// Start of the array. Reset, and go about our day.
reset( $iteration );
@ -155,55 +159,61 @@ final class WP_Hook implements Iterator, ArrayAccess {
}
}
}
unset( $iteration );
}
/**
* Unhooks a function or method from a specific filter action.
* Removes a callback function from the specified filter hook.
*
* @since 4.7.0
*
* @param string $tag The filter hook to which the function to be removed is hooked.
* @param callable $function_to_remove The callback to be removed from running when the filter is applied.
* @param int $priority The exact priority used when adding the original filter callback.
* @param string $hook_name The filter hook to which the function to be removed is hooked.
* @param callable $callback The callback to be removed from running when the filter is applied.
* @param int $priority The exact priority used when adding the original filter callback.
* @return bool Whether the callback existed before it was removed.
*/
public function remove_filter( $tag, $function_to_remove, $priority ) {
$function_key = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
public function remove_filter( $hook_name, $callback, $priority ) {
$function_key = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
$exists = isset( $this->callbacks[ $priority ][ $function_key ] );
if ( $exists ) {
unset( $this->callbacks[ $priority ][ $function_key ] );
if ( ! $this->callbacks[ $priority ] ) {
unset( $this->callbacks[ $priority ] );
if ( $this->nesting_level > 0 ) {
$this->resort_active_iterations();
}
}
}
return $exists;
}
/**
* Checks if a specific action has been registered for this hook.
* Checks if a specific callback has been registered for this hook.
*
* When using the `$function_to_check` argument, this function may return a non-boolean value
* When using the `$callback` argument, this function may return a non-boolean value
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
*
* @since 4.7.0
*
* @param string $tag Optional. The name of the filter hook. Default empty.
* @param callable|false $function_to_check Optional. The callback to check for. Default false.
* @return bool|int If `$function_to_check` is omitted, returns boolean for whether the hook has
* anything registered. When checking a specific function, the priority of that
* hook is returned, or false if the function is not attached.
* @param string $hook_name Optional. The name of the filter hook. Default empty.
* @param callable|false $callback Optional. The callback to check for. Default false.
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
* anything registered. When checking a specific function, the priority
* of that hook is returned, or false if the function is not attached.
*/
public function has_filter( $tag = '', $function_to_check = false ) {
if ( false === $function_to_check ) {
public function has_filter( $hook_name = '', $callback = false ) {
if ( false === $callback ) {
return $this->has_filters();
}
$function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false );
$function_key = _wp_filter_build_unique_id( $hook_name, $callback, false );
if ( ! $function_key ) {
return false;
}
@ -230,6 +240,7 @@ final class WP_Hook implements Iterator, ArrayAccess {
return true;
}
}
return false;
}
@ -334,6 +345,7 @@ final class WP_Hook implements Iterator, ArrayAccess {
do {
$priority = current( $this->iterations[ $nesting_level ] );
foreach ( $this->callbacks[ $priority ] as $the_ ) {
call_user_func_array( $the_['function'], $args );
}
@ -348,7 +360,8 @@ final class WP_Hook implements Iterator, ArrayAccess {
*
* @since 4.7.0
*
* @return int|false If the hook is running, return the current priority level. If it isn't running, return false.
* @return int|false If the hook is running, return the current priority level.
* If it isn't running, return false.
*/
public function current_priority() {
if ( false === current( $this->iterations ) ) {
@ -391,11 +404,12 @@ final class WP_Hook implements Iterator, ArrayAccess {
/** @var WP_Hook[] $normalized */
$normalized = array();
foreach ( $filters as $tag => $callback_groups ) {
foreach ( $filters as $hook_name => $callback_groups ) {
if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
$normalized[ $tag ] = $callback_groups;
$normalized[ $hook_name ] = $callback_groups;
continue;
}
$hook = new WP_Hook();
// Loop through callback groups.
@ -403,11 +417,13 @@ final class WP_Hook implements Iterator, ArrayAccess {
// Loop through callbacks.
foreach ( $callbacks as $cb ) {
$hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
$hook->add_filter( $hook_name, $cb['function'], $priority, $cb['accepted_args'] );
}
}
$normalized[ $tag ] = $hook;
$normalized[ $hook_name ] = $hook;
}
return $normalized;
}

View File

@ -48,7 +48,7 @@ if ( ! isset( $wp_current_filter ) ) {
}
/**
* Hook a function or method to a specific filter action.
* Adds a callback function to a filter hook.
*
* WordPress offers filter hooks to allow plugins to modify
* various types of internal data at runtime.
@ -101,58 +101,34 @@ if ( ! isset( $wp_current_filter ) ) {
*
* @global WP_Hook[] $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
*
* @param string $tag The name of the filter to hook the $function_to_add callback to.
* @param callable $function_to_add The callback to be run when the filter is applied.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the action. Default 10.
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
* @return true
* @param string $hook_name The name of the filter to add the callback to.
* @param callable $callback The callback to be run when the filter is applied.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular filter are executed.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the filter. Default 10.
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
* @return true Always returns true.
*/
function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
global $wp_filter;
if ( ! isset( $wp_filter[ $tag ] ) ) {
$wp_filter[ $tag ] = new WP_Hook();
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
$wp_filter[ $hook_name ] = new WP_Hook();
}
$wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args );
$wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args );
return true;
}
/**
* Checks if any filter has been registered for a hook.
*
* When using the `$function_to_check` argument, this function may return a non-boolean value
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
*
* @since 2.5.0
*
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
*
* @param string $tag The name of the filter hook.
* @param callable|false $function_to_check Optional. The callback to check for. Default false.
* @return bool|int If `$function_to_check` is omitted, returns boolean for whether the hook has
* anything registered. When checking a specific function, the priority of that
* hook is returned, or false if the function is not attached.
*/
function has_filter( $tag, $function_to_check = false ) {
global $wp_filter;
if ( ! isset( $wp_filter[ $tag ] ) ) {
return false;
}
return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check );
}
/**
* Calls the callback functions that have been added to a filter hook.
*
* The callback functions attached to the filter hook are invoked by calling
* this function. This function can be used to create a new filter hook by
* simply calling this function with the name of the new hook specified using
* the `$tag` parameter.
* This function invokes all functions attached to filter hook `$hook_name`.
* It is possible to create new filter hooks by simply calling this function,
* specifying the name of the new hook using the `$hook_name` parameter.
*
* The function also allows for multiple additional arguments to be passed to hooks.
*
@ -179,37 +155,38 @@ function has_filter( $tag, $function_to_check = false ) {
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
* @global string[] $wp_current_filter Stores the list of current filters with the current one last.
*
* @param string $tag The name of the filter hook.
* @param mixed $value The value to filter.
* @param mixed ...$args Additional parameters to pass to the callback functions.
* @param string $hook_name The name of the filter hook.
* @param mixed $value The value to filter.
* @param mixed ...$args Additional parameters to pass to the callback functions.
* @return mixed The filtered value after all hooked functions are applied to it.
*/
function apply_filters( $tag, $value ) {
function apply_filters( $hook_name, $value ) {
global $wp_filter, $wp_current_filter;
$args = func_get_args();
// Do 'all' actions first.
if ( isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
_wp_call_all_hook( $args );
}
if ( ! isset( $wp_filter[ $tag ] ) ) {
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
if ( isset( $wp_filter['all'] ) ) {
array_pop( $wp_current_filter );
}
return $value;
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
}
// Don't pass the tag name to WP_Hook.
array_shift( $args );
$filtered = $wp_filter[ $tag ]->apply_filters( $value, $args );
$filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );
array_pop( $wp_current_filter );
@ -222,37 +199,38 @@ function apply_filters( $tag, $value ) {
* @since 3.0.0
*
* @see apply_filters() This function is identical, but the arguments passed to the
* functions hooked to `$tag` are supplied using an array.
* functions hooked to `$hook_name` are supplied using an array.
*
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
* @global string[] $wp_current_filter Stores the list of current filters with the current one last.
*
* @param string $tag The name of the filter hook.
* @param array $args The arguments supplied to the functions hooked to $tag.
* @param string $hook_name The name of the filter hook.
* @param array $args The arguments supplied to the functions hooked to `$hook_name`.
* @return mixed The filtered value after all hooked functions are applied to it.
*/
function apply_filters_ref_array( $tag, $args ) {
function apply_filters_ref_array( $hook_name, $args ) {
global $wp_filter, $wp_current_filter;
// Do 'all' actions first.
if ( isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
_wp_call_all_hook( $all_args );
}
if ( ! isset( $wp_filter[ $tag ] ) ) {
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
if ( isset( $wp_filter['all'] ) ) {
array_pop( $wp_current_filter );
}
return $args[0];
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
}
$filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args );
$filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args );
array_pop( $wp_current_filter );
@ -260,13 +238,38 @@ function apply_filters_ref_array( $tag, $args ) {
}
/**
* Removes a function from a specified filter hook.
* Checks if any filter has been registered for a hook.
*
* This function removes a function attached to a specified filter hook. This
* method can be used to remove default functions attached to a specific filter
* When using the `$callback` argument, this function may return a non-boolean value
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
*
* @since 2.5.0
*
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
*
* @param string $hook_name The name of the filter hook.
* @param callable|false $callback Optional. The callback to check for. Default false.
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
* anything registered. When checking a specific function, the priority
* of that hook is returned, or false if the function is not attached.
*/
function has_filter( $hook_name, $callback = false ) {
global $wp_filter;
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
return false;
}
return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback );
}
/**
* Removes a callback function from a filter hook.
*
* This can be used to remove default functions attached to a specific filter
* hook and possibly replace them with a substitute.
*
* To remove a hook, the $function_to_remove and $priority arguments must match
* To remove a hook, the `$callback` and `$priority` arguments must match
* when the hook was added. This goes for both filters and actions. No warning
* will be given on removal failure.
*
@ -274,19 +277,21 @@ function apply_filters_ref_array( $tag, $args ) {
*
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
*
* @param string $tag The filter hook to which the function to be removed is hooked.
* @param callable $function_to_remove The name of the function which should be removed.
* @param int $priority Optional. The priority of the function. Default 10.
* @return bool Whether the function existed before it was removed.
* @param string $hook_name The filter hook to which the function to be removed is hooked.
* @param callable $callback The name of the function which should be removed.
* @param int $priority Optional. The priority of the function. Default 10.
* @return bool Whether the function existed before it was removed.
*/
function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
function remove_filter( $hook_name, $callback, $priority = 10 ) {
global $wp_filter;
$r = false;
if ( isset( $wp_filter[ $tag ] ) ) {
$r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority );
if ( ! $wp_filter[ $tag ]->callbacks ) {
unset( $wp_filter[ $tag ] );
if ( isset( $wp_filter[ $hook_name ] ) ) {
$r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority );
if ( ! $wp_filter[ $hook_name ]->callbacks ) {
unset( $wp_filter[ $hook_name ] );
}
}
@ -294,23 +299,25 @@ function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
}
/**
* Remove all of the hooks from a filter.
* Removes all of the callback functions from a filter hook.
*
* @since 2.7.0
*
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
*
* @param string $tag The filter to remove hooks from.
* @param int|false $priority Optional. The priority number to remove. Default false.
* @return true True when finished.
* @param string $hook_name The filter to remove callbacks from.
* @param int|false $priority Optional. The priority number to remove them from.
* Default false.
* @return true Always returns true.
*/
function remove_all_filters( $tag, $priority = false ) {
function remove_all_filters( $hook_name, $priority = false ) {
global $wp_filter;
if ( isset( $wp_filter[ $tag ] ) ) {
$wp_filter[ $tag ]->remove_all_filters( $priority );
if ( ! $wp_filter[ $tag ]->has_filters() ) {
unset( $wp_filter[ $tag ] );
if ( isset( $wp_filter[ $hook_name ] ) ) {
$wp_filter[ $hook_name ]->remove_all_filters( $priority );
if ( ! $wp_filter[ $hook_name ]->has_filters() ) {
unset( $wp_filter[ $hook_name ] );
}
}
@ -318,39 +325,29 @@ function remove_all_filters( $tag, $priority = false ) {
}
/**
* Retrieve the name of the current filter or action.
* Retrieves the name of the current filter hook.
*
* @since 2.5.0
*
* @global string[] $wp_current_filter Stores the list of current filters with the current one last
*
* @return string Hook name of the current filter or action.
* @return string Hook name of the current filter.
*/
function current_filter() {
global $wp_current_filter;
return end( $wp_current_filter );
}
/**
* Retrieve the name of the current action.
*
* @since 3.9.0
*
* @return string Hook name of the current action.
*/
function current_action() {
return current_filter();
}
/**
* Retrieve the name of a filter currently being processed.
* Returns whether or not a filter hook is 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
* This function allows detection for any filter currently being executed
* (regardless of whether it's the most recent filter to fire, in the case of
* hooks called from hook callbacks) to be verified.
*
* @since 3.9.0
@ -359,35 +356,22 @@ function current_action() {
* @see did_action()
* @global string[] $wp_current_filter Current filter.
*
* @param null|string $filter Optional. Filter to check. Defaults to null, which
* checks if any filter is currently being run.
* @param null|string $hook_name Optional. Filter hook to check. Defaults to null,
* which checks if any filter is currently being run.
* @return bool Whether the filter is currently in the stack.
*/
function doing_filter( $filter = null ) {
function doing_filter( $hook_name = null ) {
global $wp_current_filter;
if ( null === $filter ) {
if ( null === $hook_name ) {
return ! empty( $wp_current_filter );
}
return in_array( $filter, $wp_current_filter, true );
return in_array( $hook_name, $wp_current_filter, true );
}
/**
* Retrieve the name of an action currently being processed.
*
* @since 3.9.0
*
* @param string|null $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.
* Adds a callback function to an action hook.
*
* Actions are the hooks that the WordPress core launches at specific points
* during execution, or when specific events occur. Plugins can specify that
@ -396,26 +380,26 @@ function doing_action( $action = null ) {
*
* @since 1.2.0
*
* @param string $tag The name of the action to which the $function_to_add is hooked.
* @param callable $function_to_add The name of the function you wish to be called.
* @param string $hook_name The name of the action to add the callback to.
* @param callable $callback The callback to be run when the action is called.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed. Default 10.
* associated with a particular action are executed.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the action.
* in the order in which they were added to the action. Default 10.
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
* @return true Will always return true.
* @return true Always returns true.
*/
function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
return add_filter( $tag, $function_to_add, $priority, $accepted_args );
function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
return add_filter( $hook_name, $callback, $priority, $accepted_args );
}
/**
* Execute functions hooked on a specific action hook.
* Calls the callback functions that have been added to an action hook.
*
* This function invokes all functions attached to action hook `$tag`. It is
* possible to create new action hooks by simply calling this function,
* specifying the name of the new hook using the `$tag` parameter.
* This function invokes all functions attached to action hook `$hook_name`.
* It is possible to create new action hooks by simply calling this function,
* specifying the name of the new hook using the `$hook_name` parameter.
*
* You can pass extra arguments to the hooks, much like you can with `apply_filters()`.
*
@ -443,35 +427,36 @@ function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1
* @global int[] $wp_actions Stores the number of times each action was triggered.
* @global string[] $wp_current_filter Stores the list of current filters with the current one last.
*
* @param string $tag The name of the action to be executed.
* @param mixed ...$arg Optional. Additional arguments which are passed on to the
* functions hooked to the action. Default empty.
* @param string $hook_name The name of the action to be executed.
* @param mixed ...$arg Optional. Additional arguments which are passed on to the
* functions hooked to the action. Default empty.
*/
function do_action( $tag, ...$arg ) {
function do_action( $hook_name, ...$arg ) {
global $wp_filter, $wp_actions, $wp_current_filter;
if ( ! isset( $wp_actions[ $tag ] ) ) {
$wp_actions[ $tag ] = 1;
if ( ! isset( $wp_actions[ $hook_name ] ) ) {
$wp_actions[ $hook_name ] = 1;
} else {
++$wp_actions[ $tag ];
++$wp_actions[ $hook_name ];
}
// Do 'all' actions first.
if ( isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
_wp_call_all_hook( $all_args );
}
if ( ! isset( $wp_filter[ $tag ] ) ) {
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
if ( isset( $wp_filter['all'] ) ) {
array_pop( $wp_current_filter );
}
return;
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
}
if ( empty( $arg ) ) {
@ -481,74 +466,55 @@ function do_action( $tag, ...$arg ) {
$arg[0] = $arg[0][0];
}
$wp_filter[ $tag ]->do_action( $arg );
$wp_filter[ $hook_name ]->do_action( $arg );
array_pop( $wp_current_filter );
}
/**
* Retrieve the number of times an action is fired.
*
* @since 2.1.0
*
* @global int[] $wp_actions Stores the number of times each action was triggered.
*
* @param string $tag The name of the action hook.
* @return int The number of times action hook $tag is fired.
*/
function did_action( $tag ) {
global $wp_actions;
if ( ! isset( $wp_actions[ $tag ] ) ) {
return 0;
}
return $wp_actions[ $tag ];
}
/**
* Calls the callback functions that have been added to an action hook, specifying arguments in an array.
*
* @since 2.1.0
*
* @see do_action() This function is identical, but the arguments passed to the
* functions hooked to `$tag` are supplied using an array.
* functions hooked to `$hook_name` are supplied using an array.
*
* @global WP_Hook[] $wp_filter Stores all of the filters and actions.
* @global int[] $wp_actions Stores the number of times each action was triggered.
* @global string[] $wp_current_filter Stores the list of current filters with the current one last.
*
* @param string $tag The name of the action to be executed.
* @param array $args The arguments supplied to the functions hooked to `$tag`.
* @param string $hook_name The name of the action to be executed.
* @param array $args The arguments supplied to the functions hooked to `$hook_name`.
*/
function do_action_ref_array( $tag, $args ) {
function do_action_ref_array( $hook_name, $args ) {
global $wp_filter, $wp_actions, $wp_current_filter;
if ( ! isset( $wp_actions[ $tag ] ) ) {
$wp_actions[ $tag ] = 1;
if ( ! isset( $wp_actions[ $hook_name ] ) ) {
$wp_actions[ $hook_name ] = 1;
} else {
++$wp_actions[ $tag ];
++$wp_actions[ $hook_name ];
}
// Do 'all' actions first.
if ( isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
_wp_call_all_hook( $all_args );
}
if ( ! isset( $wp_filter[ $tag ] ) ) {
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
if ( isset( $wp_filter['all'] ) ) {
array_pop( $wp_current_filter );
}
return;
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $tag;
$wp_current_filter[] = $hook_name;
}
$wp_filter[ $tag ]->do_action( $args );
$wp_filter[ $hook_name ]->do_action( $args );
array_pop( $wp_current_filter );
}
@ -556,52 +522,100 @@ function do_action_ref_array( $tag, $args ) {
/**
* Checks if any action has been registered for a hook.
*
* When using the `$function_to_check` argument, this function may return a non-boolean value
* When using the `$callback` argument, this function may return a non-boolean value
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
*
* @since 2.5.0
*
* @see has_filter() has_action() is an alias of has_filter().
*
* @param string $tag The name of the action hook.
* @param callable|false $function_to_check Optional. The callback to check for. Default false.
* @return bool|int If `$function_to_check` is omitted, returns boolean for whether the hook has
* anything registered. When checking a specific function, the priority of that
* hook is returned, or false if the function is not attached.
* @param string $hook_name The name of the action hook.
* @param callable|false $callback Optional. The callback to check for. Default false.
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
* anything registered. When checking a specific function, the priority
* of that hook is returned, or false if the function is not attached.
*/
function has_action( $tag, $function_to_check = false ) {
return has_filter( $tag, $function_to_check );
function has_action( $hook_name, $callback = false ) {
return has_filter( $hook_name, $callback );
}
/**
* Removes a function from a specified action hook.
* Removes a callback function from an action hook.
*
* This function removes a function attached to a specified action hook. This
* method can be used to remove default functions attached to a specific filter
* This can be used to remove default functions attached to a specific action
* hook and possibly replace them with a substitute.
*
* To remove a hook, the `$callback` and `$priority` arguments must match
* when the hook was added. This goes for both filters and actions. No warning
* will be given on removal failure.
*
* @since 1.2.0
*
* @param string $tag The action hook to which the function to be removed is hooked.
* @param callable $function_to_remove The name of the function which should be removed.
* @param int $priority Optional. The priority of the function. Default 10.
* @param string $hook_name The action hook to which the function to be removed is hooked.
* @param callable $callback The name of the function which should be removed.
* @param int $priority Optional. The priority of the function. Default 10.
* @return bool Whether the function is removed.
*/
function remove_action( $tag, $function_to_remove, $priority = 10 ) {
return remove_filter( $tag, $function_to_remove, $priority );
function remove_action( $hook_name, $callback, $priority = 10 ) {
return remove_filter( $hook_name, $callback, $priority );
}
/**
* Remove all of the hooks from an action.
* Removes all of the callback functions from an action hook.
*
* @since 2.7.0
*
* @param string $tag The action to remove hooks from.
* @param int|false $priority The priority number to remove them from. Default false.
* @return true True when finished.
* @param string $hook_name The action to remove callbacks from.
* @param int|false $priority Optional. The priority number to remove them from.
* Default false.
* @return true Always returns true.
*/
function remove_all_actions( $tag, $priority = false ) {
return remove_all_filters( $tag, $priority );
function remove_all_actions( $hook_name, $priority = false ) {
return remove_all_filters( $hook_name, $priority );
}
/**
* Retrieves the name of the current action hook.
*
* @since 3.9.0
*
* @return string Hook name of the current action.
*/
function current_action() {
return current_filter();
}
/**
* Returns whether or not an action hook is currently being processed.
*
* @since 3.9.0
*
* @param string|null $hook_name Optional. Action hook 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( $hook_name = null ) {
return doing_filter( $hook_name );
}
/**
* Retrieves the number of times an action has been fired during the current request.
*
* @since 2.1.0
*
* @global int[] $wp_actions Stores the number of times each action was triggered.
*
* @param string $hook_name The name of the action hook.
* @return int The number of times the action hook has been fired.
*/
function did_action( $hook_name ) {
global $wp_actions;
if ( ! isset( $wp_actions[ $hook_name ] ) ) {
return 0;
}
return $wp_actions[ $hook_name ];
}
/**
@ -624,20 +638,20 @@ function remove_all_actions( $tag, $priority = false ) {
*
* @see _deprecated_hook()
*
* @param string $tag The name of the filter hook.
* @param string $hook_name The name of the filter hook.
* @param array $args Array of additional function arguments to be passed to apply_filters().
* @param string $version The version of WordPress that deprecated the hook.
* @param string $replacement Optional. The hook that should have been used. Default empty.
* @param string $message Optional. A message regarding the change. Default empty.
*/
function apply_filters_deprecated( $tag, $args, $version, $replacement = '', $message = '' ) {
if ( ! has_filter( $tag ) ) {
function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
if ( ! has_filter( $hook_name ) ) {
return $args[0];
}
_deprecated_hook( $tag, $version, $replacement, $message );
_deprecated_hook( $hook_name, $version, $replacement, $message );
return apply_filters_ref_array( $tag, $args );
return apply_filters_ref_array( $hook_name, $args );
}
/**
@ -651,20 +665,20 @@ function apply_filters_deprecated( $tag, $args, $version, $replacement = '', $me
*
* @see _deprecated_hook()
*
* @param string $tag The name of the action hook.
* @param string $hook_name The name of the action hook.
* @param array $args Array of additional function arguments to be passed to do_action().
* @param string $version The version of WordPress that deprecated the hook.
* @param string $replacement Optional. The hook that should have been used. Default empty.
* @param string $message Optional. A message regarding the change. Default empty.
*/
function do_action_deprecated( $tag, $args, $version, $replacement = '', $message = '' ) {
if ( ! has_action( $tag ) ) {
function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
if ( ! has_action( $hook_name ) ) {
return;
}
_deprecated_hook( $tag, $version, $replacement, $message );
_deprecated_hook( $hook_name, $version, $replacement, $message );
do_action_ref_array( $tag, $args );
do_action_ref_array( $hook_name, $args );
}
//
@ -690,6 +704,7 @@ function plugin_basename( $file ) {
$file = wp_normalize_path( $file );
arsort( $wp_plugin_paths );
foreach ( $wp_plugin_paths as $dir => $realdir ) {
if ( strpos( $file, $realdir ) === 0 ) {
$file = $dir . substr( $file, strlen( $realdir ) );
@ -724,6 +739,7 @@ function wp_register_plugin_realpath( $file ) {
// Normalize, but store as static to avoid recalculation of a constant value.
static $wp_plugin_path = null, $wpmu_plugin_path = null;
if ( ! isset( $wp_plugin_path ) ) {
$wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR );
$wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
@ -783,15 +799,15 @@ function plugin_dir_url( $file ) {
* @since 2.0.0
*
* @param string $file The filename of the plugin including the path.
* @param callable $function The function hooked to the 'activate_PLUGIN' action.
* @param callable $callback The function hooked to the 'activate_PLUGIN' action.
*/
function register_activation_hook( $file, $function ) {
function register_activation_hook( $file, $callback ) {
$file = plugin_basename( $file );
add_action( 'activate_' . $file, $function );
add_action( 'activate_' . $file, $callback );
}
/**
* Set the deactivation hook for a plugin.
* Sets the deactivation hook for a plugin.
*
* When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
* called. In the name of this hook, PLUGINNAME is replaced with the name
@ -806,15 +822,15 @@ function register_activation_hook( $file, $function ) {
* @since 2.0.0
*
* @param string $file The filename of the plugin including the path.
* @param callable $function The function hooked to the 'deactivate_PLUGIN' action.
* @param callable $callback The function hooked to the 'deactivate_PLUGIN' action.
*/
function register_deactivation_hook( $file, $function ) {
function register_deactivation_hook( $file, $callback ) {
$file = plugin_basename( $file );
add_action( 'deactivate_' . $file, $function );
add_action( 'deactivate_' . $file, $callback );
}
/**
* Set the uninstallation hook for a plugin.
* Sets the uninstallation hook for a plugin.
*
* Registers the uninstall hook that will be called when the user clicks on the
* uninstall link that calls for the plugin to uninstall itself. The link won't
@ -852,6 +868,7 @@ function register_uninstall_hook( $file, $callback ) {
*/
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
$plugin_basename = plugin_basename( $file );
if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) {
$uninstallable_plugins[ $plugin_basename ] = $callback;
update_option( 'uninstall_plugins', $uninstallable_plugins );
@ -859,7 +876,7 @@ function register_uninstall_hook( $file, $callback ) {
}
/**
* Call the 'all' hook, which will process the functions hooked into it.
* Calls the 'all' hook, which will process the functions hooked into it.
*
* The 'all' hook passes all of the arguments or parameters that were used for
* the hook, which this function was called for.
@ -883,7 +900,7 @@ function _wp_call_all_hook( $args ) {
}
/**
* Build Unique ID for storage and retrieval.
* Builds Unique ID for storage and retrieval.
*
* The old way to serialize the callback caused issues and this function is the
* solution. It works by checking for objects and creating a new property in
@ -903,33 +920,33 @@ function _wp_call_all_hook( $args ) {
*
* @since 2.2.3
* @since 5.3.0 Removed workarounds for spl_object_hash().
* `$tag` and `$priority` are no longer used,
* `$hook_name` and `$priority` are no longer used,
* and the function always returns a string.
* @access private
*
* @param string $tag Unused. The name of the filter to build ID for.
* @param callable $function The function to generate ID for.
* @param int $priority Unused. The order in which the functions
* associated with a particular action are executed.
* @param string $hook_name Unused. The name of the filter to build ID for.
* @param callable $callback The function to generate ID for.
* @param int $priority Unused. The order in which the functions
* associated with a particular action are executed.
* @return string Unique function ID for usage as array key.
*/
function _wp_filter_build_unique_id( $tag, $function, $priority ) {
if ( is_string( $function ) ) {
return $function;
function _wp_filter_build_unique_id( $hook_name, $callback, $priority ) {
if ( is_string( $callback ) ) {
return $callback;
}
if ( is_object( $function ) ) {
if ( is_object( $callback ) ) {
// Closures are currently implemented as objects.
$function = array( $function, '' );
$callback = array( $callback, '' );
} else {
$function = (array) $function;
$callback = (array) $callback;
}
if ( is_object( $function[0] ) ) {
if ( is_object( $callback[0] ) ) {
// Object class calling.
return spl_object_hash( $function[0] ) . $function[1];
} elseif ( is_string( $function[0] ) ) {
return spl_object_hash( $callback[0] ) . $callback[1];
} elseif ( is_string( $callback[0] ) ) {
// Static calling.
return $function[0] . '::' . $function[1];
return $callback[0] . '::' . $callback[1];
}
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.8-alpha-50806';
$wp_version = '5.8-alpha-50807';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.