Bundled Themes: Use wp_print_inline_script_tag() when available and include sourceURL for JS.

Instead of manually constructing the markup for `SCRIPT` tags, leverage `wp_print_inline_script_tag()` when available to do the construction while also ensuring filters may inject additional attributes on the `SCRIPT` tags, such as `nonce` for CSP. When the function is not available (prior to WP 5.7), fall back to the manual markup construction.

This also adds the `sourceURL` comments to the inline scripts to facilitate debugging, per #63887.

Developed in https://github.com/WordPress/wordpress-develop/pull/9416.

Follow-up to [60909], [60899].

Props debarghyabanerjee, westonruter, hbhalodia, peterwilsoncc, sabernhardt, poena.
See #63887, #59446.
Fixes #63806.

Built from https://develop.svn.wordpress.org/trunk@60913


git-svn-id: http://core.svn.wordpress.org/trunk@60249 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2025-10-07 23:34:35 +00:00
parent 2468de1134
commit 3c68b172f6
12 changed files with 93 additions and 31 deletions

View File

@ -412,7 +412,14 @@ endif;
* @since Twenty Fifteen 1.1
*/
function twentyfifteen_javascript_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
$js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);";
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag( $js );
} else {
echo "<script>$js</script>\n";
}
}
add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );

View File

@ -772,10 +772,11 @@ CSS;
/**
* Outputs an Underscore template for generating CSS for the color scheme.
*
* The template generates the css dynamically for instant display in the Customizer
* The template generates the CSS dynamically for instant display in the Customizer
* preview.
*
* @since Twenty Fifteen 1.0
* @since Twenty Fifteen 4.1 Added `wp_print_inline_script_tag()` support.
*/
function twentyfifteen_color_scheme_css_template() {
$colors = array(
@ -792,10 +793,19 @@ function twentyfifteen_color_scheme_css_template() {
'secondary_sidebar_textcolor' => '{{ data.secondary_sidebar_textcolor }}',
'meta_box_background_color' => '{{ data.meta_box_background_color }}',
);
?>
<script type="text/html" id="tmpl-twentyfifteen-color-scheme">
<?php echo twentyfifteen_get_color_scheme_css( $colors ); ?>
</script>
<?php
$css_template = twentyfifteen_get_color_scheme_css( $colors );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag(
$css_template,
array(
'type' => 'text/html',
'id' => 'tmpl-twentyfifteen-color-scheme',
)
);
} else {
echo '<script type="text/html" id="tmpl-twentyfifteen-color-scheme">' . $css_template . '</script>';
}
}
add_action( 'customize_controls_print_footer_scripts', 'twentyfifteen_color_scheme_css_template' );

View File

@ -406,7 +406,14 @@ add_filter( 'excerpt_more', 'twentyseventeen_excerpt_more' );
* @since Twenty Seventeen 1.0
*/
function twentyseventeen_javascript_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
$js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);";
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag( $js );
} else {
echo "<script>$js</script>\n";
}
}
add_action( 'wp_head', 'twentyseventeen_javascript_detection', 0 );

View File

@ -373,7 +373,14 @@ endif;
* @since Twenty Sixteen 1.0
*/
function twentysixteen_javascript_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
$js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);";
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag( $js );
} else {
echo "<script>$js</script>\n";
}
}
add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );

View File

@ -838,6 +838,7 @@ CSS;
* Customizer preview.
*
* @since Twenty Sixteen 1.0
* @since Twenty Sixteen 4.1 Added `wp_print_inline_script_tag()` support.
*/
function twentysixteen_color_scheme_css_template() {
$colors = array(
@ -848,11 +849,20 @@ function twentysixteen_color_scheme_css_template() {
'secondary_text_color' => '{{ data.secondary_text_color }}',
'border_color' => '{{ data.border_color }}',
);
?>
<script type="text/html" id="tmpl-twentysixteen-color-scheme">
<?php echo twentysixteen_get_color_scheme_css( $colors ); ?>
</script>
<?php
$css_template = twentysixteen_get_color_scheme_css( $colors );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag(
$css_template,
array(
'type' => 'text/html',
'id' => 'tmpl-twentysixteen-color-scheme',
)
);
} else {
echo '<script type="text/html" id="tmpl-twentysixteen-color-scheme">' . $css_template . '</script>';
}
}
add_action( 'customize_controls_print_footer_scripts', 'twentysixteen_color_scheme_css_template' );

