Skip to content
Permalink
Browse files

Script/Style Dependencies: Make sure that inline scripts for handles …

…without a source are printed.

See [36550] for `WP_Styles`.

Merges [43565] to the 5.0 branch.

Props bpayton.
Fixes #44551.



git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43788 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
pento committed Oct 22, 2018
1 parent 96a8987 commit 91f1b57473d5afb5a789ea86a1b4b62ba47abfc6
Showing with 56 additions and 1 deletion.
  1. +15 −1 src/wp-includes/class.wp-scripts.php
  2. +41 −0 tests/phpunit/tests/dependencies/scripts.php
@@ -266,6 +266,12 @@ public function do_item( $handle, $group = false ) {
$after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
}
if ( $before_handle || $after_handle ) {
$inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}";
} else {
$inline_script_tag = '';
}
if ( $this->do_concat ) {
/**
* Filters the script loader source.
@@ -307,7 +313,15 @@ public function do_item( $handle, $group = false ) {
}
// A single item may alias a set of items, by having dependencies, but no source.
if ( ! $obj->src ) {
if ( ! $src ) {
if ( $inline_script_tag ) {
if ( $this->do_concat ) {
$this->print_html .= $inline_script_tag;
} else {
echo $inline_script_tag;
}
}
return true;
}
@@ -400,6 +400,47 @@ function test_wp_add_inline_script_before_and_after() {
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
* @ticket 44551
*/
function test_wp_add_inline_script_before_for_handle_without_source() {
wp_register_script( 'test-example', '' );
wp_enqueue_script( 'test-example' );
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
$expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
* @ticket 44551
*/
function test_wp_add_inline_script_after_for_handle_without_source() {
wp_register_script( 'test-example', '' );
wp_enqueue_script( 'test-example' );
wp_add_inline_script( 'test-example', 'console.log("after");' );
$expected = "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
* @ticket 44551
*/
function test_wp_add_inline_script_before_and_after_for_handle_without_source() {
wp_register_script( 'test-example', '' );
wp_enqueue_script( 'test-example' );
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
wp_add_inline_script( 'test-example', 'console.log("after");' );
$expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
$expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
* @ticket 14853
*/

0 comments on commit 91f1b57

Please sign in to comment.
You can’t perform that action at this time.