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

In this article

Constructor.

Parameters

$theme_jsonarrayoptional
A structure that follows the theme.json schema.

Default:array( => self::LATEST_SCHEMA)

$originstringoptional
What source of data this object represents.
One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.

Source

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

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

User Contributed Notes

You must log in before being able to contribute a note or feedback.