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
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
- Scaling
- Scales with input
- Instructions
- 7–55
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 79.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–10 | none |
!$id && isset($id) | 32–34 | array_keys(), array_diff() |
!$id && isset($id) && !$dependency_ids && !->sort_item_dependencies() | 37–38 | array_keys(), array_diff(), ->sort_item_dependencies() |
!$id && isset($id) | 54–55 | array_keys(), array_diff(), __(), wp_get_list_item_separator(), sprintf(), _doing_it_wrong() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 79 | 7–55 | 10 | |
| 8.5 | 79 | 7–55 | 10 | |
| 8.4 | 79 | 7–55 | 10 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 91 | 10–64 | 10 | |
| 8.2 | 91 | 10–64 | 10 | |
| 8.1 | 91 | 10–64 | 10 | |
| 7.4 | 91 | 10–64 | 10 |
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
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- wp_get_list_item_separator()Retrieves the list item separator based on the locale.
- WP_Script_Modules::sort_item_dependencies()Recursively sorts the dependencies for a single script module identifier.
Used by · 2
- WP_Script_Modules::get_sorted_dependencies()Sorts the given script module identifiers based on their dependencies.
- WP_Script_Modules::sort_item_dependencies()Recursively sorts the dependencies for a single script module identifier.
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.
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.