From 04aabf5c634a50679187fad019966350ed1dbbca Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 20:35:14 +0000 Subject: [PATCH 01/34] numbered attributes --- src/wp-includes/class-wp-scripts.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 04ac0a4765a62..fc5794d915f93 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -499,7 +499,13 @@ public function print_inline_script( $handle, $position = 'after', $display = tr if ( $display ) { if ( 'after-non-standalone' === $position ) { - printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); + printf( + '\n%4$s\n\n', + $this->type_attr, + esc_attr( $handle ), + esc_attr( $position ), + $output + ); } else { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); } From e366d5d35976240309cb4a701a4be7cc5063aec2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 22:07:21 +0000 Subject: [PATCH 02/34] onload script --- src/wp-includes/class-wp-scripts.php | 32 ++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index fc5794d915f93..e5ab88b2f8217 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -245,6 +245,26 @@ public function print_extra_script( $handle, $display = true ) { return true; } + /** + * Prints a loader script if there is text/template registered script. + * + * @param bool $display Optional. Whether to print the extra script + * instead of just returning it. Default true. + * @return bool|string Print loader script if `$display` is true, string otherwise. + */ + public function print_template_loader_script( $display ) { + $output = << { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) +} +JS; + + $script = sprintf( "\n", $output ); + + return true; + } + /** * Processes a script dependency. * @@ -319,7 +339,12 @@ public function do_item( $handle, $group = false ) { $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { - $after_handle .= sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); + $after_handle .= sprintf( + '\n%s\n\n', + $this->type_attr, + esc_attr( $handle ), + $after_non_standalone_handle + ); } } @@ -410,10 +435,13 @@ public function do_item( $handle, $group = false ) { if ( '' !== $strategy ) { $strategy = ' ' . $strategy; + if ( ! empty( $after_non_standalone_handle ) ) { + $strategy .= ' onload=\'wpLoadAfterScripts(\"%3$s\")\''; + } } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( - "\n", + '\n', $this->type_attr, esc_url( $src ), esc_attr( $handle ), From 0fafd88b431a8e19914ef11d720b082aa82d7866 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 22:19:30 +0000 Subject: [PATCH 03/34] fix bug --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index e5ab88b2f8217..368f03bb7ab52 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -441,7 +441,7 @@ public function do_item( $handle, $group = false ) { } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( - '\n', + '\n', $this->type_attr, esc_url( $src ), esc_attr( $handle ), From 386022edd75a29d53d89a53b862a2a0c643ad1cf Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:02:18 +0000 Subject: [PATCH 04/34] sprintf issue with \n --- src/wp-includes/class-wp-scripts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 368f03bb7ab52..1fcc640476466 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -436,12 +436,12 @@ public function do_item( $handle, $group = false ) { if ( '' !== $strategy ) { $strategy = ' ' . $strategy; if ( ! empty( $after_non_standalone_handle ) ) { - $strategy .= ' onload=\'wpLoadAfterScripts(\"%3$s\")\''; + $strategy .= sprintf( " onload='wpLoadAfterScripts(\"%s\")'", esc_attr( $handle ) ); } } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( - '\n', + "\n", $this->type_attr, esc_url( $src ), esc_attr( $handle ), From fe7acc059934502bf2bc95fb0b6175cea0218a38 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:02:39 +0000 Subject: [PATCH 05/34] phpcs fixes --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 1fcc640476466..d037bb3940543 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -436,7 +436,7 @@ public function do_item( $handle, $group = false ) { if ( '' !== $strategy ) { $strategy = ' ' . $strategy; if ( ! empty( $after_non_standalone_handle ) ) { - $strategy .= sprintf( " onload='wpLoadAfterScripts(\"%s\")'", esc_attr( $handle ) ); + $strategy .= sprintf( " onload='wpLoadAfterScripts(\"%s\")'", esc_attr( $handle ) ); } } $tag = $translations . $cond_before . $before_handle; From 394405fdfc05376ff59333fe30d8e54117c5dfee Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:06:36 +0000 Subject: [PATCH 06/34] print template loader script --- src/wp-includes/class-wp-scripts.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index d037bb3940543..a0f0d7e22bb48 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -252,15 +252,19 @@ public function print_extra_script( $handle, $display = true ) { * instead of just returning it. Default true. * @return bool|string Print loader script if `$display` is true, string otherwise. */ - public function print_template_loader_script( $display ) { + public function print_template_loader_script( $display = true ) { $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) } JS; - $script = sprintf( "\n", $output ); + if ( $display ) { + echo $script; + } else { + return $script; + } return true; } From cc5dace90f5386bc1ce2cbe8fc4e69b86185258f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:16:35 +0000 Subject: [PATCH 07/34] \n to EOL --- src/wp-includes/class-wp-scripts.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index a0f0d7e22bb48..0f6cc13ea612d 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -344,10 +344,11 @@ public function do_item( $handle, $group = false ) { if ( $after_non_standalone_handle ) { $after_handle .= sprintf( - '\n%s\n\n', + '%4$s%3$s%4$s%4$s', $this->type_attr, esc_attr( $handle ), - $after_non_standalone_handle + $after_non_standalone_handle, + PHP_EOL ); } } @@ -532,11 +533,12 @@ public function print_inline_script( $handle, $position = 'after', $display = tr if ( $display ) { if ( 'after-non-standalone' === $position ) { printf( - '\n%4$s\n\n', + '%5$s%4$s%5$s%5$s', $this->type_attr, esc_attr( $handle ), esc_attr( $position ), - $output + $output, + PHP_EOL ); } else { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); From b558f345d4fc8f5f17879d266cc7fe28e8fd8d0a Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 13 Mar 2023 15:52:42 +0000 Subject: [PATCH 08/34] Loading the script before other script --- src/wp-includes/class-wp-scripts.php | 24 ------------------------ src/wp-includes/default-filters.php | 1 + src/wp-includes/script-loader.php | 26 ++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 0f6cc13ea612d..4d20b89037ca8 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -245,30 +245,6 @@ public function print_extra_script( $handle, $display = true ) { return true; } - /** - * Prints a loader script if there is text/template registered script. - * - * @param bool $display Optional. Whether to print the extra script - * instead of just returning it. Default true. - * @return bool|string Print loader script if `$display` is true, string otherwise. - */ - public function print_template_loader_script( $display = true ) { - $output = << { - let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); - scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) -} -JS; - $script = sprintf( "\n", $output ); - if ( $display ) { - echo $script; - } else { - return $script; - } - - return true; - } - /** * Processes a script dependency. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index f10dc6f280b2b..0ad5131e2af97 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -568,6 +568,7 @@ add_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_format_library_assets' ); add_action( 'enqueue_block_editor_assets', 'wp_enqueue_global_styles_css_custom_properties' ); add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' ); +add_action( 'wp_print_scripts', 'wp_print_template_loader_script' ); add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' ); add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 ); add_action( 'admin_head', 'wp_check_widget_editor_deps' ); diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index f584df22de73c..9ab37704e2281 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1843,6 +1843,32 @@ function wp_just_in_time_script_localization() { ); } + +/** + * Prints a loader script if there is text/template registered script. + * + * @param bool $display Optional. Whether to print the extra script + * instead of just returning it. Default true. + * @return bool|string Print loader script if `$display` is true, string otherwise. + */ +function wp_print_template_loader_script( $display = true ) { + $output = << { +let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); +scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) +} +JS; + $script = sprintf( "\n", $output ); + if ( $display ) { + echo $script; + } else { + return $script; + } + + return true; +} + + /** * Localizes the jQuery UI datepicker. * From f04a24644038a0eaa1c99345a4aa388face1e1f4 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 14 Mar 2023 15:51:28 +0000 Subject: [PATCH 09/34] temp commit --- src/wp-includes/class-wp-scripts.php | 8 +++++++ src/wp-includes/script-loader.php | 33 ++++++++++++---------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 4d20b89037ca8..a8247fc95361e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -121,6 +121,13 @@ class WP_Scripts extends WP_Dependencies { * @var array */ public $default_dirs; + + /** + * True if there is any inline script that is meant to load later. + * + * @var bool + */ + public $has_load_later_inline = false; /** * Holds a string which contains the type attribute for script tag. @@ -326,6 +333,7 @@ public function do_item( $handle, $group = false ) { $after_non_standalone_handle, PHP_EOL ); + $this->has_load_later_inline = true; } } diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 9ab37704e2281..2c0fa0f82c65f 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1846,27 +1846,22 @@ function wp_just_in_time_script_localization() { /** * Prints a loader script if there is text/template registered script. - * - * @param bool $display Optional. Whether to print the extra script - * instead of just returning it. Default true. - * @return bool|string Print loader script if `$display` is true, string otherwise. */ -function wp_print_template_loader_script( $display = true ) { - $output = << { -let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); -scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) -} -JS; - $script = sprintf( "\n", $output ); - if ( $display ) { - echo $script; - } else { - return $script; +function wp_print_template_loader_script() { + $wp_scripts = wp_scripts(); + if( $wp_scripts->has_load_later_inline ) { + $output = << { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) + } + JS; + printf( "\n", $output ); + } } - - return true; -} /** From b5f4df492b19edd304fb881e4c2902bc061badc0 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 15 Mar 2023 01:41:57 +0000 Subject: [PATCH 10/34] check if any delayed inline scripts --- src/wp-includes/class-wp-scripts.php | 26 ++++++++++++++++++-------- src/wp-includes/script-loader.php | 5 ++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index a8247fc95361e..b1622e12bc204 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -121,13 +121,6 @@ class WP_Scripts extends WP_Dependencies { * @var array */ public $default_dirs; - - /** - * True if there is any inline script that is meant to load later. - * - * @var bool - */ - public $has_load_later_inline = false; /** * Holds a string which contains the type attribute for script tag. @@ -326,7 +319,7 @@ public function do_item( $handle, $group = false ) { $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { - $after_handle .= sprintf( + $after_handle .= sprintf( '%4$s%3$s%4$s%4$s', $this->type_attr, esc_attr( $handle ), @@ -795,6 +788,23 @@ public function add_data( $handle, $key, $value ) { return parent::add_data( $handle, $key, $value ); } + /** + * Check all handles for any delayed inline scripts. + * + * @return bool True if script present. False if empty. + */ + public function maybe_has_delayed_inline_script() { + foreach ( $this->registered as $handle => $script ) { + if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) ) { + // non standalone after scripts of async or defer are usually delayed. + if ( $this->has_non_standalone_inline_script( $handle, 'after' ) ) { + return true; + } + } + } + return false; + } + /** * Normalize the data inside the $args parameter and support backward compatibility. * diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 2c0fa0f82c65f..177ce9239500e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1849,7 +1849,7 @@ function wp_just_in_time_script_localization() { */ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); - if( $wp_scripts->has_load_later_inline ) { + if ( $wp_scripts->maybe_has_delayed_inline_script() ) { $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); @@ -1861,8 +1861,7 @@ function wp_print_template_loader_script() { JS; printf( "\n", $output ); } - } - +} /** * Localizes the jQuery UI datepicker. From d110556951c28d4984cd92575a4cafab31e8aef2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 01:15:31 +0000 Subject: [PATCH 11/34] add new line --- src/wp-includes/script-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 177ce9239500e..98d3f75644b1f 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1859,7 +1859,7 @@ function wp_print_template_loader_script() { }) } JS; - printf( "\n", $output ); + printf( "\n", $output ); } } From b6b7022c07324848823081f3b4ab2cd900847976 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:21:46 +0000 Subject: [PATCH 12/34] backward compatibility --- src/wp-includes/script-loader.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 98d3f75644b1f..bf0b28c61c7e9 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1851,14 +1851,14 @@ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->maybe_has_delayed_inline_script() ) { $output = << { - let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); - scripts.forEach( (script) => { - script.setAttribute("type","text/javascript"); - eval(script.innerHTML); - }) - } - JS; +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} +JS; printf( "\n", $output ); } } From 9e8c37876b5027be93583831569424401aa9658e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:46:13 +0000 Subject: [PATCH 13/34] update test utility and scripts. --- tests/phpunit/includes/utils.php | 8 ++ tests/phpunit/tests/dependencies/scripts.php | 101 ++++++++++++++----- 2 files changed, 83 insertions(+), 26 deletions(-) diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index 30af6aa348490..7a4042b2cee7d 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -645,3 +645,11 @@ function test_rest_expand_compact_links( $links ) { } return $links; } + +/** + * Removes all handles from $wp_script. + */ +function unregister_all_script_handles() { + global $wp_scripts; + $wp_scripts->registered = array(); +} diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index a9b7f6a18a781..985e2438cc70c 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -71,17 +71,30 @@ public function test_wp_enqueue_script() { */ public function test_non_standalone_and_standalone_after_script_combined() { // If the main script with defer strategy has a `after` inline script; expected one with type='javascript' and other with type='module'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-isinsa-1', 'http://example.org/ms-isinsa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after one");', 'after', true ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after two");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; + $output = get_echo( 'wp_print_scripts' ); + $expected = << +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} + + + + + +EXP; $this->assertSame( $expected, $output ); } @@ -99,6 +112,7 @@ public function data_standalone_after_inline_script() { $data = array(); // If the main script with defer strategy has a `after` inline script; the inline script is not affected. + unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-1', 'http://example.org/ms-isa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-1', 'console.log("after one");', 'after', true ); $output = get_echo( 'wp_print_scripts' ); @@ -109,6 +123,7 @@ public function data_standalone_after_inline_script() { array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); // If the main script with async strategy has a `after` inline script; the inline script is not affected. + unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); $output = get_echo( 'wp_print_scripts' ); @@ -135,26 +150,53 @@ public function data_non_standalone_after_inline_script() { $data = array(); // If the main script with defer strategy has a `after` inline script; the inline script is inserted as type='module'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-1', 'http://example.org/ms-insa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-insa-1', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = << +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} + + + + +EXP; + array_push( $data, array( $expected, $output, 'Main Deferred; expected type="text/template" for inline script.' ) ); // If the main script with async strategy has a `after` inline script; the inline script is inserted as type='module'. - wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'defer' ) ); + unregister_all_script_handles(); + wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'async' ) ); wp_add_inline_script( 'ms-insa-2', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = << +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} + + + + +EXP; + array_push( $data, array( $expected, $output, 'Main Async; expected type="text/template" for inline script.' ) ); // If the main script with blocking strategy has a `after` inline script; the inline script is inserted as type='javascript'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); @@ -162,9 +204,10 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'Main blocking; expected no type attribute for inline script.' ) ); // If the main script with no strategy has a `after` inline script; the inline script is inserted as type='javascript'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); @@ -172,7 +215,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'No strategy; expected no type attribute for inline script.' ) ); return $data; } @@ -191,6 +234,7 @@ public function data_non_standalone_before_inline_script_with_defer() { $data = array(); // If the main script has a `before` inline script; all dependencies will be blocking. + unregister_all_script_handles(); wp_enqueue_script( 'ds-i1-1', 'http://example.org/ds-i1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-2', 'http://example.org/ds-i1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-3', 'http://example.org/ds-i1-3.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -207,6 +251,7 @@ public function data_non_standalone_before_inline_script_with_defer() { array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); // One of the dependency in the chain has a `before` inline script; all script above it will be blocking. + unregister_all_script_handles(); wp_enqueue_script( 'ds-i2-1', 'http://example.org/ds-i2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-2', 'http://example.org/ds-i2-2.js', array( 'ds-i2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-3', 'http://example.org/ds-i2-3.js', array( 'ds-i2-2' ), null, array( 'strategy' => 'defer' ) ); @@ -223,6 +268,7 @@ public function data_non_standalone_before_inline_script_with_defer() { array_push( $data, array( $expected, $output, 'Scripts in the chain before the script having before must be blocking.' ) ); // Top most dependency in the chain has a `before` inline script; none of the script bellow it will be blocking. + unregister_all_script_handles(); wp_enqueue_script( 'ds-i3-1', 'http://example.org/ds-i3-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i3-2', 'http://example.org/ds-i3-2.js', array( 'ds-i3-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i3-1', 'http://example.org/ms-i3-1.js', array( 'ds-i3-2' ), null, array( 'strategy' => 'defer' ) ); @@ -237,6 +283,7 @@ public function data_non_standalone_before_inline_script_with_defer() { array_push( $data, array( $expected, $output, 'Top most has before inline script. All the script in the chain defer.' ) ); // If there are two dependencies chain; rules are applied to the scripts in the chain having a `before` inline script. + unregister_all_script_handles(); wp_enqueue_script( 'ch1-ds-i4-1', 'http://example.org/ch1-ds-i4-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ch1-ds-i4-2', 'http://example.org/ch1-ds-i4-2.js', array( 'ch1-ds-i4-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ch2-ds-i4-1', 'http://example.org/ch2-ds-i4-1.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -261,16 +308,17 @@ public function data_non_standalone_before_inline_script_with_defer() { * Test standalone tests. * * @ticket 12009 - * @dataProvider data_standalone_inline_script + * @dataProvider data_standalone_before_inline_script */ - public function test_standalone_inline_script( $expected, $output, $message ) { + public function test_standalone_before_inline_script( $expected, $output, $message ) { $this->assertSame( $expected, $output, $message ); } - public function data_standalone_inline_script() { + public function data_standalone_before_inline_script() { $data = array(); // If the main script has a `before` inline script; standalone doesn't effect any script. + unregister_all_script_handles(); wp_enqueue_script( 'ds-is1-1', 'http://example.org/ds-is1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-2', 'http://example.org/ds-is1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-3', 'http://example.org/ds-is1-3.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -287,6 +335,7 @@ public function data_standalone_inline_script() { array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); // One of the dependency in the chain has a `before` inline script; standalone doesn't effect any script. + unregister_all_script_handles(); wp_enqueue_script( 'ds-is2-1', 'http://example.org/ds-is2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-2', 'http://example.org/ds-is2-2.js', array( 'ds-is2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-3', 'http://example.org/ds-is2-3.js', array( 'ds-is2-2' ), null, array( 'strategy' => 'defer' ) ); From ffc40c11859bff3079f587c3238bcff0d6fd82c1 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:48:14 +0000 Subject: [PATCH 14/34] php cs fixes --- src/wp-includes/script-loader.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index bf0b28c61c7e9..d2ae81708ee79 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1108,9 +1108,9 @@ function wp_default_scripts( $scripts ) { $scripts->add( 'mediaelement-vimeo', '/wp-includes/js/mediaelement/renderers/vimeo.min.js', array( 'mediaelement' ), '4.2.17', 1 ); $scripts->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.js", array( 'mediaelement' ), false, 1 ); $mejs_settings = array( - 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), - 'classPrefix' => 'mejs-', - 'stretching' => 'responsive', + 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), + 'classPrefix' => 'mejs-', + 'stretching' => 'responsive', /** This filter is documented in wp-includes/media.php */ 'audioShortcodeLibrary' => apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ), /** This filter is documented in wp-includes/media.php */ From 8af918c707d2003e25ec2a9a570b59331b10ae3b Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:49:31 +0000 Subject: [PATCH 15/34] fix csfix issue --- src/wp-includes/script-loader.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index d2ae81708ee79..bf0b28c61c7e9 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1108,9 +1108,9 @@ function wp_default_scripts( $scripts ) { $scripts->add( 'mediaelement-vimeo', '/wp-includes/js/mediaelement/renderers/vimeo.min.js', array( 'mediaelement' ), '4.2.17', 1 ); $scripts->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.js", array( 'mediaelement' ), false, 1 ); $mejs_settings = array( - 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), - 'classPrefix' => 'mejs-', - 'stretching' => 'responsive', + 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), + 'classPrefix' => 'mejs-', + 'stretching' => 'responsive', /** This filter is documented in wp-includes/media.php */ 'audioShortcodeLibrary' => apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ), /** This filter is documented in wp-includes/media.php */ From d952da58a2a290b5b814369ecd7f6712903efaae Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 21 Mar 2023 11:18:09 +0000 Subject: [PATCH 16/34] Update test message. Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 985e2438cc70c..cd898dba8b207 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -204,7 +204,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Main blocking; expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'The main script is blocking. Only the text/javascript type is expected for the inline script.' ) ); // If the main script with no strategy has a `after` inline script; the inline script is inserted as type='javascript'. unregister_all_script_handles(); From b71b5570e3f85e086e1f49b2a200c9ab72170da3 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 21 Mar 2023 11:20:40 +0000 Subject: [PATCH 17/34] Update test comment Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index cd898dba8b207..1570988f6bd6a 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -70,7 +70,7 @@ public function test_wp_enqueue_script() { * @ticket 12009 */ public function test_non_standalone_and_standalone_after_script_combined() { - // If the main script with defer strategy has a `after` inline script; expected one with type='javascript' and other with type='module'. + // If a main script with a `defer` strategy has an `after` inline script, we expect one instance with type='javascript' and another with type='text/template'. unregister_all_script_handles(); wp_enqueue_script( 'ms-isinsa-1', 'http://example.org/ms-isinsa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after one");', 'after', true ); From 479d6567d07033b1c0d1a9de6335d0771e39f3e8 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 21 Mar 2023 11:21:10 +0000 Subject: [PATCH 18/34] Update test error message Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 1570988f6bd6a..321fc659b208f 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -215,7 +215,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'No strategy; expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'No script strategy is present. Only the text/javascript type is expected for the inline script.' ) ); return $data; } From 968d5b7f36f3d5d999bf91a2411f4abd88e30a20 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 21 Mar 2023 12:41:38 +0000 Subject: [PATCH 19/34] change function names --- src/wp-includes/class-wp-scripts.php | 2 +- src/wp-includes/script-loader.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 521a5187859a6..4934de1be368a 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -793,7 +793,7 @@ public function add_data( $handle, $key, $value ) { * * @return bool True if script present. False if empty. */ - public function maybe_has_delayed_inline_script() { + public function has_delayed_inline_script() { foreach ( $this->registered as $handle => $script ) { if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) ) { // non standalone after scripts of async or defer are usually delayed. diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index bf0b28c61c7e9..11b57280c5f58 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1849,7 +1849,7 @@ function wp_just_in_time_script_localization() { */ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); - if ( $wp_scripts->maybe_has_delayed_inline_script() ) { + if ( $wp_scripts->has_delayed_inline_script() ) { $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); From 326555db59abe330d2d33431667968377812e01c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 22 Mar 2023 10:16:50 +0000 Subject: [PATCH 20/34] use unregister column --- tests/phpunit/includes/utils.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index 7a4042b2cee7d..f4d7dafadd502 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -651,5 +651,7 @@ function test_rest_expand_compact_links( $links ) { */ function unregister_all_script_handles() { global $wp_scripts; - $wp_scripts->registered = array(); + foreach ( $wp_scripts->registered as $handle_name => $handle ) { + wp_deregister_script( $handle_name ); + } } From 1321d6f28fdeafe9fbfd6105523a57d48155587e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Mar 2023 09:01:15 +0000 Subject: [PATCH 21/34] avoid removal of jquery libraries --- tests/phpunit/includes/utils.php | 36 +++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index f4d7dafadd502..371b7c3d09fcc 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -651,7 +651,41 @@ function test_rest_expand_compact_links( $links ) { */ function unregister_all_script_handles() { global $wp_scripts; + + /** + * Do not deregister following library through this function. + */ + $libraries = array( + 'jquery', + 'jquery-core', + 'jquery-migrate', + 'jquery-ui-core', + 'jquery-ui-accordion', + 'jquery-ui-autocomplete', + 'jquery-ui-button', + 'jquery-ui-datepicker', + 'jquery-ui-dialog', + 'jquery-ui-draggable', + 'jquery-ui-droppable', + 'jquery-ui-menu', + 'jquery-ui-mouse', + 'jquery-ui-position', + 'jquery-ui-progressbar', + 'jquery-ui-resizable', + 'jquery-ui-selectable', + 'jquery-ui-slider', + 'jquery-ui-sortable', + 'jquery-ui-spinner', + 'jquery-ui-tabs', + 'jquery-ui-tooltip', + 'jquery-ui-widget', + 'backbone', + 'underscore', + ); + foreach ( $wp_scripts->registered as $handle_name => $handle ) { - wp_deregister_script( $handle_name ); + if ( ! in_array( $handle_name, $libraries, true ) ) { + wp_deregister_script( $handle_name ); + } } } From 66ab60c7ac4ba13181a0bb4eb1e4b3ca0c417eb2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Mar 2023 09:08:07 +0000 Subject: [PATCH 22/34] Dataprovider doc block. --- tests/phpunit/tests/dependencies/scripts.php | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 289b131e49a0d..ddc3f10fdc4f9 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -107,6 +107,11 @@ public function test_standalone_after_inline_script( $expected, $output, $messag $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_standalone_after_inline_script() { $data = array(); @@ -145,6 +150,11 @@ public function test_non_standalone_after_inline_script( $expected, $output, $me $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_non_standalone_after_inline_script() { $data = array(); @@ -229,6 +239,11 @@ public function test_non_standalone_before_inline_script_with_defer( $expected, $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_non_standalone_before_inline_script_with_defer() { $data = array(); @@ -313,6 +328,11 @@ public function test_standalone_before_inline_script( $expected, $output, $messa $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_standalone_before_inline_script() { $data = array(); @@ -397,6 +417,11 @@ public function test_loading_strategy_with_valid_defer_registration( $expected, $this->assertStringContainsString( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_loading_strategy_with_valid_defer_registration() { $data = array(); From bbdad377686903cf3242102702abcfc8edfd2e8b Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Mar 2023 09:15:22 +0000 Subject: [PATCH 23/34] doc block change --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index ddc3f10fdc4f9..b2f221a616bc4 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -319,7 +319,7 @@ public function data_non_standalone_before_inline_script_with_defer() { } /** - * Test standalone tests. + * Test standalone `before` scripts. * * @ticket 12009 * @dataProvider data_standalone_before_inline_script From 966be0c59f0f43b36281e3b8d877bc667ff05bd2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:24:40 +0100 Subject: [PATCH 24/34] Update src/wp-includes/class-wp-scripts.php Co-authored-by: Mukesh Panchal --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 70dc116fcea57..017721cb6bf6b 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -789,7 +789,7 @@ public function add_data( $handle, $key, $value ) { } /** - * Check all handles for any delayed inline scripts. + * Checks all handles for any delayed inline scripts. * * @return bool True if script present. False if empty. */ From dcbf0af2f6f754ef95061dff8d4cdfbf30fac3ad Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Fri, 31 Mar 2023 08:42:48 +0100 Subject: [PATCH 25/34] Update doc text Co-authored-by: Mukesh Panchal --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 017721cb6bf6b..0972dca46963e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -791,7 +791,7 @@ public function add_data( $handle, $key, $value ) { /** * Checks all handles for any delayed inline scripts. * - * @return bool True if script present. False if empty. + * @return bool True if the inline script present, otherwise false. */ public function has_delayed_inline_script() { foreach ( $this->registered as $handle => $script ) { From b3f722575d9b7591cefefc541e8d993e75aa5ccf Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 11:09:22 +0100 Subject: [PATCH 26/34] fix type_attr no there issue --- src/wp-includes/script-loader.php | 5 +++-- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 11b57280c5f58..2501dab2cd32e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1850,7 +1850,7 @@ function wp_just_in_time_script_localization() { function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->has_delayed_inline_script() ) { - $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { @@ -1859,7 +1859,8 @@ function wp_print_template_loader_script() { }) } JS; - printf( "\n", $output ); + $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : " type='text/javascript'"; + printf( "\n%s\n\n", $type_attr, $output ); } } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 96fba823c56dc..0fa30a54ca628 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -76,7 +76,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { wp_add_inline_script( 'ms-isinsa-1', 'console.log("after two");', 'after' ); $output = get_echo( 'wp_print_scripts' ); $expected = << +\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If the main script with async strategy has a `after` inline script; the inline script is not affected. + /** + * Test `standalone` inline scripts in the `after` position. + * + * If the main script with async strategy has a `after` inline script, + * the inline script is not affected. + * + * @ticket 12009 + */ + public function test_standalone_async_main_script_with_after_inline_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); @@ -135,9 +134,7 @@ public function data_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); - - return $data; + $this->assertSame( $expected, $output ); } /** From 556ae91627f489d7fefcfcdab4c92b0b9e0086eb Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 12:06:42 +0100 Subject: [PATCH 29/34] non-standalone after script test broken --- tests/phpunit/tests/dependencies/scripts.php | 83 ++++++++++++-------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 972399ed1da9b..5a7b7904c24aa 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -98,14 +98,14 @@ public function test_non_standalone_and_standalone_after_script_combined() { } /** - * Test `standalone` inline scripts in the `after` position. + * Test `standalone` inline scripts in the `after` position with deferred main script. * - * If the main script with a `defer` loading strategy has an `after` inline script, + * If the main script with a `defer` loading strategy has an `after` inline script, * the inline script should not be affected. - * + * * @ticket 12009 */ - public function test_standalone_defer_main_script_with_after_inline_script() { + public function test_standalone_after_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-1', 'http://example.org/ms-isa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-1', 'console.log("after one");', 'after', true ); @@ -118,14 +118,14 @@ public function test_standalone_defer_main_script_with_after_inline_script() { } /** - * Test `standalone` inline scripts in the `after` position. + * Test `standalone` inline scripts in the `after` position with async main script. + * + * If the main script with async strategy has a `after` inline script, + * the inline script should not be affected. * - * If the main script with async strategy has a `after` inline script, - * the inline script is not affected. - * * @ticket 12009 */ - public function test_standalone_async_main_script_with_after_inline_script() { + public function test_standalone_after_inline_script_with_async_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); @@ -138,24 +138,15 @@ public function test_standalone_async_main_script_with_after_inline_script() { } /** - * Test non standalone inline scripts in the `after` position. + * Test non standalone inline scripts in the `after` position with deferred main script. * - * @ticket 12009 - * @dataProvider data_non_standalone_after_inline_script - */ - public function test_non_standalone_after_inline_script( $expected, $output, $message ) { - $this->assertSame( $expected, $output, $message ); - } - - /** - * Data provider. + * If a main script with a `defer` loading strategy has an `after` inline script, + * the inline script should be rendered as type='text/template'. + * The common loader script should also be injected in this case. * - * @return array + * @ticket 12009 */ - public function data_non_standalone_after_inline_script() { - $data = array(); - - // If a main script with a `defer` loading strategy has an `after` inline script, the inline script should be rendered as type='module'. + public function test_non_standalone_after_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-1', 'http://example.org/ms-insa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-insa-1', 'console.log("after one");', 'after' ); @@ -176,9 +167,19 @@ public function data_non_standalone_after_inline_script() { EXP; - array_push( $data, array( $expected, $output, 'Main Deferred; expected type="text/template" for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If a main script with an `async` loading strategy has an `after` inline script, the inline script should be rendered as type='module'. + /** + * Test non standalone inline scripts in the `after` position with async main script. + * + * If a main script with an `async` loading strategy has an `after` inline script, + * the inline script should be rendered as type='text/template'. + * The common loader script should also be injected in this case. + * + * @ticket 12009 + */ + public function test_non_standalone_after_inline_script_with_async_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'async' ) ); wp_add_inline_script( 'ms-insa-2', 'console.log("after one");', 'after' ); @@ -199,9 +200,18 @@ public function data_non_standalone_after_inline_script() { EXP; - array_push( $data, array( $expected, $output, 'Main Async; expected type="text/template" for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If a main script with a `blocking` strategy has an `after` inline script, the inline script should be rendered as type='javascript'. + /** + * Test non standalone inline scripts in the `after` position with blocking main script. + * + * If a main script with a `blocking` strategy has an `after` inline script, + * the inline script should be rendered as type='text/javascript'. + * + * @ticket 12009 + */ + public function test_non_standalone_after_inline_script_with_blocking_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); @@ -210,9 +220,18 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If a main script with no loading strategy has an `after` inline script, the inline script should be rendered as type='javascript'. + /** + * Test non standalone inline scripts in the `after` position with deferred main script. + * + * If a main script with no loading strategy has an `after` inline script, + * the inline script should be rendered as type='text/javascript'. + * + * @ticket 12009 + */ + public function test_non_standalone_after_inline_script_with_main_script_with_no_strategy() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); @@ -221,9 +240,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); - - return $data; + $this->assertSame( $expected, $output ); } /** From d4be88d31ed35d82f87dfd9115f327ba07c82fae Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 12:44:02 +0100 Subject: [PATCH 30/34] before standalone test broken --- tests/phpunit/tests/dependencies/scripts.php | 43 ++++++++++---------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 5a7b7904c24aa..9568daad7314c 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -333,31 +333,22 @@ public function data_non_standalone_before_inline_script_with_defer() { } /** - * Test standalone `before` scripts. + * Test `standalone` inline scripts in the `before` position with deferred main script. * - * @ticket 12009 - * @dataProvider data_standalone_before_inline_script - */ - public function test_standalone_before_inline_script( $expected, $output, $message ) { - $this->assertSame( $expected, $output, $message ); - } - - /** - * Data provider. + * If the main script has a `before` inline script, `standalone` doesn't apply to + * any inline script associated with the main script. * - * @return array + * @ticket 12009 */ - public function data_standalone_before_inline_script() { - $data = array(); - - // If the main script has a `before` inline script, `standalone` doesn't apply to any inline script associated with the main script. + public function test_standalone_before_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-is1-1', 'http://example.org/ds-is1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-2', 'http://example.org/ds-is1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-3', 'http://example.org/ds-is1-3.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-is1-1', 'http://example.org/ms-is1-1.js', array( 'ds-is1-1', 'ds-is1-2', 'ds-is1-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-is1-1', 'console.log("before one");', 'before', true ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -365,16 +356,27 @@ public function data_standalone_before_inline_script() { $expected .= "console.log(\"before one\");\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); - // If one of the dependencies in the chain has a `before` inline script associated with it, `standalone` doesn't apply to any inline script(s) associated with the main script. + $this->assertSame( $expected, $output ); + } + + /** + * Test `standalone` inline scripts in the `before` position with defer main script. + * + * If one of the deferred dependencies in the chain has a `before` inline `standalone` script associated with it, + * strategy of the dependencies above it remains unchanged. + * + * @ticket 12009 + */ + public function test_standalone_before_inline_script_with_defer_dependency_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-is2-1', 'http://example.org/ds-is2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-2', 'http://example.org/ds-is2-2.js', array( 'ds-is2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-3', 'http://example.org/ds-is2-3.js', array( 'ds-is2-2' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-is2-1', 'http://example.org/ms-is2-1.js', array( 'ds-is2-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-is2-2', 'console.log("before one");', 'before', true ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Scripts in the chain before the script having before must be blocking.' ) ); - return $data; + $this->assertSame( $expected, $output ); } /** From 180a6ae87c1a27183d8f50de81e3e09db3352f50 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 13:16:10 +0100 Subject: [PATCH 31/34] non standalone with before and defer --- tests/phpunit/tests/dependencies/scripts.php | 80 +++++++++++++------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 9568daad7314c..44e0df80c5cd9 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -215,11 +215,13 @@ public function test_non_standalone_after_inline_script_with_blocking_main_scrip unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; + $this->assertSame( $expected, $output ); } @@ -235,40 +237,32 @@ public function test_non_standalone_after_inline_script_with_main_script_with_no unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; + $this->assertSame( $expected, $output ); } /** * Test non standalone `before` inline scripts attached to deferred main scripts. * - * @ticket 12009 - * @dataProvider data_non_standalone_before_inline_script_with_defer - */ - public function test_non_standalone_before_inline_script_with_defer( $expected, $output, $message ) { - $this->assertSame( $expected, $output, $message ); - } - - /** - * Data provider. + * If the main script has a `before` inline script, all dependencies will be blocking. * - * @return array + * @ticket 12009 */ - public function data_non_standalone_before_inline_script_with_defer() { - $data = array(); - - // If the main script has a `before` inline script, all dependencies will be blocking. + public function test_non_standalone_before_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-i1-1', 'http://example.org/ds-i1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-2', 'http://example.org/ds-i1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-3', 'http://example.org/ds-i1-3.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i1-1', 'http://example.org/ms-i1-1.js', array( 'ds-i1-1', 'ds-i1-2', 'ds-i1-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-i1-1', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -276,16 +270,26 @@ public function data_non_standalone_before_inline_script_with_defer() { $expected .= "console.log(\"before one\");\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); - // If any of the dependencies in the chain have a `before` inline script, all scripts above it should be blocking. + $this->assertSame( $expected, $output ); + } + + /** + * Test non standalone `before` inline scripts attached to a dependency scripts in a all scripts `defer` chain. + * + * If any of the dependencies in the chain have a `before` inline script, all scripts above it should be blocking. + * + * @ticket 12009 + */ + public function test_non_standalone_before_inline_script_on_dependency_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-i2-1', 'http://example.org/ds-i2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-2', 'http://example.org/ds-i2-2.js', array( 'ds-i2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-3', 'http://example.org/ds-i2-3.js', array( 'ds-i2-2' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i2-1', 'http://example.org/ms-i2-1.js', array( 'ds-i2-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-i2-2', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Scripts in the chain before the script having before must be blocking.' ) ); - // If the top most dependency in the chain has a `before` inline script, none of the scripts bellow it will be blocking. + $this->assertSame( $expected, $output ); + } + + /** + * Test non standalone `before` inline scripts attached to top most dependency in a all scripts `defer` chain. + * + * If the top most dependency in the chain has a `before` inline script, + * none of the scripts bellow it will be blocking. + * + * @ticket 12009 + */ + public function test_non_standalone_before_inline_script_on_top_most_dependency_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-i3-1', 'http://example.org/ds-i3-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i3-2', 'http://example.org/ds-i3-2.js', array( 'ds-i3-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i3-1', 'http://example.org/ms-i3-1.js', array( 'ds-i3-2' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-i3-1', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Top most has before inline script. All the script in the chain defer.' ) ); - // If there are two dependency chains, rules are applied to the scripts in the chain that contain a `before` inline script. + $this->assertSame( $expected, $output ); + } + + /** + * Test non standalone `before` inline scripts attached to one the chain, of the two all scripts `defer` chains. + * + * If there are two dependency chains, rules are applied to the scripts in the chain that contain a `before` inline script. + * + * @ticket 12009 + */ + public function test_non_standalone_before_inline_script_on_multiple_defer_script_chain() { unregister_all_script_handles(); wp_enqueue_script( 'ch1-ds-i4-1', 'http://example.org/ch1-ds-i4-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ch1-ds-i4-2', 'http://example.org/ch1-ds-i4-2.js', array( 'ch1-ds-i4-1' ), null, array( 'strategy' => 'defer' ) ); @@ -318,7 +342,8 @@ public function data_non_standalone_before_inline_script_with_defer() { wp_enqueue_script( 'ch2-ds-i4-2', 'http://example.org/ch2-ds-i4-2.js', array( 'ch2-ds-i4-1' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ch2-ds-i4-2', 'console.log("before one");', 'before' ); wp_enqueue_script( 'ms-i4-1', 'http://example.org/ms-i4-1.js', array( 'ch2-ds-i4-1', 'ch2-ds-i4-2' ), null, array( 'strategy' => 'defer' ) ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -327,9 +352,8 @@ public function data_non_standalone_before_inline_script_with_defer() { $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Only top dependency script in chain two should be blocking.' ) ); - return $data; + $this->assertSame( $expected, $output ); } /** From 4d0140d83dc1c6126919c6122747fc9686f966d2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 14:34:30 +0100 Subject: [PATCH 32/34] used function instead of arrow function. --- src/wp-includes/script-loader.php | 6 +++--- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index c3db191fd8834..cf4b2289fcb9e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1846,15 +1846,15 @@ function wp_just_in_time_script_localization() { /** * Prints a loader script if there is text/template registered script. - * - * When injected in DOM, this script converts any text/template script + * + * When injected in DOM, this script converts any text/template script * associated with a handle to type/javascript and execute them. */ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->has_delayed_inline_script() ) { $output = << { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 44e0df80c5cd9..f16cefc76aa93 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -77,7 +77,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { $output = get_echo( 'wp_print_scripts' ); $expected = << -let wpLoadAfterScripts = ( handle ) => { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -153,7 +153,7 @@ public function test_non_standalone_after_inline_script_with_defer_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -let wpLoadAfterScripts = ( handle ) => { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -186,7 +186,7 @@ public function test_non_standalone_after_inline_script_with_async_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -let wpLoadAfterScripts = ( handle ) => { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); From a5b83a41c3d36d297100a45c6fea00f6ce59784f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 14:41:36 +0100 Subject: [PATCH 33/34] single condition --- src/wp-includes/class-wp-scripts.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 1c7883699aed4..10b81ceb6f23e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -802,11 +802,11 @@ public function add_data( $handle, $key, $value ) { */ public function has_delayed_inline_script() { foreach ( $this->registered as $handle => $script ) { - if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) ) { - // non standalone after scripts of async or defer are usually delayed. - if ( $this->has_non_standalone_inline_script( $handle, 'after' ) ) { - return true; - } + // non standalone after scripts of async or defer are usually delayed. + if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) && + $this->has_non_standalone_inline_script( $handle, 'after' ) + ) { + return true; } } return false; From 026912803937679b7b9c72bb5af763a48cc9825d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 14:59:57 +0100 Subject: [PATCH 34/34] put back handle --- src/wp-includes/script-loader.php | 2 +- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index cf4b2289fcb9e..a3979e7bc1e6e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1854,7 +1854,7 @@ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->has_delayed_inline_script() ) { $output = << { script.setAttribute("type","text/javascript"); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index f16cefc76aa93..090684c105e8b 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -77,7 +77,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { $output = get_echo( 'wp_print_scripts' ); $expected = << -function wpLoadAfterScripts() { +function wpLoadAfterScripts( handle ) { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -153,7 +153,7 @@ public function test_non_standalone_after_inline_script_with_defer_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -function wpLoadAfterScripts() { +function wpLoadAfterScripts( handle ) { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -186,7 +186,7 @@ public function test_non_standalone_after_inline_script_with_async_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -function wpLoadAfterScripts() { +function wpLoadAfterScripts( handle ) { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript");