From 6f3244b8d379a61fdacc08b989b82f4fcb24b541 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 23 Jun 2022 17:18:41 +1200 Subject: [PATCH 01/30] Add initial new space preset settings --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 111 ++++++++++++++++++ .../class-wp-theme-json-resolver-6-1.php | 19 +++ lib/compat/wordpress-6.1/theme-i18n.json | 86 ++++++++++++++ lib/compat/wordpress-6.1/theme.json | 24 +++- ...-rest-block-editor-settings-controller.php | 5 + .../src/components/use-setting/index.js | 6 + 6 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 lib/compat/wordpress-6.1/theme-i18n.json diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 946680fb44bdd7..3108bd98a04a64 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -322,4 +322,115 @@ protected function get_block_classes( $style_nodes ) { return $block_rules; } + + /** + * Presets are a set of values that serve + * to bootstrap some styles: colors, font sizes, etc. + * + * They are a unkeyed array of values such as: + * + * ```php + * array( + * array( + * 'slug' => 'unique-name-within-the-set', + * 'name' => 'Name for the UI', + * => 'value' + * ), + * ) + * ``` + * + * This contains the necessary metadata to process them: + * + * - path => Where to find the preset within the settings section. + * - prevent_override => Disables override of default presets by theme presets. + * The relationship between whether to override the defaults + * and whether the defaults are enabled is inverse: + * - If defaults are enabled => theme presets should not be overriden + * - If defaults are disabled => theme presets should be overriden + * For example, a theme sets defaultPalette to false, + * making the default palette hidden from the user. + * In that case, we want all the theme presets to be present, + * so they should override the defaults by setting this false. + * - use_default_names => whether to use the default names + * - value_key => the key that represents the value + * - value_func => optionally, instead of value_key, a function to generate + * the value that takes a preset as an argument + * (either value_key or value_func should be present) + * - css_vars => template string to use in generating the CSS Custom Property. + * Example output: "--wp--preset--duotone--blue: " will generate as many CSS Custom Properties as presets defined + * substituting the $slug for the slug's value for each preset value. + * - classes => array containing a structure with the classes to + * generate for the presets, where for each array item + * the key is the class name and the value the property name. + * The "$slug" substring will be replaced by the slug of each preset. + * For example: + * 'classes' => array( + * '.has-$slug-color' => 'color', + * '.has-$slug-background-color' => 'background-color', + * '.has-$slug-border-color' => 'border-color', + * ) + * - properties => array of CSS properties to be used by kses to + * validate the content of each preset + * by means of the remove_insecure_properties method. + */ + const PRESETS_METADATA = array( + array( + 'path' => array( 'color', 'palette' ), + 'prevent_override' => array( 'color', 'defaultPalette' ), + 'use_default_names' => false, + 'value_key' => 'color', + 'css_vars' => '--wp--preset--color--$slug', + 'classes' => array( + '.has-$slug-color' => 'color', + '.has-$slug-background-color' => 'background-color', + '.has-$slug-border-color' => 'border-color', + ), + 'properties' => array( 'color', 'background-color', 'border-color' ), + ), + array( + 'path' => array( 'color', 'gradients' ), + 'prevent_override' => array( 'color', 'defaultGradients' ), + 'use_default_names' => false, + 'value_key' => 'gradient', + 'css_vars' => '--wp--preset--gradient--$slug', + 'classes' => array( '.has-$slug-gradient-background' => 'background' ), + 'properties' => array( 'background' ), + ), + array( + 'path' => array( 'color', 'duotone' ), + 'prevent_override' => array( 'color', 'defaultDuotone' ), + 'use_default_names' => false, + 'value_func' => 'gutenberg_get_duotone_filter_property', + 'css_vars' => '--wp--preset--duotone--$slug', + 'classes' => array(), + 'properties' => array( 'filter' ), + ), + array( + 'path' => array( 'typography', 'fontSizes' ), + 'prevent_override' => false, + 'use_default_names' => true, + 'value_key' => 'size', + 'css_vars' => '--wp--preset--font-size--$slug', + 'classes' => array( '.has-$slug-font-size' => 'font-size' ), + 'properties' => array( 'font-size' ), + ), + array( + 'path' => array( 'typography', 'fontFamilies' ), + 'prevent_override' => false, + 'use_default_names' => false, + 'value_key' => 'fontFamily', + 'css_vars' => '--wp--preset--font-family--$slug', + 'classes' => array( '.has-$slug-font-family' => 'font-family' ), + 'properties' => array( 'font-family' ), + ), + array( + 'path' => array( 'spacing', 'spacingSizes' ), + 'prevent_override' => false, + 'use_default_names' => true, + 'value_key' => 'size', + 'css_vars' => '--wp--preset--spacing-size--$slug', + 'classes' => array( '.has-$slug-spacing-size' => 'spacing-size' ), + 'properties' => array( 'padding', 'margin' ), + ), + ); } diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php index 58abcc27c04ec5..efb2025d6f4ddb 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php @@ -16,6 +16,25 @@ * @access private */ class WP_Theme_JSON_Resolver_6_1 extends WP_Theme_JSON_Resolver_6_0 { + /** + * Given a theme.json structure modifies it in place + * to update certain values by its translated strings + * according to the language set by the user. + * + * @param array $theme_json The theme.json to translate. + * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. + * Default 'default'. + * @return array Returns the modified $theme_json_structure. + */ + protected static function translate( $theme_json, $domain = 'default' ) { + if ( null === static::$i18n_schema ) { + $i18n_schema = wp_json_file_decode( __DIR__ . '/theme-i18n.json' ); + static::$i18n_schema = null === $i18n_schema ? array() : $i18n_schema; + } + + return translate_settings_using_i18n_schema( static::$i18n_schema, $theme_json, $domain ); + } + /** * Return core's origin config. * diff --git a/lib/compat/wordpress-6.1/theme-i18n.json b/lib/compat/wordpress-6.1/theme-i18n.json new file mode 100644 index 00000000000000..469783409c891c --- /dev/null +++ b/lib/compat/wordpress-6.1/theme-i18n.json @@ -0,0 +1,86 @@ +{ + "title": "Style variation name", + "settings": { + "typography": { + "fontSizes": [ + { + "name": "Font size name" + } + ], + "fontFamilies": [ + { + "name": "Font family name" + } + ] + }, + "color": { + "palette": [ + { + "name": "Color name" + } + ], + "gradients": [ + { + "name": "Gradient name" + } + ], + "duotone": [ + { + "name": "Duotone name" + } + ] + }, + "spacing": { + "spacingSizes": [ + { + "name": "Space size name" + } + ] + }, + "blocks": { + "*": { + "typography": { + "fontSizes": [ + { + "name": "Font size name" + } + ], + "fontFamilies": [ + { + "name": "Font family name" + } + ] + }, + "color": { + "palette": [ + { + "name": "Color name" + } + ], + "gradients": [ + { + "name": "Gradient name" + } + ] + }, + "spacing": { + "spacingSizes": [ + { + "name": "Space size name" + } + ] + } + } + } + }, + "customTemplates": [ + { + "title": "Custom template name" + } + ], + "templateParts": [ + { + "title": "Template part name" + } + ] +} diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index 00acba508d0336..de71b8a5a6fce2 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -189,7 +189,29 @@ "blockGap": null, "margin": false, "padding": false, - "units": [ "px", "em", "rem", "vh", "vw", "%" ] + "units": [ "px", "em", "rem", "vh", "vw", "%" ], + "spacingSizes": [ + { + "name": "Small", + "slug": "small", + "size": "4px" + }, + { + "name": "Medium", + "slug": "medium", + "size": "8px" + }, + { + "name": "Large", + "slug": "large", + "size": "16px" + }, + { + "name": "Extra Large", + "slug": "x-large", + "size": "32px" + } + ] }, "typography": { "customFontSize": true, diff --git a/lib/experimental/class-wp-rest-block-editor-settings-controller.php b/lib/experimental/class-wp-rest-block-editor-settings-controller.php index 0d1aa47fb2b02b..19031fd3d3dfea 100644 --- a/lib/experimental/class-wp-rest-block-editor-settings-controller.php +++ b/lib/experimental/class-wp-rest-block-editor-settings-controller.php @@ -275,6 +275,11 @@ public function get_item_schema() { 'type' => 'array', 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), + 'spacingSizesSizes' => array( + 'description' => __( 'Active theme spacing sizes.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), + ), ), ); diff --git a/packages/block-editor/src/components/use-setting/index.js b/packages/block-editor/src/components/use-setting/index.js index 3d37a85de16934..2c955b069cd8f6 100644 --- a/packages/block-editor/src/components/use-setting/index.js +++ b/packages/block-editor/src/components/use-setting/index.js @@ -40,6 +40,12 @@ const deprecatedFlags = { ? undefined : ! settings.disableCustomFontSizes, 'typography.lineHeight': ( settings ) => settings.enableCustomLineHeight, + 'spacing.spacingSizes': ( settings ) => + settings.spacingSizes === undefined ? undefined : settings.spacingSizes, + 'spacing.customSpaceSize': ( settings ) => + settings.disableCustomSpaceSizes === undefined + ? undefined + : ! settings.disableCustomSpaceSizes, 'spacing.units': ( settings ) => { if ( settings.enableCustomUnits === undefined ) { return; From 5671bfef8c432b3b6c992ff0256ab43d8a0d9c1c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 23 Jun 2022 17:19:41 +1200 Subject: [PATCH 02/30] Update theme.json schema --- lib/compat/wordpress-6.1/theme.json | 1 + schemas/json/theme.json | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index de71b8a5a6fce2..b4feaf4aedc2e0 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -189,6 +189,7 @@ "blockGap": null, "margin": false, "padding": false, + "customSpacingSizes": true, "units": [ "px", "em", "rem", "vh", "vw", "%" ], "spacingSizes": [ { diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 75e346e0982f5b..4eb54b910de960 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -226,6 +226,33 @@ "type": "string" }, "default": [ "px", "em", "rem", "vh", "vw", "%" ] + }, + "customSpacingSizes": { + "description": "Allow users to set custom space sizes.", + "type": "boolean", + "default": true + }, + "spacingSizes": { + "description": "Space size presets for the space size selector.\nGenerates a single class (`.has-{slug}-space-size`) and custom property (`--wp--preset--space-size--{slug}`) per preset value.", + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "description": "Name of the space size preset, translatable.", + "type": "string" + }, + "slug": { + "description": "Kebab-case unique identifier for the space size preset.", + "type": "string" + }, + "size": { + "description": "CSS space-size value, including units.", + "type": "string" + } + }, + "additionalProperties": false + } } }, "additionalProperties": false From 572a074cbdbc84af7791c718a83fc0b756f895b0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 7 Jun 2022 11:25:55 +1200 Subject: [PATCH 03/30] Remove invalid deprecated settings --- packages/block-editor/src/components/use-setting/index.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/block-editor/src/components/use-setting/index.js b/packages/block-editor/src/components/use-setting/index.js index 2c955b069cd8f6..3d37a85de16934 100644 --- a/packages/block-editor/src/components/use-setting/index.js +++ b/packages/block-editor/src/components/use-setting/index.js @@ -40,12 +40,6 @@ const deprecatedFlags = { ? undefined : ! settings.disableCustomFontSizes, 'typography.lineHeight': ( settings ) => settings.enableCustomLineHeight, - 'spacing.spacingSizes': ( settings ) => - settings.spacingSizes === undefined ? undefined : settings.spacingSizes, - 'spacing.customSpaceSize': ( settings ) => - settings.disableCustomSpaceSizes === undefined - ? undefined - : ! settings.disableCustomSpaceSizes, 'spacing.units': ( settings ) => { if ( settings.enableCustomUnits === undefined ) { return; From d4b4cfea898cb01e65f2c4b489fd3fb659826080 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 23 Jun 2022 17:20:30 +1200 Subject: [PATCH 04/30] More theme.json changes --- .../wordpress-6.0/block-editor-settings.php | 146 --------------- .../wordpress-6.1/block-editor-settings.php | 166 ++++++++++++++++++ .../wordpress-6.1/class-wp-theme-json-6-1.php | 53 ++++++ lib/load.php | 1 + schemas/json/theme.json | 2 +- 5 files changed, 221 insertions(+), 147 deletions(-) create mode 100644 lib/compat/wordpress-6.1/block-editor-settings.php diff --git a/lib/compat/wordpress-6.0/block-editor-settings.php b/lib/compat/wordpress-6.0/block-editor-settings.php index 9030998aed250f..21f1f05bb45f1f 100644 --- a/lib/compat/wordpress-6.0/block-editor-settings.php +++ b/lib/compat/wordpress-6.0/block-editor-settings.php @@ -47,149 +47,3 @@ function gutenberg_is_global_styles_in_5_9( $style ) { return false; } - -/** - * Adds styles and __experimentalFeatures to the block editor settings. - * - * @param array $settings Existing block editor settings. - * - * @return array New block editor settings. - */ -function gutenberg_get_block_editor_settings( $settings ) { - // Set what is the context for this data request. - $context = 'other'; - if ( - defined( 'REST_REQUEST' ) && - REST_REQUEST && - isset( $_GET['context'] ) && - 'mobile' === $_GET['context'] - ) { - $context = 'mobile'; - } - - if ( 'other' === $context ) { - global $wp_version; - $is_wp_5_8 = version_compare( $wp_version, '5.8', '>=' ) && version_compare( $wp_version, '5.9', '<' ); - $is_wp_5_9 = version_compare( $wp_version, '5.9', '>=' ) && version_compare( $wp_version, '6.0-beta1', '<' ); - $is_wp_6_0 = version_compare( $wp_version, '6.0-beta1', '>=' ); - - // Make sure the styles array exists. - // In some contexts, like the navigation editor, it doesn't. - if ( ! isset( $settings['styles'] ) ) { - $settings['styles'] = array(); - } - - // Remove existing global styles provided by core. - $styles_without_existing_global_styles = array(); - foreach ( $settings['styles'] as $style ) { - if ( - ( $is_wp_5_8 && ! gutenberg_is_global_styles_in_5_8( $style ) ) || // Can be removed when plugin minimum version is 5.9. - ( $is_wp_5_9 && ! gutenberg_is_global_styles_in_5_9( $style ) ) || // Can be removed when plugin minimum version is 6.0. - ( $is_wp_6_0 && ( ! isset( $style['isGlobalStyles'] ) || ! $style['isGlobalStyles'] ) ) - ) { - $styles_without_existing_global_styles[] = $style; - } - } - - // Recreate global styles. - $new_global_styles = array(); - $presets = array( - array( - 'css' => 'variables', - '__unstableType' => 'presets', - 'isGlobalStyles' => true, - ), - array( - 'css' => 'presets', - '__unstableType' => 'presets', - 'isGlobalStyles' => true, - ), - ); - foreach ( $presets as $preset_style ) { - $actual_css = gutenberg_get_global_stylesheet( array( $preset_style['css'] ) ); - if ( '' !== $actual_css ) { - $preset_style['css'] = $actual_css; - $new_global_styles[] = $preset_style; - } - } - - if ( WP_Theme_JSON_Resolver::theme_has_support() ) { - $block_classes = array( - 'css' => 'styles', - '__unstableType' => 'theme', - 'isGlobalStyles' => true, - ); - $actual_css = gutenberg_get_global_stylesheet( array( $block_classes['css'] ) ); - if ( '' !== $actual_css ) { - $block_classes['css'] = $actual_css; - $new_global_styles[] = $block_classes; - } - } - - $settings['styles'] = array_merge( $new_global_styles, $styles_without_existing_global_styles ); - } - - // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php. - $settings['__experimentalFeatures'] = gutenberg_get_global_settings(); - - if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) { - $colors_by_origin = $settings['__experimentalFeatures']['color']['palette']; - $settings['colors'] = isset( $colors_by_origin['custom'] ) ? - $colors_by_origin['custom'] : ( - isset( $colors_by_origin['theme'] ) ? - $colors_by_origin['theme'] : - $colors_by_origin['default'] - ); - } - - if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) { - $gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients']; - $settings['gradients'] = isset( $gradients_by_origin['custom'] ) ? - $gradients_by_origin['custom'] : ( - isset( $gradients_by_origin['theme'] ) ? - $gradients_by_origin['theme'] : - $gradients_by_origin['default'] - ); - } - - if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { - $font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes']; - $settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ? - $font_sizes_by_origin['custom'] : ( - isset( $font_sizes_by_origin['theme'] ) ? - $font_sizes_by_origin['theme'] : - $font_sizes_by_origin['default'] - ); - } - - if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) { - $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom']; - unset( $settings['__experimentalFeatures']['color']['custom'] ); - } - if ( isset( $settings['__experimentalFeatures']['color']['customGradient'] ) ) { - $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient']; - unset( $settings['__experimentalFeatures']['color']['customGradient'] ); - } - if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { - $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize']; - unset( $settings['__experimentalFeatures']['typography']['customFontSize'] ); - } - if ( isset( $settings['__experimentalFeatures']['typography']['lineHeight'] ) ) { - $settings['enableCustomLineHeight'] = $settings['__experimentalFeatures']['typography']['lineHeight']; - unset( $settings['__experimentalFeatures']['typography']['lineHeight'] ); - } - if ( isset( $settings['__experimentalFeatures']['spacing']['units'] ) ) { - $settings['enableCustomUnits'] = $settings['__experimentalFeatures']['spacing']['units']; - unset( $settings['__experimentalFeatures']['spacing']['units'] ); - } - if ( isset( $settings['__experimentalFeatures']['spacing']['padding'] ) ) { - $settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['padding']; - unset( $settings['__experimentalFeatures']['spacing']['padding'] ); - } - - $settings['localAutosaveInterval'] = 15; - - return $settings; -} - -add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX ); diff --git a/lib/compat/wordpress-6.1/block-editor-settings.php b/lib/compat/wordpress-6.1/block-editor-settings.php new file mode 100644 index 00000000000000..ed591cf60196ae --- /dev/null +++ b/lib/compat/wordpress-6.1/block-editor-settings.php @@ -0,0 +1,166 @@ +=' ) && version_compare( $wp_version, '5.9', '<' ); + $is_wp_5_9 = version_compare( $wp_version, '5.9', '>=' ) && version_compare( $wp_version, '6.0-beta1', '<' ); + $is_wp_6_0 = version_compare( $wp_version, '6.0-beta1', '>=' ); + + // Make sure the styles array exists. + // In some contexts, like the navigation editor, it doesn't. + if ( ! isset( $settings['styles'] ) ) { + $settings['styles'] = array(); + } + + // Remove existing global styles provided by core. + $styles_without_existing_global_styles = array(); + foreach ( $settings['styles'] as $style ) { + if ( + ( $is_wp_5_8 && ! gutenberg_is_global_styles_in_5_8( $style ) ) || // Can be removed when plugin minimum version is 5.9. + ( $is_wp_5_9 && ! gutenberg_is_global_styles_in_5_9( $style ) ) || // Can be removed when plugin minimum version is 6.0. + ( $is_wp_6_0 && ( ! isset( $style['isGlobalStyles'] ) || ! $style['isGlobalStyles'] ) ) + ) { + $styles_without_existing_global_styles[] = $style; + } + } + + // Recreate global styles. + $new_global_styles = array(); + $presets = array( + array( + 'css' => 'variables', + '__unstableType' => 'presets', + 'isGlobalStyles' => true, + ), + array( + 'css' => 'presets', + '__unstableType' => 'presets', + 'isGlobalStyles' => true, + ), + ); + foreach ( $presets as $preset_style ) { + $actual_css = gutenberg_get_global_stylesheet( array( $preset_style['css'] ) ); + if ( '' !== $actual_css ) { + $preset_style['css'] = $actual_css; + $new_global_styles[] = $preset_style; + } + } + + if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + $block_classes = array( + 'css' => 'styles', + '__unstableType' => 'theme', + 'isGlobalStyles' => true, + ); + $actual_css = gutenberg_get_global_stylesheet( array( $block_classes['css'] ) ); + if ( '' !== $actual_css ) { + $block_classes['css'] = $actual_css; + $new_global_styles[] = $block_classes; + } + } + + $settings['styles'] = array_merge( $new_global_styles, $styles_without_existing_global_styles ); + } + + // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php. + $settings['__experimentalFeatures'] = gutenberg_get_global_settings(); + + if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) { + $colors_by_origin = $settings['__experimentalFeatures']['color']['palette']; + $settings['colors'] = isset( $colors_by_origin['custom'] ) ? + $colors_by_origin['custom'] : ( + isset( $colors_by_origin['theme'] ) ? + $colors_by_origin['theme'] : + $colors_by_origin['default'] + ); + } + + if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) { + $gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients']; + $settings['gradients'] = isset( $gradients_by_origin['custom'] ) ? + $gradients_by_origin['custom'] : ( + isset( $gradients_by_origin['theme'] ) ? + $gradients_by_origin['theme'] : + $gradients_by_origin['default'] + ); + } + + if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { + $font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes']; + $settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ? + $font_sizes_by_origin['custom'] : ( + isset( $font_sizes_by_origin['theme'] ) ? + $font_sizes_by_origin['theme'] : + $font_sizes_by_origin['default'] + ); + } + + if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) { + $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom']; + unset( $settings['__experimentalFeatures']['color']['custom'] ); + } + if ( isset( $settings['__experimentalFeatures']['color']['customGradient'] ) ) { + $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient']; + unset( $settings['__experimentalFeatures']['color']['customGradient'] ); + } + if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { + $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize']; + unset( $settings['__experimentalFeatures']['typography']['customFontSize'] ); + } + if ( isset( $settings['__experimentalFeatures']['typography']['lineHeight'] ) ) { + $settings['enableCustomLineHeight'] = $settings['__experimentalFeatures']['typography']['lineHeight']; + unset( $settings['__experimentalFeatures']['typography']['lineHeight'] ); + } + if ( isset( $settings['__experimentalFeatures']['spacing']['units'] ) ) { + $settings['enableCustomUnits'] = $settings['__experimentalFeatures']['spacing']['units']; + unset( $settings['__experimentalFeatures']['spacing']['units'] ); + } + if ( isset( $settings['__experimentalFeatures']['spacing']['padding'] ) ) { + $settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['padding']; + unset( $settings['__experimentalFeatures']['spacing']['padding'] ); + } + if ( isset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) { + $settings['disableCustomSpacingSize'] = ! $settings['__experimentalFeatures']['spacing']['customSpacingSize']; + unset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] ); + } + + if ( isset( $settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) { + $spacing_sizes_by_origin = $settings['__experimentalFeatures']['spacing']['spacingSizes']; + $settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ? + $spacing_sizes_by_origin['custom'] : ( + isset( $spacing_sizes_by_origin['theme'] ) ? + $spacing_sizes_by_origin['theme'] : + $spacing_sizes_by_origin['default'] + ); + } + + $settings['localAutosaveInterval'] = 15; + + return $settings; +} + +add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX ); diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 3108bd98a04a64..b4f0d807f2aaa7 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -433,4 +433,57 @@ protected function get_block_classes( $style_nodes ) { 'properties' => array( 'padding', 'margin' ), ), ); + + /** + * The valid properties under the settings key. + * + * @var array + */ + const VALID_SETTINGS = array( + 'appearanceTools' => null, + 'border' => array( + 'color' => null, + 'radius' => null, + 'style' => null, + 'width' => null, + ), + 'color' => array( + 'background' => null, + 'custom' => null, + 'customDuotone' => null, + 'customGradient' => null, + 'defaultGradients' => null, + 'defaultPalette' => null, + 'duotone' => null, + 'gradients' => null, + 'link' => null, + 'palette' => null, + 'text' => null, + ), + 'custom' => null, + 'layout' => array( + 'contentSize' => null, + 'wideSize' => null, + ), + 'spacing' => array( + 'customSpacingSize' => null, + 'spacingSizes' => null, + 'blockGap' => null, + 'margin' => null, + 'padding' => null, + 'units' => null, + ), + 'typography' => array( + 'customFontSize' => null, + 'dropCap' => null, + 'fontFamilies' => null, + 'fontSizes' => null, + 'fontStyle' => null, + 'fontWeight' => null, + 'letterSpacing' => null, + 'lineHeight' => null, + 'textDecoration' => null, + 'textTransform' => null, + ), + ); } diff --git a/lib/load.php b/lib/load.php index d76ec3181b7cfe..8c77ab5ee0ba43 100644 --- a/lib/load.php +++ b/lib/load.php @@ -124,6 +124,7 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.1 compat. require __DIR__ . '/compat/wordpress-6.1/blocks.php'; +require __DIR__ . '/compat/wordpress-6.1/block-editor-settings.php'; require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php'; require __DIR__ . '/compat/wordpress-6.1/get-global-styles-and-settings.php'; require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-6-1.php'; diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 4eb54b910de960..1c77ee8b59a3c3 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -227,7 +227,7 @@ }, "default": [ "px", "em", "rem", "vh", "vw", "%" ] }, - "customSpacingSizes": { + "customSpacingSize": { "description": "Allow users to set custom space sizes.", "type": "boolean", "default": true From b700c2ee800faf011c5f387e63129c0138010979 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 7 Jun 2022 13:37:20 +1200 Subject: [PATCH 05/30] Change size-slug to numbers to make it easier for theme authors to insert additional options --- lib/compat/wordpress-6.1/theme.json | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index b4feaf4aedc2e0..b2296b2a8c763e 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -193,24 +193,29 @@ "units": [ "px", "em", "rem", "vh", "vw", "%" ], "spacingSizes": [ { - "name": "Small", - "slug": "small", + "name": "S", + "slug": "10", "size": "4px" }, { - "name": "Medium", - "slug": "medium", + "name": "M", + "slug": "20", "size": "8px" }, { - "name": "Large", - "slug": "large", + "name": "L", + "slug": "30", "size": "16px" }, { - "name": "Extra Large", - "slug": "x-large", + "name": "XL", + "slug": "40", "size": "32px" + }, + { + "name": "2XL", + "slug": "50", + "size": "64px" } ] }, From f8590b1b65b94baf32666fe1483f19588d708ca0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 8 Jun 2022 09:46:53 +1200 Subject: [PATCH 06/30] Change naming from t-shirt sizes to full word --- lib/compat/wordpress-6.1/theme.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index b2296b2a8c763e..306a78b656c1b1 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -193,27 +193,27 @@ "units": [ "px", "em", "rem", "vh", "vw", "%" ], "spacingSizes": [ { - "name": "S", + "name": "Extra small", "slug": "10", "size": "4px" }, { - "name": "M", + "name": "Small", "slug": "20", "size": "8px" }, { - "name": "L", + "name": "Medium", "slug": "30", "size": "16px" }, { - "name": "XL", + "name": "Large", "slug": "40", "size": "32px" }, { - "name": "2XL", + "name": "Extra large", "slug": "50", "size": "64px" } From 1498862215e9875f913ce7572618f43b8fa4c245 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 8 Jun 2022 10:40:03 +1200 Subject: [PATCH 07/30] Update docs --- docs/reference-guides/theme-json-reference/theme-json-living.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/reference-guides/theme-json-reference/theme-json-living.md b/docs/reference-guides/theme-json-reference/theme-json-living.md index 6d0c02e8d27ed3..6f2d07748ddf6e 100644 --- a/docs/reference-guides/theme-json-reference/theme-json-living.md +++ b/docs/reference-guides/theme-json-reference/theme-json-living.md @@ -83,6 +83,8 @@ Settings related to spacing. | margin | boolean | false | | | padding | boolean | false | | | units | array | px,em,rem,vh,vw,% | | +| customSpacingSize | boolean | true | | +| spacingSizes | array | | name, size, slug | --- From ce4f4300fe91abef53e29017fc6c5ceb982fd69a Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 8 Jun 2022 13:59:16 +1200 Subject: [PATCH 08/30] Remove class generation for now as possibly not needed. --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index b4f0d807f2aaa7..8f2e0e1fa4bd84 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -429,7 +429,6 @@ protected function get_block_classes( $style_nodes ) { 'use_default_names' => true, 'value_key' => 'size', 'css_vars' => '--wp--preset--spacing-size--$slug', - 'classes' => array( '.has-$slug-spacing-size' => 'spacing-size' ), 'properties' => array( 'padding', 'margin' ), ), ); From 94437305402c22f6bf7b897222f89c5b4b66505d Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 8 Jun 2022 14:09:39 +1200 Subject: [PATCH 09/30] switch to empty array to match duotone --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 8f2e0e1fa4bd84..423f3109cbd57d 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -429,6 +429,7 @@ protected function get_block_classes( $style_nodes ) { 'use_default_names' => true, 'value_key' => 'size', 'css_vars' => '--wp--preset--spacing-size--$slug', + 'classes' => array(), 'properties' => array( 'padding', 'margin' ), ), ); From 8618cb9fe777423b0b508a11fee1edd0cbc9c9ec Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 10 Jun 2022 12:07:35 +1200 Subject: [PATCH 10/30] Add a spacing scale option --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 80 +++++++++++++++++++ lib/compat/wordpress-6.1/theme.json | 27 ++++--- 2 files changed, 97 insertions(+), 10 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 423f3109cbd57d..bccb967f24c198 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -432,6 +432,15 @@ protected function get_block_classes( $style_nodes ) { 'classes' => array(), 'properties' => array( 'padding', 'margin' ), ), + array( + 'path' => array( 'spacing', 'spacingScale' ), + 'prevent_override' => false, + 'use_default_names' => true, + 'value_key' => 'size', + 'css_vars' => '--wp--preset--spacing-size--$slug', + 'classes' => array(), + 'properties' => array( 'padding', 'margin' ), + ), ); /** @@ -468,6 +477,7 @@ protected function get_block_classes( $style_nodes ) { 'spacing' => array( 'customSpacingSize' => null, 'spacingSizes' => null, + 'spacingScale' => null, 'blockGap' => null, 'margin' => null, 'padding' => null, @@ -486,4 +496,74 @@ protected function get_block_classes( $style_nodes ) { 'textTransform' => null, ), ); + + /** + * Given the block settings, it extracts the CSS Custom Properties + * for the presets and adds them to the $declarations array + * following the format: + * + * ```php + * array( + * 'name' => 'property_name', + * 'value' => 'property_value, + * ) + * ``` + * + * @param array $settings Settings to process. + * @param array $origins List of origins to process. + * @return array Returns the modified $declarations. + */ + protected static function compute_preset_vars( $settings, $origins ) { + $declarations = array(); + + // Theme.json can specify either a spacingScale to autogenerate spacing size presets, or a fixed array of spacingSizes. + // The spaceScale takes presidence so will replace the spacingSizes array if present. + if ( isset( $settings['spacing']['spacingScale'] ) ) { + $settings['spacing']['spacingSizes'] = array( 'default' => static::get_spacing_sizes( $settings['spacing']['spacingScale'] ) ); + } + + foreach ( static::PRESETS_METADATA as $preset_metadata ) { + + $values_by_slug = static::get_settings_values_by_slug( $settings, $preset_metadata, $origins ); + + foreach ( $values_by_slug as $slug => $value ) { + $declarations[] = array( + 'name' => static::replace_slug_in_string( $preset_metadata['css_vars'], $slug ), + 'value' => $value, + ); + } + } + + return $declarations; + } + + /** + * Transform the spacing scale values into an array of spacing scale presets. + * + * @param array $spacing_scale scale Array of values used to create the scale of spacing presets. + * @return array An array of spacing preset sizes. + */ + protected static function get_spacing_sizes( $spacing_scale ) { + $spacing_sizes = array(); + $spacing_sizes[0] = array( + 'name' => 1, + 'slug' => 1, + 'size' => $spacing_scale['firstStep'] . $spacing_scale['units'], + ); + $current_step = $spacing_scale['firstStep']; + + for ( $x = 1; $x <= $spacing_scale['steps'] - 1; $x++ ) { + $current_step = '+' === $spacing_scale['operator'] + ? $current_step + $spacing_scale['increment'] . $spacing_scale['units'] + : $current_step * $spacing_scale['increment'] . $spacing_scale['units']; + + $spacing_sizes[ $x + 1 ] = array( + 'name' => $x + 1, + 'slug' => $x + 1, + 'size' => $current_step, + ); + } + + return $spacing_sizes; + } } diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index 306a78b656c1b1..90232cc5bc439e 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -191,31 +191,38 @@ "padding": false, "customSpacingSizes": true, "units": [ "px", "em", "rem", "vh", "vw", "%" ], + "spacingScale": { + "operator": "+", + "increment": 0.5, + "steps": 5, + "firstStep": 0.5, + "units": "rem" + }, "spacingSizes": [ { "name": "Extra small", - "slug": "10", - "size": "4px" + "slug": "1", + "size": "0.5rem" }, { "name": "Small", - "slug": "20", - "size": "8px" + "slug": "2", + "size": "1rem" }, { "name": "Medium", - "slug": "30", - "size": "16px" + "slug": "3", + "size": "1.5rem" }, { "name": "Large", - "slug": "40", - "size": "32px" + "slug": "4", + "size": "2rem" }, { "name": "Extra large", - "slug": "50", - "size": "64px" + "slug": "5", + "size": "2.5rem" } ] }, From ec158d4248e91dd2811a78e728728772bd237471 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 10 Jun 2022 14:05:21 +1200 Subject: [PATCH 11/30] An a 0 space option --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 14 ++++++++++---- lib/compat/wordpress-6.1/theme.json | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index bccb967f24c198..3cb8324a303e4e 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -544,20 +544,26 @@ protected static function compute_preset_vars( $settings, $origins ) { * @return array An array of spacing preset sizes. */ protected static function get_spacing_sizes( $spacing_scale ) { - $spacing_sizes = array(); - $spacing_sizes[0] = array( + $spacing_sizes = array(); + $spacing_sizes[] = array( + 'name' => 0, + 'slug' => 0, + 'size' => 0, + ); + $spacing_sizes[] = array( 'name' => 1, 'slug' => 1, 'size' => $spacing_scale['firstStep'] . $spacing_scale['units'], ); - $current_step = $spacing_scale['firstStep']; + + $current_step = $spacing_scale['firstStep']; for ( $x = 1; $x <= $spacing_scale['steps'] - 1; $x++ ) { $current_step = '+' === $spacing_scale['operator'] ? $current_step + $spacing_scale['increment'] . $spacing_scale['units'] : $current_step * $spacing_scale['increment'] . $spacing_scale['units']; - $spacing_sizes[ $x + 1 ] = array( + $spacing_sizes[] = array( 'name' => $x + 1, 'slug' => $x + 1, 'size' => $current_step, diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index 90232cc5bc439e..5ba128c0b387d8 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -199,6 +199,11 @@ "units": "rem" }, "spacingSizes": [ + { + "name": "None", + "slug": "0", + "size": "0" + }, { "name": "Extra small", "slug": "1", From 91b3e7f05920d297efbc307531c8012e2b1f06f4 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 10 Jun 2022 15:41:25 +1200 Subject: [PATCH 12/30] Switch back to slugs as multiples of ten to make it is for themes to add additional values between core defaults --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 4 ++-- lib/compat/wordpress-6.1/theme.json | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 3cb8324a303e4e..8b603ac7d3cdf8 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -552,7 +552,7 @@ protected static function get_spacing_sizes( $spacing_scale ) { ); $spacing_sizes[] = array( 'name' => 1, - 'slug' => 1, + 'slug' => 10, 'size' => $spacing_scale['firstStep'] . $spacing_scale['units'], ); @@ -565,7 +565,7 @@ protected static function get_spacing_sizes( $spacing_scale ) { $spacing_sizes[] = array( 'name' => $x + 1, - 'slug' => $x + 1, + 'slug' => ( $x + 1 ) * 10, 'size' => $current_step, ); } diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index 5ba128c0b387d8..85f24f669f0ccc 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -206,27 +206,27 @@ }, { "name": "Extra small", - "slug": "1", + "slug": "10", "size": "0.5rem" }, { "name": "Small", - "slug": "2", + "slug": "20", "size": "1rem" }, { "name": "Medium", - "slug": "3", + "slug": "30", "size": "1.5rem" }, { "name": "Large", - "slug": "4", + "slug": "40", "size": "2rem" }, { "name": "Extra large", - "slug": "5", + "slug": "50", "size": "2.5rem" } ] From 17ca2ce972dca105c30f1c31a883fd6773b30f46 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 13 Jun 2022 12:13:42 +1200 Subject: [PATCH 13/30] Give a theme's spacingSizes setting priority over core spacingScale --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 2 +- lib/compat/wordpress-6.1/theme.json | 34 +------------------ ...-rest-block-editor-settings-controller.php | 7 +++- schemas/json/theme.json | 27 +++++++++++++++ 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 8b603ac7d3cdf8..809b9751711c9a 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -518,7 +518,7 @@ protected static function compute_preset_vars( $settings, $origins ) { // Theme.json can specify either a spacingScale to autogenerate spacing size presets, or a fixed array of spacingSizes. // The spaceScale takes presidence so will replace the spacingSizes array if present. - if ( isset( $settings['spacing']['spacingScale'] ) ) { + if ( ! isset( $settings['spacing']['spacingSizes']['theme'] ) ) { $settings['spacing']['spacingSizes'] = array( 'default' => static::get_spacing_sizes( $settings['spacing']['spacingScale'] ) ); } diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index 85f24f669f0ccc..32b46c5d1488dd 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -197,39 +197,7 @@ "steps": 5, "firstStep": 0.5, "units": "rem" - }, - "spacingSizes": [ - { - "name": "None", - "slug": "0", - "size": "0" - }, - { - "name": "Extra small", - "slug": "10", - "size": "0.5rem" - }, - { - "name": "Small", - "slug": "20", - "size": "1rem" - }, - { - "name": "Medium", - "slug": "30", - "size": "1.5rem" - }, - { - "name": "Large", - "slug": "40", - "size": "2rem" - }, - { - "name": "Extra large", - "slug": "50", - "size": "2.5rem" - } - ] + } }, "typography": { "customFontSize": true, diff --git a/lib/experimental/class-wp-rest-block-editor-settings-controller.php b/lib/experimental/class-wp-rest-block-editor-settings-controller.php index 19031fd3d3dfea..34a6debf30bce4 100644 --- a/lib/experimental/class-wp-rest-block-editor-settings-controller.php +++ b/lib/experimental/class-wp-rest-block-editor-settings-controller.php @@ -275,11 +275,16 @@ public function get_item_schema() { 'type' => 'array', 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), - 'spacingSizesSizes' => array( + 'spacingSizes' => array( 'description' => __( 'Active theme spacing sizes.', 'gutenberg' ), 'type' => 'array', 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), + 'spacingScale' => array( + 'description' => __( 'Active theme spacing scale.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), + ), ), ); diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 1c77ee8b59a3c3..a8684d6bb95673 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -253,6 +253,33 @@ }, "additionalProperties": false } + }, + "spacingScale": { + "description": "Space size presets for the space size selector.\nGenerates a single class (`.has-{slug}-space-size`) and custom property (`--wp--preset--space-size--{slug}`) per preset value.", + "type": "object", + "properties": { + "operator": { + "description": "With + or * depending on whether scale is generated by increment or mulitplier.", + "type": "string" + }, + "increment": { + "description": "The amouunt to increment each step by", + "type": "number" + }, + "steps": { + "description": "Number of steps to generate in scale", + "type": "number" + }, + "firstStep": { + "description": "The value to starr scale from", + "type": "number" + }, + "units": { + "description": "Units that the scale uses, eg. rem, em, px", + "type": "string" + } + }, + "additionalProperties": false } }, "additionalProperties": false From 7170253f799fa8565dd725c69cacda3c6f27d2b2 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 13 Jun 2022 12:17:03 +1200 Subject: [PATCH 14/30] Update comment --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 809b9751711c9a..0993f0f593c3a7 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -517,7 +517,7 @@ protected static function compute_preset_vars( $settings, $origins ) { $declarations = array(); // Theme.json can specify either a spacingScale to autogenerate spacing size presets, or a fixed array of spacingSizes. - // The spaceScale takes presidence so will replace the spacingSizes array if present. + // A theme's spaceSizes array takes presidence so will replace the auto generated spacingSizes from core spacingScale if present. if ( ! isset( $settings['spacing']['spacingSizes']['theme'] ) ) { $settings['spacing']['spacingSizes'] = array( 'default' => static::get_spacing_sizes( $settings['spacing']['spacingScale'] ) ); } From 30e24d23e3de7228b7dc2271ad75b3ff60effa50 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 13 Jun 2022 14:49:20 +1200 Subject: [PATCH 15/30] Move setting of spacing presets into theme json merge method --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 94 +++++++++++++++++-- 1 file changed, 87 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 0993f0f593c3a7..6a7cd52e64ae24 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -516,14 +516,7 @@ protected function get_block_classes( $style_nodes ) { protected static function compute_preset_vars( $settings, $origins ) { $declarations = array(); - // Theme.json can specify either a spacingScale to autogenerate spacing size presets, or a fixed array of spacingSizes. - // A theme's spaceSizes array takes presidence so will replace the auto generated spacingSizes from core spacingScale if present. - if ( ! isset( $settings['spacing']['spacingSizes']['theme'] ) ) { - $settings['spacing']['spacingSizes'] = array( 'default' => static::get_spacing_sizes( $settings['spacing']['spacingScale'] ) ); - } - foreach ( static::PRESETS_METADATA as $preset_metadata ) { - $values_by_slug = static::get_settings_values_by_slug( $settings, $preset_metadata, $origins ); foreach ( $values_by_slug as $slug => $value ) { @@ -572,4 +565,91 @@ protected static function get_spacing_sizes( $spacing_scale ) { return $spacing_sizes; } + + /** + * Merge new incoming data. + * + * @param WP_Theme_JSON $incoming Data to merge. + */ + public function merge( $incoming ) { + $incoming_data = $incoming->get_raw_data(); + $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); + + /* + * The array_replace_recursive algorithm merges at the leaf level, + * but we don't want leaf arrays to be merged, so we overwrite it. + * + * For leaf values that are sequential arrays it will use the numeric indexes for replacement. + * We rather replace the existing with the incoming value, if it exists. + * This is the case of spacing.units. + * + * For leaf values that are associative arrays it will merge them as expected. + * This is also not the behavior we want for the current associative arrays (presets). + * We rather replace the existing with the incoming value, if it exists. + * This happens, for example, when we merge data from theme.json upon existing + * theme supports or when we merge anything coming from the same source twice. + * This is the case of color.palette, color.gradients, color.duotone, + * typography.fontSizes, or typography.fontFamilies. + * + * Additionally, for some preset types, we also want to make sure the + * values they introduce don't conflict with default values. We do so + * by checking the incoming slugs for theme presets and compare them + * with the equivalent default presets: if a slug is present as a default + * we remove it from the theme presets. + */ + $nodes = static::get_setting_nodes( $incoming_data ); + $slugs_global = static::get_default_slugs( $this->theme_json, array( 'settings' ) ); + foreach ( $nodes as $node ) { + $slugs_node = static::get_default_slugs( $this->theme_json, $node['path'] ); + $slugs = array_merge_recursive( $slugs_global, $slugs_node ); + + // Replace the spacing.units. + $path = array_merge( $node['path'], array( 'spacing', 'units' ) ); + $content = _wp_array_get( $incoming_data, $path, null ); + if ( isset( $content ) ) { + _wp_array_set( $this->theme_json, $path, $content ); + } + + // Replace the presets. + foreach ( static::PRESETS_METADATA as $preset ) { + $override_preset = ! static::get_metadata_boolean( $this->theme_json['settings'], $preset['prevent_override'], true ); + + foreach ( static::VALID_ORIGINS as $origin ) { + $base_path = array_merge( $node['path'], $preset['path'] ); + $path = array_merge( $base_path, array( $origin ) ); + $content = _wp_array_get( $incoming_data, $path, null ); + if ( ! isset( $content ) ) { + continue; + } + + if ( 'theme' === $origin && $preset['use_default_names'] ) { + foreach ( $content as &$item ) { + if ( ! array_key_exists( 'name', $item ) ) { + $name = static::get_name_from_defaults( $item['slug'], $base_path ); + if ( null !== $name ) { + $item['name'] = $name; + } + } + } + } + + if ( + ( 'theme' !== $origin ) || + ( 'theme' === $origin && $override_preset ) + ) { + _wp_array_set( $this->theme_json, $path, $content ); + } else { + $slugs_for_preset = _wp_array_get( $slugs, $preset['path'], array() ); + $content = static::filter_slugs( $content, $slugs_for_preset ); + _wp_array_set( $this->theme_json, $path, $content ); + } + } + } + + // Generate the default spacing sizes presets. + $spacing_size_defaults = static::get_spacing_sizes( _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() ) ); + _wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_size_defaults ); + } + + } } From efd0472e36af03a44bc3ffbd561dc15827b28fcb Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 16 Jun 2022 10:35:29 +1200 Subject: [PATCH 16/30] Remove unchanged method from 6.1 compat file --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 6a7cd52e64ae24..5f7476db7ac5bd 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -497,39 +497,6 @@ protected function get_block_classes( $style_nodes ) { ), ); - /** - * Given the block settings, it extracts the CSS Custom Properties - * for the presets and adds them to the $declarations array - * following the format: - * - * ```php - * array( - * 'name' => 'property_name', - * 'value' => 'property_value, - * ) - * ``` - * - * @param array $settings Settings to process. - * @param array $origins List of origins to process. - * @return array Returns the modified $declarations. - */ - protected static function compute_preset_vars( $settings, $origins ) { - $declarations = array(); - - foreach ( static::PRESETS_METADATA as $preset_metadata ) { - $values_by_slug = static::get_settings_values_by_slug( $settings, $preset_metadata, $origins ); - - foreach ( $values_by_slug as $slug => $value ) { - $declarations[] = array( - 'name' => static::replace_slug_in_string( $preset_metadata['css_vars'], $slug ), - 'value' => $value, - ); - } - } - - return $declarations; - } - /** * Transform the spacing scale values into an array of spacing scale presets. * From b6ce06f53b90e2023ab4ec79649610128dcf07e3 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 16 Jun 2022 12:08:31 +1200 Subject: [PATCH 17/30] Check for valid spacing.spacingScale values before generating spacing scale --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 5f7476db7ac5bd..4c1e9dda68f3a6 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -504,6 +504,18 @@ protected function get_block_classes( $style_nodes ) { * @return array An array of spacing preset sizes. */ protected static function get_spacing_sizes( $spacing_scale ) { + if ( ! isset( $spacing_scale['firstStep'] ) + || ! isset( $spacing_scale['units'] ) + || ! isset( $spacing_scale['operator'] ) + || ! isset( $spacing_scale['increment'] ) + || ! isset( $spacing_scale['steps'] ) + || ! is_numeric( $spacing_scale['increment'] ) + || ! is_numeric( $spacing_scale['steps'] ) + || ! is_numeric( $spacing_scale['firstStep'] ) + || ( '+' !== $spacing_scale['operator'] && '*' !== $spacing_scale['operator'] ) ) { + return null; + } + $spacing_sizes = array(); $spacing_sizes[] = array( 'name' => 0, @@ -615,7 +627,9 @@ public function merge( $incoming ) { // Generate the default spacing sizes presets. $spacing_size_defaults = static::get_spacing_sizes( _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() ) ); - _wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_size_defaults ); + if ( $spacing_size_defaults ) { + _wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_size_defaults ); + } } } From 9da88102f81a51ccb9f3d29f1cc095b098df314a Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 16 Jun 2022 12:14:08 +1200 Subject: [PATCH 18/30] Add warning message if spacingScale values invalid --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 4c1e9dda68f3a6..8c232c5e4a2c20 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -513,6 +513,7 @@ protected static function get_spacing_sizes( $spacing_scale ) { || ! is_numeric( $spacing_scale['steps'] ) || ! is_numeric( $spacing_scale['firstStep'] ) || ( '+' !== $spacing_scale['operator'] && '*' !== $spacing_scale['operator'] ) ) { + trigger_error( __( 'Some of the theme.json settings.spacing.spacingScale values are invalid' ), E_USER_NOTICE ); return null; } From 1637b5a3086bea844ed1a918402fefe70281edae Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 16 Jun 2022 12:24:45 +1200 Subject: [PATCH 19/30] Fixing issue with current_step not numeric --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 8c232c5e4a2c20..b7252274563ff8 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -513,8 +513,10 @@ protected static function get_spacing_sizes( $spacing_scale ) { || ! is_numeric( $spacing_scale['steps'] ) || ! is_numeric( $spacing_scale['firstStep'] ) || ( '+' !== $spacing_scale['operator'] && '*' !== $spacing_scale['operator'] ) ) { + if ( ! empty( $spacing_scale ) ) { trigger_error( __( 'Some of the theme.json settings.spacing.spacingScale values are invalid' ), E_USER_NOTICE ); - return null; + } + return null; } $spacing_sizes = array(); @@ -533,13 +535,13 @@ protected static function get_spacing_sizes( $spacing_scale ) { for ( $x = 1; $x <= $spacing_scale['steps'] - 1; $x++ ) { $current_step = '+' === $spacing_scale['operator'] - ? $current_step + $spacing_scale['increment'] . $spacing_scale['units'] - : $current_step * $spacing_scale['increment'] . $spacing_scale['units']; + ? $current_step + $spacing_scale['increment'] + : $current_step * $spacing_scale['increment']; $spacing_sizes[] = array( 'name' => $x + 1, 'slug' => ( $x + 1 ) * 10, - 'size' => $current_step, + 'size' => $current_step . $spacing_scale['units'], ); } From b3b3b1bbebc48c1b417dbe6f852500773821eaf2 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 16 Jun 2022 12:34:01 +1200 Subject: [PATCH 20/30] Fix linting errors --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 2 +- .../class-wp-rest-block-editor-settings-controller.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index b7252274563ff8..2dc5809091d3c2 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -514,7 +514,7 @@ protected static function get_spacing_sizes( $spacing_scale ) { || ! is_numeric( $spacing_scale['firstStep'] ) || ( '+' !== $spacing_scale['operator'] && '*' !== $spacing_scale['operator'] ) ) { if ( ! empty( $spacing_scale ) ) { - trigger_error( __( 'Some of the theme.json settings.spacing.spacingScale values are invalid' ), E_USER_NOTICE ); + trigger_error( __( 'Some of the theme.json settings.spacing.spacingScale values are invalid', 'gutenberg' ), E_USER_NOTICE ); } return null; } diff --git a/lib/experimental/class-wp-rest-block-editor-settings-controller.php b/lib/experimental/class-wp-rest-block-editor-settings-controller.php index 34a6debf30bce4..d9b726f1158da2 100644 --- a/lib/experimental/class-wp-rest-block-editor-settings-controller.php +++ b/lib/experimental/class-wp-rest-block-editor-settings-controller.php @@ -275,12 +275,12 @@ public function get_item_schema() { 'type' => 'array', 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), - 'spacingSizes' => array( + 'spacingSizes' => array( 'description' => __( 'Active theme spacing sizes.', 'gutenberg' ), 'type' => 'array', 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), - 'spacingScale' => array( + 'spacingScale' => array( 'description' => __( 'Active theme spacing scale.', 'gutenberg' ), 'type' => 'array', 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), From 962608ac49836c8c2e0a5ae34375d8a774d01ae0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 16 Jun 2022 12:43:54 +1200 Subject: [PATCH 21/30] Update docs --- docs/reference-guides/theme-json-reference/theme-json-living.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/reference-guides/theme-json-reference/theme-json-living.md b/docs/reference-guides/theme-json-reference/theme-json-living.md index 6f2d07748ddf6e..bb2ce78584b2bc 100644 --- a/docs/reference-guides/theme-json-reference/theme-json-living.md +++ b/docs/reference-guides/theme-json-reference/theme-json-living.md @@ -85,6 +85,7 @@ Settings related to spacing. | units | array | px,em,rem,vh,vw,% | | | customSpacingSize | boolean | true | | | spacingSizes | array | | name, size, slug | +| spacingScale | object | | | --- From 4892886e90dfc5ab05ba69691b2bf3303ce0f3d3 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 16 Jun 2022 12:57:15 +1200 Subject: [PATCH 22/30] Remove note about generation of classname for now --- schemas/json/theme.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index a8684d6bb95673..d707d9861a7824 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -233,7 +233,7 @@ "default": true }, "spacingSizes": { - "description": "Space size presets for the space size selector.\nGenerates a single class (`.has-{slug}-space-size`) and custom property (`--wp--preset--space-size--{slug}`) per preset value.", + "description": "Space size presets for the space size selector.\nGenerates a custom property (`--wp--preset--space-size--{slug}`) per preset value.", "type": "array", "items": { "type": "object", @@ -255,7 +255,7 @@ } }, "spacingScale": { - "description": "Space size presets for the space size selector.\nGenerates a single class (`.has-{slug}-space-size`) and custom property (`--wp--preset--space-size--{slug}`) per preset value.", + "description": "Space size presets for the space size selector.\nGenerates a custom property (`--wp--preset--space-size--{slug}`) per preset value.", "type": "object", "properties": { "operator": { From a055ce9ccefa79fc19b82061f2fa5450de04ac2d Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 20 Jun 2022 13:00:03 +1200 Subject: [PATCH 23/30] Fix theme.json schema issues --- schemas/json/theme.json | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index d707d9861a7824..ea7170a82de317 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -263,20 +263,28 @@ "type": "string" }, "increment": { - "description": "The amouunt to increment each step by", + "description": "The amount to increment each step by.", "type": "number" }, "steps": { - "description": "Number of steps to generate in scale", - "type": "number" + "description": "Number of steps to generate in scale.", + "type": "integer" }, "firstStep": { - "description": "The value to starr scale from", + "description": "The value to start scale from.", "type": "number" }, "units": { - "description": "Units that the scale uses, eg. rem, em, px", - "type": "string" + "description": "Units that the scale uses, eg. rem, em, px.", + "type": "string", + "enum": [ + "px", + "em", + "rem", + "vh", + "vw", + "%" + ] } }, "additionalProperties": false From d2ad7b4530c8ac4242a2121aec3dfc7771abd5de Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 20 Jun 2022 13:02:37 +1200 Subject: [PATCH 24/30] One more theme.json change --- schemas/json/theme.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index ea7170a82de317..d912044b8754a7 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -260,7 +260,8 @@ "properties": { "operator": { "description": "With + or * depending on whether scale is generated by increment or mulitplier.", - "type": "string" + "type": "string", + "enum": [ "+", "*" ] }, "increment": { "description": "The amount to increment each step by.", From fa70ddc86de700048123cda5ad5630a744bbf117 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 22 Jun 2022 14:39:19 +1200 Subject: [PATCH 25/30] Switch to a wider default scale using a 1.5 multiplier and t-shirt sizes --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 76 ++++++++++++++----- lib/compat/wordpress-6.1/theme.json | 8 +- schemas/json/theme.json | 4 +- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 2dc5809091d3c2..a99d45d8e186c9 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -504,14 +504,15 @@ protected function get_block_classes( $style_nodes ) { * @return array An array of spacing preset sizes. */ protected static function get_spacing_sizes( $spacing_scale ) { - if ( ! isset( $spacing_scale['firstStep'] ) + if ( ! is_numeric( $spacing_scale['steps'] ) + || ! $spacing_scale['steps'] > 0 + || ! isset( $spacing_scale['mediumStep'] ) || ! isset( $spacing_scale['units'] ) || ! isset( $spacing_scale['operator'] ) || ! isset( $spacing_scale['increment'] ) || ! isset( $spacing_scale['steps'] ) || ! is_numeric( $spacing_scale['increment'] ) - || ! is_numeric( $spacing_scale['steps'] ) - || ! is_numeric( $spacing_scale['firstStep'] ) + || ! is_numeric( $spacing_scale['mediumStep'] ) || ( '+' !== $spacing_scale['operator'] && '*' !== $spacing_scale['operator'] ) ) { if ( ! empty( $spacing_scale ) ) { trigger_error( __( 'Some of the theme.json settings.spacing.spacingScale values are invalid', 'gutenberg' ), E_USER_NOTICE ); @@ -519,33 +520,68 @@ protected static function get_spacing_sizes( $spacing_scale ) { return null; } - $spacing_sizes = array(); - $spacing_sizes[] = array( - 'name' => 0, - 'slug' => 0, - 'size' => 0, + $current_step = $spacing_scale['mediumStep']; + $steps_mid_point = round( ( ( $spacing_scale['steps'] ) / 2 ), 0 ); + + $x_count = null; + $below_sizes = array(); + + for ( $x = $steps_mid_point - 1; $x > 0; $x-- ) { + $current_step = '+' === $spacing_scale['operator'] + ? $current_step - $spacing_scale['increment'] + : $current_step / $spacing_scale['increment']; + + $below_sizes[] = array( + 'name' => $x === $steps_mid_point - 1 ? __( 'Small' ) : strval( $x_count ) . __( 'X-Small', 'gutenberg' ), + 'slug' => $x === $steps_mid_point - 1 ? 'small' : strval( $x_count ) . 'x-small', + 'size' => round( $current_step, 2 ) . $spacing_scale['units'], + ); + if ( $x === $steps_mid_point - 2 ) { + $x_count = 2; + } + if ( $x < $steps_mid_point - 2 ) { + $x_count++; + } + } + + $below_sizes = array_reverse( $below_sizes ); + array_unshift( + $below_sizes, + array( + 'name' => 0, + 'slug' => 0, + 'size' => 0, + ) ); - $spacing_sizes[] = array( - 'name' => 1, - 'slug' => 10, - 'size' => $spacing_scale['firstStep'] . $spacing_scale['units'], + $below_sizes[] = array( + 'name' => __( 'Medium', 'gutenberg' ), + 'slug' => 'medium', + 'size' => $spacing_scale['mediumStep'] . $spacing_scale['units'], ); - $current_step = $spacing_scale['firstStep']; - - for ( $x = 1; $x <= $spacing_scale['steps'] - 1; $x++ ) { + $current_step = $spacing_scale['mediumStep']; + $x_count = null; + $above_sizes = array(); + for ( $x = $steps_mid_point + 1; $x < $spacing_scale['steps']; $x++ ) { $current_step = '+' === $spacing_scale['operator'] ? $current_step + $spacing_scale['increment'] : $current_step * $spacing_scale['increment']; - $spacing_sizes[] = array( - 'name' => $x + 1, - 'slug' => ( $x + 1 ) * 10, - 'size' => $current_step . $spacing_scale['units'], + $above_sizes[] = array( + 'name' => $x === $steps_mid_point + 1 ? __( 'Large' ) : strval( $x_count ) . __( 'X-Large', 'gutenberg' ), + 'slug' => $x === $steps_mid_point + 1 ? 'large' : strval( $x_count ) . 'x-large', + 'size' => round( $current_step, 2 ) . $spacing_scale['units'], ); + + if ( $x === $steps_mid_point + 2 ) { + $x_count = 2; + } + if ( $x > $steps_mid_point + 2 ) { + $x_count++; + } } - return $spacing_sizes; + return array_merge( $below_sizes, $above_sizes ); } /** diff --git a/lib/compat/wordpress-6.1/theme.json b/lib/compat/wordpress-6.1/theme.json index 32b46c5d1488dd..9c5bd2886b2560 100644 --- a/lib/compat/wordpress-6.1/theme.json +++ b/lib/compat/wordpress-6.1/theme.json @@ -192,10 +192,10 @@ "customSpacingSizes": true, "units": [ "px", "em", "rem", "vh", "vw", "%" ], "spacingScale": { - "operator": "+", - "increment": 0.5, - "steps": 5, - "firstStep": 0.5, + "operator": "*", + "increment": 1.5, + "steps": 10, + "mediumStep": 1.5, "units": "rem" } }, diff --git a/schemas/json/theme.json b/schemas/json/theme.json index d912044b8754a7..be7ceb3433f028 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -271,8 +271,8 @@ "description": "Number of steps to generate in scale.", "type": "integer" }, - "firstStep": { - "description": "The value to start scale from.", + "mediumStep": { + "description": "The value to medium setting in the scale.", "type": "number" }, "units": { From 3f78238522a41db043b9dd7c68d84d56f0f734b4 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 22 Jun 2022 15:26:52 +1200 Subject: [PATCH 26/30] Mover generation of spacing presets until after the theme.json files are merged --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 16 ++++------------ .../class-wp-theme-json-resolver-gutenberg.php | 2 ++ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index a99d45d8e186c9..6cf527217b322f 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -499,11 +499,10 @@ protected function get_block_classes( $style_nodes ) { /** * Transform the spacing scale values into an array of spacing scale presets. - * - * @param array $spacing_scale scale Array of values used to create the scale of spacing presets. - * @return array An array of spacing preset sizes. */ - protected static function get_spacing_sizes( $spacing_scale ) { + public function get_spacing_sizes() { + $spacing_scale = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() ); + if ( ! is_numeric( $spacing_scale['steps'] ) || ! $spacing_scale['steps'] > 0 || ! isset( $spacing_scale['mediumStep'] ) @@ -581,7 +580,7 @@ protected static function get_spacing_sizes( $spacing_scale ) { } } - return array_merge( $below_sizes, $above_sizes ); + _wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), array_merge( $below_sizes, $above_sizes ) ); } /** @@ -663,13 +662,6 @@ public function merge( $incoming ) { } } } - - // Generate the default spacing sizes presets. - $spacing_size_defaults = static::get_spacing_sizes( _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() ) ); - if ( $spacing_size_defaults ) { - _wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_size_defaults ); - } } - } } diff --git a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php index 6c4e5b534a4f78..ad6f4b9942dfa2 100644 --- a/lib/experimental/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/experimental/class-wp-theme-json-resolver-gutenberg.php @@ -173,6 +173,8 @@ public static function get_merged_data( $origin = 'custom' ) { if ( 'custom' === $origin ) { $result->merge( static::get_user_data() ); } + // Generate the default spacing sizes presets. + $result->get_spacing_sizes(); return $result; } From fd684229d8787284f7bbfb0f309ae57d769075fa Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 22 Jun 2022 16:51:42 +1200 Subject: [PATCH 27/30] Switch back to numeric slugs --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 6cf527217b322f..4fa878d43be2da 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -532,7 +532,7 @@ public function get_spacing_sizes() { $below_sizes[] = array( 'name' => $x === $steps_mid_point - 1 ? __( 'Small' ) : strval( $x_count ) . __( 'X-Small', 'gutenberg' ), - 'slug' => $x === $steps_mid_point - 1 ? 'small' : strval( $x_count ) . 'x-small', + 'slug' => $x * 10, 'size' => round( $current_step, 2 ) . $spacing_scale['units'], ); if ( $x === $steps_mid_point - 2 ) { @@ -554,7 +554,7 @@ public function get_spacing_sizes() { ); $below_sizes[] = array( 'name' => __( 'Medium', 'gutenberg' ), - 'slug' => 'medium', + 'slug' => 50, 'size' => $spacing_scale['mediumStep'] . $spacing_scale['units'], ); @@ -568,7 +568,7 @@ public function get_spacing_sizes() { $above_sizes[] = array( 'name' => $x === $steps_mid_point + 1 ? __( 'Large' ) : strval( $x_count ) . __( 'X-Large', 'gutenberg' ), - 'slug' => $x === $steps_mid_point + 1 ? 'large' : strval( $x_count ) . 'x-large', + 'slug' => $x * 10, 'size' => round( $current_step, 2 ) . $spacing_scale['units'], ); From e23e954fc60c9f3936c889fd404899bf92a73418 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 23 Jun 2022 11:41:19 +1200 Subject: [PATCH 28/30] Reduce length of preset var --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index 4fa878d43be2da..cea69087606150 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -428,7 +428,7 @@ protected function get_block_classes( $style_nodes ) { 'prevent_override' => false, 'use_default_names' => true, 'value_key' => 'size', - 'css_vars' => '--wp--preset--spacing-size--$slug', + 'css_vars' => '--wp--preset--spacing--$slug', 'classes' => array(), 'properties' => array( 'padding', 'margin' ), ), @@ -437,7 +437,7 @@ protected function get_block_classes( $style_nodes ) { 'prevent_override' => false, 'use_default_names' => true, 'value_key' => 'size', - 'css_vars' => '--wp--preset--spacing-size--$slug', + 'css_vars' => '--wp--preset--spacing--$slug', 'classes' => array(), 'properties' => array( 'padding', 'margin' ), ), From 6539042e6e41d4ac5dc8529872cf67333969393b Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 23 Jun 2022 17:32:29 +1200 Subject: [PATCH 29/30] Translation fix --- lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index cea69087606150..e63c6e8a58c92e 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -531,7 +531,7 @@ public function get_spacing_sizes() { : $current_step / $spacing_scale['increment']; $below_sizes[] = array( - 'name' => $x === $steps_mid_point - 1 ? __( 'Small' ) : strval( $x_count ) . __( 'X-Small', 'gutenberg' ), + 'name' => $x === $steps_mid_point - 1 ? __( 'Small', 'gutenberg' ) : strval( $x_count ) . __( 'X-Small', 'gutenberg' ), 'slug' => $x * 10, 'size' => round( $current_step, 2 ) . $spacing_scale['units'], ); @@ -567,7 +567,7 @@ public function get_spacing_sizes() { : $current_step * $spacing_scale['increment']; $above_sizes[] = array( - 'name' => $x === $steps_mid_point + 1 ? __( 'Large' ) : strval( $x_count ) . __( 'X-Large', 'gutenberg' ), + 'name' => $x === $steps_mid_point + 1 ? __( 'Large', 'gutenberg' ) : strval( $x_count ) . __( 'X-Large', 'gutenberg' ), 'slug' => $x * 10, 'size' => round( $current_step, 2 ) . $spacing_scale['units'], ); From eef6401565014281d058a8d94cec81e1baac7ac2 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 27 Jun 2022 13:48:49 +1200 Subject: [PATCH 30/30] Remove 0 step and improve translation of tshirt size names --- .../wordpress-6.1/class-wp-theme-json-6-1.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php index e63c6e8a58c92e..f0b18bc2ab2d5f 100644 --- a/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php +++ b/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php @@ -531,7 +531,8 @@ public function get_spacing_sizes() { : $current_step / $spacing_scale['increment']; $below_sizes[] = array( - 'name' => $x === $steps_mid_point - 1 ? __( 'Small', 'gutenberg' ) : strval( $x_count ) . __( 'X-Small', 'gutenberg' ), + /* translators: %s: Muliple of t-shirt sizing, eg. 2X-Small */ + 'name' => $x === $steps_mid_point - 1 ? __( 'Small', 'gutenberg' ) : sprintf( __( '%sX-Small', 'gutenberg' ), strval( $x_count ) ), 'slug' => $x * 10, 'size' => round( $current_step, 2 ) . $spacing_scale['units'], ); @@ -544,14 +545,7 @@ public function get_spacing_sizes() { } $below_sizes = array_reverse( $below_sizes ); - array_unshift( - $below_sizes, - array( - 'name' => 0, - 'slug' => 0, - 'size' => 0, - ) - ); + $below_sizes[] = array( 'name' => __( 'Medium', 'gutenberg' ), 'slug' => 50, @@ -567,7 +561,8 @@ public function get_spacing_sizes() { : $current_step * $spacing_scale['increment']; $above_sizes[] = array( - 'name' => $x === $steps_mid_point + 1 ? __( 'Large', 'gutenberg' ) : strval( $x_count ) . __( 'X-Large', 'gutenberg' ), + /* translators: %s: Muliple of t-shirt sizing, eg. 2X-Large */ + 'name' => $x === $steps_mid_point + 1 ? __( 'Large', 'gutenberg' ) : sprintf( __( '%sX-Large', 'gutenberg' ), strval( $x_count ) ), 'slug' => $x * 10, 'size' => round( $current_step, 2 ) . $spacing_scale['units'], );