WP_Script_Modules::get_import_map(): array<string,
- Since
- 6.5.0, 7.0.0
- Source
wp-includes/class-wp-script-modules.php:644
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
array<string,- array<string, string>> Array with an
importskey mapping to an array of script module identifiers and their respective URLs, including the version query.
Performance profile
How much work a call to WP_Script_Modules::get_import_map() 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
- 28–42
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 126.
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::get_import_map()
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 · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Script_Modules::get_import_map() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!($wp_scripts instanceof) | 28–29 | array_merge(), ->get_dependencies(), array_keys(), array_merge(), array_unique() |
$wp_scripts instanceof && empty($handles) | 41–42 | array_merge(), array_merge(), ->get_dependencies(), array_keys(), array_merge(), array_unique() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 126 | 28–42 | 19 | |
| 8.5 | 126 | 28–42 | 19 | |
| 8.4 | 126 | 28–42 | 19 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 129 | 28–42 | 19 | |
| 8.2 | 129 | 28–42 | 19 | |
| 8.1 | 129 | 28–42 | 19 | |
| 7.4 | 129 | 28–42 | 19 |
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 · 5
- _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::get_dependencies()Retrieves all the dependencies for the given script module identifiers, filtered by import types.
- WP_Script_Modules::get_src()Gets the versioned URL for a script module src.
Used by · 2
- WP_Script_Modules::print_import_map()Prints the import map using a script tag with a type="importmap" attribute.
- WP_Script_Modules::print_script_module_data()Print data associated with Script Modules.
Source code
private function get_import_map(): array { global $wp_scripts; $imports = array(); // Identify script modules that are dependencies of classic scripts. $classic_script_module_dependencies = array(); if ( $wp_scripts instanceof WP_Scripts ) { $handles = array_merge( $wp_scripts->queue, $wp_scripts->to_do, $wp_scripts->done ); $processed = array(); while ( ! empty( $handles ) ) { $handle = array_pop( $handles ); if ( isset( $processed[ $handle ] ) || ! isset( $wp_scripts->registered[ $handle ] ) ) { continue; } $processed[ $handle ] = true; $module_dependencies = $wp_scripts->get_data( $handle, 'module_dependencies' ); if ( is_array( $module_dependencies ) ) { $missing_module_dependencies = array(); foreach ( $module_dependencies as $module ) { if ( is_string( $module ) ) { $id = $module; } elseif ( is_array( $module ) && isset( $module['id'] ) && is_string( $module['id'] ) ) { $id = $module['id']; } else { // Invalid module dependency was supplied by direct manipulation of the extra data. // Normally, this error scenario would be caught when WP_Scripts::add_data() is called. continue; } if ( ! isset( $this->registered[ $id ] ) ) { $missing_module_dependencies[] = $id; } else { $classic_script_module_dependencies[] = $id; } } if ( count( $missing_module_dependencies ) > 0 ) { _doing_it_wrong( 'WP_Scripts::add_data', sprintf( /* translators: 1: Script handle, 2: 'module_dependencies', 3: List of missing dependency IDs. */ __( 'The script with the handle "%1$s" was enqueued with script module dependencies ("%2$s") that are not registered: %3$s.' ), $handle, 'module_dependencies', implode( wp_get_list_item_separator(), $missing_module_dependencies ) ), '7.0.0' ); } } foreach ( $wp_scripts->registered[ $handle ]->deps as $dep ) { if ( ! isset( $processed[ $dep ] ) ) { $handles[] = $dep; } } } } // Note: the script modules in $this->queue are not included in the importmap because they get printed as scripts. $ids = array_unique( array_merge( $classic_script_module_dependencies, array_keys( $this->get_dependencies( array_merge( $this->queue, $classic_script_module_dependencies ) ) ) ) ); foreach ( $ids as $id ) { $src = $this->get_src( $id ); if ( '' !== $src ) { $imports[ $id ] = $src; } } return array( 'imports' => $imports );Changelog
Introduced in 6.5.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
array to array<string,.verified against sourceAbout 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.