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

/**
 * Returns the current theme's wanted patterns(slugs) to be
 * registered from Pattern Directory.
 *
 * @since 6.0.0
 *
 * @return string[]
 */
public function get_patterns() {
	if ( isset( $this->theme_json['patterns'] ) && is_array( $this->theme_json['patterns'] ) ) {
		return $this->theme_json['patterns'];
	}
	return array();
}

/**
 * Returns a valid theme.json as provided by a theme.
 *
 * 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.
 *
 * @since 6.0.0
 *
 * @return array
 */
public function get_data() {
	$output = $this->theme_json;
	$nodes  = static::get_setting_nodes( $output );

	/**
	 * Flatten the theme & custom origins into a single one.
	 *
	 * For example, the following:
	 *
	 * {
	 *   "settings": {
	 *     "color": {
	 *       "palette": {
	 *         "theme": [ {} ],
	 *         "custom": [ {} ]
	 *       }
	 *     }
	 *   }
	 * }
	 *
	 * will be converted to:
	 *
	 * {
	 *   "settings": {
	 *     "color": {
	 *       "palette": [ {} ]
	 *     }
	 *   }
	 * }
	 */
	foreach ( $nodes as $node ) {
		foreach ( static::PRESETS_METADATA as $preset_metadata ) {
			$path = $node['path'];
			foreach ( $preset_metadata['path'] as $preset_metadata_path ) {
				$path[] = $preset_metadata_path;
			}
			$preset = _wp_array_get( $output, $path, null );
			if ( null === $preset ) {
				continue;
			}

			$items = array();
			if ( isset( $preset['theme'] ) ) {
				foreach ( $preset['theme'] as $item ) {
					$slug = $item['slug'];
					unset( $item['slug'] );
					$items[ $slug ] = $item;
				}
			}
			if ( isset( $preset['custom'] ) ) {
				foreach ( $preset['custom'] as $item ) {
					$slug = $item['slug'];
					unset( $item['slug'] );
					$items[ $slug ] = $item;
				}
			}
			$flattened_preset = array();
			foreach ( $items as $slug => $value ) {
				$flattened_preset[] = array_merge( array( 'slug' => (string) $slug ), $value );
			}
			_wp_array_set( $output, $path, $flattened_preset );
		}
	}

	/*
	 * If all of the static::APPEARANCE_TOOLS_OPT_INS are true,
	 * this code unsets them and sets 'appearanceTools' instead.
	 */
	foreach ( $nodes as $node ) {
		$all_opt_ins_are_set = true;
		foreach ( static::APPEARANCE_TOOLS_OPT_INS as $opt_in_path ) {
			$full_path = $node['path'];
			foreach ( $opt_in_path as $opt_in_path_item ) {
				$full_path[] = $opt_in_path_item;
			}
			/*
			 * Use "unset prop" as a marker instead of "null" because
			 * "null" can be a valid value for some props (e.g. blockGap).
			 */
			$opt_in_value = _wp_array_get( $output, $full_path, 'unset prop' );
			if ( 'unset prop' === $opt_in_value ) {
				$all_opt_ins_are_set = false;
				break;
			}
		}

		if ( $all_opt_ins_are_set ) {
			$node_path_with_appearance_tools   = $node['path'];
			$node_path_with_appearance_tools[] = 'appearanceTools';
			_wp_array_set( $output, $node_path_with_appearance_tools, true );
			foreach ( static::APPEARANCE_TOOLS_OPT_INS as $opt_in_path ) {
				$full_path = $node['path'];
				foreach ( $opt_in_path as $opt_in_path_item ) {
					$full_path[] = $opt_in_path_item;
				}
				/*
				 * Use "unset prop" as a marker instead of "null" because
				 * "null" can be a valid value for some props (e.g. blockGap).
				 */
				$opt_in_value = _wp_array_get( $output, $full_path, 'unset prop' );
				if ( true !== $opt_in_value ) {
					continue;
				}

				/*
				 * The following could be improved to be path independent.
				 * At the moment it relies on a couple of assumptions:
				 *
				 * - all opt-ins having a path of size 2.
				 * - there's two sources of settings: the top-level and the block-level.
				 */
				if (
					( 1 === count( $node['path'] ) ) &&
					( 'settings' === $node['path'][0] )
				) {

Changelog

VersionDescription
6.0.0Introduced.

User Contributed Notes

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