WP_Theme_JSON::get_data()publicWP 6.0.0

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.

Метод класса: WP_Theme_JSON{}

Хуков нет.

Возвращает

Массив.

Использование

$WP_Theme_JSON = new WP_Theme_JSON();
$WP_Theme_JSON->get_data();

Список изменений

С версии 6.0.0 Введена.

Код WP_Theme_JSON::get_data() WP 6.4.3

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] )
				) {
					// Top-level settings.
					unset( $output['settings'][ $opt_in_path[0] ][ $opt_in_path[1] ] );
					if ( empty( $output['settings'][ $opt_in_path[0] ] ) ) {
						unset( $output['settings'][ $opt_in_path[0] ] );
					}
				} elseif (
					( 3 === count( $node['path'] ) ) &&
					( 'settings' === $node['path'][0] ) &&
					( 'blocks' === $node['path'][1] )
				) {
					// Block-level settings.
					$block_name = $node['path'][2];
					unset( $output['settings']['blocks'][ $block_name ][ $opt_in_path[0] ][ $opt_in_path[1] ] );
					if ( empty( $output['settings']['blocks'][ $block_name ][ $opt_in_path[0] ] ) ) {
						unset( $output['settings']['blocks'][ $block_name ][ $opt_in_path[0] ] );
					}
				}
			}
		}
	}

	wp_recursive_ksort( $output );

	return $output;
}