Make WordPress Core

Changeset 62723


Ignore:
Timestamp:
07/14/2026 06:32:47 AM (4 months ago)
Author:
westonruter
Message:

Script Loader: Enable attribute filters for concatenated scripts.

Replace the manually constructed <script> markup in _print_scripts() with wp_print_inline_script_tag() and wp_print_script_tag(). The merged inline script holding the concatenated wp_localize_script() data, and the load-scripts.php tag itself, are now passed through the wp_inline_script_attributes and wp_script_attributes filters, as every other script tag in core already is. Themes and plugins can therefore add attributes to them, such as the nonce needed to serve the admin under a Content-Security-Policy that disallows unsafe-inline.

The emitted markup is otherwise unchanged, aside from the attribute quoting and entity encoding applied by WP_HTML_Tag_Processor.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11702.
Follow-up to r56687, r60719.

Props sukhendu2002, galaxor, wildworks, westonruter.
See #51407, #57548, #58664, #59446.
Fixes #64683.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r62696 r62723  
    22312231        if ( $concat ) {
    22322232                if ( ! empty( $wp_scripts->print_code ) ) {
    2233                         echo "\n<script>\n";
    2234                         echo $wp_scripts->print_code;
    2235                         echo sprintf( "\n//# sourceURL=%s\n", rawurlencode( 'js-inline-concat-' . $concat ) );
    2236                         echo "</script>\n";
     2233                        wp_print_inline_script_tag( $wp_scripts->print_code . "\n//# sourceURL=" . rawurlencode( 'js-inline-concat-' . $concat ) );
    22372234                }
    22382235
    … …  
    22452242
    22462243                $src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}" . $concatenated . '&ver=' . $wp_scripts->default_version;
    2247                 echo "<script src='" . esc_attr( $src ) . "'></script>\n";
     2244                wp_print_script_tag( array( 'src' => $src ) );
    22482245        }
    22492246
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r62652 r62723  
    22762276        /**
    22772277         * Test script concatenation.
     2278         *
     2279         * @global WP_Scripts $wp_scripts
     2280         * @global string $wp_version
    22782281         */
    22792282        public function test_script_concatenation() {
    … …  
    22902293                $print_scripts = get_echo( '_print_scripts' );
    22912294
    2292                 $expected = "<script src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one,two,three&amp;ver={$wp_version}'></script>\n";
    2293 
    2294                 $this->assertSame( $expected, $print_scripts );
     2295                $expected = "<script src=\"/wp-admin/load-scripts.php?c=0&#038;load%5Bchunk_0%5D=one,two,three&#038;ver={$wp_version}\"></script>\n";
     2296
     2297                $this->assertEqualHTML( $expected, $print_scripts );
    22952298        }
    22962299
    … …  
    43114314        /**
    43124315         * @ticket 63887
     4316         *
     4317         * @global WP_Scripts $wp_scripts
     4318         * @global bool $concatenate_scripts
     4319         * @global string $wp_version
    43134320         */
    43144321        public function test_source_url_with_concat() {
    … …  
    43294336
    43304337                $expected = <<<HTML
    4331 
    4332 <script>
    4333 var one = {"key":"val"};var two = {"key":"val"};
    4334 //# sourceURL=js-inline-concat-one%2Ctwo
    4335 </script>
    4336 <script src="/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=one,two&ver={$wp_version}"></script>
    4337 
    4338 HTML;
     4338                <script>
     4339                var one = {"key":"val"};var two = {"key":"val"};
     4340                //# sourceURL=js-inline-concat-one%2Ctwo
     4341                </script>
     4342                <script src="/wp-admin/load-scripts.php?c=0&#038;load%5Bchunk_0%5D=one,two&#038;ver={$wp_version}"></script>
     4343
     4344                HTML;
    43394345
    43404346                $this->assertEqualHTML( $expected, $print_scripts );
Note: See TracChangeset for help on using the changeset viewer.