Skip to content

Commit dbd1ed5

Browse files
committed
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 #9416. Follow-up to [60909], [60899]. Props debarghyabanerjee, westonruter, hbhalodia, peterwilsoncc, sabernhardt, poena. See #63887, #59446. Fixes #63806. git-svn-id: https://develop.svn.wordpress.org/trunk@60913 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d671de6 commit dbd1ed5

File tree

11 files changed

+92
-30
lines changed

11 files changed

+92
-30
lines changed

src/wp-content/themes/twentyfifteen/functions.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,14 @@ function twentyfifteen_fonts_url() {
412412
* @since Twenty Fifteen 1.1
413413
*/
414414
function twentyfifteen_javascript_detection() {
415-
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
415+
$js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);";
416+
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
417+
418+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
419+
wp_print_inline_script_tag( $js );
420+
} else {
421+
echo "<script>$js</script>\n";
422+
}
416423
}
417424
add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );
418425

src/wp-content/themes/twentyfifteen/inc/customizer.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,11 @@ function twentyfifteen_get_color_scheme_css( $colors ) {
772772
/**
773773
* Outputs an Underscore template for generating CSS for the color scheme.
774774
*
775-
* The template generates the css dynamically for instant display in the Customizer
775+
* The template generates the CSS dynamically for instant display in the Customizer
776776
* preview.
777777
*
778778
* @since Twenty Fifteen 1.0
779+
* @since Twenty Fifteen 4.1 Added `wp_print_inline_script_tag()` support.
779780
*/
780781
function twentyfifteen_color_scheme_css_template() {
781782
$colors = array(
@@ -792,10 +793,19 @@ function twentyfifteen_color_scheme_css_template() {
792793
'secondary_sidebar_textcolor' => '{{ data.secondary_sidebar_textcolor }}',
793794
'meta_box_background_color' => '{{ data.meta_box_background_color }}',
794795
);
795-
?>
796-
<script type="text/html" id="tmpl-twentyfifteen-color-scheme">
797-
<?php echo twentyfifteen_get_color_scheme_css( $colors ); ?>
798-
</script>
799-
<?php
796+
797+
$css_template = twentyfifteen_get_color_scheme_css( $colors );
798+
799+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
800+
wp_print_inline_script_tag(
801+
$css_template,
802+
array(
803+
'type' => 'text/html',
804+
'id' => 'tmpl-twentyfifteen-color-scheme',
805+
)
806+
);
807+
} else {
808+
echo '<script type="text/html" id="tmpl-twentyfifteen-color-scheme">' . $css_template . '</script>';
809+
}
800810
}
801811
add_action( 'customize_controls_print_footer_scripts', 'twentyfifteen_color_scheme_css_template' );

src/wp-content/themes/twentyseventeen/functions.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,14 @@ function twentyseventeen_excerpt_more( $link ) {
406406
* @since Twenty Seventeen 1.0
407407
*/
408408
function twentyseventeen_javascript_detection() {
409-
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
409+
$js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);";
410+
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
411+
412+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
413+
wp_print_inline_script_tag( $js );
414+
} else {
415+
echo "<script>$js</script>\n";
416+
}
410417
}
411418
add_action( 'wp_head', 'twentyseventeen_javascript_detection', 0 );
412419

src/wp-content/themes/twentysixteen/functions.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,14 @@ function twentysixteen_fonts_url() {
373373
* @since Twenty Sixteen 1.0
374374
*/
375375
function twentysixteen_javascript_detection() {
376-
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
376+
$js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);";
377+
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
378+
379+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
380+
wp_print_inline_script_tag( $js );
381+
} else {
382+
echo "<script>$js</script>\n";
383+
}
377384
}
378385
add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );
379386

