Skip to content

Commit 782fb1f

Browse files
committed
Editor: Add theme.json v3 migrations.
See #6616. See also the original Gutenberg PRs: * WordPress/gutenberg#58409 * WordPress/gutenberg#61328 * WordPress/gutenberg#61842 * WordPress/gutenberg#62199 * WordPress/gutenberg#62252 Fixes #61282. Props ajlende, talldanwp, ramonopoly, ellatrix. git-svn-id: https://develop.svn.wordpress.org/trunk@58328 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3a9ecd2 commit 782fb1f

34 files changed

+677
-293
lines changed

src/wp-includes/block-editor.php

+6
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ function get_block_editor_theme_styles() {
814814
* Returns the classic theme supports settings for block editor.
815815
*
816816
* @since 6.2.0
817+
* @since 6.6.0 Add support for 'editor-spacing-sizes' theme support.
817818
*
818819
* @return array The classic theme supports settings.
819820
*/
@@ -844,5 +845,10 @@ function get_classic_theme_supports_block_editor_settings() {
844845
$theme_settings['gradients'] = $gradient_presets;
845846
}
846847

848+
$spacing_sizes = current( (array) get_theme_support( 'editor-spacing-sizes' ) );
849+
if ( false !== $spacing_sizes ) {
850+
$theme_settings['spacingSizes'] = $spacing_sizes;
851+
}
852+
847853
return $theme_settings;
848854
}

src/wp-includes/class-wp-theme-json-data.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class WP_Theme_JSON_Data {
3939
* @param array $data Array following the theme.json specification.
4040
* @param string $origin The origin of the data: default, theme, user.
4141
*/
42-
public function __construct( $data = array(), $origin = 'theme' ) {
42+
public function __construct( $data = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA ), $origin = 'theme' ) {
4343
$this->origin = $origin;
4444
$this->theme_json = new WP_Theme_JSON( $data, $this->origin );
4545
}

src/wp-includes/class-wp-theme-json-resolver.php

+37-8
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ protected static function has_same_registered_blocks( $origin ) {
220220
* @since 5.8.0
221221
* @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed.
222222
* @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports.
223+
* @since 6.6.0 Add support for 'default-font-sizes' and 'default-spacing-sizes' theme supports.
223224
*
224225
* @param array $deprecated Deprecated. Not used.
225226
* @param array $options {
@@ -243,7 +244,7 @@ public static function get_theme_data( $deprecated = array(), $options = array()
243244
$theme_json_data = static::read_json_file( $theme_json_file );
244245
$theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) );
245246
} else {
246-
$theme_json_data = array();
247+
$theme_json_data = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
247248
}
248249

249250
/**
@@ -310,6 +311,32 @@ public static function get_theme_data( $deprecated = array(), $options = array()
310311
}
311312
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;
312313

314+
if ( ! isset( $theme_support_data['settings']['typography'] ) ) {
315+
$theme_support_data['settings']['typography'] = array();
316+
}
317+
$default_font_sizes = false;
318+
if ( current_theme_supports( 'default-font-sizes' ) ) {
319+
$default_font_sizes = true;
320+
}
321+
if ( ! isset( $theme_support_data['settings']['typography']['fontSizes'] ) ) {
322+
// If the theme does not have any font sizes, we still want to show the core one.
323+
$default_font_sizes = true;
324+
}
325+
$theme_support_data['settings']['typography']['defaultFontSizes'] = $default_font_sizes;
326+
327+
if ( ! isset( $theme_support_data['settings']['spacing'] ) ) {
328+
$theme_support_data['settings']['spacing'] = array();
329+
}
330+
$default_spacing_sizes = false;
331+
if ( current_theme_supports( 'default-spacing-sizes' ) ) {
332+
$default_spacing_sizes = true;
333+
}
334+
if ( ! isset( $theme_support_data['settings']['spacing']['spacingSizes'] ) ) {
335+
// If the theme does not have any spacing sizes, we still want to show the core one.
336+
$default_spacing_sizes = true;
337+
}
338+
$theme_support_data['settings']['spacing']['defaultSpacingSizes'] = $default_spacing_sizes;
339+
313340
if ( ! isset( $theme_support_data['settings']['shadow'] ) ) {
314341
$theme_support_data['settings']['shadow'] = array();
315342
}
@@ -359,7 +386,7 @@ public static function get_block_data() {
359386
return static::$blocks;
360387
}
361388

362-
$config = array( 'version' => 2 );
389+
$config = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
363390
foreach ( $blocks as $block_name => $block_type ) {
364391
if ( isset( $block_type->supports['__experimentalStyle'] ) ) {
365392
$config['styles']['blocks'][ $block_name ] = static::remove_json_comments( $block_type->supports['__experimentalStyle'] );
@@ -494,6 +521,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
494521
* Returns the user's origin config.
495522
*
496523
* @since 5.9.0
524+
* @since 6.6.0 The 'isGlobalStylesUserThemeJSON' flag is left on the user data.
497525
*
498526
* @return WP_Theme_JSON Entity that holds styles for user data.
499527
*/
@@ -531,14 +559,18 @@ public static function get_user_data() {
531559
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
532560
$decoded_data['isGlobalStylesUserThemeJSON']
533561
) {
534-
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
535562
$config = $decoded_data;
536563
}
537564
}
538565

