Make WordPress Core

Ticket #52534: 52534-with-bc-break.patch

File 52534-with-bc-break.patch, 5.0 KB (added by jrf, 3 years ago)

Combined commits from PR 1022 - this patches the issue without BC breaks

  • src/wp-includes/class.wp-scripts.php

    From e0825296cfe825b2e5f33d0a9a71dfb6fec2ae21 Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Fri, 19 Feb 2021 10:04:19 +0100
    Subject: [PATCH] PHP 8 fixes for wp_localize_script()
    
    Part 1: WP_Scripts::localize: add tests with different data formats
    
    These tests will pass on PHP < 8.
    On PHP 8, only the `Only the first byte will be assigned to the string offset` warning for the `string` test should fail.
    
    Part 2: WP_Scripts::localize: fix PHP 8 issue and add _doing_it_wrong() for unsupported formats
    
    This removes the "Only the first byte will be assigned to the string offset" warning on PHP 8.
    
    This also adds a `_doing_it_wrong()` warning for all unsupported data formats.
    
    Includes minor adjustments to the test to expect the `_doing_it_wrong()` warning.
    
    Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com>
    Co-authored-by: Sergey Biryukov <sergeybiryukov@git.wordpress.org>
    ---
     src/wp-includes/class.wp-scripts.php         | 27 +++++++--
     tests/phpunit/tests/dependencies/scripts.php | 63 ++++++++++++++++++++
     2 files changed, 85 insertions(+), 5 deletions(-)
    
    diff --git a/src/wp-includes/class.wp-scripts.php b/src/wp-includes/class.wp-scripts.php
    index a629248df4..4d7bd9e1b2 100644
    a b class WP_Scripts extends WP_Dependencies { 
    484484                        unset( $l10n['l10n_print_after'] );
    485485                }
    486486
    487                 foreach ( (array) $l10n as $key => $value ) {
    488                         if ( ! is_scalar( $value ) ) {
    489                                 continue;
    490                         }
     487                if ( ! is_array( $l10n ) ) {
     488                        _doing_it_wrong(
     489                                __METHOD__,
     490                                sprintf(
     491                                        /* translators: 1: $l10n, 2: wp_add_inline_script() */
     492                                        __( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ),
     493                                        '<code>$l10n</code>',
     494                                        '<code>wp_add_inline_script()</code>'
     495                                ),
     496                                '5.7.0'
     497                        );
     498                }
     499
     500                if ( is_string( $l10n ) ) {
     501                        $l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' );
     502                } else {
     503                        foreach ( (array) $l10n as $key => $value ) {
     504                                if ( ! is_scalar( $value ) ) {
     505                                        continue;
     506                                }
    491507
    492                         $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
     508                                $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
     509                        }
    493510                }
    494511
    495512                $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
  • tests/phpunit/tests/dependencies/scripts.php

    diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php
    index cbe13a66df..838ac454d3 100644
    a b JS; 
    14281428                        $this->assertSame( $found, 0, "sourceMappingURL found in $js_file" );
    14291429                }
    14301430        }
     1431
     1432        /**
     1433         * @ticket 52534
     1434         * @covers ::wp_localize_script
     1435         *
     1436         * @dataProvider data_wp_localize_script_data_formats
     1437         *
     1438         * @param mixed  $l10n_data Localization data passed to wp_localize_script().
     1439         * @param string $expected  Expected transformation of localization data.
     1440         * @param string $warning   Optional. Whether a PHP native warning/error is expected. Default false.
     1441         */
     1442        public function test_wp_localize_script_data_formats( $l10n_data, $expected, $warning = false ) {
     1443                if ( $warning ) {
     1444                        if ( PHP_VERSION_ID < 80000 ) {
     1445                                $this->expectException( 'PHPUnit_Framework_Error_Warning' );
     1446                        } else {
     1447                                // As this exception will only be set on PHP 8 in combination with PHPUnit 7, this will work (for now).
     1448                                $this->expectException( 'Error' );
     1449                        }
     1450                }
     1451
     1452                if ( ! is_array( $l10n_data ) ) {
     1453                        $this->setExpectedIncorrectUsage( 'WP_Scripts::localize' );
     1454                }
     1455
     1456                wp_enqueue_script( 'test-example', 'example.com', array(), null );
     1457                wp_localize_script( 'test-example', 'testExample', $l10n_data );
     1458
     1459                $expected  = "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {$expected};\n/* ]]> */\n</script>\n";
     1460                $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     1461
     1462                $this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
     1463        }
     1464
     1465        /**
     1466         * Data provider for test_wp_localize_script_data_formats().
     1467         *
     1468         * @return array[] {
     1469         *     Array of arguments for test.
     1470         *
     1471         *     @type mixed  $l10n_data Localization data passed to wp_localize_script().
     1472         *     @type string $expected  Expected transformation of localization data.
     1473         *     @type string $warning   Optional. Whether a PHP native warning/error is expected.
     1474         * }
     1475         */
     1476        public function data_wp_localize_script_data_formats() {
     1477                return array(
     1478                        // Officially supported formats.
     1479                        array( array( 'array value, no key' ), '["array value, no key"]' ),
     1480                        array( array( 'foo' => 'bar' ), '{"foo":"bar"}' ),
     1481                        array( array( 'foo' => array( 'bar' => 'foobar' ) ), '{"foo":{"bar":"foobar"}}' ),
     1482                        array( array( 'foo' => 6.6 ), '{"foo":"6.6"}' ),
     1483                        array( array( 'foo' => 6 ), '{"foo":"6"}' ),
     1484
     1485                        // Unofficially supported format.
     1486                        array( 'string', '"string"' ),
     1487
     1488                        // Unsupported formats.
     1489                        array( 1.5, '1.5', true ),
     1490                        array( 1, '1', true ),
     1491                        array( false, '[""]' ),
     1492                );
     1493        }
    14311494}