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
20 changes: 19 additions & 1 deletion src/wp-includes/class-wp-dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ class WP_Dependencies {
*/
private $dependencies_with_missing_dependencies = array();

/**
* Handles whose dependencies are currently being resolved by all_deps(), used to detect circular dependencies.
*
* @since 7.3.0
* @var array<string, true>
*/
private $handles_being_resolved = array();

/**
* Processes the items and dependencies.
*
Expand Down Expand Up @@ -179,11 +187,13 @@ public function do_item( $handle, $group = false ) {
* Determines dependencies.
*
* Recursively builds an array of items to process taking
* dependencies into account. Does NOT catch infinite loops.
* dependencies into account. Circular dependencies are tolerated: a handle that
* appears again in its own dependency chain is treated as already resolved.
*
* @since 2.1.0
* @since 2.6.0 Moved from `WP_Scripts`.
* @since 2.8.0 Added the `$group` parameter.
* @since 7.3.0 Circular dependencies no longer cause infinite recursion.
*
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
* @param bool $recursion Optional. Internal flag that function is calling itself.
Expand All @@ -207,13 +217,19 @@ public function all_deps( $handles, $recursion = false, $group = false ) {
continue;
}

if ( isset( $this->handles_being_resolved[ $handle ] ) ) { // Circular dependency, resolved further up the stack.
continue;
}

$moved = $this->set_group( $handle, $recursion, $group );
$new_group = $this->groups[ $handle ];

if ( $queued && ! $moved ) { // Already queued and in the right group.
continue;
}

$this->handles_being_resolved[ $handle ] = true;

$keep_going = true;
$missing_dependencies = array();
if ( isset( $this->registered[ $handle ] ) && count( $this->registered[ $handle ]->deps ) > 0 ) {
Expand All @@ -235,6 +251,8 @@ public function all_deps( $handles, $recursion = false, $group = false ) {
$keep_going = false; // Item requires dependencies that don't exist.
}

unset( $this->handles_being_resolved[ $handle ] );

if ( ! $keep_going ) { // Either item or its dependencies don't exist.
if ( $recursion ) {
return false; // Abort this branch.
Expand Down
84 changes: 84 additions & 0 deletions tests/phpunit/tests/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,88 @@ public function test_get_etag_styles( $load, $hash_source_string, $expected ) {

$this->assertSame( $expected, $result, "Expected MD hash: $expected for $hash_source_string, but got: $result." );
}

/**
* Tests that a handle depending on itself does not recurse infinitely.
*
* @ticket 46089
*
* @covers WP_Dependencies::all_deps
*/
public function test_all_deps_handles_self_dependency() {
$dependencies = new WP_Dependencies();
$dependencies->add( 'one', '', array( 'one' ) );

$this->assertTrue( $dependencies->all_deps( 'one' ) );
$this->assertSame( array( 'one' ), $dependencies->to_do );
}

/**
* Tests that two handles depending on each other do not recurse infinitely.
*
* @ticket 46089
*
* @covers WP_Dependencies::all_deps
*/
public function test_all_deps_handles_circular_dependency() {
$dependencies = new WP_Dependencies();
$dependencies->add( 'one', '', array( 'two' ) );
$dependencies->add( 'two', '', array( 'one' ) );

$this->assertTrue( $dependencies->all_deps( 'one' ) );
$this->assertSame( array( 'two', 'one' ), $dependencies->to_do );
}

/**
* Tests that a circular dependency spanning several handles does not recurse infinitely.
*
* @ticket 46089
*
* @covers WP_Dependencies::all_deps
*/
public function test_all_deps_handles_indirect_circular_dependency() {
$dependencies = new WP_Dependencies();
$dependencies->add( 'one', '', array( 'two' ) );
$dependencies->add( 'two', '', array( 'three' ) );
$dependencies->add( 'three', '', array( 'one' ) );

$this->assertTrue( $dependencies->all_deps( 'one' ) );
$this->assertSame( array( 'three', 'two', 'one' ), $dependencies->to_do );
}

/**
* Tests that a dependency shared by several handles is still resolved once.
*
* @ticket 46089
*
* @covers WP_Dependencies::all_deps
*/
public function test_all_deps_still_resolves_shared_dependencies() {
$dependencies = new WP_Dependencies();
$dependencies->add( 'one', '', array( 'two', 'three' ) );
$dependencies->add( 'two', '', array( 'three' ) );
$dependencies->add( 'three', '' );

$this->assertTrue( $dependencies->all_deps( 'one' ) );
$this->assertSame( array( 'three', 'two', 'one' ), $dependencies->to_do );
}

/**
* Tests that a circular dependency with a missing dependency terminates without queueing the handle.
*
* @ticket 46089
*
* @covers WP_Dependencies::all_deps
*/
public function test_all_deps_circular_dependency_with_missing_dependency_fails() {
$this->setExpectedIncorrectUsage( 'WP_Dependencies::add' );

$dependencies = new WP_Dependencies();
$dependencies->add( 'one', '', array( 'two' ) );
$dependencies->add( 'two', '', array( 'one', 'missing' ) );

$dependencies->all_deps( 'one' );

$this->assertSame( array(), $dependencies->to_do );
}
}
36 changes: 36 additions & 0 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2774,6 +2774,42 @@ public function test_wp_set_script_translations() {
$this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}

/**
* Tests that setting translations on `wp-i18n` does not make it depend on itself recursively.
*
* @ticket 46089
*
* @covers ::wp_set_script_translations
*/
public function test_wp_set_script_translations_on_wp_i18n_does_not_recurse_infinitely() {
wp_register_script( 'wp-i18n', '/wp-i18n.js', array(), null );
wp_set_script_translations( 'wp-i18n' );
wp_enqueue_script( 'wp-i18n' );

$output = get_echo( 'wp_print_scripts' );

$this->assertSame( 1, substr_count( $output, '<script id="wp-i18n-js" src="/wp-i18n.js"></script>' ) );
}

/**
* Tests that a dependency cycle created through `wp-i18n` does not recurse infinitely.
*
* @ticket 46089
*
* @covers ::wp_set_script_translations
*/
public function test_wp_set_script_translations_with_dependency_cycle_through_wp_i18n() {
wp_register_script( 'wp-hooks', '/wp-hooks.js', array(), null );
wp_register_script( 'wp-i18n', '/wp-i18n.js', array( 'wp-hooks' ), null );
wp_set_script_translations( 'wp-hooks' );
wp_enqueue_script( 'wp-hooks' );

$output = get_echo( 'wp_print_scripts' );

$this->assertSame( 1, substr_count( $output, '<script id="wp-hooks-js" src="/wp-hooks.js"></script>' ) );
$this->assertSame( 1, substr_count( $output, '<script id="wp-i18n-js" src="/wp-i18n.js"></script>' ) );
}

/**
* @ticket 63944
*/
Expand Down
Loading