From 3ad30e04ab4cd241fe89a9070fbbe6f97208d6ee 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:14:51 +0200 Subject: [PATCH 1/6] Add changes to WP_Theme_JSON --- src/wp-includes/class-wp-theme-json.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 79d1c2e968d81..db70fe40f3a91 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -747,14 +747,14 @@ 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::LATEST_SCHEMA ), $origin = 'theme' ) { if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) { $origin = 'theme'; } - $this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); + $this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin ); $valid_block_names = array_keys( static::get_blocks_metadata() ); $valid_element_names = array_keys( static::ELEMENTS ); $valid_variations = static::get_valid_block_style_variations(); @@ -3245,12 +3245,18 @@ protected static function filter_slugs( $node, $slugs ) { * @since 6.6.0 Updated to allow 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::migrate( $theme_json ); + $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin ); $valid_block_names = array_keys( static::get_blocks_metadata() ); $valid_element_names = array_keys( static::ELEMENTS ); From def029d09981ed1b0829daf8626f4d307b0db5fc 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:16:21 +0200 Subject: [PATCH 2/6] Add changes to WP_Theme_JSON_Resolver --- src/wp-includes/class-wp-theme-json-resolver.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index db4b766b82520..0c772c12ea44b 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -525,18 +525,14 @@ public static function get_user_data() { isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) && $decoded_data['isGlobalStylesUserThemeJSON'] ) { + unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); $config = $decoded_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( $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( $config, 'custom' ); + static::$user = $theme_json->get_theme_json(); return static::$user; } From 9b0bc0c86186adc5560309673026c3eab84b56f8 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:21:38 +0200 Subject: [PATCH 3/6] Add changes to WP_Theme_JSON_Schema --- .../class-wp-theme-json-schema.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-schema.php b/src/wp-includes/class-wp-theme-json-schema.php index 366594ef3705d..112077d4b46e1 100644 --- a/src/wp-includes/class-wp-theme-json-schema.php +++ b/src/wp-includes/class-wp-theme-json-schema.php @@ -38,10 +38,11 @@ class WP_Theme_JSON_Schema { * @since 6.6.0 Migrate up to v3. * * @param array $theme_json The structure to migrate. - * + * @param string $origin Optional. What source of data this object represents. + * One of 'blocks', '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, @@ -54,7 +55,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; @@ -100,11 +101,12 @@ private static function migrate_v1_to_v2( $old ) { * * @since 6.6.0 * - * @param array $old Data to migrate. - * + * @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 ) { + private static function migrate_v2_to_v3( $old, $origin ) { // Copy everything. $new = $old; @@ -115,10 +117,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 dd17fbc530f51f4d4138d2f8a26a08fae5898ecb 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:23:31 +0200 Subject: [PATCH 4/6] Add changes to kses filters --- src/wp-includes/kses.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index 9b998bf6e2123..5a1ae2de84a4a 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -2146,7 +2146,7 @@ function wp_filter_global_styles_post( $data ) { ) { unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); - $data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data ); + $data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data, 'custom' ); $data_to_encode['isGlobalStylesUserThemeJSON'] = true; return wp_slash( wp_json_encode( $data_to_encode ) ); From c468562d43d7aadc24ad767007f08dac5493443f 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:36 +0200 Subject: [PATCH 5/6] Update since --- src/wp-includes/class-wp-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index db70fe40f3a91..4547da40b097d 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -3242,7 +3242,7 @@ protected static function filter_slugs( $node, $slugs ) { * * @since 5.9.0 * @since 6.3.2 Preserves global styles block variations when securing styles. - * @since 6.6.0 Updated to allow variation element styles. + * @since 6.6.0 Updated to allow variation element styles and $origin parameter. * * @param array $theme_json Structure to sanitize. * @param string $origin Optional. What source of data this object represents. From f57351c24a51c467c15a1a81080968b1d6e77dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 6 Jun 2024 09:46:19 +0200 Subject: [PATCH 6/6] Apply feedback --- src/wp-includes/class-wp-theme-json-resolver.php | 2 +- src/wp-includes/class-wp-theme-json-schema.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 0c772c12ea44b..2048304fdf97d 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -531,7 +531,7 @@ 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( $config, 'custom' ) ); + $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) ); static::$user = $theme_json->get_theme_json(); return static::$user; diff --git a/src/wp-includes/class-wp-theme-json-schema.php b/src/wp-includes/class-wp-theme-json-schema.php index 112077d4b46e1..aaa2f968a6395 100644 --- a/src/wp-includes/class-wp-theme-json-schema.php +++ b/src/wp-includes/class-wp-theme-json-schema.php @@ -35,7 +35,7 @@ class WP_Theme_JSON_Schema { * Function that migrates a given theme.json structure to the last version. * * @since 5.9.0 - * @since 6.6.0 Migrate up to v3. + * @since 6.6.0 Migrate up to v3 and add $origin parameter. * * @param array $theme_json The structure to migrate. * @param string $origin Optional. What source of data this object represents.