WP_Theme_JSON::remove_insecure_properties( array $theme_json ): array

In this article

Removes insecure data from theme.json.

Parameters

$theme_jsonarrayrequired
Structure to sanitize.

Return

array Sanitized structure.

Source

	}

	return $new_node;
}

/**
 * Removes insecure data from theme.json.
 *
 * @since 5.9.0
 * @since 6.3.2 Preserves global styles block variations when securing styles.
 *
 * @param array $theme_json Structure to sanitize.
 * @return array Sanitized structure.
 */
public static function remove_insecure_properties( $theme_json ) {
	$sanitized = array();

	$theme_json = WP_Theme_JSON_Schema::migrate( $theme_json );

	$valid_block_names   = array_keys( static::get_blocks_metadata() );
	$valid_element_names = array_keys( static::ELEMENTS );
	$valid_variations    = array();
	foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
		if ( ! isset( $block_meta['styleVariations'] ) ) {
			continue;
		}
		$valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
	}

	$theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );

	$blocks_metadata = static::get_blocks_metadata();
	$style_nodes     = static::get_style_nodes( $theme_json, $blocks_metadata );

	foreach ( $style_nodes as $metadata ) {
		$input = _wp_array_get( $theme_json, $metadata['path'], array() );
		if ( empty( $input ) ) {
			continue;
		}

		// The global styles custom CSS is not sanitized, but can only be edited by users with 'edit_css' capability.
		if ( isset( $input['css'] ) && current_user_can( 'edit_css' ) ) {
			$output = $input;
		} else {
			$output = static::remove_insecure_styles( $input );
		}

		/*
		 * Get a reference to element name from path.
		 * $metadata['path'] = array( 'styles', 'elements', 'link' );
		 */
		$current_element = $metadata['path'][ count( $metadata['path'] ) - 1 ];

		/*
		 * $output is stripped of pseudo selectors. Re-add and process them
		 * or insecure styles here.
		 */
		if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ] ) ) {
			foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ] as $pseudo_selector ) {
				if ( isset( $input[ $pseudo_selector ] ) ) {
					$output[ $pseudo_selector ] = static::remove_insecure_styles( $input[ $pseudo_selector ] );
				}
			}
		}

		if ( ! empty( $output ) ) {
			_wp_array_set( $sanitized, $metadata['path'], $output );
		}

		if ( isset( $metadata['variations'] ) ) {
			foreach ( $metadata['variations'] as $variation ) {
				$variation_input = _wp_array_get( $theme_json, $variation['path'], array() );
				if ( empty( $variation_input ) ) {
					continue;
				}

				$variation_output = static::remove_insecure_styles( $variation_input );
				if ( ! empty( $variation_output ) ) {
					_wp_array_set( $sanitized, $variation['path'], $variation_output );
				}
			}
		}
	}

	$setting_nodes = static::get_setting_nodes( $theme_json );
	foreach ( $setting_nodes as $metadata ) {

Changelog

VersionDescription
5.9.0Introduced.

User Contributed Notes

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