diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index b90f6647ffe2e..227dcd8c0b506 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -122,17 +122,6 @@ class WP_Scripts extends WP_Dependencies { */ public $default_dirs; - /** - * Holds a string which contains the type attribute for script tag. - * - * If the active theme does not declare HTML5 support for 'script', - * then it initializes as `type='text/javascript'`. - * - * @since 5.3.0 - * @var string - */ - private $type_attr = ''; - /** * Holds a mapping of dependents (as handles) for a given script handle. * Used to optimize recursive dependency tree checks. @@ -158,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->type_attr = " type='text/javascript'"; - } - /** * Fires when the WP_Scripts instance is initialized. * @@ -236,20 +217,10 @@ public function print_extra_script( $handle, $display = true ) { return $output; } - printf( "\n", $this->type_attr, esc_attr( $handle ) ); - - // CDATA is not needed for HTML 5. - if ( $this->type_attr ) { - echo "/* type_attr ) { - echo "/* ]]> */\n"; - } - - echo "\n"; + wp_print_inline_script_tag( + $output, + array( 'id' => "{$handle}-js-extra" ) + ); return true; } @@ -306,7 +277,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->type_attr, 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 ''. @@ -315,21 +289,22 @@ 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->type_attr, - 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, + ) ); } } @@ -348,7 +323,10 @@ 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 = wp_get_inline_script_tag( + $translations, + array( 'id' => "{$handle}-js-translations" ) + ); } if ( $this->do_concat ) { @@ -425,20 +403,18 @@ public function do_item( $handle, $group = false ) { return true; } + $attributes = array( + 'src' => $src, + 'id' => "{$handle}-js", + ); if ( '' !== $strategy ) { - $strategy = ' ' . $strategy; + $attributes[ $strategy ] = true; if ( ! empty( $after_non_standalone_handle ) ) { - $strategy .= sprintf( " onload='wpLoadAfterScripts(%s)'", esc_attr( wp_json_encode( $handle ) ) ); + $attributes['onload'] = sprintf( 'wpLoadAfterScripts(%s)', wp_json_encode( $handle ) ); } } $tag = $translations . $cond_before . $before_handle; - $tag .= sprintf( - "\n", - $this->type_attr, - esc_url( $src ), - esc_attr( $handle ), - $strategy - ); + $tag .= wp_get_script_tag( $attributes ); $tag .= $after_handle . $cond_after; /** @@ -446,7 +422,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->type_attr, - esc_attr( $handle ), - esc_attr( $position ), - $output - ); + wp_print_inline_script_tag( $output, $attributes ); } return $output; @@ -693,7 +665,10 @@ public function print_translations( $handle, $display = true ) { JS; if ( $display ) { - printf( "\n%s\n\n", $this->type_attr, 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 418bab09308d4..b4990d723cb3e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2798,7 +2798,8 @@ function wp_sanitize_script_attributes( $attributes ) { * @return string String containing `\n", wp_sanitize_script_attributes( $attributes ), $javascript ); }