Ticket #14853: 14853.4.diff
| File 14853.4.diff, 11.8 KB (added by , 11 years ago) |
|---|
-
src/wp-includes/class.wp-scripts.php
diff --git src/wp-includes/class.wp-scripts.php src/wp-includes/class.wp-scripts.php index 5d8fb17..2e87ea6 100644
class WP_Scripts extends WP_Dependencies { 25 25 public $concat_version = ''; 26 26 public $do_concat = false; 27 27 public $print_html = ''; 28 public $print_html_before = ''; 28 29 public $print_code = ''; 29 30 public $ext_handles = ''; 30 31 public $ext_version = ''; … … class WP_Scripts extends WP_Dependencies { 154 155 * @param string $handle Script handle. 155 156 */ 156 157 $srce = apply_filters( 'script_loader_src', $src, $handle ); 158 159 $before_handle = $this->print_inline_script( $handle, 'before', false ); 160 $after_handle = $this->print_inline_script( $handle, 'after', false ); 161 162 if ( $before_handle && ! $conditional ) { 163 $this->print_html_before .= sprintf( 164 "<script type='text/javascript'>\n%s\n</script>\n", 165 $before_handle 166 ); 167 } 168 169 if ( $after_handle && ! $conditional ) { 170 $this->print_html .= sprintf( 171 "<script type='text/javascript'>\n%s\n</script>\n", 172 $after_handle 173 ); 174 } 175 157 176 if ( $this->in_default_dir( $srce ) && ! $conditional ) { 158 177 $this->print_code .= $this->print_extra_script( $handle, false ); 159 178 $this->concat .= "$handle,"; … … class WP_Scripts extends WP_Dependencies { 195 214 if ( ! $src ) 196 215 return true; 197 216 198 $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}"; 217 $before_handle = $this->print_inline_script( $handle, 'before', false ); 218 if ( $before_handle ) { 219 $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle ); 220 } 221 $after_handle = $this->print_inline_script( $handle, 'after', false ); 222 if ( $after_handle ) { 223 $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle ); 224 } 225 226 $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}"; 199 227 200 228 /** 201 229 * Filter the HTML script tag of an enqueued script. … … class WP_Scripts extends WP_Dependencies { 218 246 } 219 247 220 248 /** 249 * Add extra code to a registered script. 250 * 251 * @since 4.5.0 252 * 253 * @param string $handle Name of the script to add the inline script to. Must be lowercase. 254 * @param string $data String containing the javascript to be added. 255 * @param string $position Optional. Whether to add the inline script before the handle 256 * or after. Default 'after'. 257 * 258 * @return bool True on success, false on failure. 259 */ 260 public function add_inline_script( $handle, $data, $position = 'after' ) { 261 if ( ! $data ) { 262 return false; 263 } 264 265 if ( 'after' !== $position ) { 266 $position = 'before'; 267 } 268 269 $script = (array) $this->get_data( $handle, $position ); 270 $script[] = $data; 271 272 return $this->add_data( $handle, $position, $script ); 273 } 274 275 /** 276 * Print inline scripts registered for a specific handle. 277 * 278 * @param string $handle Name of the script to add the inline script to. Must be lowercase. 279 * @param string $position Optional. Whether to add the inline script before the handle 280 * or after. Default 'after'. 281 * @param bool $echo Optional. Whether to echo the script instead of just returning it. 282 * Default true. 283 * @return string|bool Script on success, false otherwise. 284 */ 285 public function print_inline_script( $handle, $position = 'after', $echo = true ) { 286 $output = $this->get_data( $handle, $position ); 287 288 if ( empty( $output ) ) { 289 return false; 290 } 291 292 $output = trim( implode( "\n", $output ), "\n" ); 293 294 if ( $echo ) { 295 printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); 296 } 297 298 return $output; 299 } 300 301 /** 221 302 * Localizes a script, only if the script has already been added 222 303 * 223 304 * @param string $handle … … class WP_Scripts extends WP_Dependencies { 339 420 $this->concat = ''; 340 421 $this->concat_version = ''; 341 422 $this->print_html = ''; 423 $this->print_html_before = ''; 342 424 $this->ext_version = ''; 343 425 $this->ext_handles = ''; 344 426 } -
src/wp-includes/functions.wp-scripts.php
diff --git src/wp-includes/functions.wp-scripts.php src/wp-includes/functions.wp-scripts.php index c83a9d1..6741e07 100644
function wp_print_scripts( $handles = false ) { 86 86 } 87 87 88 88 /** 89 * Add extra code to a registered script. 90 * 91 * Code will only be added if the script in already in the queue. 92 * Accepts a string $data containing the Code. If two or more code blocks 93 * are added to the same script $handle, they will be printed in the order 94 * they were added, i.e. the latter added code can redeclare the previous. 95 * 96 * @since 4.5.0 97 * 98 * @see WP_Scripts::add_inline_script() 99 * 100 * @param string $handle Name of the script to add the inline script to. Must be lowercase. 101 * @param string $data String containing the javascript to be added. 102 * @param string $position Optional. Whether to add the inline script before the handle 103 * or after. Default 'after'. 104 * @return bool True on success, false on failure. 105 */ 106 function wp_add_inline_script( $handle, $data, $position = 'after' ) { 107 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 108 109 if ( false !== stripos( $data, '</script>' ) ) { 110 _doing_it_wrong( __FUNCTION__, __( 'Do not pass script tags to wp_add_inline_script().' ), '4.5.0' ); 111 $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) ); 112 } 113 114 return wp_scripts()->add_inline_script( $handle, $data, $position ); 115 } 116 117 118 /** 89 119 * Register a new script. 90 120 * 91 121 * Registers a script to be linked later using the wp_enqueue_script() function. -
src/wp-includes/script-loader.php
diff --git src/wp-includes/script-loader.php src/wp-includes/script-loader.php index 826e2ac..86c0661 100644
function _print_scripts() { 992 992 echo "</script>\n"; 993 993 } 994 994 995 if ( ! empty( $wp_scripts->print_html_before ) ) { 996 echo $wp_scripts->print_html_before; 997 } 998 995 999 $concat = str_split( $concat, 128 ); 996 1000 $concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat ); 997 1001 -
tests/phpunit/tests/dependencies/scripts.php
diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php index d7ba263..129d050 100644
class Tests_Dependencies_Scripts extends WP_UnitTestCase { 266 266 $this->assertEquals( $expected_header, $header ); 267 267 $this->assertEquals( $expected_footer, $footer ); 268 268 } 269 270 /** 271 * @ticket 14853 272 */ 273 function test_wp_add_inline_script_returns_bool() { 274 $this->assertFalse( wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ) ); 275 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 276 $this->assertTrue( wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ) ); 277 } 278 279 /** 280 * @ticket 14853 281 */ 282 function test_wp_add_inline_script_unknown_handle() { 283 $this->assertFalse( wp_add_inline_script( 'test-invalid', 'console.log("before");', 'before' ) ); 284 $this->assertEquals( '', get_echo( 'wp_print_scripts' ) ); 285 } 286 287 /** 288 * @ticket 14853 289 */ 290 function test_wp_add_inline_script_before() { 291 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 292 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 293 294 $expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 295 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 296 297 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 298 } 299 300 /** 301 * @ticket 14853 302 */ 303 function test_wp_add_inline_script_after() { 304 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 305 wp_add_inline_script( 'test-example', 'console.log("after");' ); 306 307 $expected = "<script type='text/javascript' src='http://example.com'></script>\n"; 308 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 309 310 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 311 } 312 313 /** 314 * @ticket 14853 315 */ 316 function test_wp_add_inline_script_before_and_after() { 317 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 318 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 319 wp_add_inline_script( 'test-example', 'console.log("after");' ); 320 321 $expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 322 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 323 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 324 325 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 326 } 327 328 /** 329 * @ticket 14853 330 */ 331 function test_wp_add_inline_script_multiple() { 332 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 333 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 334 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 335 wp_add_inline_script( 'test-example', 'console.log("after");' ); 336 wp_add_inline_script( 'test-example', 'console.log("after");' ); 337 338 $expected = "<script type='text/javascript'>\nconsole.log(\"before\");\nconsole.log(\"before\");\n</script>\n"; 339 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 340 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\nconsole.log(\"after\");\n</script>\n"; 341 342 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 343 } 344 345 /** 346 * @ticket 14853 347 */ 348 function test_wp_add_inline_script_localized_data_is_added_first() { 349 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 350 wp_localize_script( 'test-example', 'testExample', array( 'foo' => 'bar' ) ); 351 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 352 wp_add_inline_script( 'test-example', 'console.log("after");' ); 353 354 $expected = "<script type='text/javascript'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n"; 355 $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 356 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 357 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 358 359 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 360 } 361 362 /** 363 * @ticket 14853 364 */ 365 public function test_wp_add_inline_script_concat() { 366 global $wp_scripts; 367 368 $wp_scripts->do_concat = true; 369 $wp_scripts->default_dirs = array('/wp-admin/js/', '/wp-includes/js/'); // Default dirs as in wp-includes/script-loader.php 370 371 $expected_localized = "<!--[if gte IE 9]>\n"; 372 $expected_localized .= "<script type='text/javascript'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n"; 373 $expected_localized .= "<![endif]-->\n"; 374 375 $expected = "<!--[if gte IE 9]>\n"; 376 $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 377 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 378 $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 379 $expected .= "<![endif]-->\n"; 380 381 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 382 wp_localize_script( 'test-example', 'testExample', array( 'foo' => 'bar' ) ); 383 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 384 wp_add_inline_script( 'test-example', 'console.log("after");' ); 385 wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' ); 386 387 $this->assertEquals( $expected_localized, get_echo( 'wp_print_scripts' ) ); 388 $this->assertEquals( $expected, $wp_scripts->print_html ); 389 } 269 390 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)