wppaste
WordPress

WP_Script_Modules::sort_item_dependencies( string $id, string[] $import_types, array $sorted ): bool

Since
6.9.0
Source
wp-includes/class-wp-script-modules.php:894
Recursively sorts the dependencies for a single script module identifier.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$idstring
The identifier of the script module to sort.
$import_typesstring[]
Import types of dependencies to retrieve: 'static', 'dynamic', or both.
$sortedarray

Return value

bool
True on success, false on failure (e.g., missing dependency).

Performance profile

How much work a call to WP_Script_Modules::sort_item_dependencies() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
7–55

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 79.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Script_Modules::sort_item_dependencies()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Script_Modules::sort_item_dependencies() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7–10none
!$id && isset($id)32–34array_keys(), array_diff()
!$id && isset($id) && !$dependency_ids && !->sort_item_dependencies()37–38array_keys(), array_diff(), ->sort_item_dependencies()
!$id && isset($id)54–55array_keys(), array_diff(), __(), wp_get_list_item_separator(), sprintf(), _doing_it_wrong()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev797–5510
8.5797–5510
8.4797–551012 fewer instructions than PHP 8.3
8.39110–6410
8.29110–6410
8.19110–6410
7.49110–6410

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

Used by · 2

Source code

	private function sort_item_dependencies( string $id, array $import_types, array &$sorted ): bool {		// If already processed, don't do it again.		if ( in_array( $id, $sorted, true ) ) {			return true;		} 		// If the item doesn't exist, fail.		if ( ! isset( $this->registered[ $id ] ) ) {			return false;		} 		$dependency_ids = array();		foreach ( $this->registered[ $id ]['dependencies'] as $dependency ) {			if ( in_array( $dependency['import'], $import_types, true ) ) {				$dependency_ids[] = $dependency['id'];			}		} 		// If the item requires dependencies that do not exist, fail.		$missing_dependencies = array_diff( $dependency_ids, array_keys( $this->registered ) );		if ( count( $missing_dependencies ) > 0 ) {			if ( ! in_array( $id, $this->modules_with_missing_dependencies, true ) ) {				_doing_it_wrong(					get_class( $this ) . '::register',					sprintf(						/* translators: 1: Script module ID, 2: List of missing dependency IDs. */						__( 'The script module with the ID "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),						$id,						implode( wp_get_list_item_separator(), $missing_dependencies )					),					'6.9.1'				);				$this->modules_with_missing_dependencies[] = $id;			} 			return false;		} 		// Recursively process dependencies.		foreach ( $dependency_ids as $dependency_id ) {			if ( ! $this->sort_item_dependencies( $dependency_id, $import_types, $sorted ) ) {				// A dependency failed to resolve, so this branch fails.				return false;			}		} 		// All dependencies are sorted, so we can now add the current item.		$sorted[] = $id; 		return true;	}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

Signature, return type and hooks compared across 3 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-script-modules.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.