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.

git-svn-id: https://develop.svn.wordpress.org/trunk@50807 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2021-05-04 10:46:26 +00:00
parent 86d09a09a4
commit 96c9af127c
2 changed files with 292 additions and 259 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;
}