-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate theme.json v1 to v2 #36155
Merged
Merged
Migrate theme.json v1 to v2 #36155
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
57d4964
Migrate v1 to v2
oandregal 0959564
Update core theme.json
oandregal 4077f28
Update docs
oandregal ee17a40
Update create block theme tutorial
oandregal 6ef2871
Update theme.json schema
oandregal 0f1b0a7
Add fallbacks for old names
oandregal ebfcb9d
Migrate customRadius to radius in the client
oandregal 065ff33
Migrate customMargin to margin in the client
oandregal 01ccf2b
Migrate customPadding to padding in the client
oandregal a42946e
Migrate customLineHeight to lineHeight in the client
oandregal 9ed8905
Add notes about when something can be removed
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
/** | ||
* Class that implements a WP_Theme_JSON_Schema migrator. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Class that implements a migration from a v1 data structure into a v2 one. | ||
*/ | ||
class WP_Theme_JSON_Schema_V1_To_V2 implements WP_Theme_JSON_Schema { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Classes should have _Gutenberg suffix otherwise when we pass the class with the same name to core we are going to have a crash. |
||
/** | ||
* Maps old properties to their new location within the schema's settings. | ||
* This will be applied at both the defaults and individual block levels. | ||
*/ | ||
const RENAMED_PATHS = array( | ||
'border.customRadius' => 'border.radius', | ||
'spacing.customMargin' => 'spacing.margin', | ||
'spacing.customPadding' => 'spacing.padding', | ||
'typography.customLineHeight' => 'typography.lineHeight', | ||
); | ||
|
||
/** | ||
* Removes the custom prefixes for a few properties | ||
* that were part of v1: | ||
* | ||
* 'border.customRadius' => 'border.radius', | ||
* 'spacing.customMargin' => 'spacing.margin', | ||
* 'spacing.customPadding' => 'spacing.padding', | ||
* 'typography.customLineHeight' => 'typography.lineHeight', | ||
* | ||
* @param array $old Data to migrate. | ||
* | ||
* @return array Data without the custom prefixes. | ||
*/ | ||
public static function migrate( $old ) { | ||
// Copy everything. | ||
$new = $old; | ||
|
||
// Overwrite the things that changed. | ||
if ( isset( $old['settings'] ) ) { | ||
$new['settings'] = self::process_settings( $old['settings'] ); | ||
} | ||
|
||
// Set the new version. | ||
$new['version'] = 2; | ||
|
||
return $new; | ||
} | ||
|
||
/** | ||
* Processes the settings subtree. | ||
* | ||
* @param array $settings Array to process. | ||
* | ||
* @return array The settings in the new format. | ||
*/ | ||
private static function process_settings( $settings ) { | ||
$new_settings = $settings; | ||
|
||
// Process any renamed/moved paths within default settings. | ||
self::rename_settings( $new_settings ); | ||
|
||
// Process individual block settings. | ||
if ( isset( $new_settings['blocks'] ) && is_array( $new_settings['blocks'] ) ) { | ||
foreach ( $new_settings['blocks'] as &$block_settings ) { | ||
self::rename_settings( $block_settings ); | ||
} | ||
} | ||
|
||
return $new_settings; | ||
} | ||
|
||
/** | ||
* Processes a settings array, renaming or moving properties according to | ||
* `self::RENAMED_PATHS`. | ||
* | ||
* @param array $settings Reference to settings either defaults or an individual block's. | ||
* @return void | ||
*/ | ||
private static function rename_settings( &$settings ) { | ||
foreach ( self::RENAMED_PATHS as $original => $renamed ) { | ||
$original_path = explode( '.', $original ); | ||
$renamed_path = explode( '.', $renamed ); | ||
$current_value = _wp_array_get( $settings, $original_path, null ); | ||
|
||
if ( null !== $current_value ) { | ||
gutenberg_experimental_set( $settings, $renamed_path, $current_value ); | ||
self::unset_setting_by_path( $settings, $original_path ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Removes a property from within the provided settings by its path. | ||
* | ||
* @param array $settings Reference to the current settings array. | ||
* @param array $path Path to the property to be removed. | ||
* | ||
* @return void | ||
*/ | ||
private static function unset_setting_by_path( &$settings, $path ) { | ||
$tmp_settings = &$settings; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
$last_key = array_pop( $path ); | ||
foreach ( $path as $key ) { | ||
$tmp_settings = &$tmp_settings[ $key ]; | ||
} | ||
|
||
unset( $tmp_settings[ $last_key ] ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think one day we will need a v2 to v3 migration and v3 to v4...
Having a file "lib/class-wp-theme-json-schema-v1-to-v2.php" and class per migration "WP_Theme_JSON_Schema_V1_To_V2" does not scale well. Files and classes are part of the public API.
Could we have a single migrate class that then has private methods inside per migration we need?