-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
cc7d781
0247f3d
a33b712
a6dece9
4252f57
552948c
46f43e7
37d2a43
64ffed5
4f03b0b
d078f86
0329e09
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Adds settings to the mobile block editor. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Adds settings to the mobile block editor. | ||
* | ||
* This is used by the settings REST endpoint and it should land in core | ||
* as soon as lib/class-wp-rest-block-editor-settings-controller.php does. | ||
* | ||
* @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 ); |
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'; | ||
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(); | ||
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. We've done some modifications to the data available in theme.json that we expect to land in 6.0. While this doesn't have any changes with respect to what's in WordPress core, we still need to make sure this calls the |
||
|
||
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'] : ( | ||
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. |
||
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'] ); | ||
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 PR brings back this This line had been accidentally removed in #32898 while backporting changes from this core patch https://core.trac.wordpress.org/changeset/51203 (note how the core patch still had the |
||
} | ||
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 ); |
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' ); | ||
|
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.