Skip to content
Open
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
32 changes: 30 additions & 2 deletions src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
}
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -501,6 +525,10 @@ public function do_item( $handle, $group = false ) {
echo $tag;
}

if ( $restore_concat_after_print ) {
$this->do_concat = true;
}

return true;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<script src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one&amp;ver={$wp_version}'></script>\n";
$expected .= "<script src='https://example.com/external.js' id='external-js'></script>\n";
$expected .= "<script src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=three&amp;ver={$wp_version}'></script>\n";

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

/**
* @ticket 55628
*
Expand Down
Loading