Ticket #29722: 29722.3.diff
File 29722.3.diff, 2.6 KB (added by , 10 years ago) |
---|
-
src/wp-includes/class.wp-scripts.php
208 208 * 209 209 * @param string $handle 210 210 * @param string $object_name 211 * @param array $l10n211 * @param array|scalar $l10n 212 212 * @return bool 213 213 */ 214 214 public function localize( $handle, $object_name, $l10n ) { 215 if ( $handle === 'jquery' ) 215 if ( $handle === 'jquery' ) { 216 216 $handle = 'jquery-core'; 217 } 217 218 218 219 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present 219 220 $after = $l10n['l10n_print_after']; … … 220 221 unset($l10n['l10n_print_after']); 221 222 } 222 223 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 } 226 231 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 } 228 234 } 229 235 230 236 $script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; -
tests/phpunit/tests/dependencies/scripts.php
156 156 $this->assertEquals( '', get_echo( 'wp_print_scripts' ) ); 157 157 } 158 158 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 159 180 /** 160 181 * Testing 'wp_register_script' return boolean success/failure value. 161 182 *