WP_Theme_JSON::sanitize()protected staticWP 5.8.0

Sanitizes the input according to the schemas.

{} Это метод класса: WP_Theme_JSON{}

Хуков нет.

Возвращает

Массив. The sanitized output.

Использование

$result = WP_Theme_JSON::sanitize( $input, $valid_block_names, $valid_element_names );
$input(массив) (обязательный)
Structure to sanitize.
$valid_block_names(массив) (обязательный)
List of valid block names.
$valid_element_names(массив) (обязательный)
List of valid element names.

Список изменений

С версии 5.8.0 Введена.
С версии 5.9.0 Added the $valid_block_names and $valid_element_name parameters.

Код WP_Theme_JSON::sanitize() WP 6.0.2

protected static function sanitize( $input, $valid_block_names, $valid_element_names ) {
	$output = array();

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

	$output = array_intersect_key( $input, array_flip( static::VALID_TOP_LEVEL_KEYS ) );

	// Some styles are only meant to be available at the top-level (e.g.: blockGap),
	// hence, the schema for blocks & elements should not have them.
	$styles_non_top_level = static::VALID_STYLES;
	foreach ( array_keys( $styles_non_top_level ) as $section ) {
		foreach ( array_keys( $styles_non_top_level[ $section ] ) as $prop ) {
			if ( 'top' === $styles_non_top_level[ $section ][ $prop ] ) {
				unset( $styles_non_top_level[ $section ][ $prop ] );
			}
		}
	}

	// Build the schema based on valid block & element names.
	$schema                 = array();
	$schema_styles_elements = array();
	foreach ( $valid_element_names as $element ) {
		$schema_styles_elements[ $element ] = $styles_non_top_level;
	}
	$schema_styles_blocks   = array();
	$schema_settings_blocks = array();
	foreach ( $valid_block_names as $block ) {
		$schema_settings_blocks[ $block ]           = static::VALID_SETTINGS;
		$schema_styles_blocks[ $block ]             = $styles_non_top_level;
		$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
	}
	$schema['styles']             = static::VALID_STYLES;
	$schema['styles']['blocks']   = $schema_styles_blocks;
	$schema['styles']['elements'] = $schema_styles_elements;
	$schema['settings']           = static::VALID_SETTINGS;
	$schema['settings']['blocks'] = $schema_settings_blocks;

	// Remove anything that's not present in the schema.
	foreach ( array( 'styles', 'settings' ) as $subtree ) {
		if ( ! isset( $input[ $subtree ] ) ) {
			continue;
		}

		if ( ! is_array( $input[ $subtree ] ) ) {
			unset( $output[ $subtree ] );
			continue;
		}

		$result = static::remove_keys_not_in_schema( $input[ $subtree ], $schema[ $subtree ] );

		if ( empty( $result ) ) {
			unset( $output[ $subtree ] );
		} else {
			$output[ $subtree ] = $result;
		}
	}

	return $output;
}