Make WordPress Core

Ticket #29722: 29722.3.diff

File 29722.3.diff, 2.6 KB (added by jtsternberg, 10 years ago)

allow scalars. With supporting unit test

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

     
    208208         *
    209209         * @param string $handle
    210210         * @param string $object_name
    211          * @param array $l10n
     211         * @param array|scalar $l10n
    212212         * @return bool
    213213         */
    214214        public function localize( $handle, $object_name, $l10n ) {
    215                 if ( $handle === 'jquery' )
     215                if ( $handle === 'jquery' ) {
    216216                        $handle = 'jquery-core';
     217                }
    217218
    218219                if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
    219220                        $after = $l10n['l10n_print_after'];
     
    220221                        unset($l10n['l10n_print_after']);
    221222                }
    222223
    223                 foreach ( (array) $l10n as $key => $value ) {
    224                         if ( !is_scalar($value) )
    225                                 continue;
     224                if ( is_scalar( $l10n ) ) {
     225                        $l10n = html_entity_decode( (string) $l10n, ENT_QUOTES, 'UTF-8');
     226                } else {
     227                        foreach ( (array) $l10n as $key => $value ) {
     228                                if ( ! is_scalar( $value ) ) {
     229                                        continue;
     230                                }
    226231
    227                         $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
     232                                $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
     233                        }
    228234                }
    229235
    230236                $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
  • tests/phpunit/tests/dependencies/scripts.php

     
    156156                $this->assertEquals( '', get_echo( 'wp_print_scripts' ) );
    157157        }
    158158
     159        /**
     160         * Testing `wp_localize_script` with scalar and array values.
     161         * @ticket 29722
     162         */
     163        function test_wp_localize_script_with_non_array() {
     164                // Enqueue & localize variables
     165                wp_enqueue_script( 'test-conditional-with-data', 'example.com', array(), null );
     166                wp_localize_script( 'test-conditional-with-data', 'data', true );
     167                wp_localize_script( 'test-conditional-with-data', 'dataArray', array( 'var' => 'string' ) );
     168                $expected = "<script type='text/javascript'>\n/* <![CDATA[ */\n";
     169                $expected.= "var data = \"1\";\n";
     170                $expected.= "var dataArray = {\"var\":\"string\"};\n";
     171                $expected.= "/* ]]> */\n";
     172                $expected.= "</script>\n<script type='text/javascript' src='http://example.com'></script>\n";
     173
     174                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     175
     176                // No scripts left to print
     177                $this->assertEquals( '', get_echo( 'wp_print_scripts' ) );
     178        }
     179
    159180    /**
    160181     * Testing 'wp_register_script' return boolean success/failure value.
    161182     *