Make WordPress Core

Changeset 61260


Ignore:
Timestamp:
11/18/2025 10:10:49 AM (3 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].

Reviewed by westonruter.
Merges [61223] to the 6.9 branch.

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

Location:
branches/6.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.9

  • branches/6.9/src/wp-includes/class-wp-scripts.php

    r61176 r61260  
    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        }
  • branches/6.9/tests/phpunit/tests/dependencies/scripts.php

    r61176 r61260  
    40764076        $this->assertEqualHTML( $expected, $print_scripts );
    40774077    }
     4078
     4079    /**
     4080     * Ensure that `::print_translations()` does not include the sourceURL comment when `$display` is false.
     4081     *
     4082     * @ticket 63887
     4083     * @covers ::print_translations
     4084     */
     4085    public function test_print_translations_no_display_no_sourceurl() {
     4086        global $wp_scripts;
     4087        $this->add_html5_script_theme_support();
     4088
     4089        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     4090        wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
     4091        wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
     4092
     4093        $translations_script_data = $wp_scripts->print_translations( 'test-example', false );
     4094        $this->assertStringNotContainsStringIgnoringCase( 'sourceURL=', $translations_script_data );
     4095    }
    40784096}
Note: See TracChangeset for help on using the changeset viewer.