Make WordPress Core

Changeset 50408


Ignore:
Timestamp:
02/22/2021 11:21:56 PM (5 years ago)
Author:
peterwilsoncc
Message:

Script Loader: Prevent wp_localize_script() warnings.

Prevent wp_localize_script() (via WP_Scripts::localize()) throwing warnings in PHP 8 when the translation data is passed as a string. This maintains backward compatibility with earlier versions of PHP.

Introduce a _doing_it_wrong() notice to WP_Scripts::localize() if the translation data is not passed as an array.

Props jrf, peterwilsoncc, SergeyBiryukov.
Fixes #52534.

Location:
trunk
Files:
2 edited

Legend:

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

    r49758 r50408  
    485485                }
    486486
    487                 foreach ( (array) $l10n as $key => $value ) {
    488                         if ( ! is_scalar( $value ) ) {
    489                                 continue;
     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                                }
     507
     508                                $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
    490509                        }
    491 
    492                         $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
    493510                }
    494511
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r50287 r50408  
    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}
Note: See TracChangeset for help on using the changeset viewer.