Skip to content

Commit 68beef3

Browse files
ajlendeajlendeoandregal
authored
Migrate theme.json based on origin (#62305)
Co-authored-by: ajlende <[email protected]> Co-authored-by: oandregal <[email protected]>
1 parent 06470bb commit 68beef3

5 files changed

+28
-23
lines changed

backport-changelog/6.6/6737.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/WordPress/wordpress-develop/pull/6737
2+
3+
* https://github.com/WordPress/gutenberg/pull/62305

lib/class-wp-theme-json-gutenberg.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -734,14 +734,14 @@ public static function get_element_class_name( $element ) {
734734
*
735735
* @param array $theme_json A structure that follows the theme.json schema.
736736
* @param string $origin Optional. What source of data this object represents.
737-
* One of 'default', 'theme', or 'custom'. Default 'theme'.
737+
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
738738
*/
739739
public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA ), $origin = 'theme' ) {
740740
if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
741741
$origin = 'theme';
742742
}
743743

744-
$this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json );
744+
$this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin );
745745
$registry = WP_Block_Type_Registry::get_instance();
746746
$valid_block_names = array_keys( $registry->get_all_registered() );
747747
$valid_element_names = array_keys( static::ELEMENTS );
@@ -3278,15 +3278,21 @@ protected static function filter_slugs( $node, $slugs ) {
32783278
* Removes insecure data from theme.json.
32793279
*
32803280
* @since 5.9.0
3281-
* @since 6.6.0 Added support for block style variation element styles.
3281+
* @since 6.6.0 Added support for block style variation element styles and $origin parameter.
32823282
*
32833283
* @param array $theme_json Structure to sanitize.
3284+
* @param string $origin Optional. What source of data this object represents.
3285+
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
32843286
* @return array Sanitized structure.
32853287
*/
3286-
public static function remove_insecure_properties( $theme_json ) {
3288+
public static function remove_insecure_properties( $theme_json, $origin = 'theme' ) {
3289+
if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
3290+
$origin = 'theme';
3291+
}
3292+
32873293
$sanitized = array();
32883294

3289-
$theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json );
3295+
$theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin );
32903296

32913297
$valid_block_names = array_keys( static::get_blocks_metadata() );
32923298
$valid_element_names = array_keys( static::ELEMENTS );

lib/class-wp-theme-json-resolver-gutenberg.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -534,18 +534,14 @@ public static function get_user_data() {
534534
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
535535
$decoded_data['isGlobalStylesUserThemeJSON']
536536
) {
537+
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
537538
$config = $decoded_data;
538539
}
539540
}
540541

541542
/** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */
542-
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) );
543-
$config = $theme_json->get_data();
544-
545-
// Needs to be set for schema migrations of user data.
546-
$config['isGlobalStylesUserThemeJSON'] = true;
547-
548-
static::$user = new WP_Theme_JSON_Gutenberg( $config, 'custom' );
543+
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) );
544+
static::$user = $theme_json->get_theme_json();
549545

550546
return static::$user;
551547
}

lib/class-wp-theme-json-schema-gutenberg.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ class WP_Theme_JSON_Schema_Gutenberg {
4040
* @since 5.9.0
4141
* @since 6.6.0 Migrate up to v3.
4242
*
43-
* @param array $theme_json The structure to migrate.
43+
* @param array $theme_json The structure to migrate.
44+
* @param string $origin Optional. What source of data this object represents.
45+
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
4446
*
4547
* @return array The structure in the last version.
4648
*/
47-
public static function migrate( $theme_json ) {
49+
public static function migrate( $theme_json, $origin = 'theme' ) {
4850
if ( ! isset( $theme_json['version'] ) ) {
4951
$theme_json = array(
5052
'version' => WP_Theme_JSON::LATEST_SCHEMA,
@@ -57,7 +59,7 @@ public static function migrate( $theme_json ) {
5759
$theme_json = self::migrate_v1_to_v2( $theme_json );
5860
// Deliberate fall through. Once migrated to v2, also migrate to v3.
5961
case 2:
60-
$theme_json = self::migrate_v2_to_v3( $theme_json );
62+
$theme_json = self::migrate_v2_to_v3( $theme_json, $origin );
6163
}
6264

6365
return $theme_json;
@@ -103,11 +105,12 @@ private static function migrate_v1_to_v2( $old ) {
103105
*
104106
* @since 6.6.0
105107
*
106-
* @param array $old Data to migrate.
107-
*
108+
* @param array $old Data to migrate.
109+
* @param string $origin What source of data this object represents.
110+
* One of 'blocks', 'default', 'theme', or 'custom'.
108111
* @return array Data with defaultFontSizes set to false.
109112
*/
110-
private static function migrate_v2_to_v3( $old ) {
113+
private static function migrate_v2_to_v3( $old, $origin ) {
111114
// Copy everything.
112115
$new = $old;
113116

@@ -118,10 +121,7 @@ private static function migrate_v2_to_v3( $old ) {
118121
* Remaining changes do not need to be applied to the custom origin,
119122
* as they should take on the value of the theme origin.
120123
*/
121-
if (
122-
isset( $new['isGlobalStylesUserThemeJSON'] ) &&
123-
true === $new['isGlobalStylesUserThemeJSON']
124-
) {
124+
if ( 'custom' === $origin ) {
125125
return $new;
126126
}
127127

lib/experimental/kses.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function gutenberg_filter_global_styles_post( $data ) {
3131
) {
3232
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
3333

34-
$data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data );
34+
$data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data, 'custom' );
3535

3636
$data_to_encode['isGlobalStylesUserThemeJSON'] = true;
3737
return wp_slash( wp_json_encode( $data_to_encode ) );

0 commit comments

Comments
 (0)