wppaste
WordPress

WP_Theme_JSON::__construct( array $theme_json = array('version' => self::LATEST_SCHEMA), string $origin = 'theme' )

Since
5.8.0, 6.6.0
Source
wp-includes/class-wp-theme-json.php:770
Constructor.

Compatibility

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

$theme_jsonarrayoptional
A structure that follows the theme.json schema.Default: array('version' => self::LATEST_SCHEMA)
$originstringoptional
What source of data this object represents.
One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.Default: 'theme'

Performance profile

How much work a call to WP_Theme_JSON::__construct() 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 what you pass in.

Instructions
88–136

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
14

14 places 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 WP_Theme_JSON::__construct() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$spacing_scale === null && !isset($spacing_scale)88–90::WP_Theme_JSON_Schema(), ::get_blocks_metadata(), array_keys(), array_keys(), ::get_valid_block_style_variations(), ::unwrap_shared_block_style_variations(), ::sanitize(), ::maybe_opt_in_into_settings(), ::get_setting_nodes(), _wp_array_get(), _wp_array_get()
$spacing_scale !== null && !isset($spacing_scale)97–107::WP_Theme_JSON_Schema(), ::get_blocks_metadata(), array_keys(), array_keys(), ::get_valid_block_style_variations(), ::unwrap_shared_block_style_variations(), ::sanitize(), ::maybe_opt_in_into_settings(), ::get_setting_nodes(), _wp_array_get(), array_keys(), array_intersect(), _wp_array_get()
$spacing_scale === null && isset($spacing_scale)117–119::WP_Theme_JSON_Schema(), ::get_blocks_metadata(), array_keys(), array_keys(), ::get_valid_block_style_variations(), ::unwrap_shared_block_style_variations(), ::sanitize(), ::maybe_opt_in_into_settings(), ::get_setting_nodes(), _wp_array_get(), _wp_array_get(), _wp_array_get(), ::compute_spacing_sizes(), ::merge_spacing_sizes(), _wp_array_set()
$spacing_scale !== null && isset($spacing_scale)126–136::WP_Theme_JSON_Schema(), ::get_blocks_metadata(), array_keys(), array_keys(), ::get_valid_block_style_variations(), ::unwrap_shared_block_style_variations(), ::sanitize(), ::maybe_opt_in_into_settings(), ::get_setting_nodes(), _wp_array_get(), array_keys(), array_intersect(), _wp_array_get(), _wp_array_get(), ::compute_spacing_sizes(), ::merge_spacing_sizes(), _wp_array_set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17388–13613
8.517388–13613
8.417388–136133 fewer instructions than PHP 8.3
8.317691–13913
8.217691–13913
8.117691–13913
7.417691–13913

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

  • _wp_array_get()Accesses an array in depth based on a path of keys.
  • _wp_array_set()Sets an array in depth based on a path of keys.
  • WP_Theme_JSON_Schema::migrate()Function that migrates a given theme.json structure to the last version.
  • static::get_blocks_metadata()
  • static::get_valid_block_style_variations()
  • static::unwrap_shared_block_style_variations()
  • static::sanitize()
  • static::maybe_opt_in_into_settings()
  • static::get_setting_nodes()
  • static::compute_spacing_sizes()
  • static::merge_spacing_sizes()

Used by · 14

Show all 14

Source code

	public function __construct( $theme_json = array( 'version' => self::LATEST_SCHEMA ), $origin = 'theme' ) {		if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {			$origin = 'theme';		} 		$this->theme_json    = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );		$blocks_metadata     = static::get_blocks_metadata();		$valid_block_names   = array_keys( $blocks_metadata );		$valid_element_names = array_keys( static::ELEMENTS );		$valid_variations    = static::get_valid_block_style_variations( $blocks_metadata );		$this->theme_json    = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations );		$this->theme_json    = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );		$this->theme_json    = static::maybe_opt_in_into_settings( $this->theme_json ); 		// Internally, presets are keyed by origin.		$nodes = static::get_setting_nodes( $this->theme_json );		foreach ( $nodes as $node ) {			foreach ( static::PRESETS_METADATA as $preset_metadata ) {				$path = $node['path'];				foreach ( $preset_metadata['path'] as $subpath ) {					$path[] = $subpath;				}				$preset = _wp_array_get( $this->theme_json, $path, null );				if ( null !== $preset ) {					// If the preset is not already keyed by origin.					if ( isset( $preset[0] ) || empty( $preset ) ) {						_wp_array_set( $this->theme_json, $path, array( $origin => $preset ) );					}				}			}		} 		// In addition to presets, spacingScale (which generates presets) is also keyed by origin.		$scale_path    = array( 'settings', 'spacing', 'spacingScale' );		$spacing_scale = _wp_array_get( $this->theme_json, $scale_path, null );		if ( null !== $spacing_scale ) {			// If the spacingScale is not already keyed by origin.			if ( empty( array_intersect( array_keys( $spacing_scale ), static::VALID_ORIGINS ) ) ) {				_wp_array_set( $this->theme_json, $scale_path, array( $origin => $spacing_scale ) );			}		} 		// Pre-generate the spacingSizes from spacingScale.		$scale_path    = array( 'settings', 'spacing', 'spacingScale', $origin );		$spacing_scale = _wp_array_get( $this->theme_json, $scale_path, null );		if ( isset( $spacing_scale ) ) {			$sizes_path           = array( 'settings', 'spacing', 'spacingSizes', $origin );			$spacing_sizes        = _wp_array_get( $this->theme_json, $sizes_path, array() );			$spacing_scale_sizes  = static::compute_spacing_sizes( $spacing_scale );			$merged_spacing_sizes = static::merge_spacing_sizes( $spacing_scale_sizes, $spacing_sizes );			_wp_array_set( $this->theme_json, $sizes_path, $merged_spacing_sizes );		}	}

Changelog

Introduced in 5.8.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.

6.6.0
Key spacingScale by origin, and Pre-generate the spacingSizes from spacingScale.
Added unwrapping of shared block style variations into block type variations if registered.from the docblock
5.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-theme-json.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.