-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix gutenberg_get_block_editor_settings overriding other hooks #50760
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,12 @@ | |
*/ | ||
|
||
/** | ||
* Adds styles and __experimentalFeatures to the block editor settings. | ||
* Replaces core 'styles' and '__experimentalFeatures' block editor settings from | ||
* wordpress-develop/block-editor.php with the Gutenberg versions. Much of the | ||
* code is copied from get_block_editor_settings() in that file. | ||
* | ||
* This hook should run first as it completely replaces the core settings that | ||
* other hooks may need to update. | ||
* | ||
* Note: The settings that are WP version specific should be handled inside the `compat` directory. | ||
* | ||
|
@@ -15,7 +20,6 @@ | |
* @return array New block editor settings. | ||
*/ | ||
function gutenberg_get_block_editor_settings( $settings ) { | ||
// Recreate global styles. | ||
$global_styles = array(); | ||
$presets = array( | ||
array( | ||
|
@@ -74,9 +78,74 @@ function gutenberg_get_block_editor_settings( $settings ) { | |
|
||
$settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); | ||
|
||
// Copied from get_block_editor_settings() at wordpress-develop/block-editor.php. | ||
$settings['__experimentalFeatures'] = gutenberg_get_global_settings(); | ||
// These settings may need to be updated based on data coming from theme.json sources. | ||
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['disableCustomSpacingSizes'] = ! $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'] | ||
); | ||
} | ||
|
||
return $settings; | ||
} | ||
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX ); | ||
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', 0 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit worried about this change. It means that every custom filter can override the settings the plugin depends on to be available. This could make debugging issues harder. Maybe we should use the previous solution of filtering out global styles and recreating them. I checked, and // Remove existing global styles provided by core.
$existing_non_global_styles = array();
foreach ( $settings['styles'] as $style ) {
if (
! isset( $style['isGlobalStyles'] ) && ! $style['isGlobalStyles']
) {
$existing_non_global_styles[] = $style;
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is a fair concern but I'm not sure that it will really be a problem. Maybe we can try it like this for now and if that does become an issue we can go back to the old way of doing it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not against this change. However, considering that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no similar handler in the core; let's double-check if this is really needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is copied directly from https://github.com/WordPress/wordpress-develop/blob/23b007b126fa73211b7db56a38e78e601e4abbc6/src/wp-includes/block-editor.php#L443.