wppaste
WordPress

register_block_script_module_id( array $metadata, string $field_name, int $index = 0 ): string|false

Since
6.5.0
Source
wp-includes/blocks.php:169
Finds a script module ID for the selected block metadata field.

Description

Detects when a path to a file was provided and optionally finds a corresponding asset file with details necessary to register the script module with an automatically generated module ID. It returns the unprocessed script module ID otherwise.

Compatibility

WordPress
since 6.5.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.

Parameters

$metadataarray
Block metadata.
$field_namestring
Field name to pick from metadata.
$indexintoptional
Index of the script module ID to register when multiple items passed. Default 0.Default: 0

Return value

string|false
Script module ID or false on failure.

Performance profile

How much work a call to register_block_script_module_id() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–124

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

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 register_block_script_module_id() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–12none
!empty($metadata[$field_name]) && $module_id === false16–20remove_block_asset_path_prefix()
!empty($metadata[$field_name]) && $module_id !== false85–119remove_block_asset_path_prefix(), substr_replace(), generate_block_asset_handle(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_script_module()
!empty($metadata[$field_name]) && $module_id !== false96–124remove_block_asset_path_prefix(), substr_replace(), generate_block_asset_handle(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_interactivity(), ->add_client_navigation_support_to_script_module(), wp_register_script_module()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1276–123161 fewer instruction than PHP 8.5
8.51286–12416
8.41286–124162 fewer instructions than PHP 8.3
8.31306–12616
8.21306–12616
8.11306–12616
7.41306–12616

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 · 7

Used by · 1

Source code

function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {	if ( empty( $metadata[ $field_name ] ) ) {		return false;	} 	$module_id = $metadata[ $field_name ];	if ( is_array( $module_id ) ) {		if ( empty( $module_id[ $index ] ) ) {			return false;		}		$module_id = $module_id[ $index ];	} 	$module_path = remove_block_asset_path_prefix( $module_id );	if ( $module_id === $module_path ) {		return $module_id;	} 	$path                  = dirname( $metadata['file'] );	$module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );	$module_id             = generate_block_asset_handle( $metadata['name'], $field_name, $index );	$module_asset_path     = wp_normalize_path(		realpath( $module_asset_raw_path )	); 	$module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );	$module_uri       = get_block_asset_url( $module_path_norm ); 	/** @var array{ dependencies?: list<non-falsy-string|array{id: non-falsy-string, import?: 'static'|'dynamic'}>, version?: string|false|null, ... } $module_asset */	$module_asset        = ! empty( $module_asset_path ) ? require $module_asset_path : array();	$module_dependencies = $module_asset['dependencies'] ?? array();	$block_version       = $metadata['version'] ?? false;	$module_version      = $module_asset['version'] ?? $block_version; 	$supports_interactivity_true = isset( $metadata['supports']['interactivity'] ) && true === $metadata['supports']['interactivity'];	$is_interactive              = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['interactive'] ) && true === $metadata['supports']['interactivity']['interactive'] );	$supports_client_navigation  = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['clientNavigation'] ) && true === $metadata['supports']['interactivity']['clientNavigation'] ); 	$args = array(); 	// Blocks using the Interactivity API are server-side rendered, so they are	// by design not in the critical rendering path and should be deprioritized.	if ( $is_interactive ) {		$args['fetchpriority'] = 'low';		$args['in_footer']     = true;	} 	// Blocks using the Interactivity API that support client-side navigation	// must be marked as such in their script modules.	if ( $is_interactive && $supports_client_navigation ) {		wp_interactivity()->add_client_navigation_support_to_script_module( $module_id );	} 	wp_register_script_module(		$module_id,		$module_uri,		$module_dependencies,		$module_version,		$args	); 	return $module_id;}

Changelog

Introduced in 6.5.0. Unchanged from 6.7.7 through 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.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks.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.