diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php index 81e0aa952d..c1e5ff0bbe 100644 --- a/src/wp-includes/plugin.php +++ b/src/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/tests/phpunit/tests/filters.php b/tests/phpunit/tests/filters.php index 6bcd437365..51ada1f981 100644 --- a/tests/phpunit/tests/filters.php +++ b/tests/phpunit/tests/filters.php @@ -215,6 +215,49 @@ class Tests_Filters extends WP_UnitTestCase { $this->assertFalse( has_filter( $tag ) ); } + /** + * @ticket 53218 + */ + public function test_filter_with_ref_value() { + $obj = new stdClass(); + $ref = &$obj; + $a = new MockAction(); + $tag = __FUNCTION__; + + add_action( $tag, array( $a, 'filter' ) ); + + $filtered = apply_filters( $tag, $ref ); + + $args = $a->get_args(); + $this->assertSame( $args[0][0], $obj ); + $this->assertSame( $filtered, $obj ); + // Just in case we don't trust assertSame(). + $obj->foo = true; + $this->assertNotEmpty( $args[0][0]->foo ); + $this->assertNotEmpty( $filtered->foo ); + } + + /** + * @ticket 53218 + */ + public function test_filter_with_ref_argument() { + $obj = new stdClass(); + $ref = &$obj; + $a = new MockAction(); + $tag = __FUNCTION__; + $val = 'Hello'; + + add_action( $tag, array( $a, 'filter' ), 10, 2 ); + + apply_filters( $tag, $val, $ref ); + + $args = $a->get_args(); + $this->assertSame( $args[0][1], $obj ); + // Just in case we don't trust assertSame(). + $obj->foo = true; + $this->assertNotEmpty( $args[0][1]->foo ); + } + /** * @ticket 9886 */