Skip to content

Commit

Permalink
Add default-spacing-sizes and default-font-sizes options for clas…
Browse files Browse the repository at this point in the history
…sic themes (WordPress#62252)

Co-authored-by: ajlende <ajlende@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
  • Loading branch information
4 people authored and patil-vipul committed Jun 17, 2024
1 parent bcfc770 commit 7a5c589
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 19 deletions.
3 changes: 2 additions & 1 deletion backport-changelog/6.6/6616.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ https://github.com/WordPress/wordpress-develop/pull/6616

* https://github.com/WordPress/gutenberg/pull/58409
* https://github.com/WordPress/gutenberg/pull/61328
* https://github.com/WordPress/gutenberg/pull/61842
* https://github.com/WordPress/gutenberg/pull/61842
* https://github.com/WordPress/gutenberg/pull/62252
29 changes: 15 additions & 14 deletions docs/how-to-guides/themes/global-settings-and-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,21 @@ There's one special setting property, `appearanceTools`, which is a boolean and

To retain backward compatibility, the existing `add_theme_support` declarations that configure the block editor are retrofit in the proper categories for the top-level section. For example, if a theme uses `add_theme_support('disable-custom-colors')`, it'll be the same as setting `settings.color.custom` to `false`. If the `theme.json` contains any settings, these will take precedence over the values declared via `add_theme_support`. This is the complete list of equivalences:

| add_theme_support | theme.json setting |
| --------------------------- | --------------------------------------------------------- |
| `custom-line-height` | Set `typography.lineHeight` to `true`. |
| `custom-spacing` | Set `spacing.padding` to `true`. |
| `custom-units` | Provide the list of units via `spacing.units`. |
| `disable-custom-colors` | Set `color.custom` to `false`. |
| `disable-custom-font-sizes` | Set `typography.customFontSize` to `false`. |
| `disable-custom-gradients` | Set `color.customGradient` to `false`. |
| `editor-color-palette` | Provide the list of colors via `color.palette`. |
| `editor-font-sizes` | Provide the list of font size via `typography.fontSizes`. |
| `editor-gradient-presets` | Provide the list of gradients via `color.gradients`. |
| `appearance-tools` | Set `appearanceTools` to `true`. |
| `border` | Set `border: color, radius, style, width` to `true`. |
| `link-color ` | Set `color.link` to `true`. |
| add_theme_support | theme.json setting |
| --------------------------- | ------------------------------------------------------------- |
| `custom-line-height` | Set `typography.lineHeight` to `true`. |
| `custom-spacing` | Set `spacing.padding` to `true`. |
| `custom-units` | Provide the list of units via `spacing.units`. |
| `disable-custom-colors` | Set `color.custom` to `false`. |
| `disable-custom-font-sizes` | Set `typography.customFontSize` to `false`. |
| `disable-custom-gradients` | Set `color.customGradient` to `false`. |
| `editor-color-palette` | Provide the list of colors via `color.palette`. |
| `editor-font-sizes` | Provide the list of font size via `typography.fontSizes`. |
| `editor-gradient-presets` | Provide the list of gradients via `color.gradients`. |
| `editor-spacing-sizes` | Provide the list of spacing sizes via `spacing.spacingSizes`. |
| `appearance-tools` | Set `appearanceTools` to `true`. |
| `border` | Set `border: color, radius, style, width` to `true`. |
| `link-color ` | Set `color.link` to `true`. |

#### Presets

Expand Down
7 changes: 7 additions & 0 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -3630,6 +3630,13 @@ public static function get_from_editor_settings( $settings ) {
$theme_settings['settings']['spacing']['padding'] = $settings['enableCustomSpacing'];
}

if ( isset( $settings['spacingSizes'] ) ) {
if ( ! isset( $theme_settings['settings']['spacing'] ) ) {
$theme_settings['settings']['spacing'] = array();
}
$theme_settings['settings']['spacing']['spacingSizes'] = $settings['spacingSizes'];
}

return $theme_settings;
}

