WordPress at Your Fingertips
function is not described

WP_Theme_JSON::sanitize() private WP 5.8.0

Sanitizes the input according to the schemas.

{} It's a method of the class: WP_Theme_JSON{}

No Hooks.

Return

Array. The sanitized output.

Usage

$result = WP_Theme_JSON::sanitize( $input );
$input(array) (required)
Structure to sanitize.

Changelog

Since 5.8.0 Introduced.

Code of WP_Theme_JSON::sanitize() WP 5.8.2

private static function sanitize( $input ) {
	$output = array();

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

	$allowed_top_level_keys = self::ALLOWED_TOP_LEVEL_KEYS;
	$allowed_settings       = self::ALLOWED_SETTINGS;
	$allowed_styles         = self::ALLOWED_STYLES;
	$allowed_blocks         = array_keys( self::get_blocks_metadata() );
	$allowed_elements       = array_keys( self::ELEMENTS );

	$output = array_intersect_key( $input, array_flip( $allowed_top_level_keys ) );

	// Build the schema.
	$schema                 = array();
	$schema_styles_elements = array();
	foreach ( $allowed_elements as $element ) {
		$schema_styles_elements[ $element ] = $allowed_styles;
	}
	$schema_styles_blocks   = array();
	$schema_settings_blocks = array();
	foreach ( $allowed_blocks as $block ) {
		$schema_settings_blocks[ $block ]           = $allowed_settings;
		$schema_styles_blocks[ $block ]             = $allowed_styles;
		$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
	}
	$schema['styles']             = $allowed_styles;
	$schema['styles']['blocks']   = $schema_styles_blocks;
	$schema['styles']['elements'] = $schema_styles_elements;
	$schema['settings']           = $allowed_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 = self::remove_keys_not_in_schema( $input[ $subtree ], $schema[ $subtree ] );

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

	return $output;
}