Make WordPress Core

Ticket #52534: 52534.diff

File 52534.diff, 3.3 KB (added by SergeyBiryukov, 4 years ago)
  • src/wp-includes/class.wp-scripts.php

     
    484484                        unset( $l10n['l10n_print_after'] );
    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                        $l10n = html_entity_decode( (string) $l10n, ENT_QUOTES, 'UTF-8' );
     500                } else {
     501                        foreach ( $l10n as $key => $value ) {
     502                                if ( ! is_scalar( $value ) ) {
     503                                        continue;
     504                                }
     505
     506                                $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
    490507                        }
    491 
    492                         $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
    493508                }
    494509
    495510                $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
  • tests/phpunit/tests/dependencies/scripts.php

     
    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 $unsupported Optional. Whether the data format is unsupported. Default false.
     1441         */
     1442        function test_wp_localize_script_data_formats( $l10n_data, $expected, $unsupported = false ) {
     1443                if ( $unsupported ) {
     1444                        $this->setExpectedIncorrectUsage( 'WP_Scripts::localize' );
     1445                }
     1446
     1447                wp_enqueue_script( 'test-example', 'example.com', array(), null );
     1448                wp_localize_script( 'test-example', 'testExample', $l10n_data );
     1449
     1450                $expected  = "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {$expected};\n/* ]]> */\n</script>\n";
     1451                $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     1452
     1453                $this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
     1454
     1455        }
     1456
     1457        /**
     1458         * Data provider for test_wp_localize_script_data_formats().
     1459         *
     1460         * @return array[] {
     1461         *     Array of arguments for test.
     1462         *
     1463         *     @type mixed  $l10n_data   Localization data passed to wp_localize_script().
     1464         *     @type string $expected    Expected transformation of localization data.
     1465         *     @type string $unsupported Optional. Whether the data format is unsupported.
     1466         * }
     1467         */
     1468        function data_wp_localize_script_data_formats() {
     1469                return array(
     1470                        array( array( 'foo' => 'bar' ), '{"foo":"bar"}' ),
     1471                        array( 'string', '"string"', true ),
     1472                        array( 1, '"1"', true ),
     1473                        array( array( 'foo' => array( 'bar' => 'foobar' ) ), '{"foo":{"bar":"foobar"}}' ),
     1474                        array( array( 'foo' => 6.6 ), '{"foo":"6.6"}' ),
     1475                        array( array( 'foo' => 6 ), '{"foo":"6"}' ),
     1476                );
     1477        }
    14311478}