WordPress.org

Make WordPress Core

Ticket #48312: 48312-fix-and-test.patch

File 48312-fix-and-test.patch, 1.8 KB (added by jrf, 4 months ago)

do_action(): Fix PHP-4 style passing of objects not longer broken out of array, includes updated unit test.

  • src/wp-includes/plugin.php

    From a9f37dce0c48b6735c11378307ed4bb47fd3f0e3 Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Tue, 15 Oct 2019 21:25:51 +0200
    Subject: [PATCH] `do_action()`: Fix PHP-4 style passing of objects not longer
     broken out of array
    
    ---
     src/wp-includes/plugin.php      |  3 +++
     tests/phpunit/tests/actions.php | 21 +++++++++++++++++++++
     2 files changed, 24 insertions(+)
    
    diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php
    index 3725292dcc..7a9d18b7a8 100644
    a b function do_action( $tag, ...$arg ) { 
    470470
    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
    475478        $wp_filter[ $tag ]->do_action( $arg );
  • tests/phpunit/tests/actions.php

    diff --git a/tests/phpunit/tests/actions.php b/tests/phpunit/tests/actions.php
    index d23e6a63b2..3fb0f1d223 100644
    a b class Tests_Actions extends WP_UnitTestCase { 
    557557                $p1->post_title = 'Bar1';
    558558                $p2->post_title = 'Bar2';
    559559        }
     560
     561        /**
     562         * Tests PHP 4 notation for calling actions while passing in an object by reference.
     563         *
     564         * @ticket 48312
     565         *
     566         * @group trac48312
     567         */
     568        function test_action_args_4() {
     569                $a   = new MockAction();
     570                $tag = __FUNCTION__;
     571                $val = new stdClass;
     572
     573                add_action( $tag, array( &$a, 'action' ) );
     574                // call the action with PHP 4 notation for passing object by reference
     575                do_action( $tag, array( &$val ) );
     576
     577                $call_count = $a->get_call_count();
     578                $argsvar    = $a->get_args();
     579                $this->assertSame( array( $val ), array_pop( $argsvar ) );
     580        }
    560581}