WP_Theme_JSON::remove_keys_not_in_schema( array $tree, array $schema ): array

Given a tree, removes the keys that are not present in the schema.


Description

It is recursive and modifies the input in-place.


Top ↑

Parameters

$tree array Required
Input to process.
$schema array Required
Schema to adhere to.

Top ↑

Return

array The modified $tree.


Top ↑

Source

File: wp-includes/class-wp-theme-json.php. View all references

protected static function remove_keys_not_in_schema( $tree, $schema ) {
	$tree = array_intersect_key( $tree, $schema );

	foreach ( $schema as $key => $data ) {
		if ( ! isset( $tree[ $key ] ) ) {
			continue;
		}

		if ( is_array( $schema[ $key ] ) && is_array( $tree[ $key ] ) ) {
			$tree[ $key ] = static::remove_keys_not_in_schema( $tree[ $key ], $schema[ $key ] );

			if ( empty( $tree[ $key ] ) ) {
				unset( $tree[ $key ] );
			}
		} elseif ( is_array( $schema[ $key ] ) && ! is_array( $tree[ $key ] ) ) {
			unset( $tree[ $key ] );
		}
	}

	return $tree;
}

Top ↑

Changelog

Changelog
Version Description
5.8.0 Introduced.

Top ↑

User Contributed Notes

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