From 7b995fb309b5ae752d68f1602271182e45104df2 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 29 Mar 2022 12:45:37 +0100 Subject: [PATCH 01/13] heme Export: Remove default theme.json properties on export --- .../class-wp-theme-json-gutenberg.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php index b963477a30a9f5..6efde710ae02cd 100644 --- a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php +++ b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php @@ -159,9 +159,27 @@ public function get_data() { } } + $flattened_theme_json = static::unset_default_properties( $flattened_theme_json ); + wp_recursive_ksort( $flattened_theme_json ); return $flattened_theme_json; } + protected static function unset_default_properties( $theme_json ) { + if ( isset( $theme_json['settings']['color']['custom'] ) && $theme_json['settings']['color']['custom'] ) { + unset( $theme_json['settings']['color']['custom'] ); + } + + if ( isset( $theme_json['settings']['color']['customGradient'] ) && $theme_json['settings']['color']['customGradient'] ) { + unset( $theme_json['settings']['color']['customGradient'] ); + } + + if ( isset( $theme_json['settings']['typography']['customFontSize'] ) && $theme_json['settings']['typography']['customFontSize'] ) { + unset( $theme_json['settings']['typography']['customFontSize'] ); + } + + return $theme_json; + } + } From a33c4ba8002fa9f7af27811968d7e4d1923d5f0a Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 1 Apr 2022 12:21:32 +0100 Subject: [PATCH 02/13] Remove false default properties --- .../class-wp-theme-json-gutenberg.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php index 6efde710ae02cd..414d7227175e9e 100644 --- a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php +++ b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php @@ -166,7 +166,14 @@ public function get_data() { return $flattened_theme_json; } + /** + * Removes default properties from theme.json + * + * @param array The theme.json to update + * @return array An updated version of theme.json + */ protected static function unset_default_properties( $theme_json ) { + // Unset properties that default to true. if ( isset( $theme_json['settings']['color']['custom'] ) && $theme_json['settings']['color']['custom'] ) { unset( $theme_json['settings']['color']['custom'] ); } @@ -179,6 +186,19 @@ protected static function unset_default_properties( $theme_json ) { unset( $theme_json['settings']['typography']['customFontSize'] ); } + // Unset properties that default to false. + if ( isset( $theme_json['settings']['typography']['lineHeight'] ) && ! $theme_json['settings']['typography']['lineHeight'] ) { + unset( $theme_json['settings']['typography']['lineHeight'] ); + } + + if ( isset( $theme_json['settings']['spacing']['units'] ) && ! $theme_json['settings']['spacing']['units'] ) { + unset( $theme_json['settings']['spacing']['units'] ); + } + + if ( isset( $theme_json['settings']['spacing']['padding'] ) && ! $theme_json['settings']['spacing']['padding'] ) { + unset( $theme_json['settings']['spacing']['padding'] ); + } + return $theme_json; } From 1058f99f274c992d9c484446bbe731861398d0e0 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 1 Apr 2022 12:58:55 +0100 Subject: [PATCH 03/13] remove empty arrays --- .../class-wp-theme-json-gutenberg.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php index 414d7227175e9e..1d57d1bae6b8f3 100644 --- a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php +++ b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php @@ -199,6 +199,23 @@ protected static function unset_default_properties( $theme_json ) { unset( $theme_json['settings']['spacing']['padding'] ); } + // Remove empty arrays. + if ( empty( $theme_json['settings']['color'] ) ) { + unset( $theme_json['settings']['color'] ); + } + + if ( empty( $theme_json['settings']['typography'] ) ) { + unset( $theme_json['settings']['typography'] ); + } + + if ( empty( $theme_json['settings']['spacing'] ) ) { + unset( $theme_json['settings']['spacing'] ); + } + + if ( empty( $theme_json['settings'] ) ) { + unset( $theme_json['settings'] ); + } + return $theme_json; } From f9bebf3680c5739fde460c54d573f9fff8a73a32 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 1 Apr 2022 13:49:55 +0100 Subject: [PATCH 04/13] update docblock --- lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php index 1d57d1bae6b8f3..7cfc3ebfed5ccb 100644 --- a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php +++ b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php @@ -169,7 +169,7 @@ public function get_data() { /** * Removes default properties from theme.json * - * @param array The theme.json to update + * @param array $theme_json The theme.json to update * @return array An updated version of theme.json */ protected static function unset_default_properties( $theme_json ) { From b15254e80385ba259815f7690d22603c854dc9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 1 Apr 2022 16:47:13 +0200 Subject: [PATCH 05/13] test empty get_data --- phpunit/class-wp-theme-json-test.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index b138564896cb7c..5dfcd1e346e86a 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -2451,4 +2451,26 @@ function test_export_data_deals_with_empty_theme_data() { $this->assertEqualSetsWithIndex( $expected, $actual ); } + function test_export_data_deals_with_empty_data() { + $theme_v2 = new WP_Theme_JSON_Gutenberg( + array( + 'version' => 2, + ), + 'theme' + ); + $actual_v2 = $theme_v2->get_data(); + $expected_v2 = array( 'version' => 2 ); + $this->assertEqualSetsWithIndex( $expected_v2, $actual_v2 ); + + $theme_v1 = new WP_Theme_JSON_Gutenberg( + array( + 'version' => 1, + ), + 'theme' + ); + $actual_v1 = $theme_v1->get_data(); + $expected_v1 = array( 'version' => 2 ); + $this->assertEqualSetsWithIndex( $expected_v1, $actual_v1 ); + + } } From e59944f8632d30c7f5cb4ebfcea7cb052db155c3 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Mon, 4 Apr 2022 17:55:22 +0100 Subject: [PATCH 06/13] separate getting theme data from adding supoorts --- .../wordpress-6.0/block-template-utils.php | 2 +- ...class-wp-theme-json-resolver-gutenberg.php | 43 +++++++++++-------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index 5a8f0c5eebab30..b9f26447bdb41b 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -97,7 +97,7 @@ function gutenberg_generate_block_templates_export_file() { } // Load theme.json into the zip file. - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data(); + $tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data_without_supports(); $tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() ); $theme_json_raw = $tree->get_data(); diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 7714b6256d136b..2822f3b07a70de 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -31,25 +31,8 @@ public static function get_theme_data( $deprecated = array() ) { if ( ! empty( $deprecated ) ) { _deprecated_argument( __METHOD__, '5.9' ); } - if ( null === static::$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 = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_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; - } - } + static::get_theme_data_without_supports(); /* * We want the presets and settings declared in theme.json @@ -88,4 +71,28 @@ public static function get_theme_data( $deprecated = array() ) { return $with_theme_supports; } + + public static function get_theme_data_without_supports() { + if ( null === static::$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 = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_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; + } + } + + return static::$theme; + } } From 5b0596c2abb5a3034b06bb370f842e857a331acd Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 5 Apr 2022 16:40:33 +0100 Subject: [PATCH 07/13] remove unused code --- .../class-wp-theme-json-gutenberg.php | 56 ------------------- 1 file changed, 56 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php index 7cfc3ebfed5ccb..512b1704a94e79 100644 --- a/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php +++ b/lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php @@ -159,64 +159,8 @@ public function get_data() { } } - $flattened_theme_json = static::unset_default_properties( $flattened_theme_json ); - wp_recursive_ksort( $flattened_theme_json ); return $flattened_theme_json; } - - /** - * Removes default properties from theme.json - * - * @param array $theme_json The theme.json to update - * @return array An updated version of theme.json - */ - protected static function unset_default_properties( $theme_json ) { - // Unset properties that default to true. - if ( isset( $theme_json['settings']['color']['custom'] ) && $theme_json['settings']['color']['custom'] ) { - unset( $theme_json['settings']['color']['custom'] ); - } - - if ( isset( $theme_json['settings']['color']['customGradient'] ) && $theme_json['settings']['color']['customGradient'] ) { - unset( $theme_json['settings']['color']['customGradient'] ); - } - - if ( isset( $theme_json['settings']['typography']['customFontSize'] ) && $theme_json['settings']['typography']['customFontSize'] ) { - unset( $theme_json['settings']['typography']['customFontSize'] ); - } - - // Unset properties that default to false. - if ( isset( $theme_json['settings']['typography']['lineHeight'] ) && ! $theme_json['settings']['typography']['lineHeight'] ) { - unset( $theme_json['settings']['typography']['lineHeight'] ); - } - - if ( isset( $theme_json['settings']['spacing']['units'] ) && ! $theme_json['settings']['spacing']['units'] ) { - unset( $theme_json['settings']['spacing']['units'] ); - } - - if ( isset( $theme_json['settings']['spacing']['padding'] ) && ! $theme_json['settings']['spacing']['padding'] ) { - unset( $theme_json['settings']['spacing']['padding'] ); - } - - // Remove empty arrays. - if ( empty( $theme_json['settings']['color'] ) ) { - unset( $theme_json['settings']['color'] ); - } - - if ( empty( $theme_json['settings']['typography'] ) ) { - unset( $theme_json['settings']['typography'] ); - } - - if ( empty( $theme_json['settings']['spacing'] ) ) { - unset( $theme_json['settings']['spacing'] ); - } - - if ( empty( $theme_json['settings'] ) ) { - unset( $theme_json['settings'] ); - } - - return $theme_json; - } - } From 508e0614ce7067b823c4519c977702dd18096e78 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 5 Apr 2022 17:24:00 +0100 Subject: [PATCH 08/13] Add an export function --- .../wordpress-6.0/block-template-utils.php | 22 +------------- ...class-wp-theme-json-resolver-gutenberg.php | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index b9f26447bdb41b..741c12c063d797 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -97,29 +97,9 @@ function gutenberg_generate_block_templates_export_file() { } // Load theme.json into the zip file. - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data_without_supports(); - $tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() ); - - $theme_json_raw = $tree->get_data(); - // If a version is defined, add a schema. - if ( $theme_json_raw['version'] ) { - global $wp_version; - $theme_json_version = substr( $wp_version, 0, strpos( $wp_version, '-' ) ); - if ( defined( 'IS_GUTENBERG_PLUGIN' ) ) { - $theme_json_version = 'trunk'; - } - $schema = array( '$schema' => 'https://schemas.wp.org/wp/' . $theme_json_version . '/theme.json' ); - $theme_json_raw = array_merge( $schema, $theme_json_raw ); - } - - // Convert to a string. - $theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); - // Replace 4 spaces with a tab. - $theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); - // Add the theme.json file to the zip. $zip->addFromString( 'theme.json', - $theme_json_tabbed + WP_Theme_JSON_Resolver_Gutenberg::export() ); // Save changes to the zip file. diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 2822f3b07a70de..35a2c8227f34b2 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -72,7 +72,7 @@ public static function get_theme_data( $deprecated = array() ) { return $with_theme_supports; } - public static function get_theme_data_without_supports() { + private static function get_theme_data_without_supports() { if ( null === static::$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' ) ); @@ -95,4 +95,32 @@ public static function get_theme_data_without_supports() { return static::$theme; } + + /** + * Prepares theme.json for export. + */ + public static function export() { + $tree = static::get_theme_data_without_supports(); + $tree->merge( static::get_user_data() ); + + $theme_json_raw = $tree->get_data(); + // If a version is defined, add a schema. + if ( $theme_json_raw['version'] ) { + global $wp_version; + $theme_json_version = substr( $wp_version, 0, strpos( $wp_version, '-' ) ); + if ( defined( 'IS_GUTENBERG_PLUGIN' ) ) { + $theme_json_version = 'trunk'; + } + $schema = array( '$schema' => 'https://schemas.wp.org/wp/' . $theme_json_version . '/theme.json' ); + $theme_json_raw = array_merge( $schema, $theme_json_raw ); + } + + // Convert to a string. + $theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); + + // Replace 4 spaces with a tab. + $theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); + + return $theme_json_tabbed; + } } From e05e8923e4aaa19a90563791122f44e89a0b3e4f Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 5 Apr 2022 17:40:04 +0100 Subject: [PATCH 09/13] linting fix --- phpunit/class-wp-theme-json-test.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 5dfcd1e346e86a..a2367d13f48975 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -2452,9 +2452,9 @@ function test_export_data_deals_with_empty_theme_data() { } function test_export_data_deals_with_empty_data() { - $theme_v2 = new WP_Theme_JSON_Gutenberg( + $theme_v2 = new WP_Theme_JSON_Gutenberg( array( - 'version' => 2, + 'version' => 2, ), 'theme' ); @@ -2462,15 +2462,14 @@ function test_export_data_deals_with_empty_data() { $expected_v2 = array( 'version' => 2 ); $this->assertEqualSetsWithIndex( $expected_v2, $actual_v2 ); - $theme_v1 = new WP_Theme_JSON_Gutenberg( + $theme_v1 = new WP_Theme_JSON_Gutenberg( array( - 'version' => 1, + 'version' => 1, ), 'theme' ); $actual_v1 = $theme_v1->get_data(); $expected_v1 = array( 'version' => 2 ); $this->assertEqualSetsWithIndex( $expected_v1, $actual_v1 ); - } } From 69b037400f4c6b2a426cdb0441fbb428d4603063 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 6 Apr 2022 09:19:30 +0100 Subject: [PATCH 10/13] Update docblock --- lib/experimental/class-wp-theme-json-resolver-gutenberg.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 35a2c8227f34b2..3acb0bdc0191ed 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -72,6 +72,10 @@ public static function get_theme_data( $deprecated = array() ) { return $with_theme_supports; } + /** + * Gets the data from the theme.json file and converts it to an object. + * Also merges with the parent theme. + */ private static function get_theme_data_without_supports() { if ( null === static::$theme ) { $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); From f0fb68be7718e36f42ee060feb5d63bd0d846777 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 6 Apr 2022 09:39:46 +0100 Subject: [PATCH 11/13] add a flag to determine whether to include theme supports in get_theme_data --- .../wordpress-6.0/block-template-utils.php | 24 ++++++++++- ...class-wp-theme-json-resolver-gutenberg.php | 40 +++---------------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index 741c12c063d797..42f11eb0bc17a9 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -97,9 +97,31 @@ function gutenberg_generate_block_templates_export_file() { } // Load theme.json into the zip file. + $tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data( array( 'with_supports' => false ) ); + // Merge with user data. + $tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() ); + + $theme_json_raw = $tree->get_data(); + // If a version is defined, add a schema. + if ( $theme_json_raw['version'] ) { + global $wp_version; + $theme_json_version = substr( $wp_version, 0, strpos( $wp_version, '-' ) ); + if ( defined( 'IS_GUTENBERG_PLUGIN' ) ) { + $theme_json_version = 'trunk'; + } + $schema = array( '$schema' => 'https://schemas.wp.org/wp/' . $theme_json_version . '/theme.json' ); + $theme_json_raw = array_merge( $schema, $theme_json_raw ); + } + + // Convert to a string. + $theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); + + // Replace 4 spaces with a tab. + $theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); + $zip->addFromString( 'theme.json', - WP_Theme_JSON_Resolver_Gutenberg::export() + $theme_json_tabbed ); // Save changes to the zip file. diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 3acb0bdc0191ed..3a6c48b2f5f5fe 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -24,15 +24,15 @@ class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_0 { * 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() ) { - if ( ! empty( $deprecated ) ) { - _deprecated_argument( __METHOD__, '5.9' ); - } + public static function get_theme_data( $settings = array( 'with_supports' => true ) ) { static::get_theme_data_without_supports(); + if ( ! $settings['with_supports'] ) { + return static::$theme; + } /* * We want the presets and settings declared in theme.json @@ -76,7 +76,7 @@ public static function get_theme_data( $deprecated = array() ) { * Gets the data from the theme.json file and converts it to an object. * Also merges with the parent theme. */ - private static function get_theme_data_without_supports() { + protected static function get_theme_data_without_supports() { if ( null === static::$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' ) ); @@ -99,32 +99,4 @@ private static function get_theme_data_without_supports() { return static::$theme; } - - /** - * Prepares theme.json for export. - */ - public static function export() { - $tree = static::get_theme_data_without_supports(); - $tree->merge( static::get_user_data() ); - - $theme_json_raw = $tree->get_data(); - // If a version is defined, add a schema. - if ( $theme_json_raw['version'] ) { - global $wp_version; - $theme_json_version = substr( $wp_version, 0, strpos( $wp_version, '-' ) ); - if ( defined( 'IS_GUTENBERG_PLUGIN' ) ) { - $theme_json_version = 'trunk'; - } - $schema = array( '$schema' => 'https://schemas.wp.org/wp/' . $theme_json_version . '/theme.json' ); - $theme_json_raw = array_merge( $schema, $theme_json_raw ); - } - - // Convert to a string. - $theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); - - // Replace 4 spaces with a tab. - $theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); - - return $theme_json_tabbed; - } } From 54c85bf34f721a7c2313f94347713b2ffba29bb5 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 6 Apr 2022 09:40:35 +0100 Subject: [PATCH 12/13] restore comment --- lib/compat/wordpress-6.0/block-template-utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index 42f11eb0bc17a9..2bc0b307cc3a54 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -119,6 +119,7 @@ function gutenberg_generate_block_templates_export_file() { // Replace 4 spaces with a tab. $theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); + // Add the theme.json file to the zip. $zip->addFromString( 'theme.json', $theme_json_tabbed From e68ba3eea976a5121bc93c169caa9305d00ee728 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 6 Apr 2022 12:12:47 +0100 Subject: [PATCH 13/13] add a second param for settings and remove the new function --- .../wordpress-6.0/block-template-utils.php | 2 +- ...class-wp-theme-json-resolver-gutenberg.php | 55 +++++++++---------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index 2bc0b307cc3a54..d224e9097768c9 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -97,7 +97,7 @@ function gutenberg_generate_block_templates_export_file() { } // Load theme.json into the zip file. - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data( array( 'with_supports' => false ) ); + $tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data( array(), array( 'with_supports' => false ) ); // Merge with user data. $tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() ); diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 3a6c48b2f5f5fe..2e743d0c1f9ccc 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -24,12 +24,35 @@ class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_0 { * 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( $settings = array( 'with_supports' => true ) ) { + public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) { + if ( ! empty( $deprecated ) ) { + _deprecated_argument( __METHOD__, '5.9' ); + } + + if ( null === static::$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 = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_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; + } + } - static::get_theme_data_without_supports(); if ( ! $settings['with_supports'] ) { return static::$theme; } @@ -71,32 +94,4 @@ public static function get_theme_data( $settings = array( 'with_supports' => tru return $with_theme_supports; } - - /** - * Gets the data from the theme.json file and converts it to an object. - * Also merges with the parent theme. - */ - protected static function get_theme_data_without_supports() { - if ( null === static::$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 = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_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; - } - } - - return static::$theme; - } }