Expand Down
29 changes: 28 additions & 1 deletion lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ protected static function has_same_registered_blocks( $origin ) {
* @since 5.8.0
* @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed.
* @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports.
* @since 6.6.0 Add support for 'default-font-sizes' and 'default-spacing-sizes' theme supports.
*
* @param array $deprecated Deprecated. Not used.
* @param array $options {
Expand Down Expand Up @@ -291,7 +292,7 @@ public static function get_theme_data( $deprecated = array(), $options = array()
* So we take theme supports, transform it to theme.json shape
* and merge the static::$theme upon that.
*/
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_classic_theme_supports_block_editor_settings() );
if ( ! wp_theme_has_theme_json() ) {
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
$theme_support_data['settings']['color'] = array();
Expand All @@ -317,6 +318,32 @@ public static function get_theme_data( $deprecated = array(), $options = array()
}
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;

if ( ! isset( $theme_support_data['settings']['typography'] ) ) {
$theme_support_data['settings']['typography'] = array();
}
$default_font_sizes = false;
if ( current_theme_supports( 'default-font-sizes' ) ) {
$default_font_sizes = true;
}
if ( ! isset( $theme_support_data['settings']['typography']['fontSizes'] ) ) {
// If the theme does not have any font sizes, we still want to show the core one.
$default_font_sizes = true;
}
$theme_support_data['settings']['typography']['defaultFontSizes'] = $default_font_sizes;

if ( ! isset( $theme_support_data['settings']['spacing'] ) ) {
$theme_support_data['settings']['spacing'] = array();
}
$default_spacing_sizes = false;
if ( current_theme_supports( 'default-spacing-sizes' ) ) {
$default_spacing_sizes = true;
}
if ( ! isset( $theme_support_data['settings']['spacing']['spacingSizes'] ) ) {
// If the theme does not have any spacing sizes, we still want to show the core one.
$default_spacing_sizes = true;
}
$theme_support_data['settings']['spacing']['defaultSpacingSizes'] = $default_spacing_sizes;

if ( ! isset( $theme_support_data['settings']['shadow'] ) ) {
$theme_support_data['settings']['shadow'] = array();
}
Expand Down
50 changes: 50 additions & 0 deletions lib/compat/wordpress-6.6/block-editor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Block Editor API.
*
* @package Gutenberg
* @subpackage Editor
*/

/**
* Returns the classic theme supports settings for block editor.
*
* @since 6.2.0
* @since 6.6.0 Add support for custom spacing sizes.
*
* @return array The classic theme supports settings.
*/
function gutenberg_get_classic_theme_supports_block_editor_settings() {
$theme_settings = array(
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ),
'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
'enableCustomSpacing' => get_theme_support( 'custom-spacing' ),
'enableCustomUnits' => get_theme_support( 'custom-units' ),
);

// Theme settings.
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
if ( false !== $color_palette ) {
$theme_settings['colors'] = $color_palette;
}

$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
if ( false !== $font_sizes ) {
$theme_settings['fontSizes'] = $font_sizes;
}

$gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
if ( false !== $gradient_presets ) {
$theme_settings['gradients'] = $gradient_presets;
}

$spacing_sizes = current( (array) get_theme_support( 'editor-spacing-sizes' ) );
if ( false !== $spacing_sizes ) {
$theme_settings['spacingSizes'] = $spacing_sizes;
}

return $theme_settings;
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.6 compat.
require __DIR__ . '/compat/wordpress-6.6/admin-bar.php';
require __DIR__ . '/compat/wordpress-6.6/blocks.php';
require __DIR__ . '/compat/wordpress-6.6/block-editor.php';
require __DIR__ . '/compat/wordpress-6.6/compat.php';
require __DIR__ . '/compat/wordpress-6.6/resolve-patterns.php';
require __DIR__ . '/compat/wordpress-6.6/block-bindings/pattern-overrides.php';
Expand Down
10 changes: 10 additions & 0 deletions packages/core-data/src/entity-types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ declare module './base-entity-records' {
* Custom font sizes if defined by the theme.
*/
'editor-font-sizes': boolean | FontSize[];
/**
* Custom spacing sizes if defined by the theme.
*/
'editor-spacing-sizes': boolean | SpacingSize[];
/**
* Custom gradient presets if defined by the theme.
*/
Expand Down Expand Up @@ -212,6 +216,12 @@ declare module './base-entity-records' {
slug: string;
}

export interface SpacingSize {
name: string;
size: number;
slug: string;
}

export interface GradientPreset {
name: string;
gradient: string;
Expand Down
6 changes: 3 additions & 3 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3161,7 +3161,7 @@ public function test_get_editor_settings_blank() {

public function test_get_editor_settings_custom_units_can_be_disabled() {
add_theme_support( 'custom-units', array() );
$actual = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );
$actual = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_classic_theme_supports_block_editor_settings() );
remove_theme_support( 'custom-units' );

$expected = array(
Expand All @@ -3174,7 +3174,7 @@ public function test_get_editor_settings_custom_units_can_be_disabled() {

public function test_get_editor_settings_custom_units_can_be_enabled() {
add_theme_support( 'custom-units' );
$actual = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );
$actual = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_classic_theme_supports_block_editor_settings() );
remove_theme_support( 'custom-units' );

$expected = array(
Expand All @@ -3187,7 +3187,7 @@ public function test_get_editor_settings_custom_units_can_be_enabled() {

public function test_get_editor_settings_custom_units_can_be_filtered() {
add_theme_support( 'custom-units', 'rem', 'em' );
$actual = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );
$actual = WP_Theme_JSON_Gutenberg::get_from_editor_settings( gutenberg_get_classic_theme_supports_block_editor_settings() );
remove_theme_support( 'custom-units' );

$expected = array(
Expand Down

0 comments on commit 7a5c589

Please sign in to comment.