From 43febecd662227715f29e72e1eb60034b80f8f42 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 27 Apr 2023 11:05:05 -0700 Subject: [PATCH 1/5] Rename type_attr variable to is_html5 --- src/wp-includes/class-wp-scripts.php | 29 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index b90f6647ffe2e..935bb661ebe31 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -123,15 +123,16 @@ class WP_Scripts extends WP_Dependencies { public $default_dirs; /** - * Holds a string which contains the type attribute for script tag. + * Whether script tags should be generated as HTML5. * * If the active theme does not declare HTML5 support for 'script', - * then it initializes as `type='text/javascript'`. + * then scripts are output with `type='text/javascript'`. * * @since 5.3.0 - * @var string + * @since 6.3.0 Renamed from type_attr to is_html5. + * @var bool */ - private $type_attr = ''; + private $is_html5 = true; /** * Holds a mapping of dependents (as handles) for a given script handle. @@ -163,7 +164,7 @@ function_exists( 'is_admin' ) && ! is_admin() && function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' ) ) { - $this->type_attr = " type='text/javascript'"; + $this->is_html5 = false; } /** @@ -236,16 +237,16 @@ public function print_extra_script( $handle, $display = true ) { return $output; } - printf( "\n", $this->type_attr, esc_attr( $handle ) ); + printf( "\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ) ); // CDATA is not needed for HTML 5. - if ( $this->type_attr ) { + if ( ! $this->is_html5 ) { echo "/* type_attr ) { + if ( ! $this->is_html5 ) { echo "/* ]]> */\n"; } @@ -306,7 +307,7 @@ public function do_item( $handle, $group = false ) { $before_handle = $this->print_inline_script( $handle, 'before', false ); if ( $before_handle ) { - $before_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $before_handle ); + $before_handle = sprintf( "\n%s\n\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), $before_handle ); } // Eligible loading strategies will only be 'async', 'defer', or ''. @@ -317,7 +318,7 @@ public function do_item( $handle, $group = false ) { if ( $after_handle ) { $after_handle = sprintf( "\n%3\$s\n\n", - $this->type_attr, + $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), $after_handle ); @@ -348,7 +349,7 @@ public function do_item( $handle, $group = false ) { $translations = $this->print_translations( $handle, false ); if ( $translations ) { - $translations = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $translations ); + $translations = sprintf( "\n%s\n\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), $translations ); } if ( $this->do_concat ) { @@ -434,7 +435,7 @@ public function do_item( $handle, $group = false ) { $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( "\n", - $this->type_attr, + $this->is_html5 ? '' : " type='text/javascript'", esc_url( $src ), esc_attr( $handle ), $strategy @@ -527,7 +528,7 @@ public function print_inline_script( $handle, $position = 'after', $display = tr printf( $script_output, - $this->type_attr, + $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), esc_attr( $position ), $output @@ -693,7 +694,7 @@ public function print_translations( $handle, $display = true ) { JS; if ( $display ) { - printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $output ); + printf( "\n%s\n\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), $output ); } return $output; From 97375425dc30e64b864c5a79348d4df78d644db4 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 27 Apr 2023 12:33:34 -0700 Subject: [PATCH 2/5] Eliminte manual construction of script tags --- src/wp-includes/class-wp-scripts.php | 93 ++++++++++++++-------------- src/wp-includes/script-loader.php | 11 +++- 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 935bb661ebe31..2293181dc2866 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -237,20 +237,12 @@ public function print_extra_script( $handle, $display = true ) { return $output; } - printf( "\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ) ); - - // CDATA is not needed for HTML 5. - if ( ! $this->is_html5 ) { - echo "/* is_html5 ) { - echo "/* ]]> */\n"; - } - - echo "\n"; + wp_print_inline_script_tag( + $output, + array( + 'id' => "{$handle}-js-extra", + ) + ); return true; } @@ -307,7 +299,10 @@ public function do_item( $handle, $group = false ) { $before_handle = $this->print_inline_script( $handle, 'before', false ); if ( $before_handle ) { - $before_handle = sprintf( "\n%s\n\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), $before_handle ); + $before_handle = wp_get_inline_script_tag( + $before_handle, + array( 'id' => "{$handle}-js-before" ) + ); } // Eligible loading strategies will only be 'async', 'defer', or ''. @@ -316,21 +311,24 @@ public function do_item( $handle, $group = false ) { $after_handle = $this->print_inline_script( $handle, $after, false ); if ( $after_handle ) { - $after_handle = sprintf( - "\n%3\$s\n\n", - $this->is_html5 ? '' : " type='text/javascript'", - esc_attr( $handle ), - $after_handle + $after_handle = wp_get_inline_script_tag( + $after_handle, + array( + 'id' => "{$handle}-js-after", + ) ); } if ( '' !== $strategy ) { $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { - $after_handle .= sprintf( - "\n", - esc_attr( $handle ), - $after_non_standalone_handle + $after_handle .= wp_get_inline_script_tag( + $after_non_standalone_handle, + array( + 'type' => 'text/template', + 'id' => "{$handle}-js-after", + 'data-wp-executes-after' => $handle, + ) ); } } @@ -349,7 +347,10 @@ public function do_item( $handle, $group = false ) { $translations = $this->print_translations( $handle, false ); if ( $translations ) { - $translations = sprintf( "\n%s\n\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), $translations ); + $translations = wp_get_inline_script_tag( + $translations, + array( 'id' => "{$handle}-js-translations" ) + ); } if ( $this->do_concat ) { @@ -426,20 +427,15 @@ public function do_item( $handle, $group = false ) { return true; } - if ( '' !== $strategy ) { - $strategy = ' ' . $strategy; - if ( ! empty( $after_non_standalone_handle ) ) { - $strategy .= sprintf( " onload='wpLoadAfterScripts(%s)'", esc_attr( wp_json_encode( $handle ) ) ); - } + $attributes = array( + 'src' => $src, + 'id' => "{$handle}-js", + ); + if ( '' !== $strategy && ! empty( $after_non_standalone_handle ) ) { + $attributes['onload'] = sprintf( 'wpLoadAfterScripts(%s)', wp_json_encode( $handle ) ); } $tag = $translations . $cond_before . $before_handle; - $tag .= sprintf( - "\n", - $this->is_html5 ? '' : " type='text/javascript'", - esc_url( $src ), - esc_attr( $handle ), - $strategy - ); + $tag .= wp_get_script_tag( $attributes ); $tag .= $after_handle . $cond_after; /** @@ -447,7 +443,7 @@ public function do_item( $handle, $group = false ) { * * @since 4.1.0 * - * @param string $tag The `\n"; + $attributes['id'] = "{$handle}-js-after"; + $attributes['data-wp-executes-after'] = $handle; } else { - $script_output = "\n%4\$s\n\n"; + $attributes['id'] = "{$handle}-js-{$position}"; } - printf( - $script_output, - $this->is_html5 ? '' : " type='text/javascript'", - esc_attr( $handle ), - esc_attr( $position ), - $output - ); + wp_print_inline_script_tag( $output, $attributes ); } return $output; @@ -694,7 +686,12 @@ public function print_translations( $handle, $display = true ) { JS; if ( $display ) { - printf( "\n%s\n\n", $this->is_html5 ? '' : " type='text/javascript'", esc_attr( $handle ), $output ); + wp_print_inline_script_tag( + $output, + array( + 'id' => "{$handle}-js-translations", + ) + ); } return $output; diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index ee29f82597660..035736c365101 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2794,7 +2794,8 @@ function wp_sanitize_script_attributes( $attributes ) { * @return string String containing `\n", wp_sanitize_script_attributes( $attributes ), $javascript ); } From 10b05723e03003227c40601353174a314af17dc9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 27 Apr 2023 12:34:36 -0700 Subject: [PATCH 3/5] Remove now-unused is_html5/type_attr member variable --- src/wp-includes/class-wp-scripts.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 2293181dc2866..6be0f26e0e22c 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -122,18 +122,6 @@ class WP_Scripts extends WP_Dependencies { */ public $default_dirs; - /** - * Whether script tags should be generated as HTML5. - * - * If the active theme does not declare HTML5 support for 'script', - * then scripts are output with `type='text/javascript'`. - * - * @since 5.3.0 - * @since 6.3.0 Renamed from type_attr to is_html5. - * @var bool - */ - private $is_html5 = true; - /** * Holds a mapping of dependents (as handles) for a given script handle. * Used to optimize recursive dependency tree checks. @@ -159,14 +147,6 @@ public function __construct() { * @since 3.4.0 */ public function init() { - if ( - function_exists( 'is_admin' ) && ! is_admin() - && - function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' ) - ) { - $this->is_html5 = false; - } - /** * Fires when the WP_Scripts instance is initialized. * From 1079fe54a5945a55a18a2a4da354cf260dab5b51 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 27 Apr 2023 12:53:18 -0700 Subject: [PATCH 4/5] Fix printing async/defer strategy --- src/wp-includes/class-wp-scripts.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 6be0f26e0e22c..8fb540feea022 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -411,8 +411,11 @@ public function do_item( $handle, $group = false ) { 'src' => $src, 'id' => "{$handle}-js", ); - if ( '' !== $strategy && ! empty( $after_non_standalone_handle ) ) { - $attributes['onload'] = sprintf( 'wpLoadAfterScripts(%s)', wp_json_encode( $handle ) ); + if ( '' !== $strategy ) { + $attributes[ $strategy ] = true; + if ( ! empty( $after_non_standalone_handle ) ) { + $attributes['onload'] = sprintf( 'wpLoadAfterScripts(%s)', wp_json_encode( $handle ) ); + } } $tag = $translations . $cond_before . $before_handle; $tag .= wp_get_script_tag( $attributes ); From ee0ababacc72abd7986904e645f735e608cf57ab Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 27 Apr 2023 14:29:57 -0700 Subject: [PATCH 5/5] Consistently format single-item associative arrays --- src/wp-includes/class-wp-scripts.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 8fb540feea022..227dcd8c0b506 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -219,9 +219,7 @@ public function print_extra_script( $handle, $display = true ) { wp_print_inline_script_tag( $output, - array( - 'id' => "{$handle}-js-extra", - ) + array( 'id' => "{$handle}-js-extra" ) ); return true; @@ -293,9 +291,7 @@ public function do_item( $handle, $group = false ) { if ( $after_handle ) { $after_handle = wp_get_inline_script_tag( $after_handle, - array( - 'id' => "{$handle}-js-after", - ) + array( 'id' => "{$handle}-js-after" ) ); } if ( '' !== $strategy ) { @@ -671,9 +667,7 @@ public function print_translations( $handle, $display = true ) { if ( $display ) { wp_print_inline_script_tag( $output, - array( - 'id' => "{$handle}-js-translations", - ) + array( 'id' => "{$handle}-js-translations" ) ); }