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

 */
public function __construct( $theme_json = array(), $origin = 'theme' ) {
	if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
		$origin = 'theme';
	}

	$this->theme_json    = WP_Theme_JSON_Schema::migrate( $theme_json );
	$valid_block_names   = array_keys( static::get_blocks_metadata() );
	$valid_element_names = array_keys( static::ELEMENTS );
	$valid_variations    = array();
	foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
		if ( ! isset( $block_meta['styleVariations'] ) ) {
			continue;
		}
		$valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
	}
	$theme_json       = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
	$this->theme_json = static::maybe_opt_in_into_settings( $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 ) );
				}
			}
		}
	}
}

/**
 * Enables some opt-in settings if theme declared support.
 *
 * @since 5.9.0
 *
 * @param array $theme_json A theme.json structure to modify.
 * @return array The modified theme.json structure.
 */
protected static function maybe_opt_in_into_settings( $theme_json ) {
	$new_theme_json = $theme_json;

	if (
		isset( $new_theme_json['settings']['appearanceTools'] ) &&
		true === $new_theme_json['settings']['appearanceTools']
	) {
		static::do_opt_in_into_settings( $new_theme_json['settings'] );
	}

	if ( isset( $new_theme_json['settings']['blocks'] ) && is_array( $new_theme_json['settings']['blocks'] ) ) {
		foreach ( $new_theme_json['settings']['blocks'] as &$block ) {
			if ( isset( $block['appearanceTools'] ) && ( true === $block['appearanceTools'] ) ) {
				static::do_opt_in_into_settings( $block );
			}
		}
	}

	return $new_theme_json;
}

/**
 * Enables some settings.
 *
 * @since 5.9.0
 *
 * @param array $context The context to which the settings belong.
 */
protected static function do_opt_in_into_settings( &$context ) {
	foreach ( static::APPEARANCE_TOOLS_OPT_INS as $path ) {
		/*
		 * Use "unset prop" as a marker instead of "null" because
		 * "null" can be a valid value for some props (e.g. blockGap).
		 */
		if ( 'unset prop' === _wp_array_get( $context, $path, 'unset prop' ) ) {
			_wp_array_set( $context, $path, true );
		}
	}

	unset( $context['appearanceTools'] );
}

/**
 * Sanitizes the input according to the schemas.
 *
 * @since 5.8.0
 * @since 5.9.0 Added the `$valid_block_names` and `$valid_element_name` parameters.
 * @since 6.3.0 Added the `$valid_variations` parameter.
 *
 * @param array $input               Structure to sanitize.
 * @param array $valid_block_names   List of valid block names.
 * @param array $valid_element_names List of valid element names.
 * @param array $valid_variations    List of valid variations per block.
 * @return array The sanitized output.
 */
protected static function sanitize( $input, $valid_block_names, $valid_element_names, $valid_variations ) {

	$output = array();

	if ( ! is_array( $input ) ) {
		return $output;
	}

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.