From 69c2a5c433610dff870e2c8dfacdcfe0c48aad2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Wed, 3 Nov 2021 18:36:39 +0100 Subject: [PATCH] Refactor theme.json migrations to be a single class --- lib/class-wp-theme-json-gutenberg.php | 33 +- ...-json-schema-v1-remove-custom-prefixes.php | 116 ------ lib/class-wp-theme-json-schema-v1-to-v2.php | 111 ------ ...-v1.php => class-wp-theme-json-schema.php} | 214 ++++++++++- lib/interface-wp-theme-json-schema.php | 23 -- lib/load.php | 5 +- ...hp => class-wp-theme-json-schema-test.php} | 351 +++++++++++++++++- ...-schema-v1-remove-custom-prefixes-test.php | 182 --------- ...ass-wp-theme-json-schema-v1-to-v2-test.php | 76 ---- 9 files changed, 554 insertions(+), 557 deletions(-) delete mode 100644 lib/class-wp-theme-json-schema-v1-remove-custom-prefixes.php delete mode 100644 lib/class-wp-theme-json-schema-v1-to-v2.php rename lib/{class-wp-theme-json-schema-v0-to-v1.php => class-wp-theme-json-schema.php} (54%) delete mode 100644 lib/interface-wp-theme-json-schema.php rename phpunit/{class-wp-theme-json-schema-v0-to-v1-test.php => class-wp-theme-json-schema-test.php} (60%) delete mode 100644 phpunit/class-wp-theme-json-schema-v1-remove-custom-prefixes-test.php delete mode 100644 phpunit/class-wp-theme-json-schema-v1-to-v2-test.php diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index d1d5e9aa0b7d3e..ebac66858f82f6 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -286,7 +286,7 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { $origin = 'theme'; } - $theme_json = self::migrate( $theme_json ); + $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); $valid_block_names = array_keys( self::get_blocks_metadata() ); $valid_element_names = array_keys( self::ELEMENTS ); @@ -305,35 +305,6 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { } } - /** - * Function that migrates a given theme.json structure to the last version. - * - * @param array $theme_json The structure to migrate. - * - * @return array The structure in the last version. - */ - private static function migrate( $theme_json ) { - // Can be removed when the plugin minimum required version is WordPress 5.8. - // This doesn't need to land in WordPress core. - if ( ! isset( $theme_json['version'] ) || 0 === $theme_json['version'] ) { - $theme_json = WP_Theme_JSON_Schema_V0_To_V1::migrate( $theme_json ); - } - - // Provide backwards compatibility for settings that did not land in 5.8 - // and have had their `custom` prefixed removed since. - // Can be removed when the plugin minimum required version is WordPress 5.9. - // This doesn't need to land in WordPress core. - if ( 1 === $theme_json['version'] ) { - $theme_json = WP_Theme_JSON_Schema_V1_Remove_Custom_Prefixes::migrate( $theme_json ); - } - - if ( 1 === $theme_json['version'] ) { - $theme_json = WP_Theme_JSON_Schema_V1_To_V2::migrate( $theme_json ); - } - - return $theme_json; - } - /** * Sanitizes the input according to the schemas. * @@ -1455,7 +1426,7 @@ private static function is_safe_css_declaration( $property_name, $property_value public static function remove_insecure_properties( $theme_json ) { $sanitized = array(); - $theme_json = self::migrate( $theme_json ); + $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); $valid_block_names = array_keys( self::get_blocks_metadata() ); $valid_element_names = array_keys( self::ELEMENTS ); diff --git a/lib/class-wp-theme-json-schema-v1-remove-custom-prefixes.php b/lib/class-wp-theme-json-schema-v1-remove-custom-prefixes.php deleted file mode 100644 index 4b2e933e002292..00000000000000 --- a/lib/class-wp-theme-json-schema-v1-remove-custom-prefixes.php +++ /dev/null @@ -1,116 +0,0 @@ - 'border.color', - 'border.customStyle' => 'border.style', - 'border.customWidth' => 'border.width', - 'typography.customFontStyle' => 'typography.fontStyle', - 'typography.customFontWeight' => 'typography.fontWeight', - 'typography.customLetterSpacing' => 'typography.letterSpacing', - 'typography.customTextDecorations' => 'typography.textDecoration', - 'typography.customTextTransforms' => 'typography.textTransform', - ); - - /** - * Removes the custom prefixes for a few properties that only worked in the plugin: - * - * 'border.customColor' => 'border.color', - * 'border.customStyle' => 'border.style', - * 'border.customWidth' => 'border.width', - * 'typography.customFontStyle' => 'typography.fontStyle', - * 'typography.customFontWeight' => 'typography.fontWeight', - * 'typography.customLetterSpacing' => 'typography.letterSpacing', - * 'typography.customTextDecorations' => 'typography.textDecoration', - * 'typography.customTextTransforms' => 'typography.textTransform', - * - * @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 change. - if ( isset( $old['settings'] ) ) { - $new['settings'] = self::process_settings( $old['settings'] ); - } - - 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 ] ); - } -} diff --git a/lib/class-wp-theme-json-schema-v1-to-v2.php b/lib/class-wp-theme-json-schema-v1-to-v2.php deleted file mode 100644 index eb6a9ca175a354..00000000000000 --- a/lib/class-wp-theme-json-schema-v1-to-v2.php +++ /dev/null @@ -1,111 +0,0 @@ - '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 ] ); - } -} diff --git a/lib/class-wp-theme-json-schema-v0-to-v1.php b/lib/class-wp-theme-json-schema.php similarity index 54% rename from lib/class-wp-theme-json-schema-v0-to-v1.php rename to lib/class-wp-theme-json-schema.php index e4187da03a7ac8..c2b7c2a67d74ab 100644 --- a/lib/class-wp-theme-json-schema-v0-to-v1.php +++ b/lib/class-wp-theme-json-schema.php @@ -9,21 +9,76 @@ * Class that migrates a given structure in v0 schema to one * that follows the v1 schema. */ -class WP_Theme_JSON_Schema_V0_To_V1 implements WP_Theme_JSON_Schema { +class WP_Theme_JSON_Schema { /** * How to address all the blocks - * in the theme.json file. + * in the v0 of the theme.json file. */ - const ALL_BLOCKS_NAME = 'defaults'; + const V0_ALL_BLOCKS_NAME = 'defaults'; /** * How to address the root block - * in the theme.json file. + * in the v0 of the theme.json file. * * @var string */ - const ROOT_BLOCK_NAME = 'root'; + const V0_ROOT_BLOCK_NAME = 'root'; + + /** + * Maps old properties to their new location within the schema's settings for v1. + * This will be applied at both the defaults and individual block levels. + */ + const V1_RENAMED_PATHS = array( + 'border.customColor' => 'border.color', + 'border.customStyle' => 'border.style', + 'border.customWidth' => 'border.width', + 'typography.customFontStyle' => 'typography.fontStyle', + 'typography.customFontWeight' => 'typography.fontWeight', + 'typography.customLetterSpacing' => 'typography.letterSpacing', + 'typography.customTextDecorations' => 'typography.textDecoration', + 'typography.customTextTransforms' => 'typography.textTransform', + ); + + /** + * 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 V1_TO_V2_RENAMED_PATHS = array( + 'border.customRadius' => 'border.radius', + 'spacing.customMargin' => 'spacing.margin', + 'spacing.customPadding' => 'spacing.padding', + 'typography.customLineHeight' => 'typography.lineHeight', + ); + + /** + * Function that migrates a given theme.json structure to the last version. + * + * @param array $theme_json The structure to migrate. + * + * @return array The structure in the last version. + */ + public static function migrate( $theme_json ) { + // Can be removed when the plugin minimum required version is WordPress 5.8. + // This doesn't need to land in WordPress core. + if ( ! isset( $theme_json['version'] ) || 0 === $theme_json['version'] ) { + $theme_json = self::migrate_v0_to_v1( $theme_json ); + } + + // Provide backwards compatibility for settings that did not land in 5.8 + // and have had their `custom` prefixed removed since. + // Can be removed when the plugin minimum required version is WordPress 5.9. + // This doesn't need to land in WordPress core. + if ( 1 === $theme_json['version'] ) { + $theme_json = self::migrate_v1_remove_custom_prefixes( $theme_json ); + } + + if ( 1 === $theme_json['version'] ) { + $theme_json = self::migrate_v1_to_v2( $theme_json ); + } + + return $theme_json; + } /** * Converts a v0 data structure into a v1 one. @@ -84,16 +139,16 @@ class WP_Theme_JSON_Schema_V0_To_V1 implements WP_Theme_JSON_Schema { * * @return array Data in v1 schema. */ - public static function migrate( $old ) { + private static function migrate_v0_to_v1( $old ) { // Copy everything. $new = $old; // Overwrite the things that change. if ( isset( $old['settings'] ) ) { - $new['settings'] = self::process_settings( $old['settings'] ); + $new['settings'] = self::migrate_v0_to_v1_process_settings( $old['settings'] ); } if ( isset( $old['styles'] ) ) { - $new['styles'] = self::process_styles( $old['styles'] ); + $new['styles'] = self::migrate_v0_to_v1_process_styles( $old['styles'] ); } $new['version'] = 1; @@ -108,7 +163,7 @@ public static function migrate( $old ) { * * @return array The settings in the new format. */ - private static function process_settings( $settings ) { + private static function migrate_v0_to_v1_process_settings( $settings ) { $new = array(); $blocks_to_consolidate = array( 'core/heading/h1' => 'core/heading', @@ -217,7 +272,7 @@ private static function process_settings( $settings ) { * * @return array The styles in the new format. */ - private static function process_styles( $styles ) { + private static function migrate_v0_to_v1_process_styles( $styles ) { $new = array(); $block_heading = array( 'core/heading/h1' => 'h1', @@ -314,4 +369,143 @@ private static function process_styles( $styles ) { return $new; } + + /** + * Removes the custom prefixes for a few properties that only worked in the plugin: + * + * 'border.customColor' => 'border.color', + * 'border.customStyle' => 'border.style', + * 'border.customWidth' => 'border.width', + * 'typography.customFontStyle' => 'typography.fontStyle', + * 'typography.customFontWeight' => 'typography.fontWeight', + * 'typography.customLetterSpacing' => 'typography.letterSpacing', + * 'typography.customTextDecorations' => 'typography.textDecoration', + * 'typography.customTextTransforms' => 'typography.textTransform', + * + * @param array $old Data to migrate. + * + * @return array Data without the custom prefixes. + */ + private static function migrate_v1_remove_custom_prefixes( $old ) { + // Copy everything. + $new = $old; + + // Overwrite the things that change. + if ( isset( $old['settings'] ) ) { + $new['settings'] = self::migrate_v1_remove_custom_prefixes_process_settings( $old['settings'] ); + } + + return $new; + } + + /** + * Processes the settings subtree. + * + * @param array $settings Array to process. + * + * @return array The settings in the new format. + */ + private static function migrate_v1_remove_custom_prefixes_process_settings( $settings ) { + $new_settings = $settings; + + // Process any renamed/moved paths within default settings. + self::rename_settings( $new_settings, self::V1_RENAMED_PATHS ); + + // 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, self::V1_RENAMED_PATHS ); + } + } + + return $new_settings; + } + + /** + * 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. + */ + private static function migrate_v1_to_v2( $old ) { + // Copy everything. + $new = $old; + + // Overwrite the things that changed. + if ( isset( $old['settings'] ) ) { + $new['settings'] = self::migrate_v1_to_v2_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 migrate_v1_to_v2_process_settings( $settings ) { + $new_settings = $settings; + + // Process any renamed/moved paths within default settings. + self::rename_settings( $new_settings, self::V1_TO_V2_RENAMED_PATHS ); + + // 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, self::V1_TO_V2_RENAMED_PATHS ); + } + } + + return $new_settings; + } + + /** + * Processes a settings array, renaming or moving properties. + * + * @param array $settings Reference to settings either defaults or an individual block's. + * @param arary $paths_to_rename Paths to rename. + */ + private static function rename_settings( &$settings, $paths_to_rename ) { + foreach ( $paths_to_rename 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 ] ); + } } diff --git a/lib/interface-wp-theme-json-schema.php b/lib/interface-wp-theme-json-schema.php deleted file mode 100644 index 69c7418c0fcaad..00000000000000 --- a/lib/interface-wp-theme-json-schema.php +++ /dev/null @@ -1,23 +0,0 @@ - array( 'defaults' => array( @@ -172,7 +172,7 @@ function test_migrate() { $this->assertEqualSetsWithIndex( $expected, $actual ); } - function test_get_settings() { + function test_migrate_v0_to_v1_get_settings() { $defaults = WP_Theme_JSON_Schema_V0_To_V1::ALL_BLOCKS_NAME; $root = WP_Theme_JSON_Schema_V0_To_V1::ROOT_BLOCK_NAME; $theme_json = new WP_Theme_JSON_Gutenberg( @@ -358,7 +358,7 @@ function test_get_settings() { $this->assertEqualSetsWithIndex( $expected, $actual ); } - function test_get_stylesheet() { + function test_migrate_v0_to_v1_get_stylesheet() { $root_name = WP_Theme_JSON_Schema_V0_To_V1::ROOT_BLOCK_NAME; $all_blocks_name = WP_Theme_JSON_Schema_V0_To_V1::ALL_BLOCKS_NAME; $theme_json = new WP_Theme_JSON_Gutenberg( array() ); @@ -498,4 +498,347 @@ function test_get_stylesheet() { $this->assertEquals( $variables, $theme_json->get_stylesheet( array( 'variables' ) ) ); } + function test_migrate_v1_remove_custom_prefixes() { + $theme_json_v1 = array( + 'version' => 1, + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'name' => 'Pale Pink', + 'slug' => 'pale-pink', + 'color' => '#f78da7', + ), + array( + 'name' => 'Vivid Red', + 'slug' => 'vivid-red', + 'color' => '#cf2e2e', + ), + ), + 'custom' => false, + 'link' => true, + ), + 'border' => array( + 'customColor' => false, + 'customRadius' => false, + 'customStyle' => false, + 'customWidth' => false, + ), + 'typography' => array( + 'customFontStyle' => false, + 'customFontWeight' => false, + 'customLetterSpacing' => false, + 'customTextDecorations' => false, + 'customTextTransforms' => false, + ), + 'blocks' => array( + 'core/group' => array( + 'border' => array( + 'customColor' => true, + 'customRadius' => true, + 'customStyle' => true, + 'customWidth' => true, + ), + 'typography' => array( + 'customFontStyle' => true, + 'customFontWeight' => true, + 'customLetterSpacing' => true, + 'customTextDecorations' => true, + 'customTextTransforms' => true, + ), + ), + ), + ), + 'styles' => array( + 'color' => array( + 'background' => 'purple', + ), + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'background' => 'red', + ), + 'spacing' => array( + 'padding' => array( + 'top' => '10px', + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'yellow', + ), + ), + ), + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'red', + ), + ), + ), + ), + ); + + $actual = WP_Theme_JSON_Schema_V1_Remove_Custom_Prefixes::migrate( $theme_json_v1 ); + + $expected = array( + 'version' => 1, + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'name' => 'Pale Pink', + 'slug' => 'pale-pink', + 'color' => '#f78da7', + ), + array( + 'name' => 'Vivid Red', + 'slug' => 'vivid-red', + 'color' => '#cf2e2e', + ), + ), + 'custom' => false, + 'link' => true, + ), + 'border' => array( + 'color' => false, + 'customRadius' => false, + 'style' => false, + 'width' => false, + ), + 'typography' => array( + 'fontStyle' => false, + 'fontWeight' => false, + 'letterSpacing' => false, + 'textDecoration' => false, + 'textTransform' => false, + ), + 'blocks' => array( + 'core/group' => array( + 'border' => array( + 'color' => true, + 'customRadius' => true, + 'style' => true, + 'width' => true, + ), + 'typography' => array( + 'fontStyle' => true, + 'fontWeight' => true, + 'letterSpacing' => true, + 'textDecoration' => true, + 'textTransform' => true, + ), + ), + ), + ), + 'styles' => array( + 'color' => array( + 'background' => 'purple', + ), + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'background' => 'red', + ), + 'spacing' => array( + 'padding' => array( + 'top' => '10px', + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'yellow', + ), + ), + ), + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'red', + ), + ), + ), + ), + ); + + $this->assertEqualSetsWithIndex( $expected, $actual ); + } + + function test_migrate_v1_to_v2() { + $theme_json_v1 = array( + 'version' => 1, + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'name' => 'Pale Pink', + 'slug' => 'pale-pink', + 'color' => '#f78da7', + ), + array( + 'name' => 'Vivid Red', + 'slug' => 'vivid-red', + 'color' => '#cf2e2e', + ), + ), + 'custom' => false, + 'link' => true, + ), + 'border' => array( + 'customColor' => false, + 'customRadius' => false, + 'customStyle' => false, + 'customWidth' => false, + ), + 'typography' => array( + 'customFontStyle' => false, + 'customFontWeight' => false, + 'customLetterSpacing' => false, + 'customTextDecorations' => false, + 'customTextTransforms' => false, + ), + 'blocks' => array( + 'core/group' => array( + 'border' => array( + 'customColor' => true, + 'customRadius' => true, + 'customStyle' => true, + 'customWidth' => true, + ), + 'typography' => array( + 'customFontStyle' => true, + 'customFontWeight' => true, + 'customLetterSpacing' => true, + 'customTextDecorations' => true, + 'customTextTransforms' => true, + ), + ), + ), + ), + 'styles' => array( + 'color' => array( + 'background' => 'purple', + ), + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'background' => 'red', + ), + 'spacing' => array( + 'padding' => array( + 'top' => '10px', + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'yellow', + ), + ), + ), + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'red', + ), + ), + ), + ), + ); + + $actual = WP_Theme_JSON_Schema_V1_Remove_Custom_Prefixes::migrate( $theme_json_v1 ); + + $expected = array( + 'version' => 1, + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'name' => 'Pale Pink', + 'slug' => 'pale-pink', + 'color' => '#f78da7', + ), + array( + 'name' => 'Vivid Red', + 'slug' => 'vivid-red', + 'color' => '#cf2e2e', + ), + ), + 'custom' => false, + 'link' => true, + ), + 'border' => array( + 'color' => false, + 'customRadius' => false, + 'style' => false, + 'width' => false, + ), + 'typography' => array( + 'fontStyle' => false, + 'fontWeight' => false, + 'letterSpacing' => false, + 'textDecoration' => false, + 'textTransform' => false, + ), + 'blocks' => array( + 'core/group' => array( + 'border' => array( + 'color' => true, + 'customRadius' => true, + 'style' => true, + 'width' => true, + ), + 'typography' => array( + 'fontStyle' => true, + 'fontWeight' => true, + 'letterSpacing' => true, + 'textDecoration' => true, + 'textTransform' => true, + ), + ), + ), + ), + 'styles' => array( + 'color' => array( + 'background' => 'purple', + ), + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'background' => 'red', + ), + 'spacing' => array( + 'padding' => array( + 'top' => '10px', + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'yellow', + ), + ), + ), + ), + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => 'red', + ), + ), + ), + ), + ); + + $this->assertEqualSetsWithIndex( $expected, $actual ); + } } diff --git a/phpunit/class-wp-theme-json-schema-v1-remove-custom-prefixes-test.php b/phpunit/class-wp-theme-json-schema-v1-remove-custom-prefixes-test.php deleted file mode 100644 index 4d6f05e7887e99..00000000000000 --- a/phpunit/class-wp-theme-json-schema-v1-remove-custom-prefixes-test.php +++ /dev/null @@ -1,182 +0,0 @@ - 1, - 'settings' => array( - 'color' => array( - 'palette' => array( - array( - 'name' => 'Pale Pink', - 'slug' => 'pale-pink', - 'color' => '#f78da7', - ), - array( - 'name' => 'Vivid Red', - 'slug' => 'vivid-red', - 'color' => '#cf2e2e', - ), - ), - 'custom' => false, - 'link' => true, - ), - 'border' => array( - 'customColor' => false, - 'customRadius' => false, - 'customStyle' => false, - 'customWidth' => false, - ), - 'typography' => array( - 'customFontStyle' => false, - 'customFontWeight' => false, - 'customLetterSpacing' => false, - 'customTextDecorations' => false, - 'customTextTransforms' => false, - ), - 'blocks' => array( - 'core/group' => array( - 'border' => array( - 'customColor' => true, - 'customRadius' => true, - 'customStyle' => true, - 'customWidth' => true, - ), - 'typography' => array( - 'customFontStyle' => true, - 'customFontWeight' => true, - 'customLetterSpacing' => true, - 'customTextDecorations' => true, - 'customTextTransforms' => true, - ), - ), - ), - ), - 'styles' => array( - 'color' => array( - 'background' => 'purple', - ), - 'blocks' => array( - 'core/group' => array( - 'color' => array( - 'background' => 'red', - ), - 'spacing' => array( - 'padding' => array( - 'top' => '10px', - ), - ), - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => 'yellow', - ), - ), - ), - ), - ), - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => 'red', - ), - ), - ), - ), - ); - - $actual = WP_Theme_JSON_Schema_V1_Remove_Custom_Prefixes::migrate( $theme_json_v1 ); - - $expected = array( - 'version' => 1, - 'settings' => array( - 'color' => array( - 'palette' => array( - array( - 'name' => 'Pale Pink', - 'slug' => 'pale-pink', - 'color' => '#f78da7', - ), - array( - 'name' => 'Vivid Red', - 'slug' => 'vivid-red', - 'color' => '#cf2e2e', - ), - ), - 'custom' => false, - 'link' => true, - ), - 'border' => array( - 'color' => false, - 'customRadius' => false, - 'style' => false, - 'width' => false, - ), - 'typography' => array( - 'fontStyle' => false, - 'fontWeight' => false, - 'letterSpacing' => false, - 'textDecoration' => false, - 'textTransform' => false, - ), - 'blocks' => array( - 'core/group' => array( - 'border' => array( - 'color' => true, - 'customRadius' => true, - 'style' => true, - 'width' => true, - ), - 'typography' => array( - 'fontStyle' => true, - 'fontWeight' => true, - 'letterSpacing' => true, - 'textDecoration' => true, - 'textTransform' => true, - ), - ), - ), - ), - 'styles' => array( - 'color' => array( - 'background' => 'purple', - ), - 'blocks' => array( - 'core/group' => array( - 'color' => array( - 'background' => 'red', - ), - 'spacing' => array( - 'padding' => array( - 'top' => '10px', - ), - ), - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => 'yellow', - ), - ), - ), - ), - ), - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => 'red', - ), - ), - ), - ), - ); - - $this->assertEqualSetsWithIndex( $expected, $actual ); - } -} diff --git a/phpunit/class-wp-theme-json-schema-v1-to-v2-test.php b/phpunit/class-wp-theme-json-schema-v1-to-v2-test.php deleted file mode 100644 index 6e83afbb51d34f..00000000000000 --- a/phpunit/class-wp-theme-json-schema-v1-to-v2-test.php +++ /dev/null @@ -1,76 +0,0 @@ - 1, - 'settings' => array( - 'border' => array( - 'customRadius' => true, - ), - 'spacing' => array( - 'customMargin' => true, - 'customPadding' => true, - ), - 'typography' => array( - 'customLineHeight' => true, - ), - 'blocks' => array( - 'core/group' => array( - 'border' => array( - 'customRadius' => false, - ), - 'spacing' => array( - 'customMargin' => false, - 'customPadding' => false, - ), - 'typography' => array( - 'customLineHeight' => false, - ), - ), - ), - ), - ); - - $actual = WP_Theme_JSON_Schema_V1_to_V2::migrate( $theme_json_v1 ); - - $expected = array( - 'version' => 2, - 'settings' => array( - 'border' => array( - 'radius' => true, - ), - 'spacing' => array( - 'margin' => true, - 'padding' => true, - ), - 'typography' => array( - 'lineHeight' => true, - ), - 'blocks' => array( - 'core/group' => array( - 'border' => array( - 'radius' => false, - ), - 'spacing' => array( - 'margin' => false, - 'padding' => false, - ), - 'typography' => array( - 'lineHeight' => false, - ), - ), - ), - ), - ); - - $this->assertEqualSetsWithIndex( $expected, $actual ); - } -}