WP_Theme_JSON::get_data(): array

In this article

Returns a valid theme.json as provided by a theme.

Description

Unlike get_raw_data() this returns the presets flattened, as provided by a theme.
This also uses appearanceTools instead of their opt-ins if all of them are true.

Return

array

Source

 * @param array $origins List of origins to process.
 * @return string SVG filters.
 */
public function get_svg_filters( $origins ) {
	$blocks_metadata = static::get_blocks_metadata();
	$setting_nodes   = static::get_setting_nodes( $this->theme_json, $blocks_metadata );

	$filters = '';
	foreach ( $setting_nodes as $metadata ) {
		$node = _wp_array_get( $this->theme_json, $metadata['path'], array() );
		if ( empty( $node['color']['duotone'] ) ) {
			continue;
		}

		$duotone_presets = $node['color']['duotone'];

		foreach ( $origins as $origin ) {
			if ( ! isset( $duotone_presets[ $origin ] ) ) {
				continue;
			}
			foreach ( $duotone_presets[ $origin ] as $duotone_preset ) {
				$filters .= wp_get_duotone_filter_svg( $duotone_preset );
			}
		}
	}

	return $filters;
}

/**
 * Determines whether a presets should be overridden or not.
 *
 * @since 5.9.0
 * @deprecated 6.0.0 Use 'get_metadata_boolean' instead.
 *
 * @param array      $theme_json The theme.json like structure to inspect.
 * @param array      $path       Path to inspect.
 * @param bool|array $override   Data to compute whether to override the preset.
 * @return bool
 */
protected static function should_override_preset( $theme_json, $path, $override ) {
	_deprecated_function( __METHOD__, '6.0.0', 'get_metadata_boolean' );

	if ( is_bool( $override ) ) {
		return $override;
	}

	/*
	 * 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 leaves 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';
	$default_content = _wp_array_get( $this->theme_json, $path, null );
	if ( ! $default_content ) {
		return null;
	}
	foreach ( $default_content as $item ) {
		if ( $slug === $item['slug'] ) {

Changelog

VersionDescription
6.0.0Introduced.

User Contributed Notes

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