From 23fc1b5cbb09e42bc144d8f4b45a508311af78a3 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Fri, 26 Jan 2024 10:16:52 -0600 Subject: [PATCH] Add migration for theme.json v2 to v3 --- lib/class-wp-theme-json-gutenberg.php | 3 +- lib/class-wp-theme-json-schema-gutenberg.php | 39 +++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 7c75a3de8d46f..b880e883cb74e 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -678,9 +678,10 @@ public static function get_element_class_name( $element ) { * * @since 5.8.0 * @since 5.9.0 Changed value from 1 to 2. + * @since 6.5.0 Changed value from 2 to 3. * @var int */ - const LATEST_SCHEMA = 2; + const LATEST_SCHEMA = 3; /** * Constructor. diff --git a/lib/class-wp-theme-json-schema-gutenberg.php b/lib/class-wp-theme-json-schema-gutenberg.php index 8373e133c5aec..20ff5b3e28e80 100644 --- a/lib/class-wp-theme-json-schema-gutenberg.php +++ b/lib/class-wp-theme-json-schema-gutenberg.php @@ -38,6 +38,7 @@ class WP_Theme_JSON_Schema_Gutenberg { * Function that migrates a given theme.json structure to the last version. * * @since 5.9.0 + * @since 6.5.0 Migrate up to v3. * * @param array $theme_json The structure to migrate. * @@ -50,8 +51,12 @@ public static function migrate( $theme_json ) { ); } - if ( 1 === $theme_json['version'] ) { - $theme_json = self::migrate_v1_to_v2( $theme_json ); + // No breaks so all migrations will run in order starting with the current. + switch( $theme_json['version'] ) { + case 1: + $theme_json = self::migrate_v1_to_v2( $theme_json ); + case 2: + $theme_json = self::migrate_v2_to_v3( $theme_json ); } return $theme_json; @@ -87,6 +92,36 @@ private static function migrate_v1_to_v2( $old ) { return $new; } + /** + * Sets settings.typography.defaultFontSizes to false as it drives + * PRESETS_METADATA prevent_override in class-wp-theme-json.php which was + * hardcoded to false in v2 but defaults to true in v3. + * + * @since 6.5.0 + * + * @param array $old Data to migrate. + * + * @return array Data with defaultFontSizes set to false. + */ + private static function migrate_v2_to_v3( $old ) { + // Copy everything. + $new = $old; + + // Overwrite the things that changed. + if ( ! isset( $old['settings'] ) ) { + $new['settings'] = array(); + } + if ( ! isset( $old['settings']['typography'] ) ) { + $new['settings']['typography'] = array(); + } + $new['settings']['typography']['defaultFontSizes'] = false; + + // Set the new version. + $new['version'] = 3; + + return $new; + } + /** * Processes the settings subtree. *