Skip to content

Commit

Permalink
Add migration for theme.json v2 to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Feb 1, 2024
1 parent ffb6ef1 commit 23fc1b5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 37 additions & 2 deletions lib/class-wp-theme-json-schema-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 23fc1b5

Please sign in to comment.