diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index e48658a1e7f7c..372cfc1fdc1ce 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -366,6 +366,8 @@ public function do_item( $handle, $group = false ) { $translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) ); } + $restore_concat_after_print = false; + if ( $this->do_concat ) { /** * Filters the script loader source. @@ -393,8 +395,27 @@ public function do_item( $handle, $group = false ) { $this->concat_version .= "$handle$ver"; return true; } else { - $this->ext_handles .= "$handle,"; - $this->ext_version .= "$handle$ver"; + $has_pending_dependents = false; + + if ( $src ) { + foreach ( $this->get_dependents( $handle ) as $dependent ) { + if ( in_array( $dependent, $this->to_do, true ) && ! in_array( $dependent, $this->done, true ) ) { + $has_pending_dependents = true; + break; + } + } + } + + if ( $has_pending_dependents ) { + $this->do_concat = false; + + _print_scripts(); + $this->reset(); + $restore_concat_after_print = true; + } else { + $this->ext_handles .= "$handle,"; + $this->ext_version .= "$handle$ver"; + } } } @@ -448,6 +469,9 @@ public function do_item( $handle, $group = false ) { $src = esc_url_raw( apply_filters( 'script_loader_src', $src, $handle ) ); if ( ! $src ) { + if ( $restore_concat_after_print ) { + $this->do_concat = true; + } return true; } @@ -501,6 +525,10 @@ public function do_item( $handle, $group = false ) { echo $tag; } + if ( $restore_concat_after_print ) { + $this->do_concat = true; + } + return true; } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 41c9673915b93..cf32f9fcf2732 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -3756,6 +3756,35 @@ public function data_wp_localize_script_data_formats() { ); } + /** + * @ticket 15833 + * + * @covers WP_Scripts::do_item + */ + public function test_concatenation_preserves_external_dependency_order() { + global $wp_scripts, $wp_version; + + $wp_scripts->do_concat = true; + $wp_scripts->default_dirs = array( '/default/' ); + + wp_enqueue_script( 'one', '/default/one.js', array(), null ); + wp_enqueue_script( 'external', 'https://example.com/external.js', array( 'one' ), null ); + wp_enqueue_script( 'three', '/default/three.js', array( 'external' ), null ); + + $print_scripts = get_echo( + static function () { + wp_print_scripts(); + _print_scripts(); + } + ); + + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + + $this->assertEqualHTML( $expected, $print_scripts ); + } + /** * @ticket 55628 *