Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of get_default_block_editor_settings #46112

Merged
merged 13 commits into from
Dec 6, 2022
99 changes: 99 additions & 0 deletions lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,106 @@
* @access private
*/
class WP_Theme_JSON_Resolver_6_2 extends WP_Theme_JSON_Resolver_6_1 {
/**
* Returns the theme's data.
*
* Data from theme.json will be backfilled from existing
* theme supports, if any. Note that if the same data
* is present in theme.json and in theme supports,
* the theme.json takes precedence.
*
* @param array $deprecated Deprecated argument.
* @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data.
* @return WP_Theme_JSON_Gutenberg Entity that holds theme data.
*/
public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __METHOD__, '5.9' );
}

// When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin.
if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
$theme_json_file = static::get_file_path_from_theme( 'theme.json' );
$wp_theme = wp_get_theme();
if ( '' !== $theme_json_file ) {
$theme_json_data = static::read_json_file( $theme_json_file );
$theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) );
} else {
$theme_json_data = array();
}

/**
* Filters the data provided by the theme for global styles and settings.
*
* @since 6.1.0
*
* @param WP_Theme_JSON_Data Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
$theme_json_data = $theme_json->get_data();
static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );

if ( $wp_theme->parent() ) {
// Get parent theme.json.
$parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true );
if ( '' !== $parent_theme_json_file ) {
$parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
$parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) );
$parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data );

/*
* Merge the child theme.json into the parent theme.json.
* The child theme takes precedence over the parent.
*/
$parent_theme->merge( static::$theme );
static::$theme = $parent_theme;
}
}
}

if ( ! $settings['with_supports'] ) {
return static::$theme;
}

/*
* We want the presets and settings declared in theme.json
* to override the ones declared via theme supports.
* 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( gutenberg_get_block_theme_supports() );
if ( ! wp_theme_has_support() ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
$theme_support_data['settings']['color'] = array();
}

$default_palette = false;
if ( current_theme_supports( 'default-color-palette' ) ) {
$default_palette = true;
}
if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) {
// If the theme does not have any palette, we still want to show the core one.
$default_palette = true;
}
$theme_support_data['settings']['color']['defaultPalette'] = $default_palette;

$default_gradients = false;
if ( current_theme_supports( 'default-gradient-presets' ) ) {
$default_gradients = true;
}
if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) {
// If the theme does not have any gradients, we still want to show the core ones.
$default_gradients = true;
}
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;

// Classic themes without a theme.json don't support global duotone.
$theme_support_data['settings']['color']['defaultDuotone'] = false;
}
$with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data );
$with_theme_supports->merge( static::$theme );
return $with_theme_supports;
}
/**
* Returns the custom post type that contains the user's origin config
* for the active theme or a void array if none are found.
Expand Down
35 changes: 35 additions & 0 deletions lib/compat/wordpress-6.2/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,41 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) {
}

/**
* Repeated logic from `get_default_block_editor_settings` function. When implemented into core,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also be ok to have two functions in core: get_default_block_editor_settings and the very specific get_legacy_theme_supports_for_theme_json.

* remove logic from `get_default_block_editor_settings` and simple call this function instead.
*
* @return array
*/
function gutenberg_get_block_theme_supports() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you consider a name such as gutenberg_get_legacy_theme_supports_for_theme_json? Or something along that vein. It's more in line with what this code is used for. The current naming may imply that it returns all theme supports, and may mislead people.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orignally was in experimental, but it would mean, that this code would do nothing in the plugin - 999b258

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow. Perhaps you meant to comment on this other thread? https://github.com/WordPress/gutenberg/pull/46112/files#r1037282457

If so, how does it do nothing if this code stays in lib/experimental? The whole Gutenberg uses the class WP_Theme_JSON_Resolver_Gutenberg defined in this file.

$theme_settings = array(
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code that uses this data (I didn't find a more recent definition of this code in Gutenberg), we need:

  • disableCustomColors
  • disableCustomGradients
  • disableCustomFontSizes
  • enableCustomLineHeight
  • enableCustomUnits
  • enableCustomSpacing

It seems we can get rid of disableLayoutStyles in this method?

'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;
}

return $theme_settings;
}

oandregal marked this conversation as resolved.
Show resolved Hide resolved
* Private function to clean the caches used by gutenberg_get_global_settings method.
*
* @access private
Expand Down
97 changes: 0 additions & 97 deletions lib/experimental/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,103 +16,6 @@
* @access private
*/
class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_2 {
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something in this file that still needs to live in the experimental folder: the call to gutenberg_add_registered_webfonts_to_theme_json.

This is in my list of things to look at to give it some clarity and improve the developer experience. Given that the change required here is a one-liner (get_default_block_editor_settings to gutenberg_get_block_theme_supports) and we need to look at this separately, would you mind making your changes in this file directly instead of moving it to 6.2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified in this slack thread.

* Returns the theme's data.
*
* Data from theme.json will be backfilled from existing
* theme supports, if any. Note that if the same data
* is present in theme.json and in theme supports,
* the theme.json takes precedence.
*
* @param array $deprecated Deprecated argument.
* @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data.
* @return WP_Theme_JSON_Gutenberg Entity that holds theme data.
*/
public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __METHOD__, '5.9' );
}

// When backporting to core, remove the instanceof Gutenberg class check, as it is only required for the Gutenberg plugin.
if ( null === static::$theme || ! static::$theme instanceof WP_Theme_JSON_Gutenberg ) {
$wp_theme = wp_get_theme();
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
$theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) );
$theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data );

/**
* Filters the data provided by the theme for global styles & settings.
*
* @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) );
$theme_json_data = $theme_json->get_data();
static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );

if ( $wp_theme->parent() ) {
// Get parent theme.json.
$parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
$parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) );
$parent_theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $parent_theme_json_data );
$parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data );

// Merge the child theme.json into the parent theme.json.
// The child theme takes precedence over the parent.
$parent_theme->merge( static::$theme );
static::$theme = $parent_theme;
}
}

if ( ! $settings['with_supports'] ) {
return static::$theme;
}

/*
* We want the presets and settings declared in theme.json
* to override the ones declared via theme supports.
* 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_default_block_editor_settings() );
if ( ! wp_theme_has_theme_json() ) {
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
$theme_support_data['settings']['color'] = array();
}

$default_palette = false;
if ( current_theme_supports( 'default-color-palette' ) ) {
$default_palette = true;
}
if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) {
// If the theme does not have any palette, we still want to show the core one.
$default_palette = true;
}
$theme_support_data['settings']['color']['defaultPalette'] = $default_palette;

$default_gradients = false;
if ( current_theme_supports( 'default-gradient-presets' ) ) {
$default_gradients = true;
}
if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) {
// If the theme does not have any gradients, we still want to show the core ones.
$default_gradients = true;
}
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;

// Classic themes without a theme.json don't support global duotone.
$theme_support_data['settings']['color']['defaultDuotone'] = false;

// Allow themes to enable appearance tools via theme_support.
if ( current_theme_supports( 'appearance-tools' ) ) {
$theme_support_data['settings']['appearanceTools'] = true;
}
}
$with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data );
$with_theme_supports->merge( static::$theme );

return $with_theme_supports;
}

/**
* Gets the styles for blocks from the block.json file.
*
Expand Down