WP_Theme_JSON::remove_keys_not_in_schema() WordPress Method

The WP_Theme_JSON::remove_keys_not_in_schema() function is used to remove any extra keys that are not specified in the schema for the current theme. This is useful for making sure that only the data that is needed is stored in the database.

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

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) Returns the modified $tree.


Top ↑

Source

File: wp-includes/class-wp-theme-json.php

	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
VersionDescription
5.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.