diff options
Diffstat (limited to 'wp-includes/class-wp-theme-json-resolver.php')
-rw-r--r-- | wp-includes/class-wp-theme-json-resolver.php | 345 |
1 files changed, 297 insertions, 48 deletions
diff --git a/wp-includes/class-wp-theme-json-resolver.php b/wp-includes/class-wp-theme-json-resolver.php index 3ad5ecf..5caaf0b 100644 --- a/wp-includes/class-wp-theme-json-resolver.php +++ b/wp-includes/class-wp-theme-json-resolver.php @@ -172,9 +172,18 @@ class WP_Theme_JSON_Resolver { * * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ - $theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) ); - $config = $theme_json->get_data(); - static::$core = new WP_Theme_JSON( $config, 'default' ); + $theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) ); + + /* + * 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::$core = $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + static::$core = new WP_Theme_JSON( $config, 'default' ); + } return static::$core; } @@ -221,6 +230,8 @@ class WP_Theme_JSON_Resolver { * @since 5.8.0 * @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed. * @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports. + * @since 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. * * @param array $deprecated Deprecated. Not used. * @param array $options { @@ -244,9 +255,33 @@ class WP_Theme_JSON_Resolver { $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(); + $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. * @@ -254,9 +289,18 @@ class WP_Theme_JSON_Resolver { * * @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' ) ); - $theme_json_data = $theme_json->get_data(); - static::$theme = new WP_Theme_JSON( $theme_json_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. @@ -288,33 +332,25 @@ class WP_Theme_JSON_Resolver { */ $theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() ); if ( ! wp_theme_has_theme_json() ) { - if ( ! isset( $theme_support_data['settings']['color'] ) ) { - $theme_support_data['settings']['color'] = array(); - } - - $default_palette = false; - if ( current_theme_supports( 'default-color-palette' ) ) { - $default_palette = true; - } - if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) { - // If the theme does not have any palette, we still want to show the core one. - $default_palette = true; - } - $theme_support_data['settings']['color']['defaultPalette'] = $default_palette; - - $default_gradients = false; - if ( current_theme_supports( 'default-gradient-presets' ) ) { - $default_gradients = true; - } - if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) { - // If the theme does not have any gradients, we still want to show the core ones. - $default_gradients = true; - } - $theme_support_data['settings']['color']['defaultGradients'] = $default_gradients; + /* + * 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' ); - if ( ! isset( $theme_support_data['settings']['shadow'] ) ) { - $theme_support_data['settings']['shadow'] = array(); - } /* * Shadow presets are explicitly disabled for classic themes until a * decision is made for whether the default presets should match the @@ -361,7 +397,7 @@ class WP_Theme_JSON_Resolver { return static::$blocks; } - $config = array( 'version' => 2 ); + $config = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA ); foreach ( $blocks as $block_name => $block_type ) { if ( isset( $block_type->supports['__experimentalStyle'] ) ) { $config['styles']['blocks'][ $block_name ] = static::remove_json_comments( $block_type->supports['__experimentalStyle'] ); @@ -387,9 +423,18 @@ class WP_Theme_JSON_Resolver { * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) ); - $config = $theme_json->get_data(); - static::$blocks = new WP_Theme_JSON( $config, 'blocks' ); + /* + * 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::$blocks = $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + static::$blocks = new WP_Theme_JSON( $config, 'blocks' ); + } + return static::$blocks; } @@ -498,6 +543,8 @@ class WP_Theme_JSON_Resolver { * Returns the user's origin config. * * @since 5.9.0 + * @since 6.6.0 The 'isGlobalStylesUserThemeJSON' flag is left on the user data. + * Register the block style variations coming from the user data. * * @return WP_Theme_JSON Entity that holds styles for user data. */ @@ -514,7 +561,7 @@ class WP_Theme_JSON_Resolver { $json_decoding_error = json_last_error(); if ( JSON_ERROR_NONE !== $json_decoding_error ) { - trigger_error( 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() ); + wp_trigger_error( __METHOD__, 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() ); /** * Filters the data provided by the user for global styles & settings. * @@ -523,8 +570,17 @@ class WP_Theme_JSON_Resolver { * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. */ $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); - $config = $theme_json->get_data(); - return new WP_Theme_JSON( $config, 'custom' ); + + /* + * 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 ) { + return $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + return new WP_Theme_JSON( $config, 'custom' ); + } } /* @@ -542,9 +598,18 @@ class WP_Theme_JSON_Resolver { } /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */ - $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); - $config = $theme_json->get_data(); - static::$user = new WP_Theme_JSON( $config, 'custom' ); + $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); + + /* + * 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::$user = $theme_json->get_theme_json(); + } else { + $config = $theme_json->get_data(); + static::$user = new WP_Theme_JSON( $config, 'custom' ); + } return static::$user; } @@ -592,7 +657,6 @@ class WP_Theme_JSON_Resolver { $result = new WP_Theme_JSON(); $result->merge( static::get_core_data() ); if ( 'default' === $origin ) { - $result->set_spacing_sizes(); return $result; } @@ -603,12 +667,10 @@ class WP_Theme_JSON_Resolver { $result->merge( static::get_theme_data() ); if ( 'theme' === $origin ) { - $result->set_spacing_sizes(); return $result; } $result->merge( static::get_user_data() ); - $result->set_spacing_sizes(); return $result; } @@ -707,16 +769,45 @@ class WP_Theme_JSON_Resolver { return $nested_json_files; } + /** + * Determines if a supplied style variation matches the provided scope. + * + * For backwards compatibility, if a variation does not define any scope + * related property, e.g. `blockTypes`, it is assumed to be a theme style + * variation. + * + * @since 6.6.0 + * + * @param array $variation Theme.json shaped style variation object. + * @param string $scope Scope to check e.g. theme, block etc. + * @return boolean + */ + private static function style_variation_has_scope( $variation, $scope ) { + if ( 'block' === $scope ) { + return isset( $variation['blockTypes'] ); + } + + if ( 'theme' === $scope ) { + return ! isset( $variation['blockTypes'] ); + } + + return false; + } /** * Returns the style variations defined by the theme. * * @since 6.0.0 * @since 6.2.0 Returns parent theme variations if theme is a child. + * @since 6.6.0 Added configurable scope parameter to allow filtering + * theme.json partial files by the scope to which they + * can be applied e.g. theme vs block etc. + * Added basic caching for read theme.json partial files. * + * @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. * @return array */ - public static function get_style_variations() { + public static function get_style_variations( $scope = 'theme' ) { $variation_files = array(); $variations = array(); $base_directory = get_stylesheet_directory() . '/styles'; @@ -738,8 +829,8 @@ class WP_Theme_JSON_Resolver { } ksort( $variation_files ); foreach ( $variation_files as $path => $file ) { - $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); - if ( is_array( $decoded_file ) ) { + $decoded_file = self::read_json_file( $path ); + if ( is_array( $decoded_file ) && static::style_variation_has_scope( $decoded_file, $scope ) ) { $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); $variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data(); if ( empty( $variation['title'] ) ) { @@ -750,4 +841,162 @@ class WP_Theme_JSON_Resolver { } return $variations; } + + /** + * Resolves relative paths in theme.json styles to theme absolute paths + * and returns them in an array that can be embedded + * as the value of `_link` object in REST API responses. + * + * @since 6.6.0 + * + * @param WP_Theme_JSON $theme_json A theme json instance. + * @return array An array of resolved paths. + */ + public static function get_resolved_theme_uris( $theme_json ) { + $resolved_theme_uris = array(); + + if ( ! $theme_json instanceof WP_Theme_JSON ) { + return $resolved_theme_uris; + } + + $theme_json_data = $theme_json->get_raw_data(); + + // Top level styles. + $background_image_url = isset( $theme_json_data['styles']['background']['backgroundImage']['url'] ) ? $theme_json_data['styles']['background']['backgroundImage']['url'] : null; + + /* + * The same file convention when registering web fonts. + * See: WP_Font_Face_Resolver::to_theme_file_uri. + */ + $placeholder = 'file:./'; + if ( + isset( $background_image_url ) && + is_string( $background_image_url ) && + // Skip if the src doesn't start with the placeholder, as there's nothing to replace. + str_starts_with( $background_image_url, $placeholder ) + ) { + $file_type = wp_check_filetype( $background_image_url ); + $src_url = str_replace( $placeholder, '', $background_image_url ); + $resolved_theme_uri = array( + 'name' => $background_image_url, + 'href' => sanitize_url( get_theme_file_uri( $src_url ) ), + 'target' => 'styles.background.backgroundImage.url', + ); + if ( isset( $file_type['type'] ) ) { + $resolved_theme_uri['type'] = $file_type['type']; + } + $resolved_theme_uris[] = $resolved_theme_uri; + } + + return $resolved_theme_uris; + } + + /** + * Resolves relative paths in theme.json styles to theme absolute paths + * and merges them with incoming theme JSON. + * + * @since 6.6.0 + * + * @param WP_Theme_JSON $theme_json A theme json instance. + * @return WP_Theme_JSON Theme merged with resolved paths, if any found. + */ + public static function resolve_theme_file_uris( $theme_json ) { + $resolved_urls = static::get_resolved_theme_uris( $theme_json ); + if ( empty( $resolved_urls ) ) { + return $theme_json; + } + + $resolved_theme_json_data = array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + ); + + foreach ( $resolved_urls as $resolved_url ) { + $path = explode( '.', $resolved_url['target'] ); + _wp_array_set( $resolved_theme_json_data, $path, $resolved_url['href'] ); + } + + $theme_json->merge( new WP_Theme_JSON( $resolved_theme_json_data ) ); + + return $theme_json; + } + + /** + * Adds variations sourced from block style variations files to the supplied theme.json data. + * + * @since 6.6.0 + * + * @param array $data Array following the theme.json specification. + * @param array $variations Shared block style variations. + * @return array Theme json data including shared block style variation definitions. + */ + private static function inject_variations_from_block_style_variation_files( $data, $variations ) { + if ( empty( $variations ) ) { + return $data; + } + + foreach ( $variations as $variation ) { + if ( empty( $variation['styles'] ) || empty( $variation['blockTypes'] ) ) { + continue; + } + + $variation_name = $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ); + + foreach ( $variation['blockTypes'] as $block_type ) { + // First, override partial styles with any top-level styles. + $top_level_data = $data['styles']['variations'][ $variation_name ] ?? array(); + if ( ! empty( $top_level_data ) ) { + $variation['styles'] = array_replace_recursive( $variation['styles'], $top_level_data ); + } + + // Then, override styles so far with any block-level styles. + $block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array(); + if ( ! empty( $block_level_data ) ) { + $variation['styles'] = array_replace_recursive( $variation['styles'], $block_level_data ); + } + + $path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name ); + _wp_array_set( $data, $path, $variation['styles'] ); + } + } + + return $data; + } + + /** + * Adds variations sourced from the block styles registry to the supplied theme.json data. + * + * @since 6.6.0 + * + * @param array $data Array following the theme.json specification. + * @return array Theme json data including shared block style variation definitions. + */ + private static function inject_variations_from_block_styles_registry( $data ) { + $registry = WP_Block_Styles_Registry::get_instance(); + $styles = $registry->get_all_registered(); + + foreach ( $styles as $block_type => $variations ) { + foreach ( $variations as $variation_name => $variation ) { + if ( empty( $variation['style_data'] ) ) { + continue; + } + + // First, override registry styles with any top-level styles. + $top_level_data = $data['styles']['variations'][ $variation_name ] ?? array(); + if ( ! empty( $top_level_data ) ) { + $variation['style_data'] = array_replace_recursive( $variation['style_data'], $top_level_data ); + } + + // Then, override styles so far with any block-level styles. + $block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array(); + if ( ! empty( $block_level_data ) ) { + $variation['style_data'] = array_replace_recursive( $variation['style_data'], $block_level_data ); + } + + $path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name ); + _wp_array_set( $data, $path, $variation['style_data'] ); + } + } + + return $data; + } } |