WP_Theme_JSON::remove_keys_not_in_schema()
Given a tree, removes the keys that are not present in the schema.
It is recursive and modifies the input in-place.
Method of the class: WP_Theme_JSON{}
No Hooks.
Return
Array
. The modified $tree.
Usage
$result = WP_Theme_JSON::remove_keys_not_in_schema( $tree, $schema );
- $tree(array) (required)
- Input to process.
- $schema(array) (required)
- Schema to adhere to.
Changelog
Since 5.8.0 | Introduced. |
WP_Theme_JSON::remove_keys_not_in_schema() WP Theme JSON::remove keys not in schema code WP 6.6.2
protected static function remove_keys_not_in_schema( $tree, $schema ) { if ( ! is_array( $tree ) ) { return $tree; } foreach ( $tree as $key => $value ) { // Remove keys not in the schema or with null/empty values. if ( ! array_key_exists( $key, $schema ) ) { unset( $tree[ $key ] ); continue; } if ( is_array( $schema[ $key ] ) ) { if ( ! is_array( $value ) ) { unset( $tree[ $key ] ); } elseif ( wp_is_numeric_array( $value ) ) { // If indexed, process each item in the array. foreach ( $value as $item_key => $item_value ) { if ( isset( $schema[ $key ][0] ) && is_array( $schema[ $key ][0] ) ) { $tree[ $key ][ $item_key ] = self::remove_keys_not_in_schema( $item_value, $schema[ $key ][0] ); } else { // If the schema does not define a further structure, keep the value as is. $tree[ $key ][ $item_key ] = $item_value; } } } else { // If associative, process as a single object. $tree[ $key ] = self::remove_keys_not_in_schema( $value, $schema[ $key ] ); if ( empty( $tree[ $key ] ) ) { unset( $tree[ $key ] ); } } } } return $tree; }