Make WordPress Core


Ignore:
Timestamp:
10/07/2025 11:32:04 PM (10 months ago)
Author:
westonruter
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentyfifteen/inc/customizer.php

    r60521 r60913  
    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 */
    780781function twentyfifteen_color_scheme_css_template() {
     
    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}
    801811add_action( 'customize_controls_print_footer_scripts', 'twentyfifteen_color_scheme_css_template' );
Note: See TracChangeset for help on using the changeset viewer.