wppaste
WordPress

WP_Script_Modules::print_script_module_data()

Since
6.7.0
Source
wp-includes/class-wp-script-modules.php:1016
Print data associated with Script Modules.

Description

The data will be embedded in the page HTML and can be read by Script Modules on page load.

Compatibility

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

Performance profile

How much work a call to WP_Script_Modules::print_script_module_data() 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
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

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

Instructions
21–24

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

Plugin surface
1 hook

Third-party callbacks on 'script_module_data_{$module_id}' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below WP_Script_Modules::print_script_module_data()

Further down the call graph this can also reach cache, serialize, query 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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always21–24array_unique(), ->get_import_map(), array_keys(), array_keys()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 66 instructions, 21–24 executed per call, 11 branches. The work does not change between versions.

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.

Hooks and filters fired · 1

One hook fires while WP_Script_Modules::print_script_module_data() runs, in this order:

  1. apply_filters( script_module_data_{$module_id} )filterline 1080 (+64 into the body)

    Filters data associated with a given Script Module.

Uses · 5

Source code

	public function print_script_module_data(): void {		$modules = array();		foreach ( array_unique( $this->queue ) as $id ) {			if ( '@wordpress/a11y' === $id ) {				$this->a11y_available = true;			}			$modules[ $id ] = true;		}		foreach ( array_keys( $this->get_import_map()['imports'] ) as $id ) {			if ( '@wordpress/a11y' === $id ) {				$this->a11y_available = true;			}			$modules[ $id ] = true;		} 		foreach ( array_keys( $modules ) as $module_id ) {			/**			 * Filters data associated with a given Script Module.			 *			 * Script Modules may require data that is required for initialization or is essential			 * to have immediately available on page load. These are suitable use cases for			 * this data.			 *			 * The dynamic portion of the hook name, `$module_id`, refers to the Script Module ID			 * that the data is associated with.			 *			 * This is best suited to pass essential data that must be available to the module for			 * initialization or immediately on page load. It does not replace the REST API or			 * fetching data from the client.			 *			 * Example:			 *			 *     add_filter(			 *         'script_module_data_MyScriptModuleID',			 *         function ( array $data ): array {			 *             $data['dataForClient'] = 'ok';			 *             return $data;			 *         }			 *     );			 *			 * If the filter returns no data (an empty array), nothing will be embedded in the page.			 *			 * The data for a given Script Module, if provided, will be JSON serialized in a script			 * tag with an ID of the form `wp-script-module-data-{$module_id}`.			 *			 * The data can be read on the client with a pattern like the following; you are encouraged			 * to use this pattern _verbatim_ to avoid common pitfalls or vulnerabilities:			 *			 * Example:			 *			 *     const dataContainer = document.querySelector( 'script[id="wp-script-module-data-MyScriptModuleID"]' );			 *     let data = {};			 *     if ( dataContainer instanceof HTMLScriptElement ) {			 *         try {			 *             data = JSON.parse( dataContainer.text );			 *         } catch {}			 *     }			 *     // data.dataForClient === 'ok';			 *     initMyScriptModuleWithData( data );			 *			 * @since 6.7.0			 *			 * @param array $data The data associated with the Script Module.			 */			$data = apply_filters( "script_module_data_{$module_id}", array() ); 			if ( is_array( $data ) && array() !== $data ) {				/*				 * This data will be printed as JSON inside a script tag like this:				 *   <script type="application/json"></script>				 *				 * A script tag must be closed by a sequence beginning with `</`. It's impossible to				 * close a script tag without using `<`. We ensure that `<` is escaped and `/` can				 * remain unescaped, so `</script>` will be printed as `\u003C/script\u00E3`.				 *				 *   - JSON_HEX_TAG: All < and > are converted to \u003C and \u003E.				 *   - JSON_UNESCAPED_SLASHES: Don't escape /.				 *				 * If the page will use UTF-8 encoding, it's safe to print unescaped unicode:				 *

Changelog

Introduced in 6.7.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/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.