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

	 * The relationship between whether to override the defaults
	 * and whether the defaults are enabled is inverse:
	 *
	 * - If defaults are enabled  => theme presets should not be overridden
	 * - If defaults are disabled => theme presets should be overridden
	 *
	 * For example, a theme sets defaultPalette to false,
	 * making the default palette hidden from the user.
	 * In that case, we want all the theme presets to be present,
	 * so they should override the defaults.
	 */
	if ( is_array( $override ) ) {
		$value = _wp_array_get( $theme_json, array_merge( $path, $override ) );
		if ( isset( $value ) ) {
			return ! $value;
		}

		// Search the top-level key if none was found for this node.
		$value = _wp_array_get( $theme_json, array_merge( array( 'settings' ), $override ) );
		if ( isset( $value ) ) {
			return ! $value;
		}

		return true;
	}
}

/**
 * Returns the default slugs for all the presets in an associative array
 * whose keys are the preset paths and the leafs is the list of slugs.
 *
 * For example:
 *
 *     array(
 *       'color' => array(
 *         'palette'   => array( 'slug-1', 'slug-2' ),
 *         'gradients' => array( 'slug-3', 'slug-4' ),
 *       ),
 *     )
 *
 * @since 5.9.0
 *
 * @param array $data      A theme.json like structure.
 * @param array $node_path The path to inspect. It's 'settings' by default.
 * @return array
 */
protected static function get_default_slugs( $data, $node_path ) {
	$slugs = array();

	foreach ( static::PRESETS_METADATA as $metadata ) {
		$path = $node_path;
		foreach ( $metadata['path'] as $leaf ) {
			$path[] = $leaf;
		}
		$path[] = 'default';

		$preset = _wp_array_get( $data, $path, null );
		if ( ! isset( $preset ) ) {
			continue;
		}

		$slugs_for_preset = array();
		foreach ( $preset as $item ) {
			if ( isset( $item['slug'] ) ) {
				$slugs_for_preset[] = $item['slug'];
			}
		}

		_wp_array_set( $slugs, $metadata['path'], $slugs_for_preset );
	}

	return $slugs;
}

/**
 * Gets a `default`'s preset name by a provided slug.
 *
 * @since 5.9.0
 *
 * @param string $slug The slug we want to find a match from default presets.
 * @param array  $base_path The path to inspect. It's 'settings' by default.
 * @return string|null
 */
protected function get_name_from_defaults( $slug, $base_path ) {
	$path            = $base_path;
	$path[]          = 'default';

Changelog

VersionDescription
5.9.0Introduced.

User Contributed Notes

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