From 9e82a0bf01c0f0afcf549dd2d12654b9878e8d11 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 25 Nov 2022 18:28:38 +0000 Subject: [PATCH 01/12] Remove usage of get_default_block_editor_settings`. --- ...class-wp-theme-json-resolver-gutenberg.php | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 2f1ee93db30b53..ca871eb008784e 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -65,6 +65,32 @@ public static function get_theme_data( $deprecated = array(), $settings = array( if ( ! $settings['with_supports'] ) { return static::$theme; } + + $editor_settings = array( + 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), + 'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ), + 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), + 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), + 'enableCustomUnits' => get_theme_support( 'custom-units' ), + ); + + // Theme settings. + $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); + if ( false !== $color_palette ) { + $editor_settings['colors'] = $color_palette; + } + + $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); + if ( false !== $font_sizes ) { + $editor_settings['fontSizes'] = $font_sizes; + } + + $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); + if ( false !== $gradient_presets ) { + $editor_settings['gradients'] = $gradient_presets; + } /* * We want the presets and settings declared in theme.json @@ -72,7 +98,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array( * So we take theme supports, transform it to theme.json shape * and merge the static::$theme upon that. */ - $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_default_block_editor_settings() ); + $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( $editor_settings ); if ( ! wp_theme_has_theme_json() ) { if ( ! isset( $theme_support_data['settings']['color'] ) ) { $theme_support_data['settings']['color'] = array(); From b0a97645995b92f06ea22c413c2b2a1800cc8f3f Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 29 Nov 2022 12:46:19 +0000 Subject: [PATCH 02/12] Remove to it's own function. --- ...class-wp-theme-json-resolver-gutenberg.php | 28 +------------- .../get-global-styles-and-settings.php | 38 +++++++++++++++++++ lib/load.php | 1 + 3 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 lib/experimental/get-global-styles-and-settings.php diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index ca871eb008784e..da1ac16edb16ce 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -65,32 +65,6 @@ public static function get_theme_data( $deprecated = array(), $settings = array( if ( ! $settings['with_supports'] ) { return static::$theme; } - - $editor_settings = array( - 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), - 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), - 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), - 'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ), - 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), - 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), - 'enableCustomUnits' => get_theme_support( 'custom-units' ), - ); - - // Theme settings. - $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); - if ( false !== $color_palette ) { - $editor_settings['colors'] = $color_palette; - } - - $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); - if ( false !== $font_sizes ) { - $editor_settings['fontSizes'] = $font_sizes; - } - - $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); - if ( false !== $gradient_presets ) { - $editor_settings['gradients'] = $gradient_presets; - } /* * We want the presets and settings declared in theme.json @@ -98,7 +72,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array( * So we take theme supports, transform it to theme.json shape * and merge the static::$theme upon that. */ - $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( $editor_settings ); + $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_block_theme_supports() ); if ( ! wp_theme_has_theme_json() ) { if ( ! isset( $theme_support_data['settings']['color'] ) ) { $theme_support_data['settings']['color'] = array(); diff --git a/lib/experimental/get-global-styles-and-settings.php b/lib/experimental/get-global-styles-and-settings.php new file mode 100644 index 00000000000000..3443caeb6c9fb9 --- /dev/null +++ b/lib/experimental/get-global-styles-and-settings.php @@ -0,0 +1,38 @@ + get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), + 'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ), + 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), + 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), + 'enableCustomUnits' => get_theme_support( 'custom-units' ), + ); + + // Theme settings. + $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); + if ( false !== $color_palette ) { + $theme_settings['colors'] = $color_palette; + } + + $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); + if ( false !== $font_sizes ) { + $theme_settings['fontSizes'] = $font_sizes; + } + + $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); + if ( false !== $gradient_presets ) { + $theme_settings['gradients'] = $gradient_presets; + } + + return $theme_settings; +} diff --git a/lib/load.php b/lib/load.php index 70dbf944535255..88af3859b35187 100644 --- a/lib/load.php +++ b/lib/load.php @@ -115,6 +115,7 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) { require __DIR__ . '/experimental/html/index.php'; } +require __DIR__ . '/experimental/get-global-styles-and-settings.php'; require __DIR__ . '/experimental/class-wp-theme-json-gutenberg.php'; require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php'; require __DIR__ . '/experimental/class-wp-webfonts.php'; From 2e646a7691a69878d96ebeb558f398d2770ee999 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 29 Nov 2022 14:38:03 +0000 Subject: [PATCH 03/12] Fix lints. --- lib/experimental/get-global-styles-and-settings.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/experimental/get-global-styles-and-settings.php b/lib/experimental/get-global-styles-and-settings.php index 3443caeb6c9fb9..b90ca952f1494b 100644 --- a/lib/experimental/get-global-styles-and-settings.php +++ b/lib/experimental/get-global-styles-and-settings.php @@ -1,5 +1,9 @@ get_theme_support( 'disable-custom-colors' ), 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), From 999b258ae546c63dfbd903e88ece6062a6e3c13a Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 30 Nov 2022 17:29:33 +0000 Subject: [PATCH 04/12] Move to 6.2 class. --- .../class-wp-theme-json-resolver-6-2.php | 99 +++++++++++++++++++ .../get-global-styles-and-settings.php | 36 +++++++ ...class-wp-theme-json-resolver-gutenberg.php | 96 ------------------ .../get-global-styles-and-settings.php | 42 -------- lib/load.php | 1 - 5 files changed, 135 insertions(+), 139 deletions(-) delete mode 100644 lib/experimental/get-global-styles-and-settings.php diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index 7adb37ffbd9e01..b5df82aa86404c 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -16,7 +16,106 @@ * @access private */ class WP_Theme_JSON_Resolver_6_2 extends WP_Theme_JSON_Resolver_6_1 { + /** + * 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. + * + * @param array $deprecated Deprecated argument. + * @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data. + * @return WP_Theme_JSON_Gutenberg Entity that holds theme data. + */ + public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) { + if ( ! empty( $deprecated ) ) { + _deprecated_argument( __METHOD__, '5.9' ); + } + // When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin. + if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { + $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); + $wp_theme = wp_get_theme(); + if ( '' !== $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(); + } + + /** + * Filters the data provided by the theme for global styles and settings. + * + * @since 6.1.0 + * + * @param WP_Theme_JSON_Data 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_Gutenberg( $theme_json_data ); + + if ( $wp_theme->parent() ) { + // Get parent theme.json. + $parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true ); + if ( '' !== $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_Gutenberg( $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 ( ! $settings['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( gutenberg_get_block_theme_supports() ); + if ( ! wp_theme_has_support() ) { + 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; + + // Classic themes without a theme.json don't support global duotone. + $theme_support_data['settings']['color']['defaultDuotone'] = false; + } + $with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data ); + $with_theme_supports->merge( static::$theme ); + return $with_theme_supports; + } /** * Determines whether the active theme has a theme.json file. * diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index e74ef3144ef095..cb49f8e283c961 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -227,3 +227,39 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings(); return _wp_array_get( $settings, $path, $settings ); } + +/** + * Repeated logic from `get_default_block_editor_settings` function. When implemented into core, + * remove logic from `get_default_block_editor_settings` and simple call this function instead. + * + * @return array + */ +function gutenberg_get_block_theme_supports() { + $theme_settings = array( + 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), + 'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ), + 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), + 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), + 'enableCustomUnits' => get_theme_support( 'custom-units' ), + ); + + // Theme settings. + $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); + if ( false !== $color_palette ) { + $theme_settings['colors'] = $color_palette; + } + + $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); + if ( false !== $font_sizes ) { + $theme_settings['fontSizes'] = $font_sizes; + } + + $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); + if ( false !== $gradient_presets ) { + $theme_settings['gradients'] = $gradient_presets; + } + + return $theme_settings; +} diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index da1ac16edb16ce..bd906c81c97c67 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -16,102 +16,6 @@ * @access private */ class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_2 { - /** - * 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. - * - * @param array $deprecated Deprecated argument. - * @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data. - * @return WP_Theme_JSON_Gutenberg Entity that holds theme data. - */ - public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) { - if ( ! empty( $deprecated ) ) { - _deprecated_argument( __METHOD__, '5.9' ); - } - - // When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin. - if ( null === static::$theme || ! static::$theme instanceof WP_Theme_JSON_Gutenberg ) { - $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); - $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); - $theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data ); - - /** - * Filters the data provided by the theme for global styles & settings. - * - * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. - */ - $theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) ); - $theme_json_data = $theme_json->get_data(); - static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data ); - - if ( wp_get_theme()->parent() ) { - // Get parent theme.json. - $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); - $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); - $parent_theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $parent_theme_json_data ); - $parent_theme = new WP_Theme_JSON_Gutenberg( $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 ( ! $settings['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_Gutenberg::get_from_editor_settings( gutenberg_get_block_theme_supports() ); - 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; - - // Classic themes without a theme.json don't support global duotone. - $theme_support_data['settings']['color']['defaultDuotone'] = false; - - // 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_Gutenberg( $theme_support_data ); - $with_theme_supports->merge( static::$theme ); - - return $with_theme_supports; - } - /** * Gets the styles for blocks from the block.json file. * diff --git a/lib/experimental/get-global-styles-and-settings.php b/lib/experimental/get-global-styles-and-settings.php deleted file mode 100644 index b90ca952f1494b..00000000000000 --- a/lib/experimental/get-global-styles-and-settings.php +++ /dev/null @@ -1,42 +0,0 @@ - get_theme_support( 'disable-custom-colors' ), - 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), - 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), - 'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ), - 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), - 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), - 'enableCustomUnits' => get_theme_support( 'custom-units' ), - ); - - // Theme settings. - $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); - if ( false !== $color_palette ) { - $theme_settings['colors'] = $color_palette; - } - - $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); - if ( false !== $font_sizes ) { - $theme_settings['fontSizes'] = $font_sizes; - } - - $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); - if ( false !== $gradient_presets ) { - $theme_settings['gradients'] = $gradient_presets; - } - - return $theme_settings; -} diff --git a/lib/load.php b/lib/load.php index 88af3859b35187..70dbf944535255 100644 --- a/lib/load.php +++ b/lib/load.php @@ -115,7 +115,6 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) { require __DIR__ . '/experimental/html/index.php'; } -require __DIR__ . '/experimental/get-global-styles-and-settings.php'; require __DIR__ . '/experimental/class-wp-theme-json-gutenberg.php'; require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php'; require __DIR__ . '/experimental/class-wp-webfonts.php'; From 4d1a053aa6801c59f95afde515e3f856464b9ef9 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 30 Nov 2022 17:32:40 +0000 Subject: [PATCH 05/12] WP_Theme_JSON -> WP_Theme_JSON_Gutenberg. --- lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index b5df82aa86404c..75a7a5c2cab893 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -83,7 +83,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array( * 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( gutenberg_get_block_theme_supports() ); + $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_block_theme_supports() ); if ( ! wp_theme_has_support() ) { if ( ! isset( $theme_support_data['settings']['color'] ) ) { $theme_support_data['settings']['color'] = array(); From 725db895b6a037cf8d0f6501a13825751b8289e0 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 1 Dec 2022 16:46:08 +0000 Subject: [PATCH 06/12] Update lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André <583546+oandregal@users.noreply.github.com> --- lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index f10635ba2cd91c..62ea48bd6cbc68 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -84,7 +84,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array( * and merge the static::$theme upon that. */ $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_block_theme_supports() ); - if ( ! wp_theme_has_support() ) { + if ( ! wp_theme_has_theme_json() ) { if ( ! isset( $theme_support_data['settings']['color'] ) ) { $theme_support_data['settings']['color'] = array(); } From 0f3c45b4ff10e06c4028cd53e53c2cc13e278566 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 1 Dec 2022 17:42:31 +0000 Subject: [PATCH 07/12] Move functionality around again. --- ...class-wp-theme-json-resolver-gutenberg.php | 100 ++++++++++++++++++ .../get-global-styles-and-settings.php | 41 +++++++ lib/load.php | 1 + 3 files changed, 142 insertions(+) create mode 100644 lib/experimental/get-global-styles-and-settings.php diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index fa4c0c87cfdbce..a4eccd9c586836 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -16,6 +16,106 @@ * @access private */ class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_2 { + /** + * 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. + * + * @param array $deprecated Deprecated argument. + * @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data. + * @return WP_Theme_JSON_Gutenberg Entity that holds theme data. + */ + public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) { + if ( ! empty( $deprecated ) ) { + _deprecated_argument( __METHOD__, '5.9' ); + } + + // When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin. + if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { + $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); + $wp_theme = wp_get_theme(); + if ( '' !== $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(); + } + + /** + * Filters the data provided by the theme for global styles and settings. + * + * @since 6.1.0 + * + * @param WP_Theme_JSON_Data 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_Gutenberg( $theme_json_data ); + + if ( $wp_theme->parent() ) { + // Get parent theme.json. + $parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true ); + if ( '' !== $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_Gutenberg( $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 ( ! $settings['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_Gutenberg::get_from_editor_settings( gutenberg_get_legacy_theme_supports_for_theme_json() ); + if ( ! wp_theme_has_support() ) { + 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; + + // Classic themes without a theme.json don't support global duotone. + $theme_support_data['settings']['color']['defaultDuotone'] = false; + } + $with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data ); + $with_theme_supports->merge( static::$theme ); + return $with_theme_supports; + } /** * Gets the styles for blocks from the block.json file. * diff --git a/lib/experimental/get-global-styles-and-settings.php b/lib/experimental/get-global-styles-and-settings.php new file mode 100644 index 00000000000000..1cba8fb4b20272 --- /dev/null +++ b/lib/experimental/get-global-styles-and-settings.php @@ -0,0 +1,41 @@ + get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), + 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), + 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), + 'enableCustomUnits' => get_theme_support( 'custom-units' ), + ); + + // Theme settings. + $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); + if ( false !== $color_palette ) { + $theme_settings['colors'] = $color_palette; + } + + $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); + if ( false !== $font_sizes ) { + $theme_settings['fontSizes'] = $font_sizes; + } + + $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); + if ( false !== $gradient_presets ) { + $theme_settings['gradients'] = $gradient_presets; + } + + return $theme_settings; +} diff --git a/lib/load.php b/lib/load.php index 0439c5d2abff56..3b413297f211a6 100644 --- a/lib/load.php +++ b/lib/load.php @@ -116,6 +116,7 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) { require __DIR__ . '/experimental/html/index.php'; } +require __DIR__ . '/experimental/get-global-styles-and-settings.php'; require __DIR__ . '/experimental/class-wp-theme-json-gutenberg.php'; require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php'; require __DIR__ . '/experimental/class-wp-webfonts.php'; From c978b885f177fec999e911e49da438dcd80d20ed Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 1 Dec 2022 17:45:09 +0000 Subject: [PATCH 08/12] Fix merge conflict --- .../class-wp-theme-json-resolver-6-2.php | 100 ------------------ .../get-global-styles-and-settings.php | 37 +------ 2 files changed, 1 insertion(+), 136 deletions(-) diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index 62ea48bd6cbc68..eebb2ccde9c845 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -16,106 +16,6 @@ * @access private */ class WP_Theme_JSON_Resolver_6_2 extends WP_Theme_JSON_Resolver_6_1 { - /** - * 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. - * - * @param array $deprecated Deprecated argument. - * @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data. - * @return WP_Theme_JSON_Gutenberg Entity that holds theme data. - */ - public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) { - if ( ! empty( $deprecated ) ) { - _deprecated_argument( __METHOD__, '5.9' ); - } - - // When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin. - if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { - $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); - $wp_theme = wp_get_theme(); - if ( '' !== $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(); - } - - /** - * Filters the data provided by the theme for global styles and settings. - * - * @since 6.1.0 - * - * @param WP_Theme_JSON_Data 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_Gutenberg( $theme_json_data ); - - if ( $wp_theme->parent() ) { - // Get parent theme.json. - $parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true ); - if ( '' !== $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_Gutenberg( $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 ( ! $settings['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_Gutenberg::get_from_editor_settings( gutenberg_get_block_theme_supports() ); - 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; - - // Classic themes without a theme.json don't support global duotone. - $theme_support_data['settings']['color']['defaultDuotone'] = false; - } - $with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data ); - $with_theme_supports->merge( static::$theme ); - return $with_theme_supports; - } /** * Returns the custom post type that contains the user's origin config * for the active theme or a void array if none are found. diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 251bcb60859e00..22e1a37bb078ce 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -183,42 +183,7 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { return _wp_array_get( $settings, $path, $settings ); } -/** - * Repeated logic from `get_default_block_editor_settings` function. When implemented into core, - * remove logic from `get_default_block_editor_settings` and simple call this function instead. - * - * @return array - */ -function gutenberg_get_block_theme_supports() { - $theme_settings = array( - 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), - 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), - 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), - 'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ), - 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), - 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), - 'enableCustomUnits' => get_theme_support( 'custom-units' ), - ); - - // Theme settings. - $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); - if ( false !== $color_palette ) { - $theme_settings['colors'] = $color_palette; - } - - $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); - if ( false !== $font_sizes ) { - $theme_settings['fontSizes'] = $font_sizes; - } - - $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); - if ( false !== $gradient_presets ) { - $theme_settings['gradients'] = $gradient_presets; - } - - return $theme_settings; -} - +/* * Private function to clean the caches used by gutenberg_get_global_settings method. * * @access private From 90cc1ce33169563fef16f12f302bba50a49601ed Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 1 Dec 2022 17:49:58 +0000 Subject: [PATCH 09/12] Fix more issues. --- .../class-wp-theme-json-resolver-6-2.php | 1 + .../get-global-styles-and-settings.php | 2 +- .../class-wp-theme-json-resolver-gutenberg.php | 15 ++++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index eebb2ccde9c845..2cce8cec7e46e1 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -16,6 +16,7 @@ * @access private */ class WP_Theme_JSON_Resolver_6_2 extends WP_Theme_JSON_Resolver_6_1 { + /** * Returns the custom post type that contains the user's origin config * for the active theme or a void array if none are found. diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 22e1a37bb078ce..1bf1ea23b6417f 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -183,7 +183,7 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { return _wp_array_get( $settings, $path, $settings ); } -/* +/** * Private function to clean the caches used by gutenberg_get_global_settings method. * * @access private diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index a4eccd9c586836..98b7f827433992 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -47,11 +47,9 @@ public static function get_theme_data( $deprecated = array(), $settings = array( /** * Filters the data provided by the theme for global styles and settings. * - * @since 6.1.0 - * - * @param WP_Theme_JSON_Data Class to access and update the underlying data. + * @param WP_Theme_JSON_Data_Gutenberg 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 = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) ); $theme_json_data = $theme_json->get_data(); static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data ); @@ -84,7 +82,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array( * and merge the static::$theme upon that. */ $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_legacy_theme_supports_for_theme_json() ); - if ( ! wp_theme_has_support() ) { + if ( ! wp_theme_has_theme_json() ) { if ( ! isset( $theme_support_data['settings']['color'] ) ) { $theme_support_data['settings']['color'] = array(); } @@ -111,11 +109,18 @@ public static function get_theme_data( $deprecated = array(), $settings = array( // Classic themes without a theme.json don't support global duotone. $theme_support_data['settings']['color']['defaultDuotone'] = false; + + // 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_Gutenberg( $theme_support_data ); $with_theme_supports->merge( static::$theme ); + return $with_theme_supports; } + /** * Gets the styles for blocks from the block.json file. * From f595fba7e27e651124dbbfde4c4d05a4d0a65c81 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 1 Dec 2022 17:51:51 +0000 Subject: [PATCH 10/12] More moving. --- .../get-global-styles-and-settings.php | 35 ++++++++++++++++ .../get-global-styles-and-settings.php | 41 ------------------- lib/load.php | 1 - 3 files changed, 35 insertions(+), 42 deletions(-) delete mode 100644 lib/experimental/get-global-styles-and-settings.php diff --git a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php index fd6113c7405c4a..0829a09d084c70 100644 --- a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php @@ -54,3 +54,38 @@ function ( $item ) { } } } + +/** + * Repeated logic from `get_default_block_editor_settings` function. When implemented into core, + * remove logic from `get_default_block_editor_settings` and simple call this function instead. + * + * @return array + */ +function gutenberg_get_legacy_theme_supports_for_theme_json() { + $theme_settings = array( + 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), + 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), + 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), + 'enableCustomUnits' => get_theme_support( 'custom-units' ), + ); + + // Theme settings. + $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); + if ( false !== $color_palette ) { + $theme_settings['colors'] = $color_palette; + } + + $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); + if ( false !== $font_sizes ) { + $theme_settings['fontSizes'] = $font_sizes; + } + + $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); + if ( false !== $gradient_presets ) { + $theme_settings['gradients'] = $gradient_presets; + } + + return $theme_settings; +} diff --git a/lib/experimental/get-global-styles-and-settings.php b/lib/experimental/get-global-styles-and-settings.php deleted file mode 100644 index 1cba8fb4b20272..00000000000000 --- a/lib/experimental/get-global-styles-and-settings.php +++ /dev/null @@ -1,41 +0,0 @@ - get_theme_support( 'disable-custom-colors' ), - 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), - 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), - 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), - 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), - 'enableCustomUnits' => get_theme_support( 'custom-units' ), - ); - - // Theme settings. - $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); - if ( false !== $color_palette ) { - $theme_settings['colors'] = $color_palette; - } - - $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); - if ( false !== $font_sizes ) { - $theme_settings['fontSizes'] = $font_sizes; - } - - $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); - if ( false !== $gradient_presets ) { - $theme_settings['gradients'] = $gradient_presets; - } - - return $theme_settings; -} diff --git a/lib/load.php b/lib/load.php index 3b413297f211a6..0439c5d2abff56 100644 --- a/lib/load.php +++ b/lib/load.php @@ -116,7 +116,6 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) { require __DIR__ . '/experimental/html/index.php'; } -require __DIR__ . '/experimental/get-global-styles-and-settings.php'; require __DIR__ . '/experimental/class-wp-theme-json-gutenberg.php'; require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php'; require __DIR__ . '/experimental/class-wp-webfonts.php'; From fcc46b2bb85fb1c52c60018d1067ba7e4fdc6bfa Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 1 Dec 2022 17:54:45 +0000 Subject: [PATCH 11/12] Revert changes. --- ...class-wp-theme-json-resolver-gutenberg.php | 82 +++++++++++++------ 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 98b7f827433992..938674e1443562 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -34,18 +34,13 @@ public static function get_theme_data( $deprecated = array(), $settings = array( } // When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin. - if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { - $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); - $wp_theme = wp_get_theme(); - if ( '' !== $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(); - } + if ( null === static::$theme || ! static::$theme instanceof WP_Theme_JSON_Gutenberg ) { + $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); + $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + $theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data ); /** - * Filters the data provided by the theme for global styles and settings. + * Filters the data provided by the theme for global styles & settings. * * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. */ @@ -53,21 +48,17 @@ public static function get_theme_data( $deprecated = array(), $settings = array( $theme_json_data = $theme_json->get_data(); static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data ); - if ( $wp_theme->parent() ) { + if ( wp_get_theme()->parent() ) { // Get parent theme.json. - $parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true ); - if ( '' !== $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_Gutenberg( $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; - } + $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); + $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); + $parent_theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $parent_theme_json_data ); + $parent_theme = new WP_Theme_JSON_Gutenberg( $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; } } @@ -173,4 +164,47 @@ private static function remove_JSON_comments( $array ) { return $array; } + /** + * Returns the data merged from multiple origins. + * + * There are three sources of data (origins) for a site: + * default, theme, and custom. The custom's has higher priority + * than the theme's, and the theme's higher than default's. + * + * Unlike the getters {@link get_core_data}, + * {@link get_theme_data}, and {@link get_user_data}, + * this method returns data after it has been merged + * with the previous origins. This means that if the same piece of data + * is declared in different origins (user, theme, and core), + * the last origin overrides the previous. + * + * For example, if the user has set a background color + * for the paragraph block, and the theme has done it as well, + * the user preference wins. + * + * @since 5.8.0 + * @since 5.9.0 Added user data, removed the `$settings` parameter, + * added the `$origin` parameter. + * + * @param string $origin Optional. To what level should we merge data. + * Valid values are 'theme' or 'custom'. Default 'custom'. + * @return WP_Theme_JSON + */ + public static function get_merged_data( $origin = 'custom' ) { + if ( is_array( $origin ) ) { + _deprecated_argument( __FUNCTION__, '5.9' ); + } + + $result = new WP_Theme_JSON_Gutenberg(); + $result->merge( static::get_core_data() ); + $result->merge( static::get_block_data() ); + $result->merge( static::get_theme_data() ); + if ( 'custom' === $origin ) { + $result->merge( static::get_user_data() ); + } + // Generate the default spacing sizes presets. + $result->set_spacing_sizes(); + + return $result; + } } From 0a2fcfdfc2ae957ecceb3dc612e413c684d6576d Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 1 Dec 2022 17:57:24 +0000 Subject: [PATCH 12/12] More reverts. --- ...class-wp-theme-json-resolver-gutenberg.php | 50 ++----------------- 1 file changed, 4 insertions(+), 46 deletions(-) diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 938674e1443562..17f2dc87f04d00 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -35,8 +35,9 @@ public static function get_theme_data( $deprecated = array(), $settings = array( // When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin. if ( null === static::$theme || ! static::$theme instanceof WP_Theme_JSON_Gutenberg ) { + $wp_theme = wp_get_theme(); $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); - $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + $theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) ); $theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data ); /** @@ -48,10 +49,10 @@ public static function get_theme_data( $deprecated = array(), $settings = array( $theme_json_data = $theme_json->get_data(); static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data ); - if ( wp_get_theme()->parent() ) { + if ( $wp_theme->parent() ) { // Get parent theme.json. $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); - $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); + $parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) ); $parent_theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $parent_theme_json_data ); $parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data ); @@ -164,47 +165,4 @@ private static function remove_JSON_comments( $array ) { return $array; } - /** - * Returns the data merged from multiple origins. - * - * There are three sources of data (origins) for a site: - * default, theme, and custom. The custom's has higher priority - * than the theme's, and the theme's higher than default's. - * - * Unlike the getters {@link get_core_data}, - * {@link get_theme_data}, and {@link get_user_data}, - * this method returns data after it has been merged - * with the previous origins. This means that if the same piece of data - * is declared in different origins (user, theme, and core), - * the last origin overrides the previous. - * - * For example, if the user has set a background color - * for the paragraph block, and the theme has done it as well, - * the user preference wins. - * - * @since 5.8.0 - * @since 5.9.0 Added user data, removed the `$settings` parameter, - * added the `$origin` parameter. - * - * @param string $origin Optional. To what level should we merge data. - * Valid values are 'theme' or 'custom'. Default 'custom'. - * @return WP_Theme_JSON - */ - public static function get_merged_data( $origin = 'custom' ) { - if ( is_array( $origin ) ) { - _deprecated_argument( __FUNCTION__, '5.9' ); - } - - $result = new WP_Theme_JSON_Gutenberg(); - $result->merge( static::get_core_data() ); - $result->merge( static::get_block_data() ); - $result->merge( static::get_theme_data() ); - if ( 'custom' === $origin ) { - $result->merge( static::get_user_data() ); - } - // Generate the default spacing sizes presets. - $result->set_spacing_sizes(); - - return $result; - } }