Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,16 @@ public function do_item( $handle, $group = false ) {

$translations = $this->print_translations( $handle, false );
if ( $translations ) {
$translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
/*
* The sourceURL comment is not included by WP_Scripts::print_translations()
* when `$display` is `false` to prevent issues where the script tag contents are used
* by extenders for other purposes, for example concatenated with other script content.
*
* Include the sourceURL comment here as it would be when printed directly.
*/
$source_url = rawurlencode( "{$handle}-js-translations" );
$translations .= "\n//# sourceURL={$source_url}";
$translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
}

if ( $this->do_concat ) {
Expand Down Expand Up @@ -722,18 +731,17 @@ public function print_translations( $handle, $display = true ) {
return false;
}

$source_url = rawurlencode( "{$handle}-js-translations" );

$output = <<<JS
( function( domain, translations ) {
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
localeData[""].domain = domain;
wp.i18n.setLocaleData( localeData, domain );
} )( "{$domain}", {$json_translations} );
//# sourceURL={$source_url}
JS;

if ( $display ) {
$source_url = rawurlencode( "{$handle}-js-translations" );
$output .= "\n//# sourceURL={$source_url}";
wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-translations" ) );
}

Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4075,4 +4075,22 @@ public function test_source_url_with_concat() {

$this->assertEqualHTML( $expected, $print_scripts );
}

/**
* Ensure that `::print_translations()` does not include the sourceURL comment when `$display` is false.
*
* @ticket 63887
* @covers ::print_translations
*/
public function test_print_translations_no_display_no_sourceurl() {
global $wp_scripts;
$this->add_html5_script_theme_support();

wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );

$translations_script_data = $wp_scripts->print_translations( 'test-example', false );
$this->assertStringNotContainsStringIgnoringCase( 'sourceURL=', $translations_script_data );
}
}
Loading