Skip to content

Commit 999b258

Browse files
committed
Move to 6.2 class.
1 parent 2e646a7 commit 999b258

5 files changed

+135
-139
lines changed

lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php

+99
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,106 @@
1616
* @access private
1717
*/
1818
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+
}
1935

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+
}
20119
/**
21120
* Determines whether the active theme has a theme.json file.
22121
*

lib/compat/wordpress-6.2/get-global-styles-and-settings.php

+36
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,39 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) {
227227
$settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings();
228228
return _wp_array_get( $settings, $path, $settings );
229229
}
230+
231+
/**
232+
* Repeated logic from `get_default_block_editor_settings` function. When implemented into core,
233+
* remove logic from `get_default_block_editor_settings` and simple call this function instead.
234+
*
235+
* @return array
236+
*/
237+
function gutenberg_get_block_theme_supports() {
238+
$theme_settings = array(
239+
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
240+
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
241+
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
242+
'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ),
243+
'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
244+
'enableCustomSpacing' => get_theme_support( 'custom-spacing' ),
245+
'enableCustomUnits' => get_theme_support( 'custom-units' ),
246+
);
247+
248+
// Theme settings.
249+
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
250+
if ( false !== $color_palette ) {
251+
$theme_settings['colors'] = $color_palette;
252+
}
253+
254+
$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
255+
if ( false !== $font_sizes ) {
256+
$theme_settings['fontSizes'] = $font_sizes;
257+
}
258+
259+
$gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
260+
if ( false !== $gradient_presets ) {
261+
$theme_settings['gradients'] = $gradient_presets;
262+
}
263+
264+
return $theme_settings;
265+
}

lib/experimental/class-wp-theme-json-resolver-gutenberg.php

-96
Original file line numberDiff line numberDiff line change
@@ -16,102 +16,6 @@
1616
* @access private
1717
*/
1818
class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_2 {
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-
}
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::$theme instanceof WP_Theme_JSON_Gutenberg ) {
38-
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
39-
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
40-
$theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data );
41-
42-
/**
43-
* Filters the data provided by the theme for global styles & settings.
44-
*
45-
* @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data.
46-
*/
47-
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) );
48-
$theme_json_data = $theme_json->get_data();
49-
static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );
50-
51-
if ( wp_get_theme()->parent() ) {
52-
// Get parent theme.json.
53-
$parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
54-
$parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
55-
$parent_theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $parent_theme_json_data );
56-
$parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data );
57-
58-
// Merge the child theme.json into the parent theme.json.
59-
// The child theme takes precedence over the parent.
60-
$parent_theme->merge( static::$theme );
61-
static::$theme = $parent_theme;
62-
}
63-
}
64-
65-
if ( ! $settings['with_supports'] ) {
66-
return static::$theme;
67-
}
68-
69-
/*
70-
* We want the presets and settings declared in theme.json
71-
* to override the ones declared via theme supports.
72-
* So we take theme supports, transform it to theme.json shape
73-
* and merge the static::$theme upon that.
74-
*/
75-
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_block_theme_supports() );
76-
if ( ! wp_theme_has_theme_json() ) {
77-
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
78-
$theme_support_data['settings']['color'] = array();
79-
}
80-
81-
$default_palette = false;
82-
if ( current_theme_supports( 'default-color-palette' ) ) {
83-
$default_palette = true;
84-
}
85-
if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) {
86-
// If the theme does not have any palette, we still want to show the core one.
87-
$default_palette = true;
88-
}
89-
$theme_support_data['settings']['color']['defaultPalette'] = $default_palette;
90-
91-
$default_gradients = false;
92-
if ( current_theme_supports( 'default-gradient-presets' ) ) {
93-
$default_gradients = true;
94-
}
95-
if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) {
96-
// If the theme does not have any gradients, we still want to show the core ones.
97-
$default_gradients = true;
98-
}
99-
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;
100-
101-
// Classic themes without a theme.json don't support global duotone.
102-
$theme_support_data['settings']['color']['defaultDuotone'] = false;
103-
104-
// Allow themes to enable appearance tools via theme_support.
105-
if ( current_theme_supports( 'appearance-tools' ) ) {
106-
$theme_support_data['settings']['appearanceTools'] = true;
107-
}
108-
}
109-
$with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data );
110-
$with_theme_supports->merge( static::$theme );
111-
112-
return $with_theme_supports;
113-
}
114-
11519
/**
11620
* Gets the styles for blocks from the block.json file.
11721
*

lib/experimental/get-global-styles-and-settings.php

-42
This file was deleted.

lib/load.php

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ function gutenberg_is_experiment_enabled( $name ) {
115115
if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
116116
require __DIR__ . '/experimental/html/index.php';
117117
}
118-
require __DIR__ . '/experimental/get-global-styles-and-settings.php';
119118
require __DIR__ . '/experimental/class-wp-theme-json-gutenberg.php';
120119
require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php';
121120
require __DIR__ . '/experimental/class-wp-webfonts.php';

0 commit comments

Comments
 (0)