Make WordPress Core

Changeset 46568


Ignore:
Timestamp:
10/22/2019 12:09:39 AM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/plugin.php

    r46451 r46568  
    471471    if ( empty( $arg ) ) {
    472472        $arg[] = '';
     473    } elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) {
     474        // Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`.
     475        $arg[0] = $arg[0][0];
    473476    }
    474477
  • trunk/tests/phpunit/tests/actions.php

    r42343 r46568  
    152152        $argsvar3 = $a3->get_args();
    153153        $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar3 ) );
     154    }
     155
     156    /**
     157     * Tests PHP 4 notation for calling actions while passing in an object by reference.
     158     *
     159     * @ticket 48312
     160     */
     161    function test_action_args_with_php4_syntax() {
     162        $a   = new MockAction();
     163        $tag = __FUNCTION__;
     164        $val = new stdClass;
     165
     166        add_action( $tag, array( &$a, 'action' ) );
     167        // Ñall the action with PHP 4 notation for passing object by reference.
     168        do_action( $tag, array( &$val ) );
     169
     170        $call_count = $a->get_call_count();
     171        $argsvar    = $a->get_args();
     172        $this->assertSame( array( $val ), array_pop( $argsvar ) );
    154173    }
    155174
Note: See TracChangeset for help on using the changeset viewer.