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:755
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
- Scaling
- Scales with input
- Instructions
- 88–136
- Plugin surface
- None
- Called by
- 14
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 173.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 173 | 88–136 | 13 | |
| 8.5 | 173 | 88–136 | 13 | |
| 8.4 | 173 | 88–136 | 13 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 176 | 91–139 | 13 | |
| 8.2 | 176 | 91–139 | 13 | |
| 8.1 | 176 | 91–139 | 13 | |
| 7.4 | 176 | 91–139 | 13 |
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
- WP_REST_Global_Styles_Controller::get_theme_items()Returns the given theme global styles variations.
- WP_REST_Global_Styles_Controller::prepare_item_for_response()Prepare a global styles config output for response.
- WP_REST_Global_Styles_Revisions_Controller::prepare_item_for_response()Prepares the revision for the REST response.
- WP_Theme_JSON_Data::__construct()Constructor.
- WP_Theme_JSON_Data::update_with()Updates the theme.json with the the given data.
- WP_Theme_JSON_Resolver::get_block_data()Gets the styles for blocks from the block.json file.
- WP_Theme_JSON_Resolver::get_core_data()Returns core's origin config.
- WP_Theme_JSON_Resolver::get_merged_data()Returns the data merged from multiple origins.
- WP_Theme_JSON_Resolver::get_style_variations()Returns the style variations defined by the theme.
- WP_Theme_JSON_Resolver::get_theme_data()Returns the theme's data.
- WP_Theme_JSON_Resolver::get_user_data()Returns the user's origin config.
- WP_Theme_JSON_Resolver::resolve_theme_file_uris()Resolves relative paths in theme.json styles to theme absolute paths and merges them with incoming theme JSON.
Show all 14
- _wp_add_block_level_preset_styles()Render the block level presets stylesheet.
- wp_render_block_style_variation_support_styles()Renders the block style variation's styles.
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.
Signature, return type and hooks compared across 5 parsed releases.
Added unwrapping of shared block style variations into block type variations if registered.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.