WP_Theme_JSON_Resolver::get_theme_data()public staticWP 5.8.0

Returns the theme's data.

Data from theme.json will be backfilled from existing theme supports, if any. Note that if the same data is present in theme.json and in theme supports, the theme.json takes precedence.

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

Хуки из метода

Возвращает

WP_Theme_JSON. Entity that holds theme data.

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

$result = WP_Theme_JSON_Resolver::get_theme_data( $deprecated, $options );
$deprecated(массив)
Deprecated. Not used.
По умолчанию: array()
$options(массив)

Options arguments.

По умолчанию: array()

  • with_supports(true|false)
    Whether to include theme supports in the data.
    По умолчанию: true

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

С версии 5.8.0 Введена.
С версии 5.9.0 Theme supports have been inlined and the $theme_support_data argument removed.
С версии 6.0.0 Added an $options parameter to allow the theme data to be returned without theme supports.
С версии 6.6.0 Add support for 'default-font-sizes' and 'default-spacing-sizes' theme supports. Added registration and merging of block style variations from partial theme.json files and the block styles registry.

Код WP_Theme_JSON_Resolver::get_theme_data() WP 6.7.1

public static function get_theme_data( $deprecated = array(), $options = array() ) {
	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __METHOD__, '5.9.0' );
	}

	$options = wp_parse_args( $options, array( 'with_supports' => true ) );

	if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
		$wp_theme        = wp_get_theme();
		$theme_json_file = $wp_theme->get_file_path( 'theme.json' );
		if ( is_readable( $theme_json_file ) ) {
			$theme_json_data = static::read_json_file( $theme_json_file );
			$theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) );
		} else {
			$theme_json_data = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
		}

		/*
		 * Register variations defined by theme partials (theme.json files in the styles directory).
		 * This is required so the variations pass sanitization of theme.json data.
		 */
		$variations = static::get_style_variations( 'block' );
		wp_register_block_style_variations_from_theme_json_partials( $variations );

		/*
		 * Source variations from the block registry and block style variation files. Then, merge them into the existing theme.json data.
		 *
		 * In case the same style properties are defined in several sources, this is how we should resolve the values,
		 * from higher to lower priority:
		 *
		 * - styles.blocks.blockType.variations from theme.json
		 * - styles.variations from theme.json
		 * - variations from block style variation files
		 * - variations from block styles registry
		 *
		 * See test_add_registered_block_styles_to_theme_data and test_unwraps_block_style_variations.
		 *
		 */
		$theme_json_data = static::inject_variations_from_block_style_variation_files( $theme_json_data, $variations );
		$theme_json_data = static::inject_variations_from_block_styles_registry( $theme_json_data );

		/**
		 * Filters the data provided by the theme for global styles and settings.
		 *
		 * @since 6.1.0
		 *
		 * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
		 */
		$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );

		/*
		 * Backward compatibility for extenders returning a WP_Theme_JSON_Data
		 * compatible class that is not a WP_Theme_JSON_Data object.
		 */
		if ( $theme_json instanceof WP_Theme_JSON_Data ) {
			static::$theme = $theme_json->get_theme_json();
		} else {
			$config        = $theme_json->get_data();
			static::$theme = new WP_Theme_JSON( $config );
		}

		if ( $wp_theme->parent() ) {
			// Get parent theme.json.
			$parent_theme_json_file = $wp_theme->parent()->get_file_path( 'theme.json' );
			if ( $theme_json_file !== $parent_theme_json_file && is_readable( $parent_theme_json_file ) ) {
				$parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
				$parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) );
				$parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );

				/*
				 * Merge the child theme.json into the parent theme.json.
				 * The child theme takes precedence over the parent.
				 */
				$parent_theme->merge( static::$theme );
				static::$theme = $parent_theme;
			}
		}
	}

	if ( ! $options['with_supports'] ) {
		return static::$theme;
	}

	/*
	 * We want the presets and settings declared in theme.json
	 * to override the ones declared via theme supports.
	 * So we take theme supports, transform it to theme.json shape
	 * and merge the static::$theme upon that.
	 */
	$theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );
	if ( ! wp_theme_has_theme_json() ) {
		/*
		 * Unlike block themes, classic themes without a theme.json disable
		 * default presets when custom preset theme support is added. This
		 * behavior can be overridden by using the corresponding default
		 * preset theme support.
		 */
		$theme_support_data['settings']['color']['defaultPalette']        =
			! isset( $theme_support_data['settings']['color']['palette'] ) ||
			current_theme_supports( 'default-color-palette' );
		$theme_support_data['settings']['color']['defaultGradients']      =
			! isset( $theme_support_data['settings']['color']['gradients'] ) ||
			current_theme_supports( 'default-gradient-presets' );
		$theme_support_data['settings']['typography']['defaultFontSizes'] =
			! isset( $theme_support_data['settings']['typography']['fontSizes'] ) ||
			current_theme_supports( 'default-font-sizes' );
		$theme_support_data['settings']['spacing']['defaultSpacingSizes'] =
			! isset( $theme_support_data['settings']['spacing']['spacingSizes'] ) ||
			current_theme_supports( 'default-spacing-sizes' );

		/*
		 * Shadow presets are explicitly disabled for classic themes until a
		 * decision is made for whether the default presets should match the
		 * other presets or if they should be disabled by default in classic
		 * themes. See https://github.com/WordPress/gutenberg/issues/59989.
		 */
		$theme_support_data['settings']['shadow']['defaultPresets'] = false;

		// Allow themes to enable link color setting via theme_support.
		if ( current_theme_supports( 'link-color' ) ) {
			$theme_support_data['settings']['color']['link'] = true;
		}

		// Allow themes to enable all border settings via theme_support.
		if ( current_theme_supports( 'border' ) ) {
			$theme_support_data['settings']['border']['color']  = true;
			$theme_support_data['settings']['border']['radius'] = true;
			$theme_support_data['settings']['border']['style']  = true;
			$theme_support_data['settings']['border']['width']  = true;
		}

		// Allow themes to enable appearance tools via theme_support.
		if ( current_theme_supports( 'appearance-tools' ) ) {
			$theme_support_data['settings']['appearanceTools'] = true;
		}
	}
	$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
	$with_theme_supports->merge( static::$theme );
	return $with_theme_supports;
}