539566
/** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */
540-
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
541-
static::$user = $theme_json->get_theme_json();
567+
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
568+
$config = $theme_json->get_data();
569+
570+
// Needs to be set for schema migrations of user data.
571+
$config['isGlobalStylesUserThemeJSON'] = true;
572+
573+
static::$user = new WP_Theme_JSON( $config, 'custom' );
542574

543575
return static::$user;
544576
}
@@ -586,7 +618,6 @@ public static function get_merged_data( $origin = 'custom' ) {
586618
$result = new WP_Theme_JSON();
587619
$result->merge( static::get_core_data() );
588620
if ( 'default' === $origin ) {
589-
$result->set_spacing_sizes();
590621
return $result;
591622
}
592623

@@ -597,12 +628,10 @@ public static function get_merged_data( $origin = 'custom' ) {
597628

598629
$result->merge( static::get_theme_data() );
599630
if ( 'theme' === $origin ) {
600-
$result->set_spacing_sizes();
601631
return $result;
602632
}
603633

604634
$result->merge( static::get_user_data() );
605-
$result->set_spacing_sizes();
606635

607636
return $result;
608637
}

src/wp-includes/class-wp-theme-json-schema.php

+91-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class WP_Theme_JSON_Schema {
3535
* Function that migrates a given theme.json structure to the last version.
3636
*
3737
* @since 5.9.0
38+
* @since 6.6.0 Migrate up to v3.
3839
*
3940
* @param array $theme_json The structure to migrate.
4041
*
@@ -47,8 +48,14 @@ public static function migrate( $theme_json ) {
4748
);
4849
}
4950

50-
if ( 1 === $theme_json['version'] ) {
51-
$theme_json = self::migrate_v1_to_v2( $theme_json );
51+
// Migrate each version in order starting with the current version.
52+
switch ( $theme_json['version'] ) {
53+
case 1:
54+
$theme_json = self::migrate_v1_to_v2( $theme_json );
55+
// no break
56+
case 2:
57+
$theme_json = self::migrate_v2_to_v3( $theme_json );
58+
// no break
5259
}
5360

5461
return $theme_json;
@@ -84,6 +91,88 @@ private static function migrate_v1_to_v2( $old ) {
8491
return $new;
8592
}
8693

94+
/**
95+
* Migrates from v2 to v3.
96+
*
97+
* - Sets settings.typography.defaultFontSizes to false.
98+
*
99+
* @since 6.6.0
100+
*
101+
* @param array $old Data to migrate.
102+
*
103+
* @return array Data with defaultFontSizes set to false.
104+
*/
105+
private static function migrate_v2_to_v3( $old ) {
106+
// Copy everything.
107+
$new = $old;
108+
109+
// Set the new version.
110+
$new['version'] = 3;
111+
112+
/*
113+
* Remaining changes do not need to be applied to the custom origin,
114+
* as they should take on the value of the theme origin.
115+
*/
116+
if (
117+
isset( $new['isGlobalStylesUserThemeJSON'] ) &&
118+
true === $new['isGlobalStylesUserThemeJSON']
119+
) {
120+
return $new;
121+
}
122+
123+
/*
124+
* Even though defaultFontSizes and defaultSpacingSizes are new
125+
* settings, we need to migrate them as they each control
126+
* PRESETS_METADATA prevent_override values which were previously
127+
* hardcoded to false. This only needs to happen when the theme provides
128+
* fontSizes or spacingSizes as they could match the default ones and
129+
* affect the generated CSS.
130+
*/
131+
if ( isset( $old['settings']['typography']['fontSizes'] ) ) {
132+
if ( ! isset( $new['settings'] ) ) {
133+
$new['settings'] = array();
134+
}
135+
if ( ! isset( $new['settings']['typography'] ) ) {
136+
$new['settings']['typography'] = array();
137+
}
138+
$new['settings']['typography']['defaultFontSizes'] = false;
139+
}
140+
141+
/*
142+
* Similarly to defaultFontSizes, we need to migrate defaultSpacingSizes
143+
* as it controls the PRESETS_METADATA prevent_override which was
144+
* previously hardcoded to false. This only needs to happen when the
145+
* theme provided spacing sizes via spacingSizes or spacingScale.
146+
*/
147+
if (
148+
isset( $old['settings']['spacing']['spacingSizes'] ) ||
149+
isset( $old['settings']['spacing']['spacingScale'] )
150+
) {
151+
if ( ! isset( $new['settings'] ) ) {
152+
$new['settings'] = array();
153+
}
154+
if ( ! isset( $new['settings']['spacing'] ) ) {
155+
$new['settings']['spacing'] = array();
156+
}
157+
$new['settings']['spacing']['defaultSpacingSizes'] = false;
158+
}
159+
160+
/*
161+
* In v3 spacingSizes is merged with the generated spacingScale sizes
162+
* instead of completely replacing them. The v3 behavior is what was
163+
* documented for the v2 schema, but the code never actually did work
164+
* that way. Instead of surprising users with a behavior change two
165+
* years after the fact at the same time as a v3 update is introduced,
166+
* we'll continue using the "bugged" behavior for v2 themes. And treat
167+
* the "bug fix" as a breaking change for v3.
168+
*/
169+
if ( isset( $old['settings']['spacing']['spacingSizes'] ) ) {
170+
unset( $new['settings']['spacing']['spacingScale'] );
171+
}
172+
173+
return $new;
174+
}
175+
87176
/**
88177
* Processes the settings subtree.
89178
*

0 commit comments

Comments
 (0)