From 25ed69e4b7ac15ecbd0ea3c552633e16d10be8d2 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Thu, 4 Jan 2024 02:35:49 +0100 Subject: [PATCH 1/3] drop support for PHP 4 in favor of PHP 7+ native types --- src/wp-includes/plugin.php | 5 +--- tests/phpunit/includes/utils.php | 20 +++++++++++++++ tests/phpunit/tests/actions.php | 44 +++++++++++++++++--------------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php index 77c1eb4ef669b..f2b2a00dba097 100644 --- a/src/wp-includes/plugin.php +++ b/src/wp-includes/plugin.php @@ -507,11 +507,8 @@ function do_action( $hook_name, ...$arg ) { $wp_current_filter[] = $hook_name; } - if ( empty( $arg ) ) { + if ( array() === $arg ) { $arg[] = ''; - } elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) { - // Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`. - $arg[0] = $arg[0][0]; } $wp_filter[ $hook_name ]->do_action( $arg ); diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index ead788f996ec9..ff03f6518c27a 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -133,6 +133,26 @@ public function action2( $arg ) { return $arg; } + /** + * @since 6.6.0 + */ + public function action3( array $arg ) { + $current_filter = $this->current_filter(); + + if ( $this->debug ) { + dmp( __FUNCTION__, $current_filter ); + } + + $this->events[] = array( + 'action' => __FUNCTION__, + 'hook_name' => $current_filter, + 'tag' => $current_filter, // Back compat. + 'args' => func_get_args(), + ); + + return $arg; + } + /** * @since UT (3.7.0) */ diff --git a/tests/phpunit/tests/actions.php b/tests/phpunit/tests/actions.php index e25183f75913d..12035a508e2cb 100644 --- a/tests/phpunit/tests/actions.php +++ b/tests/phpunit/tests/actions.php @@ -96,6 +96,29 @@ public function test_has_action() { $this->assertFalse( has_action( $hook_name ) ); } + /** + * One tag with array with 1 object to ensure it will stay an array and not become an object + * + * @ticket 60190 + * + * @covers ::do_action + */ + public function test_action_array_of_object_arg() { + $a = new MockAction(); + $hook_name = __FUNCTION__; + $val = new stdClass(); + $arg = array( $val ); + + add_action( $hook_name, array( $a, 'action3' ) ); + do_action( $hook_name, $arg ); + + $call_count = $a->get_call_count(); + $argsvar = $a->get_args(); + + // nested since func_get_args() returns the args as an array + $this->assertSame( array( $arg ), array_pop( $argsvar ) ); + } + /** * One tag with multiple actions. * @@ -208,27 +231,6 @@ public function test_action_args_3() { $this->assertSame( array( $val1, $val2 ), array_pop( $argsvar3 ) ); } - /** - * Tests PHP 4 notation for calling actions while passing in an object by reference. - * - * @ticket 48312 - * - * @covers ::do_action - */ - public function test_action_args_with_php4_syntax() { - $a = new MockAction(); - $hook_name = __FUNCTION__; - $val = new stdClass(); - - add_action( $hook_name, array( &$a, 'action' ) ); - // Call the action with PHP 4 notation for passing object by reference. - do_action( $hook_name, array( &$val ) ); - - $call_count = $a->get_call_count(); - $argsvar = $a->get_args(); - $this->assertSame( array( $val ), array_pop( $argsvar ) ); - } - /** * @ticket 60193 * From 0fadc1550c0f256a7b8f3068769e5c434eb6b1cb Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 21 May 2025 22:24:55 +0100 Subject: [PATCH 2/3] Update the tests. --- tests/phpunit/includes/utils.php | 2 +- tests/phpunit/tests/actions.php | 46 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index ff03f6518c27a..83b0ba6ef7d36 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -134,7 +134,7 @@ public function action2( $arg ) { } /** - * @since 6.6.0 + * @since 6.9.0 */ public function action3( array $arg ) { $current_filter = $this->current_filter(); diff --git a/tests/phpunit/tests/actions.php b/tests/phpunit/tests/actions.php index 12035a508e2cb..363944ff77a3b 100644 --- a/tests/phpunit/tests/actions.php +++ b/tests/phpunit/tests/actions.php @@ -96,29 +96,6 @@ public function test_has_action() { $this->assertFalse( has_action( $hook_name ) ); } - /** - * One tag with array with 1 object to ensure it will stay an array and not become an object - * - * @ticket 60190 - * - * @covers ::do_action - */ - public function test_action_array_of_object_arg() { - $a = new MockAction(); - $hook_name = __FUNCTION__; - $val = new stdClass(); - $arg = array( $val ); - - add_action( $hook_name, array( $a, 'action3' ) ); - do_action( $hook_name, $arg ); - - $call_count = $a->get_call_count(); - $argsvar = $a->get_args(); - - // nested since func_get_args() returns the args as an array - $this->assertSame( array( $arg ), array_pop( $argsvar ) ); - } - /** * One tag with multiple actions. * @@ -231,6 +208,29 @@ public function test_action_args_3() { $this->assertSame( array( $val1, $val2 ), array_pop( $argsvar3 ) ); } + /** + * One tag with array with 1 object to ensure it will stay an array and not become an object + * + * @ticket 60190 + * + * @covers ::do_action + */ + public function test_action_array_of_object_arg() { + $a = new MockAction(); + $hook_name = __FUNCTION__; + $val = new stdClass(); + $arg = array( $val ); + + add_action( $hook_name, array( $a, 'action3' ) ); + do_action( $hook_name, $arg ); + + $call_count = $a->get_call_count(); + $argsvar = $a->get_args(); + + // nested since func_get_args() returns the args as an array + $this->assertSame( array( $arg ), array_pop( $argsvar ) ); + } + /** * @ticket 60193 * From 37ffc6a78fbfab4d4a5e54e82ad039eccbaacdce Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 23 May 2025 12:22:53 +0100 Subject: [PATCH 3/3] Docs. --- tests/phpunit/tests/actions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/actions.php b/tests/phpunit/tests/actions.php index d017b955651f9..e698ca0ef2cf9 100644 --- a/tests/phpunit/tests/actions.php +++ b/tests/phpunit/tests/actions.php @@ -209,8 +209,9 @@ public function test_action_args_3() { } /** - * One tag with array with 1 object to ensure it will stay an array and not become an object + * One hook which is passed an array containing one object. This test ensures it will stay an array and not become an object. * + * @ticket 48312 * @ticket 60190 * * @covers ::do_action