wppaste
WordPress

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

Since
5.8.0, 5.9.0, 6.3.0, 6.6.0
Source
wp-includes/class-wp-theme-json.php:1276
Sanitizes the input according to the schemas.

Compatibility

WordPress
since 6.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$inputarray
Structure to sanitize.
$valid_block_namesarray
List of valid block names.
$valid_element_namesarray
List of valid element names.
$valid_variationsarray
List of valid variations per block.

Return value

array
The sanitized output.

Performance profile

How much work a call to WP_Theme_JSON::sanitize() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
7–75

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 315.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme_JSON::sanitize() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_array($input)7none
is_array($input)69–75array_flip(), array_intersect_key(), array_keys(), ::get_viewport_media_queries(), ::schema_in_root_and_per_origin()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 315 instructions, 7–75 executed per call, 55 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 5

  • static::get_viewport_media_queries()
  • static::schema_in_root_and_per_origin()
  • static::remove_keys_not_in_schema()
  • static::sanitize_viewport_settings()
  • static::resolve_custom_css_format()

Source code

	protected static function sanitize( $input, $valid_block_names, $valid_element_names, $valid_variations ) {		$output = array(); 		if ( ! is_array( $input ) ) {			return $output;		} 		// Preserve only the top most level keys.		$output = array_intersect_key( $input, array_flip( static::VALID_TOP_LEVEL_KEYS ) ); 		/*		 * Remove any rules that are annotated as "top" in VALID_STYLES constant.		 * 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 ) {			// array_key_exists() needs to be used instead of isset() because the value can be null.			if ( array_key_exists( $section, $styles_non_top_level ) && is_array( $styles_non_top_level[ $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();		$responsive_media_queries = static::get_viewport_media_queries( $input['settings']['viewport'] ?? null ); 		/*		 * Set allowed element pseudo selectors and responsive breakpoint states.		 * Target data structure in schema:		 * e.g.		 * - top level elements: `$schema['styles']['elements']['link'][':hover']`.		 * - block level elements: `$schema['styles']['blocks']['core/button']['elements']['link'][':hover']`.		 * - block responsive elements: `$schema['styles']['blocks']['core/button']['@tablet']['elements']['link'][':hover']`.		 */		foreach ( $valid_element_names as $element ) {			$schema_styles_elements[ $element ] = $styles_non_top_level; 			if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] ) ) {				foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {					$schema_styles_elements[ $element ][ $pseudo_selector ] = $styles_non_top_level;				}			} 			// Add responsive breakpoint states for elements.			foreach ( array_keys( $responsive_media_queries ) as $breakpoint_state ) {				$schema_styles_elements[ $element ][ $breakpoint_state ] = $styles_non_top_level;			}		} 		$schema_styles_blocks   = array();		$schema_settings_blocks = array(); 		/*		 * Generate a schema for blocks.		 * - Block styles can contain `elements`, `variations`, and responsive breakpoint state definitions.		 * - Variations definitions cannot be nested.		 * - Variations can contain styles for inner `blocks`, `elements`, and responsive breakpoint states.		 * - Variation inner `blocks` styles can contain `elements` and responsive breakpoint states.		 *		 * As each variation needs both a `blocks` schema and responsive `blocks` schemas		 * for further nested inner `blocks`, the overall schema is generated in multiple passes.		 */		foreach ( $valid_block_names as $block ) {			$schema_settings_blocks[ $block ] = static::VALID_SETTINGS;			// `viewport` and `blockVisibility` are global-only settings and cannot be set per block for now.			unset( $schema_settings_blocks[ $block ]['viewport'] );			unset( $schema_settings_blocks[ $block ]['blockVisibility'] );			$schema_styles_blocks[ $block ]             = $styles_non_top_level;			$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements; 			// Add responsive breakpoint states for all blocks.			foreach ( array_keys( $responsive_media_queries ) as $breakpoint_state ) {				$schema_styles_blocks[ $block ][ $breakpoint_state ]             = $styles_non_top_level;				$schema_styles_blocks[ $block ][ $breakpoint_state ]['elements'] = $schema_styles_elements;

Changelog

Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.6.0
Updated schema to allow extended block style variations.from the docblock
6.3.0
Added the $valid_variations parameter.from the docblock
5.9.0
Added the $valid_block_names and $valid_element_name parameters.from the docblock
5.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-theme-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.