WP_Theme_JSON::sanitize( array $input, array $valid_block_names, array $valid_element_names, array $valid_variations ): array

In this article

Sanitizes the input according to the schemas.

Parameters

$inputarrayrequired
Structure to sanitize.
$valid_block_namesarrayrequired
List of valid block names.
$valid_element_namesarrayrequired
List of valid element names.
$valid_variationsarrayrequired
List of valid variations per block.

Return

array The sanitized output.

Source

}

/**
 * Options that settings.appearanceTools enables.
 *
 * @since 6.0.0
 * @since 6.2.0 Added `dimensions.minHeight` and `position.sticky`.
 * @since 6.4.0 Added `background.backgroundImage`.
 * @since 6.5.0 Added `background.backgroundSize` and `dimensions.aspectRatio`.
 * @var array
 */
const APPEARANCE_TOOLS_OPT_INS = array(
	array( 'background', 'backgroundImage' ),
	array( 'background', 'backgroundSize' ),
	array( 'border', 'color' ),
	array( 'border', 'radius' ),
	array( 'border', 'style' ),
	array( 'border', 'width' ),
	array( 'color', 'link' ),
	array( 'color', 'heading' ),
	array( 'color', 'button' ),
	array( 'color', 'caption' ),
	array( 'dimensions', 'aspectRatio' ),
	array( 'dimensions', 'minHeight' ),
	array( 'position', 'sticky' ),
	array( 'spacing', 'blockGap' ),
	array( 'spacing', 'margin' ),
	array( 'spacing', 'padding' ),
	array( 'typography', 'lineHeight' ),
);

/**
 * The latest version of the schema in use.
 *
 * @since 5.8.0
 * @since 5.9.0 Changed value from 1 to 2.
 * @since 6.6.0 Changed value from 2 to 3.
 * @var int
 */
const LATEST_SCHEMA = 3;

/**
 * Constructor.
 *
 * @since 5.8.0
 * @since 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.
 *
 * @param array  $theme_json A structure that follows the theme.json schema.
 * @param string $origin     Optional. What source of data this object represents.
 *                           One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
 */
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 );
	$valid_block_names   = array_keys( static::get_blocks_metadata() );
	$valid_element_names = array_keys( static::ELEMENTS );
	$valid_variations    = static::get_valid_block_style_variations();
	$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 );
	}
}

/**
 * Unwraps shared block style variations.
 *

Changelog

VersionDescription
6.3.0Added the $valid_variations parameter.
5.9.0Added the $valid_block_names and $valid_element_name parameters.
5.8.0Introduced.

User Contributed Notes

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