diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 8c3b5a4ba9f64..b15bd3f8e904e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -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 ) { @@ -722,18 +731,17 @@ public function print_translations( $handle, $display = true ) { return false; } - $source_url = rawurlencode( "{$handle}-js-translations" ); - $output = << "{$handle}-js-translations" ) ); } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index ebde14efbf261..cf6960099cb6c 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -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 ); + } }