From 0f092a41be2e9d5b4fb0febd6803eda7a4f57992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:00:04 +0200 Subject: [PATCH 1/6] Rename WP_Theme_JSON_Data to WP_Theme_JSON_Data_Gutenberg for backporting --- lib/compat/wordpress-6.1/class-wp-theme-json-data.php | 4 ++-- .../wordpress-6.1/class-wp-theme-json-resolver-6-1.php | 6 +++--- lib/experimental/class-wp-theme-json-resolver-gutenberg.php | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-data.php b/lib/compat/wordpress-6.1/class-wp-theme-json-data.php index d03b2227558ead..98d55f546f802e 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-data.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-data.php @@ -8,7 +8,7 @@ /** * Class to update with a theme.json structure. */ -class WP_Theme_JSON_Data { +class WP_Theme_JSON_Data_Gutenberg { /** * Container of the data to update. @@ -40,7 +40,7 @@ public function __construct( $data = array(), $origin = 'theme' ) { * * @param array $new_data Array following the theme.json specification. * - * @return WP_Theme_JSON_Data the modified data. + * @return WP_Theme_JSON_Data_Gutenberg The own instance with access to the modified data. */ public function update_with( $new_data ) { $this->theme_json->merge( new WP_Theme_JSON_Gutenberg( $new_data, $this->origin ) ); diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php index d210fa16d23108..e78a1885caea00 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php @@ -56,7 +56,7 @@ public static function get_core_data() { $config = static::read_json_file( __DIR__ . '/theme.json' ); $config = static::translate( $config ); - $config = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data( $config, 'default' ) ); + $config = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data_Gutenberg( $config, 'default' ) ); static::$core = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'default' ); return static::$core; @@ -81,7 +81,7 @@ public static function get_user_data() { $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() ); - $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); + $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); return new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' ); } @@ -97,7 +97,7 @@ public static function get_user_data() { } } - $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); + $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); static::$user = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' ); return static::$user; diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 30b93d85e698eb..d394e908ef623a 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -39,7 +39,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array( $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 ); - $theme_json_data = apply_filters( 'global_styles_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); + $theme_json_data = apply_filters( 'global_styles_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) ); static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data->get_data() ); if ( wp_get_theme()->parent() ) { @@ -130,7 +130,7 @@ public static function get_block_data() { } } - $config = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data( $config, 'core' ) ); + $config = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data_Gutenberg( $config, 'core' ) ); // Core here means it's the lower level part of the styles chain. // It can be a core or a third-party block. From 8546271549bf25b36987c4fdb1b29da5009bc43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:03:14 +0200 Subject: [PATCH 2/6] Document the filters inline --- .../class-wp-theme-json-resolver-6-1.php | 15 +++++++++++++++ .../class-wp-theme-json-resolver-gutenberg.php | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php index e78a1885caea00..3e275cc3c3498c 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php @@ -56,6 +56,11 @@ public static function get_core_data() { $config = static::read_json_file( __DIR__ . '/theme.json' ); $config = static::translate( $config ); + /** + * Filters the default data provided by WordPress for global styles & settings. + * + * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. + */ $config = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data_Gutenberg( $config, 'default' ) ); static::$core = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'default' ); @@ -81,6 +86,11 @@ public static function get_user_data() { $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() ); + /** + * Filters the data provided by the user for global styles & settings. + * + * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. + */ $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); return new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' ); } @@ -97,6 +107,11 @@ public static function get_user_data() { } } + /** + * Filters the data provided by the user for global styles & settings. + * + * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. + */ $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); static::$user = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' ); diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index d394e908ef623a..3483ac547bc97c 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -39,6 +39,11 @@ public static function get_theme_data( $deprecated = array(), $settings = array( $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_data = apply_filters( 'global_styles_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) ); static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data->get_data() ); @@ -130,6 +135,11 @@ public static function get_block_data() { } } + /** + * Filters the data provided by the blocks for global styles & settings. + * + * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. + */ $config = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data_Gutenberg( $config, 'core' ) ); // Core here means it's the lower level part of the styles chain. From 07e7cecf517fe81cc43ca69252eb49c94ca5573e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:10:38 +0200 Subject: [PATCH 3/6] Do not override $config with a different type --- .../class-wp-theme-json-resolver-6-1.php | 15 +++++++++------ .../class-wp-theme-json-resolver-gutenberg.php | 10 ++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php index 3e275cc3c3498c..ef9f373fed08d2 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php @@ -61,8 +61,9 @@ public static function get_core_data() { * * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. */ - $config = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data_Gutenberg( $config, 'default' ) ); - static::$core = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'default' ); + $theme_json = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data_Gutenberg( $config, 'default' ) ); + $config = $theme_json->get_data(); + static::$core = new WP_Theme_JSON_Gutenberg( $config, 'default' ); return static::$core; } @@ -91,8 +92,9 @@ public static function get_user_data() { * * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. */ - $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); - return new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' ); + $theme_json = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); + $config = $theme_json->get_data(); + return new WP_Theme_JSON_Gutenberg( $config, 'custom' ); } // Very important to verify if the flag isGlobalStylesUserThemeJSON is true. @@ -112,8 +114,9 @@ public static function get_user_data() { * * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. */ - $config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); - static::$user = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' ); + $theme_json = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); + $config = $theme_json->get_data(); + static::$user = new WP_Theme_JSON_Gutenberg( $config, 'custom' ); return static::$user; } diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 3483ac547bc97c..6a707e2c9dcdc2 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -44,8 +44,9 @@ public static function get_theme_data( $deprecated = array(), $settings = array( * * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. */ - $theme_json_data = apply_filters( 'global_styles_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) ); - static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data->get_data() ); + $theme_json = apply_filters( 'global_styles_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. @@ -140,11 +141,12 @@ public static function get_block_data() { * * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. */ - $config = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data_Gutenberg( $config, 'core' ) ); + $theme_json = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data_Gutenberg( $config, 'core' ) ); + $config = $theme_json->get_data(); // Core here means it's the lower level part of the styles chain. // It can be a core or a third-party block. - return new WP_Theme_JSON_Gutenberg( $config->get_data(), 'core' ); + return new WP_Theme_JSON_Gutenberg( $config, 'core' ); } /** From e74261c229427ad02adc7d6d49586ea490a0a20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:16:48 +0200 Subject: [PATCH 4/6] Rename class-wp-theme-json-data to class-wp-theme-json-gutenberg --- ...eme-json-data.php => class-wp-theme-json-data-gutenberg.php} | 0 lib/load.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/compat/wordpress-6.1/{class-wp-theme-json-data.php => class-wp-theme-json-data-gutenberg.php} (100%) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-data.php b/lib/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php similarity index 100% rename from lib/compat/wordpress-6.1/class-wp-theme-json-data.php rename to lib/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php diff --git a/lib/load.php b/lib/load.php index 81a63b49769b66..1493b5ee797916 100644 --- a/lib/load.php +++ b/lib/load.php @@ -83,7 +83,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.1/block-editor-settings.php'; require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php'; require __DIR__ . '/compat/wordpress-6.1/get-global-styles-and-settings.php'; -require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-data.php'; +require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php'; require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-6-1.php'; require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php'; require __DIR__ . '/compat/wordpress-6.1/block-template-utils.php'; From 0ceb8b069b90205afd0c605ad093f757d567381a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:17:24 +0200 Subject: [PATCH 5/6] Better defaults for WP_Theme_JSON_Data --- .../wordpress-6.1/class-wp-theme-json-data-gutenberg.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php b/lib/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php index 98d55f546f802e..9add04bb65b097 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php @@ -22,7 +22,7 @@ class WP_Theme_JSON_Data_Gutenberg { * * @var string */ - private $origin = null; + private $origin = ''; /** * Constructor. @@ -44,7 +44,6 @@ public function __construct( $data = array(), $origin = 'theme' ) { */ public function update_with( $new_data ) { $this->theme_json->merge( new WP_Theme_JSON_Gutenberg( $new_data, $this->origin ) ); - return $this; } From 7a21e1ca190bef4cc335bd30f7f0aa0accaa01a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:34:17 +0200 Subject: [PATCH 6/6] Fix lint issues --- .../wordpress-6.1/class-wp-theme-json-resolver-6-1.php | 8 ++++---- .../class-wp-theme-json-resolver-gutenberg.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php index ef9f373fed08d2..f05a4b1f752548 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php @@ -54,15 +54,15 @@ public static function get_core_data() { return static::$core; } - $config = static::read_json_file( __DIR__ . '/theme.json' ); - $config = static::translate( $config ); + $config = static::read_json_file( __DIR__ . '/theme.json' ); + $config = static::translate( $config ); /** * Filters the default data provided by WordPress for global styles & settings. * * @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data. */ - $theme_json = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data_Gutenberg( $config, 'default' ) ); - $config = $theme_json->get_data(); + $theme_json = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data_Gutenberg( $config, 'default' ) ); + $config = $theme_json->get_data(); static::$core = new WP_Theme_JSON_Gutenberg( $config, 'default' ); return static::$core; diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 6a707e2c9dcdc2..a3f9538056cb66 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -46,7 +46,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array( */ $theme_json = apply_filters( 'global_styles_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 ); + static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data ); if ( wp_get_theme()->parent() ) { // Get parent theme.json.