View File

@ -660,10 +660,14 @@ add_filter( 'walker_nav_menu_start_el', 'twentytwenty_nav_menu_social_icons', 10
* @since Twenty Twenty 1.0
*/
function twentytwenty_no_js_class() {
$js = "document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );";
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
?>
<script>document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );</script>
<?php
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag( $js );
} else {
echo "<script>$js</script>\n";
}
}
add_action( 'wp_head', 'twentytwenty_no_js_class' );

View File

@ -363,9 +363,14 @@ class Twenty_Twenty_One_Dark_Mode {
* @return void
*/
public function the_script() {
echo '<script>';
include get_template_directory() . '/assets/js/dark-mode-toggler.js'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
echo '</script>';
$path = 'assets/js/dark-mode-toggler.js';
$js = rtrim( file_get_contents( trailingslashit( get_template_directory() ) . $path ) );
$js .= "\n//# sourceURL=" . esc_url_raw( trailingslashit( get_template_directory_uri() ) . $path );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag( $js );
} else {
printf( "<script>%s</script>\n", $js );
}
}
/**

View File

@ -630,13 +630,18 @@ function twentytwentyone_the_html_classes() {
* @return void
*/
function twentytwentyone_add_ie_class() {
?>
<script>
if ( -1 !== navigator.userAgent.indexOf( 'MSIE' ) || -1 !== navigator.appVersion.indexOf( 'Trident/' ) ) {
document.body.classList.add( 'is-IE' );
$script = "
if ( -1 !== navigator.userAgent.indexOf('MSIE') || -1 !== navigator.appVersion.indexOf('Trident/') ) {
document.body.classList.add('is-IE');
}
";
$script .= '//# sourceURL=' . rawurlencode( __FUNCTION__ );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag( $script );
} else {
echo "<script>$script</script>\n";
}
</script>
<?php
}
add_action( 'wp_footer', 'twentytwentyone_add_ie_class' );

View File

@ -74,7 +74,14 @@ add_action( 'wp_head', 'twenty_twenty_one_pingback_header' );
* @return void
*/
function twenty_twenty_one_supports_js() {
echo '<script>document.body.classList.remove("no-js");</script>';
$js = "document.body.classList.remove('no-js');";
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
wp_print_inline_script_tag( $js );
} else {
echo "<script>$js</script>\n";
}
}
add_action( 'wp_footer', 'twenty_twenty_one_supports_js' );

View File

@ -520,7 +520,7 @@ function get_post_embed_html( $width, $height, $post = null ) {
*/
$js_path = '/js/wp-embed' . wp_scripts_get_suffix() . '.js';
$output .= wp_get_inline_script_tag(
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . includes_url( $js_path )
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . esc_url_raw( includes_url( $js_path ) )
);
/**
@ -1093,7 +1093,7 @@ function wp_enqueue_embed_styles() {
function print_embed_scripts() {
$js_path = '/js/wp-embed-template' . wp_scripts_get_suffix() . '.js';
wp_print_inline_script_tag(
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . includes_url( $js_path )
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . esc_url_raw( includes_url( $js_path ) )
);
}

View File

@ -5992,7 +5992,7 @@ function _print_emoji_detection_script() {
$emoji_loader_script_path = '/js/wp-emoji-loader' . wp_scripts_get_suffix() . '.js';
wp_print_inline_script_tag(
rtrim( file_get_contents( ABSPATH . WPINC . $emoji_loader_script_path ) ) . "\n" .
'//# sourceURL=' . includes_url( $emoji_loader_script_path ),
'//# sourceURL=' . esc_url_raw( includes_url( $emoji_loader_script_path ) ),
array(
'type' => 'module',
)

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.9-alpha-60912';
$wp_version = '6.9-alpha-60913';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.