src/wp-content/themes/twentysixteen/inc/customizer.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ function twentysixteen_get_color_scheme_css( $colors ) {
838838
* Customizer preview.
839839
*
840840
* @since Twenty Sixteen 1.0
841+
* @since Twenty Sixteen 4.1 Added `wp_print_inline_script_tag()` support.
841842
*/
842843
function twentysixteen_color_scheme_css_template() {
843844
$colors = array(
@@ -848,11 +849,20 @@ function twentysixteen_color_scheme_css_template() {
848849
'secondary_text_color' => '{{ data.secondary_text_color }}',
849850
'border_color' => '{{ data.border_color }}',
850851
);
851-
?>
852-
<script type="text/html" id="tmpl-twentysixteen-color-scheme">
853-
<?php echo twentysixteen_get_color_scheme_css( $colors ); ?>
854-
</script>
855-
<?php
852+
853+
$css_template = twentysixteen_get_color_scheme_css( $colors );
854+
855+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
856+
wp_print_inline_script_tag(
857+
$css_template,
858+
array(
859+
'type' => 'text/html',
860+
'id' => 'tmpl-twentysixteen-color-scheme',
861+
)
862+
);
863+
} else {
864+
echo '<script type="text/html" id="tmpl-twentysixteen-color-scheme">' . $css_template . '</script>';
865+
}
856866
}
857867
add_action( 'customize_controls_print_footer_scripts', 'twentysixteen_color_scheme_css_template' );
858868

src/wp-content/themes/twentytwenty/inc/template-tags.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,14 @@ function twentytwenty_nav_menu_social_icons( $item_output, $item, $depth, $args
660660
* @since Twenty Twenty 1.0
661661
*/
662662
function twentytwenty_no_js_class() {
663+
$js = "document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );";
664+
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
663665

664-
?>
665-
<script>document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );</script>
666-
<?php
666+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
667+
wp_print_inline_script_tag( $js );
668+
} else {
669+
echo "<script>$js</script>\n";
670+
}
667671
}
668672

669673
add_action( 'wp_head', 'twentytwenty_no_js_class' );

src/wp-content/themes/twentytwentyone/classes/class-twenty-twenty-one-dark-mode.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,14 @@ public function the_html( $attrs = array() ) {
363363
* @return void
364364
*/
365365
public function the_script() {
366-
echo '<script>';
367-
include get_template_directory() . '/assets/js/dark-mode-toggler.js'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
368-
echo '</script>';
366+
$path = 'assets/js/dark-mode-toggler.js';
367+
$js = rtrim( file_get_contents( trailingslashit( get_template_directory() ) . $path ) );
368+
$js .= "\n//# sourceURL=" . esc_url_raw( trailingslashit( get_template_directory_uri() ) . $path );
369+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
370+
wp_print_inline_script_tag( $js );
371+
} else {
372+
printf( "<script>%s</script>\n", $js );
373+
}
369374
}
370375

371376
/**

src/wp-content/themes/twentytwentyone/functions.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,18 @@ function twentytwentyone_the_html_classes() {
630630
* @return void
631631
*/
632632
function twentytwentyone_add_ie_class() {
633-
?>
634-
<script>
635-
if ( -1 !== navigator.userAgent.indexOf( 'MSIE' ) || -1 !== navigator.appVersion.indexOf( 'Trident/' ) ) {
636-
document.body.classList.add( 'is-IE' );
633+
$script = "
634+
if ( -1 !== navigator.userAgent.indexOf('MSIE') || -1 !== navigator.appVersion.indexOf('Trident/') ) {
635+
document.body.classList.add('is-IE');
636+
}
637+
";
638+
$script .= '//# sourceURL=' . rawurlencode( __FUNCTION__ );
639+
640+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
641+
wp_print_inline_script_tag( $script );
642+
} else {
643+
echo "<script>$script</script>\n";
637644
}
638-
</script>
639-
<?php
640645
}
641646
add_action( 'wp_footer', 'twentytwentyone_add_ie_class' );
642647

src/wp-content/themes/twentytwentyone/inc/template-functions.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ function twenty_twenty_one_pingback_header() {
7474
* @return void
7575
*/
7676
function twenty_twenty_one_supports_js() {
77-
echo '<script>document.body.classList.remove("no-js");</script>';
77+
$js = "document.body.classList.remove('no-js');";
78+
$js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
79+
80+
if ( function_exists( 'wp_print_inline_script_tag' ) ) {
81+
wp_print_inline_script_tag( $js );
82+
} else {
83+
echo "<script>$js</script>\n";
84+
}
7885
}
7986
add_action( 'wp_footer', 'twenty_twenty_one_supports_js' );
8087

src/wp-includes/embed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ function get_post_embed_html( $width, $height, $post = null ) {
520520
*/
521521
$js_path = '/js/wp-embed' . wp_scripts_get_suffix() . '.js';
522522
$output .= wp_get_inline_script_tag(
523-
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . includes_url( $js_path )
523+
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . esc_url_raw( includes_url( $js_path ) )
524524
);
525525

526526
/**
@@ -1093,7 +1093,7 @@ function wp_enqueue_embed_styles() {
10931093
function print_embed_scripts() {
10941094
$js_path = '/js/wp-embed-template' . wp_scripts_get_suffix() . '.js';
10951095
wp_print_inline_script_tag(
1096-
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . includes_url( $js_path )
1096+
trim( file_get_contents( ABSPATH . WPINC . $js_path ) ) . "\n//# sourceURL=" . esc_url_raw( includes_url( $js_path ) )
10971097
);
10981098
}
10991099

0 commit comments

Comments
 (0)