|
16 | 16 | * @access private
|
17 | 17 | */
|
18 | 18 | class WP_Theme_JSON_Resolver_6_2 extends WP_Theme_JSON_Resolver_6_1 {
|
| 19 | + /** |
| 20 | + * Returns the theme's data. |
| 21 | + * |
| 22 | + * Data from theme.json will be backfilled from existing |
| 23 | + * theme supports, if any. Note that if the same data |
| 24 | + * is present in theme.json and in theme supports, |
| 25 | + * the theme.json takes precedence. |
| 26 | + * |
| 27 | + * @param array $deprecated Deprecated argument. |
| 28 | + * @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data. |
| 29 | + * @return WP_Theme_JSON_Gutenberg Entity that holds theme data. |
| 30 | + */ |
| 31 | + public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) { |
| 32 | + if ( ! empty( $deprecated ) ) { |
| 33 | + _deprecated_argument( __METHOD__, '5.9' ); |
| 34 | + } |
19 | 35 |
|
| 36 | + // When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin. |
| 37 | + if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { |
| 38 | + $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); |
| 39 | + $wp_theme = wp_get_theme(); |
| 40 | + if ( '' !== $theme_json_file ) { |
| 41 | + $theme_json_data = static::read_json_file( $theme_json_file ); |
| 42 | + $theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) ); |
| 43 | + } else { |
| 44 | + $theme_json_data = array(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Filters the data provided by the theme for global styles and settings. |
| 49 | + * |
| 50 | + * @since 6.1.0 |
| 51 | + * |
| 52 | + * @param WP_Theme_JSON_Data Class to access and update the underlying data. |
| 53 | + */ |
| 54 | + $theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); |
| 55 | + $theme_json_data = $theme_json->get_data(); |
| 56 | + static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data ); |
| 57 | + |
| 58 | + if ( $wp_theme->parent() ) { |
| 59 | + // Get parent theme.json. |
| 60 | + $parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true ); |
| 61 | + if ( '' !== $parent_theme_json_file ) { |
| 62 | + $parent_theme_json_data = static::read_json_file( $parent_theme_json_file ); |
| 63 | + $parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) ); |
| 64 | + $parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data ); |
| 65 | + |
| 66 | + /* |
| 67 | + * Merge the child theme.json into the parent theme.json. |
| 68 | + * The child theme takes precedence over the parent. |
| 69 | + */ |
| 70 | + $parent_theme->merge( static::$theme ); |
| 71 | + static::$theme = $parent_theme; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if ( ! $settings['with_supports'] ) { |
| 77 | + return static::$theme; |
| 78 | + } |
| 79 | + |
| 80 | + /* |
| 81 | + * We want the presets and settings declared in theme.json |
| 82 | + * to override the ones declared via theme supports. |
| 83 | + * So we take theme supports, transform it to theme.json shape |
| 84 | + * and merge the static::$theme upon that. |
| 85 | + */ |
| 86 | + $theme_support_data = WP_Theme_JSON::get_from_editor_settings( gutenberg_get_block_theme_supports() ); |
| 87 | + if ( ! wp_theme_has_support() ) { |
| 88 | + if ( ! isset( $theme_support_data['settings']['color'] ) ) { |
| 89 | + $theme_support_data['settings']['color'] = array(); |
| 90 | + } |
| 91 | + |
| 92 | + $default_palette = false; |
| 93 | + if ( current_theme_supports( 'default-color-palette' ) ) { |
| 94 | + $default_palette = true; |
| 95 | + } |
| 96 | + if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) { |
| 97 | + // If the theme does not have any palette, we still want to show the core one. |
| 98 | + $default_palette = true; |
| 99 | + } |
| 100 | + $theme_support_data['settings']['color']['defaultPalette'] = $default_palette; |
| 101 | + |
| 102 | + $default_gradients = false; |
| 103 | + if ( current_theme_supports( 'default-gradient-presets' ) ) { |
| 104 | + $default_gradients = true; |
| 105 | + } |
| 106 | + if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) { |
| 107 | + // If the theme does not have any gradients, we still want to show the core ones. |
| 108 | + $default_gradients = true; |
| 109 | + } |
| 110 | + $theme_support_data['settings']['color']['defaultGradients'] = $default_gradients; |
| 111 | + |
| 112 | + // Classic themes without a theme.json don't support global duotone. |
| 113 | + $theme_support_data['settings']['color']['defaultDuotone'] = false; |
| 114 | + } |
| 115 | + $with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data ); |
| 116 | + $with_theme_supports->merge( static::$theme ); |
| 117 | + return $with_theme_supports; |
| 118 | + } |
20 | 119 | /**
|
21 | 120 | * Determines whether the active theme has a theme.json file.
|
22 | 121 | *
|
|
0 commit comments