From 99d5986b0ce8223de9be629815604480e9a96d27 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 22 Oct 2019 00:09:39 +0000 Subject: [PATCH] Plugins: Restore backward compatibility for PHP4-style passing of `array( &$this )` as action argument to `do_action()`. This is a follow-up to [46149] to avoid unnecessary breakage in case of using the old notation. Props david.binda, jrf. Reviewed by azaozz. Fixes #48312. git-svn-id: https://develop.svn.wordpress.org/trunk@46568 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/plugin.php | 3 +++ tests/phpunit/tests/actions.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php index 3725292dcc..6e9de6c3f4 100644 --- a/src/wp-includes/plugin.php +++ b/src/wp-includes/plugin.php @@ -470,6 +470,9 @@ function do_action( $tag, ...$arg ) { if ( empty( $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[ $tag ]->do_action( $arg ); diff --git a/tests/phpunit/tests/actions.php b/tests/phpunit/tests/actions.php index d23e6a63b2..e83b6bfc15 100644 --- a/tests/phpunit/tests/actions.php +++ b/tests/phpunit/tests/actions.php @@ -153,6 +153,25 @@ class Tests_Actions extends WP_UnitTestCase { $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar3 ) ); } + /** + * Tests PHP 4 notation for calling actions while passing in an object by reference. + * + * @ticket 48312 + */ + function test_action_args_with_php4_syntax() { + $a = new MockAction(); + $tag = __FUNCTION__; + $val = new stdClass; + + add_action( $tag, array( &$a, 'action' ) ); + // Ņall the action with PHP 4 notation for passing object by reference. + do_action( $tag, array( &$val ) ); + + $call_count = $a->get_call_count(); + $argsvar = $a->get_args(); + $this->assertSame( array( $val ), array_pop( $argsvar ) ); + } + function test_action_priority() { $a = new MockAction(); $tag = __FUNCTION__;