From 09923cc040d1b65486dc98d3981a8b2c03693c01 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sat, 7 May 2022 02:51:54 +0000 Subject: [PATCH] Script Loader: Fix i18n edge case breaking dependencies. Prevent concatenation of scripts if the text domain is defined to ensure the dependency order is respected. This accounts for an edge case in which replacing a core script via a plugin and a lack of translations (eg, for a US English site) could cause the JavaScript files to be ordered incorrectly. Follow up to [52937]. Props audrasjb, boniu91, chaion07, costdev, hellofromtonya, jsnajdr, mukesh27, ndiego, ugyensupport. Fixes #55628. git-svn-id: https://develop.svn.wordpress.org/trunk@53360 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class.wp-scripts.php | 4 ++- tests/phpunit/tests/dependencies/scripts.php | 34 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class.wp-scripts.php b/src/wp-includes/class.wp-scripts.php index aebf3a3b59..246d1fe6a2 100644 --- a/src/wp-includes/class.wp-scripts.php +++ b/src/wp-includes/class.wp-scripts.php @@ -311,6 +311,8 @@ class WP_Scripts extends WP_Dependencies { $inline_script_tag = ''; } + $translations_stop_concat = ! empty( $obj->textdomain ); + $translations = $this->print_translations( $handle, false ); if ( $translations ) { $translations = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $translations ); @@ -327,7 +329,7 @@ class WP_Scripts extends WP_Dependencies { */ $srce = apply_filters( 'script_loader_src', $src, $handle ); - if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) { + if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations_stop_concat ) ) { $this->do_concat = false; // Have to print the so-far concatenated scripts right away to maintain the right order. diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index a31628e26b..e799247352 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -1481,4 +1481,38 @@ JS; array( false, '[""]' ), ); } + + /** + * @ticket 55628 + * @covers ::wp_set_script_translations + */ + public function test_wp_external_wp_i18n_print_order() { + global $wp_scripts; + + $wp_scripts->do_concat = true; + $wp_scripts->default_dirs = array( '/default/' ); + + // wp-i18n script in a non-default directory. + wp_register_script( 'wp-i18n', '/plugins/wp-i18n.js', array(), null ); + // Script in default dir that's going to be concatenated. + wp_enqueue_script( 'jquery-core', '/default/jquery-core.js', array(), null ); + // Script in default dir that depends on wp-i18n. + wp_enqueue_script( 'common', '/default/common.js', array(), null ); + wp_set_script_translations( 'common' ); + + $print_scripts = get_echo( + function() { + wp_print_scripts(); + _print_scripts(); + } + ); + + // The non-default script should end concatenation and maintain order. + $ver = get_bloginfo( 'version' ); + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + + $this->assertSame( $expected, $print_scripts ); + } }