Make WordPress Core


Ignore:
Timestamp:
11/12/2025 11:34:56 AM (11 months ago)
Author:
jonsurrell
Message:

Script Loader: Omit sourceURL from translation data when not printed.

The data returned from WP_Scripts::print_translations() when $display is false may be used in unpredictable ways that are incompatible with sourceURL comments. Omit the sourceURL comment in this case.

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

Follow-up to [60719].

Props jonsurrell, ralucastn, westonruter, peterwilsoncc.
See #63887.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-scripts.php

    r61176 r61223  
    351351                $translations = $this->print_translations( $handle, false );
    352352                if ( $translations ) {
    353                         $translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
     353                        /*
     354                         * The sourceURL comment is not included by WP_Scripts::print_translations()
     355                         * when `$display` is `false` to prevent issues where the script tag contents are used
     356                         * by extenders for other purposes, for example concatenated with other script content.
     357                         *
     358                         * Include the sourceURL comment here as it would be when printed directly.
     359                         */
     360                        $source_url    = rawurlencode( "{$handle}-js-translations" );
     361                        $translations .= "\n//# sourceURL={$source_url}";
     362                        $translations  = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
    354363                }
    355364
    … …  
    723732                }
    724733
    725                 $source_url = rawurlencode( "{$handle}-js-translations" );
    726 
    727734                $output = <<<JS
    728735( function( domain, translations ) {
    … …  
    731738        wp.i18n.setLocaleData( localeData, domain );
    732739} )( "{$domain}", {$json_translations} );
    733 //# sourceURL={$source_url}
    734740JS;
    735741
    736742                if ( $display ) {
     743                        $source_url = rawurlencode( "{$handle}-js-translations" );
     744                        $output    .= "\n//# sourceURL={$source_url}";
    737745                        wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-translations" ) );
    738746                }
Note: See TracChangeset for help on using the changeset viewer.