Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

WP_Theme_JSON::sanitize( array $input )

Sanitizes the input according to the schemas.


Parameters Parameters

$input

(array) (Required) Structure to sanitize.


Top ↑

Return Return

(array) The sanitized output.


Top ↑

Source Source

File: wp-includes/class-wp-theme-json.php

	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;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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