From 6571e44299e05965bb29d2ad15c6126d3e8c4ef5 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 18 Mar 2022 18:23:04 +0000 Subject: [PATCH] Plugins: Convert `apply_filters()` into a proper variadic function. This makes its signature more correct by implementing the spread operator, and adjusts the internal logic correspondingly without affecting performance. Props jrf, SergeyBiryukov, davidbaumwald, mauriac, johnbillion Fixes #53218 Built from https://develop.svn.wordpress.org/trunk@52952 git-svn-id: http://core.svn.wordpress.org/trunk@52541 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/plugin.php | 14 ++++++++------ wp-includes/version.php | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/wp-includes/plugin.php b/wp-includes/plugin.php index 81e0aa952d..c1e5ff0bbe 100644 --- a/wp-includes/plugin.php +++ b/wp-includes/plugin.php @@ -151,6 +151,8 @@ function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); * * @since 0.71 + * @since 6.0.0 Formalized the existing and already documented `...$args` parameter + * by adding it to the function signature. * * @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. @@ -160,15 +162,15 @@ function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) * @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( $hook_name, $value ) { +function apply_filters( $hook_name, $value, ...$args ) { global $wp_filter, $wp_current_filter; - $args = func_get_args(); - // Do 'all' actions first. if ( isset( $wp_filter['all'] ) ) { $wp_current_filter[] = $hook_name; - _wp_call_all_hook( $args ); + + $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection + _wp_call_all_hook( $all_args ); } if ( ! isset( $wp_filter[ $hook_name ] ) ) { @@ -183,8 +185,8 @@ function apply_filters( $hook_name, $value ) { $wp_current_filter[] = $hook_name; } - // Don't pass the tag name to WP_Hook. - array_shift( $args ); + // Pass the value to WP_Hook. + array_unshift( $args, $value ); $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 0e36b3b996..748e82cefe 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-alpha-52951'; +$wp_version = '6.0-alpha-52952'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.