diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 10b81ceb6f23e..374d34ff44a47 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -329,8 +329,6 @@ public function do_item( $handle, $group = false ) { PHP_EOL ); $this->type_attr = $initial_type_attr; - - $this->has_load_later_inline = true; } } @@ -362,7 +360,10 @@ public function do_item( $handle, $group = false ) { */ $srce = apply_filters( 'script_loader_src', $src, $handle ); - if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations_stop_concat ) ) { + // Used as a conditional to prevent script concatenation. + $is_deferred_or_async_handle = in_array( $strategy, array( 'defer', 'async' ), true ); + + if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations_stop_concat || $is_deferred_or_async_handle ) ) { $this->do_concat = false; // Have to print the so-far concatenated scripts right away to maintain the right order. @@ -516,7 +517,7 @@ public function print_inline_script( $handle, $position = 'after', $display = tr $initial_type_attr = $this->type_attr; $this->type_attr = " type='text/template'"; printf( - '%5$s%4$s%5$s%5$s', + '%5$s%4$s%5$s%5$s', $this->type_attr, esc_attr( $handle ), esc_attr( $position ), diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index a3979e7bc1e6e..ffa37b20ab89d 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1856,8 +1856,8 @@ function wp_print_template_loader_script() { $output = << { - script.setAttribute("type","text/javascript"); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); eval(script.innerHTML); }) } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 090684c105e8b..6b267c9623d99 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -14,6 +14,9 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { protected $wp_scripts_print_translations_output; + // Stores a string reference to a default scripts directory name, utilised by certain tests. + protected $default_scripts_dir = '/directory/'; + public function set_up() { parent::set_up(); $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null; @@ -79,8 +82,8 @@ public function test_non_standalone_and_standalone_after_script_combined() { \n"; + $expected .= "\n"; + + $this->assertSame( $expected, $print_scripts ); + } + + /** + * Test script concatenation with `async` main script. + * + * @ticket 12009 + */ + public function test_concatenate_with_async_strategy() { + global $wp_scripts, $concatenate_scripts; + + $old_value = $concatenate_scripts; + $concatenate_scripts = true; + + $wp_scripts->do_concat = true; + $wp_scripts->default_dirs = array( $this->default_scripts_dir ); + + wp_enqueue_script( 'one-concat-dep-1', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'two-concat-dep-1', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'three-concat-dep-1', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'main-async-script-1', '/main-script.js', array(), null, array( 'strategy' => 'async' ) ); + + wp_print_scripts(); + $print_scripts = get_echo( '_print_scripts' ); + + // reset global before asserting. + $concatenate_scripts = $old_value; + + $ver = get_bloginfo( 'version' ); + $expected = "\n"; + $expected .= "\n"; + + $this->assertSame( $expected, $print_scripts ); + } + + /** + * Test script concatenation with blocking scripts before and after a `defer` script. + * + * @ticket 12009 + */ + public function test_concatenate_with_blocking_script_before_and_after_script_with_defer_strategy() { + global $wp_scripts, $concatenate_scripts; + + $old_value = $concatenate_scripts; + $concatenate_scripts = true; + + $wp_scripts->do_concat = true; + $wp_scripts->default_dirs = array( $this->default_scripts_dir ); + + wp_enqueue_script( 'one-concat-dep-2', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'two-concat-dep-2', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'three-concat-dep-2', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'deferred-script-2', '/main-script.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_enqueue_script( 'four-concat-dep-2', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'five-concat-dep-2', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'six-concat-dep-2', $this->default_scripts_dir . 'script.js' ); + + wp_print_scripts(); + $print_scripts = get_echo( '_print_scripts' ); + + // reset global before asserting. + $concatenate_scripts = $old_value; + + $ver = get_bloginfo( 'version' ); + $expected = "\n"; + $expected .= "\n"; + + $this->assertSame( $expected, $print_scripts ); + } + /** * @ticket 42804 */ @@ -694,11 +796,11 @@ public function test_script_concatenation() { global $wp_scripts; $wp_scripts->do_concat = true; - $wp_scripts->default_dirs = array( '/directory/' ); + $wp_scripts->default_dirs = array( $this->default_scripts_dir ); - wp_enqueue_script( 'one', '/directory/script.js' ); - wp_enqueue_script( 'two', '/directory/script.js' ); - wp_enqueue_script( 'three', '/directory/script.js' ); + wp_enqueue_script( 'one', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'two', $this->default_scripts_dir . 'script.js' ); + wp_enqueue_script( 'three', $this->default_scripts_dir . 'script.js' ); wp_print_scripts(); $print_scripts = get_echo( '_print_scripts' ); @@ -1091,21 +1193,21 @@ public function test_wp_add_inline_script_before_with_concat() { global $wp_scripts; $wp_scripts->do_concat = true; - $wp_scripts->default_dirs = array( '/directory/' ); + $wp_scripts->default_dirs = array( $this->default_scripts_dir ); - wp_enqueue_script( 'one', '/directory/one.js' ); - wp_enqueue_script( 'two', '/directory/two.js' ); - wp_enqueue_script( 'three', '/directory/three.js' ); + wp_enqueue_script( 'one', $this->default_scripts_dir . 'one.js' ); + wp_enqueue_script( 'two', $this->default_scripts_dir . 'two.js' ); + wp_enqueue_script( 'three', $this->default_scripts_dir . 'three.js' ); wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); wp_add_inline_script( 'two', 'console.log("before two");', 'before' ); $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertSame( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1117,19 +1219,19 @@ public function test_wp_add_inline_script_before_with_concat2() { global $wp_scripts; $wp_scripts->do_concat = true; - $wp_scripts->default_dirs = array( '/directory/' ); + $wp_scripts->default_dirs = array( $this->default_scripts_dir ); - wp_enqueue_script( 'one', '/directory/one.js' ); - wp_enqueue_script( 'two', '/directory/two.js' ); - wp_enqueue_script( 'three', '/directory/three.js' ); + wp_enqueue_script( 'one', $this->default_scripts_dir . 'one.js' ); + wp_enqueue_script( 'two', $this->default_scripts_dir . 'two.js' ); + wp_enqueue_script( 'three', $this->default_scripts_dir . 'three.js' ); wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertSame( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1141,23 +1243,23 @@ public function test_wp_add_inline_script_after_with_concat() { global $wp_scripts; $wp_scripts->do_concat = true; - $wp_scripts->default_dirs = array( '/directory/' ); + $wp_scripts->default_dirs = array( $this->default_scripts_dir ); - wp_enqueue_script( 'one', '/directory/one.js' ); - wp_enqueue_script( 'two', '/directory/two.js' ); - wp_enqueue_script( 'three', '/directory/three.js' ); - wp_enqueue_script( 'four', '/directory/four.js' ); + wp_enqueue_script( 'one', $this->default_scripts_dir . 'one.js' ); + wp_enqueue_script( 'two', $this->default_scripts_dir . 'two.js' ); + wp_enqueue_script( 'three', $this->default_scripts_dir . 'three.js' ); + wp_enqueue_script( 'four', $this->default_scripts_dir . 'four.js' ); wp_add_inline_script( 'two', 'console.log("after two");' ); wp_add_inline_script( 'three', 'console.log("after three");' ); $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $this->assertSame( $expected, get_echo( 'wp_print_scripts' ) ); }