From 818a849ee6368b58fb2e904756eede67070a2b2f Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Tue, 4 Jun 2024 15:20:37 -0500 Subject: [PATCH 1/7] Pass origin into migrate instead of relying on isGlobalStylesUserThemeJSON --- lib/class-wp-theme-json-gutenberg.php | 2 +- lib/class-wp-theme-json-resolver-gutenberg.php | 4 +--- lib/class-wp-theme-json-schema-gutenberg.php | 17 +++++++++-------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index ad4a9f21872bec..621e75cfcfb057 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -741,7 +741,7 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut $origin = 'theme'; } - $this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); + $this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin ); $registry = WP_Block_Type_Registry::get_instance(); $valid_block_names = array_keys( $registry->get_all_registered() ); $valid_element_names = array_keys( static::ELEMENTS ); diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index a02ae74a91fdac..18422b290f8ca9 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -534,6 +534,7 @@ public static function get_user_data() { isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) && $decoded_data['isGlobalStylesUserThemeJSON'] ) { + unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); $config = $decoded_data; } } @@ -542,9 +543,6 @@ public static function get_user_data() { $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); $config = $theme_json->get_data(); - // Needs to be set for schema migrations of user data. - $config['isGlobalStylesUserThemeJSON'] = true; - static::$user = new WP_Theme_JSON_Gutenberg( $config, 'custom' ); return static::$user; diff --git a/lib/class-wp-theme-json-schema-gutenberg.php b/lib/class-wp-theme-json-schema-gutenberg.php index 43b6ea6dcc7410..64df6ba7e4b9fc 100644 --- a/lib/class-wp-theme-json-schema-gutenberg.php +++ b/lib/class-wp-theme-json-schema-gutenberg.php @@ -40,11 +40,13 @@ class WP_Theme_JSON_Schema_Gutenberg { * @since 5.9.0 * @since 6.6.0 Migrate up to v3. * - * @param array $theme_json The structure to migrate. + * @param array $theme_json The structure to migrate. + * @param string $origin Optional. What source of data this object represents. + * One of 'default', 'theme', or 'custom'. Default 'theme'. * * @return array The structure in the last version. */ - public static function migrate( $theme_json ) { + public static function migrate( $theme_json, $origin = 'theme' ) { if ( ! isset( $theme_json['version'] ) ) { $theme_json = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA, @@ -57,7 +59,7 @@ public static function migrate( $theme_json ) { $theme_json = self::migrate_v1_to_v2( $theme_json ); // Deliberate fall through. Once migrated to v2, also migrate to v3. case 2: - $theme_json = self::migrate_v2_to_v3( $theme_json ); + $theme_json = self::migrate_v2_to_v3( $theme_json, $origin ); } return $theme_json; @@ -104,10 +106,12 @@ private static function migrate_v1_to_v2( $old ) { * @since 6.6.0 * * @param array $old Data to migrate. + * @param string $origin What source of data this object represents. + * One of 'default', 'theme', or 'custom'. * * @return array Data with defaultFontSizes set to false. */ - private static function migrate_v2_to_v3( $old ) { + private static function migrate_v2_to_v3( $old, $origin ) { // Copy everything. $new = $old; @@ -118,10 +122,7 @@ private static function migrate_v2_to_v3( $old ) { * Remaining changes do not need to be applied to the custom origin, * as they should take on the value of the theme origin. */ - if ( - isset( $new['isGlobalStylesUserThemeJSON'] ) && - true === $new['isGlobalStylesUserThemeJSON'] - ) { + if ( 'custom' === $origin ) { return $new; } From b14c869151313ce6393b87f6e534eeed9698443e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 5 Jun 2024 09:25:18 +0200 Subject: [PATCH 2/7] Bring back changes from 61262 --- lib/class-wp-theme-json-resolver-gutenberg.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 18422b290f8ca9..507876af4952a8 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -540,10 +540,8 @@ public static function get_user_data() { } /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */ - $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); - $config = $theme_json->get_data(); - - static::$user = new WP_Theme_JSON_Gutenberg( $config, 'custom' ); + $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); + static::$user = $theme_json->get_theme_json(); return static::$user; } From bb01521bee42e2a827109e2246dbfebcc51d4b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:11:43 +0200 Subject: [PATCH 3/7] Cover the other use of migrate --- lib/class-wp-theme-json-gutenberg.php | 12 +++++++++--- lib/experimental/kses.php | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 621e75cfcfb057..1e31db0be9939d 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -734,7 +734,7 @@ public static function get_element_class_name( $element ) { * * @param array $theme_json A structure that follows the theme.json schema. * @param string $origin Optional. What source of data this object represents. - * One of 'default', 'theme', or 'custom'. Default 'theme'. + * One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'. */ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA ), $origin = 'theme' ) { if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) { @@ -3281,12 +3281,18 @@ protected static function filter_slugs( $node, $slugs ) { * @since 6.6.0 Added support for block style variation element styles. * * @param array $theme_json Structure to sanitize. + * @param string $origin Optional. What source of data this object represents. + * One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'. * @return array Sanitized structure. */ - public static function remove_insecure_properties( $theme_json ) { + public static function remove_insecure_properties( $theme_json, $origin = 'theme' ) { + if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) { + $origin = 'theme'; + } + $sanitized = array(); - $theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); + $theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin ); $valid_block_names = array_keys( static::get_blocks_metadata() ); $valid_element_names = array_keys( static::ELEMENTS ); diff --git a/lib/experimental/kses.php b/lib/experimental/kses.php index bed5c58697e626..160704417195b3 100644 --- a/lib/experimental/kses.php +++ b/lib/experimental/kses.php @@ -31,7 +31,7 @@ function gutenberg_filter_global_styles_post( $data ) { ) { unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); - $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data ); + $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data, 'custom' ); $data_to_encode['isGlobalStylesUserThemeJSON'] = true; return wp_slash( wp_json_encode( $data_to_encode ) ); From 6374f852e8e2214501f361306f77d429dc8b1f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:17:38 +0200 Subject: [PATCH 4/7] Fix comment --- lib/class-wp-theme-json-schema-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-schema-gutenberg.php b/lib/class-wp-theme-json-schema-gutenberg.php index 64df6ba7e4b9fc..87fc32af586ebf 100644 --- a/lib/class-wp-theme-json-schema-gutenberg.php +++ b/lib/class-wp-theme-json-schema-gutenberg.php @@ -42,7 +42,7 @@ class WP_Theme_JSON_Schema_Gutenberg { * * @param array $theme_json The structure to migrate. * @param string $origin Optional. What source of data this object represents. - * One of 'default', 'theme', or 'custom'. Default 'theme'. + * One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'. * * @return array The structure in the last version. */ From 12829161b3161f56e4f9c2e235f03b40f45e9f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:20:36 +0200 Subject: [PATCH 5/7] Update comment --- lib/class-wp-theme-json-schema-gutenberg.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-theme-json-schema-gutenberg.php b/lib/class-wp-theme-json-schema-gutenberg.php index 87fc32af586ebf..af0977ada1b307 100644 --- a/lib/class-wp-theme-json-schema-gutenberg.php +++ b/lib/class-wp-theme-json-schema-gutenberg.php @@ -105,10 +105,9 @@ private static function migrate_v1_to_v2( $old ) { * * @since 6.6.0 * - * @param array $old Data to migrate. - * @param string $origin What source of data this object represents. - * One of 'default', 'theme', or 'custom'. - * + * @param array $old Data to migrate. + * @param string $origin What source of data this object represents. + * One of 'blocks', 'default', 'theme', or 'custom'. * @return array Data with defaultFontSizes set to false. */ private static function migrate_v2_to_v3( $old, $origin ) { From b50c7e2badf12cfcd3e2a7457e8cb14548bab033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:30:38 +0200 Subject: [PATCH 6/7] Add backport file --- backport-changelog/6.6/6737.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backport-changelog/6.6/6737.md diff --git a/backport-changelog/6.6/6737.md b/backport-changelog/6.6/6737.md new file mode 100644 index 00000000000000..84e2234ca4e79a --- /dev/null +++ b/backport-changelog/6.6/6737.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/6737 + +* https://github.com/WordPress/gutenberg/pull/62305 From c4f4ae6c47a66c633dc6a1a1718b22065c7baf98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:34:29 +0200 Subject: [PATCH 7/7] Update since --- lib/class-wp-theme-json-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 1e31db0be9939d..a1e79a56a683bf 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -3278,7 +3278,7 @@ protected static function filter_slugs( $node, $slugs ) { * Removes insecure data from theme.json. * * @since 5.9.0 - * @since 6.6.0 Added support for block style variation element styles. + * @since 6.6.0 Added support for block style variation element styles and $origin parameter. * * @param array $theme_json Structure to sanitize. * @param string $origin Optional. What source of data this object represents.