WP_Theme_JSON::sanitize() WordPress Method
The WP_Theme_JSON::sanitize() method is used to sanitize a theme's JSON file. This is done by removing any invalid characters from the JSON file. This method is important because it ensures that the JSON file is valid and can be correctly parsed by the Wordpress system.
WP_Theme_JSON::sanitize( array $input, array $valid_block_names, array $valid_element_names ) #
Sanitizes the input according to the schemas.
Contents
Parameters
- $input
(array)(Required)Structure to sanitize.
- $valid_block_names
(array)(Required)List of valid block names.
- $valid_element_names
(array)(Required)List of valid element names.
Return
(array) The sanitized output.
Source
File: wp-includes/class-wp-theme-json.php
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; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
5.9.0 | Added the $valid_block_names and $valid_element_name parameters. |
5.8.0 | Introduced. |