From 244f435e226864fb85ec599a8753e4e8eb7bab9c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 11 Aug 2018 17:41:29 +0000 Subject: [PATCH] Script/Style Dependencies: Make sure that inline scripts for handles without a source are printed. See [36550] for `WP_Styles`. Props bpayton. Fixes #44551. git-svn-id: https://develop.svn.wordpress.org/trunk@43565 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class.wp-scripts.php | 16 +++++++- tests/phpunit/tests/dependencies/scripts.php | 41 ++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class.wp-scripts.php b/src/wp-includes/class.wp-scripts.php index cf26b05f16..781de8c109 100644 --- a/src/wp-includes/class.wp-scripts.php +++ b/src/wp-includes/class.wp-scripts.php @@ -271,6 +271,12 @@ class WP_Scripts extends WP_Dependencies { $after_handle = sprintf( "\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. @@ -312,7 +318,15 @@ class WP_Scripts extends WP_Dependencies { } // 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; } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 20ade29c0e..dba0908248 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -405,6 +405,47 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $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 = "\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 = "\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 = "\n"; + $expected .= "\n"; + + $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + } + /** * @ticket 14853 */