-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Move editor settings code to compat folder #39030
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cc7d781
Extract function to file in compat folder
oandregal 0247f3d
Backport changes from core
oandregal a33b712
Rename to gutenberg_get_block_editor_settings
oandregal a6dece9
Backport changes from core
oandregal 4252f57
Cover for unstable types in 5.8 and 5.9
oandregal 552948c
Fix lint issue
oandregal 46f43e7
Extract mobile settings to the experimental folder
oandregal 37d2a43
Port changes
oandregal 64ffed5
Do not output styles in mobile
oandregal 4f03b0b
Move to 6.0 compat folder.
oandregal d078f86
Add comment
oandregal 0329e09
Fix lint issues
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Adds settings to the mobile block editor. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Adds settings to the mobile block editor. | ||
* | ||
* @param array $settings Existing block editor settings. | ||
* | ||
* @return array New block editor settings. | ||
*/ | ||
function gutenberg_get_block_editor_settings_mobile( $settings ) { | ||
if ( | ||
defined( 'REST_REQUEST' ) && | ||
REST_REQUEST && | ||
isset( $_GET['context'] ) && | ||
'mobile' === $_GET['context'] | ||
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 missed a check, see #39806 |
||
) { | ||
$settings['__experimentalStyles'] = gutenberg_get_global_styles(); | ||
} | ||
|
||
return $settings; | ||
} | ||
|
||
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_mobile', PHP_INT_MAX ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
/** | ||
* Adds settings to the block editor. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* 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'; | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( | ||
is_callable( 'get_current_screen' ) && | ||
function_exists( 'gutenberg_is_edit_site_page' ) && | ||
gutenberg_is_edit_site_page( get_current_screen()->id ) | ||
) { | ||
$context = 'site-editor'; | ||
} | ||
|
||
if ( | ||
defined( 'REST_REQUEST' ) && | ||
REST_REQUEST && | ||
isset( $_GET['context'] ) && | ||
'mobile' === $_GET['context'] | ||
) { | ||
$context = 'mobile'; | ||
} | ||
|
||
if ( 'other' === $context ) { | ||
// Make sure the styles array exists. | ||
// In some contexts, like the navigation editor, it doesn't. | ||
if ( ! isset( $settings['styles'] ) ) { | ||
$settings['styles'] = array(); | ||
} | ||
|
||
$styles_without_existing_global_styles = array(); | ||
foreach ( $settings['styles'] as $style ) { | ||
if ( | ||
! isset( $style['__unstableType'] ) || | ||
// '__unstableType' is 'globalStyles' for WordPress 5.8 and 'presets' for WordPress 5.9. | ||
// | ||
// Note that styles classified as'theme', can be from the theme stylesheet | ||
// or from the theme.json (the styles section). | ||
// We are unable to identify which is which, so we can't remove and recreate those. | ||
// Instead, we reload the theme.json styles from the plugin. | ||
// Because they'll use the same selectors and load later, | ||
// they'll have higher priority than core's. | ||
// | ||
// Theoretically, this approach with 'theme' styles could be problematic: | ||
// if we remove style properties in the plugin, if selectors change, etc. | ||
// We need to address this issue directly in core by alowing to identify | ||
// styles coming from theme.json. | ||
// | ||
// A final note about 'theme' styles: this flag is used to identify theme | ||
// styles that may need to be removed if the user toggles | ||
// "Preferences > Use theme styles" in the preferences modal. | ||
// | ||
! in_array( $style['__unstableType'], array( 'globalStyles', 'presets' ), true ) | ||
) { | ||
$styles_without_existing_global_styles[] = $style; | ||
} | ||
} | ||
|
||
$new_global_styles = array(); | ||
$presets = array( | ||
array( | ||
'css' => 'variables', | ||
'__unstableType' => 'presets', | ||
), | ||
array( | ||
'css' => 'presets', | ||
'__unstableType' => 'presets', | ||
), | ||
); | ||
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', | ||
); | ||
$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'] ); | ||
} | ||
|
||
return $settings; | ||
} | ||
|
||
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,164 +5,6 @@ | |
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Adds the necessary settings for the Global Styles client UI. | ||
* | ||
* @param array $settings Existing block editor settings. | ||
* | ||
* @return array New block editor settings. | ||
*/ | ||
function gutenberg_experimental_global_styles_settings( $settings ) { | ||
// Set what is the context for this data request. | ||
$context = 'other'; | ||
if ( | ||
is_callable( 'get_current_screen' ) && | ||
function_exists( 'gutenberg_is_edit_site_page' ) && | ||
gutenberg_is_edit_site_page( get_current_screen()->id ) | ||
) { | ||
$context = 'site-editor'; | ||
} | ||
|
||
if ( | ||
defined( 'REST_REQUEST' ) && | ||
REST_REQUEST && | ||
isset( $_GET['context'] ) && | ||
'mobile' === $_GET['context'] | ||
) { | ||
$context = 'mobile'; | ||
} | ||
|
||
if ( 'mobile' === $context && WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { | ||
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 has been extracted to a separate file, see https://github.com/WordPress/gutenberg/pull/39030/files#r814083346 |
||
$settings['__experimentalStyles'] = gutenberg_get_global_styles(); | ||
} | ||
|
||
if ( 'other' === $context ) { | ||
// Make sure the styles array exists. | ||
// In some contexts, like the navigation editor, it doesn't. | ||
if ( ! isset( $settings['styles'] ) ) { | ||
$settings['styles'] = array(); | ||
} | ||
|
||
// Reset existing global styles. | ||
$styles_without_existing_global_styles = array(); | ||
foreach ( $settings['styles'] as $style ) { | ||
if ( | ||
! isset( $style['__unstableType'] ) || | ||
// '__unstableType' is 'globalStyles' for WordPress 5.8 and 'presets' for WordPress 5.9. | ||
// | ||
// Note that styles classified as'theme', can be from the theme stylesheet | ||
// or from the theme.json (the styles section). | ||
// We are unable to identify which is which, so we can't remove and recreate those. | ||
// Instead, we reload the theme.json styles from the plugin. | ||
// Because they'll use the same selectors and load later, | ||
// they'll have higher priority than core's. | ||
// | ||
// Theoretically, this approach with 'theme' styles could be problematic: | ||
// if we remove style properties in the plugin, if selectors change, etc. | ||
// We need to address this issue directly in core by alowing to identify | ||
// styles coming from theme.json. | ||
// | ||
// A final note about 'theme' styles: this flag is used to identify theme | ||
// styles that may need to be removed if the user toggles | ||
// "Preferences > Use theme styles" in the preferences modal. | ||
// | ||
! in_array( $style['__unstableType'], array( 'globalStyles', 'presets' ), true ) | ||
) { | ||
$styles_without_existing_global_styles[] = $style; | ||
} | ||
} | ||
|
||
$new_global_styles = array(); | ||
|
||
$css_variables = gutenberg_get_global_stylesheet( array( 'variables' ) ); | ||
if ( '' !== $css_variables ) { | ||
$new_global_styles[] = array( | ||
'css' => $css_variables, | ||
'__unstableType' => 'presets', | ||
); | ||
} | ||
|
||
$css_presets = gutenberg_get_global_stylesheet( array( 'presets' ) ); | ||
if ( '' !== $css_presets ) { | ||
$new_global_styles[] = array( | ||
'css' => $css_presets, | ||
'__unstableType' => 'presets', | ||
); | ||
} | ||
|
||
if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) { | ||
$css_blocks = gutenberg_get_global_stylesheet( array( 'styles' ) ); | ||
if ( '' !== $css_blocks ) { | ||
$new_global_styles[] = array( | ||
'css' => $css_blocks, | ||
'__unstableType' => 'theme', | ||
); | ||
} | ||
} | ||
|
||
$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['user'] : ( | ||
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']; | ||
} | ||
if ( isset( $settings['__experimentalFeatures']['spacing']['padding'] ) ) { | ||
$settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['padding']; | ||
unset( $settings['__experimentalFeatures']['spacing']['padding'] ); | ||
} | ||
|
||
return $settings; | ||
} | ||
|
||
/** | ||
* Sanitizes global styles user content removing unsafe rules. | ||
* | ||
|
@@ -232,8 +74,6 @@ function gutenberg_global_styles_force_filtered_html_on_import_filter( $arg ) { | |
return $arg; | ||
} | ||
|
||
add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); | ||
|
||
// kses actions&filters. | ||
add_action( 'init', 'gutenberg_global_styles_kses_init' ); | ||
add_action( 'set_current_user', 'gutenberg_global_styles_kses_init' ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 has been extracted to the experimental folder because we don't know yet what's going to happen with the REST Endpoint this method complements. To land in core in the same release as the endpoint.