wppaste
WordPress

WP_Script_Modules::get_import_map(): array<string,

Since
6.5.0, 7.0.0
Source
wp-includes/class-wp-script-modules.php:644
Returns the import map array.

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 imports key 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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
28–42

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

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::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.

WhenInstructionsCalls it makes
!($wp_scripts instanceof)28–29array_merge(), ->get_dependencies(), array_keys(), array_merge(), array_unique()
$wp_scripts instanceof && empty($handles)41–42array_merge(), array_merge(), ->get_dependencies(), array_keys(), array_merge(), array_unique()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12628–4219
8.512628–4219
8.412628–42193 fewer instructions than PHP 8.3
8.312928–4219
8.212928–4219
8.112928–4219
7.412928–4219

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

Used by · 2

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

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

7.0.4
Return type changed from array to array<string,.verified against source
7.0.0
Script module dependencies ('module_dependencies') of classic scripts are now included.from the docblock
6.5.0
Introduced.from the docblock

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.