Skip to content

Commit 72380e3

Browse files
Editor: Backport foundation for Layout block support refactor (part 1).
Backports the following changes from the Gutenberg repository: * [WordPress/gutenberg/40875 gutenberg/40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json * [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544 * [WordPress/gutenberg/42087 gutenberg/42087] Theme.json: Add block support feature level selectors for blocks gutenberg/42087 * [WordPress/gutenberg/43792 gutenberg/43792] Global Styles: Split root layout rules into a different function gutenberg/43792 * [WordPress/gutenberg/42544 gutenberg/42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg/42544 * [WordPress/gutenberg/42665 gutenberg/42665] Layout: Reduce specificity of fallback blockGap styles gutenberg/42665 * [WordPress/gutenberg/42085 gutenberg/42085] Core CSS support for root padding and alignfull blocks gutenberg/42085 Notes: * It doesn't entirely port over PR 40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`. * [54159] was reverted in [54160] due to PHPUnit test failures for tests added by the commit. Later, tests passed when applied on top of `trunk`. There were various outages today of upstream `wp-env` dependencies, which likely were the root cause of the earlier failures. For historical tracking and to make sure, recommitting [54159] but instead on top of current `trunk`. See PR 3205 for more details. * Giving additional props for those who did a deep dive investigation into the failed tests. Follow-up to [54160], [54159]. Props andrewserong, aaronrobertshaw, isabel_brison, bernhard-reiter, hellofromTonya. See #56467. git-svn-id: https://develop.svn.wordpress.org/trunk@54162 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d6e842c commit 72380e3

9 files changed

Lines changed: 1171 additions & 92 deletions

File tree

src/wp-includes/block-editor.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ function get_default_block_editor_settings() {
209209
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
210210
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
211211
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
212+
'disableLayoutStyles' => get_theme_support( 'disable-layout-styles' ),
212213
'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ),
213214
'enableCustomSpacing' => get_theme_support( 'custom-spacing' ),
214215
'enableCustomUnits' => get_theme_support( 'custom-units' ),
@@ -417,6 +418,18 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
417418
$block_classes['css'] = $actual_css;
418419
$global_styles[] = $block_classes;
419420
}
421+
} else {
422+
// If there is no `theme.json` file, ensure base layout styles are still available.
423+
$block_classes = array(
424+
'css' => 'base-layout-styles',
425+
'__unstableType' => 'base-layout',
426+
'isGlobalStyles' => true,
427+
);
428+
$actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
429+
if ( '' !== $actual_css ) {
430+
$block_classes['css'] = $actual_css;
431+
$global_styles[] = $block_classes;
432+
}
420433
}
421434

422435
$editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
@@ -474,9 +487,24 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
474487
$editor_settings['enableCustomSpacing'] = $editor_settings['__experimentalFeatures']['spacing']['padding'];
475488
unset( $editor_settings['__experimentalFeatures']['spacing']['padding'] );
476489
}
490+
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) {
491+
$editor_settings['disableCustomSpacingSizes'] = ! $editor_ettings['__experimentalFeatures']['spacing']['customSpacingSize'];
492+
unset( $editor_settings['__experimentalFeatures']['spacing']['customSpacingSize'] );
493+
}
494+
495+
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) {
496+
$spacing_sizes_by_origin = $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'];
497+
$editor_settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ?
498+
$spacing_sizes_by_origin['custom'] : (
499+
isset( $spacing_sizes_by_origin['theme'] ) ?
500+
$spacing_sizes_by_origin['theme'] :
501+
$spacing_sizes_by_origin['default']
502+
);
503+
}
477504

478505
$editor_settings['__unstableResolvedAssets'] = _wp_get_iframed_editor_assets();
479506
$editor_settings['localAutosaveInterval'] = 15;
507+
$editor_settings['disableLayoutStyles'] = current_theme_supports( 'disable-layout-styles' );
480508
$editor_settings['__experimentalDiscussionSettings'] = array(
481509
'commentOrder' => get_option( 'comment_order' ),
482510
'commentsPerPage' => get_option( 'comments_per_page' ),

src/wp-includes/class-wp-theme-json-resolver.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,54 @@ public static function get_theme_data( $deprecated = array(), $options = array()
232232
return $with_theme_supports;
233233
}
234234

235+
/**
236+
* Gets the styles for blocks from the block.json file.
237+
*
238+
* @since 6.1.0
239+
*
240+
* @return WP_Theme_JSON
241+
*/
242+
public static function get_block_data() {
243+
$registry = WP_Block_Type_Registry::get_instance();
244+
$blocks = $registry->get_all_registered();
245+
$config = array( 'version' => 1 );
246+
foreach ( $blocks as $block_name => $block_type ) {
247+
if ( isset( $block_type->supports['__experimentalStyle'] ) ) {
248+
$config['styles']['blocks'][ $block_name ] = static::remove_json_comments( $block_type->supports['__experimentalStyle'] );
249+
}
250+
251+
if (
252+
isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ) &&
253+
null === _wp_array_get( $config, array( 'styles', 'blocks', $block_name, 'spacing', 'blockGap' ), null )
254+
) {
255+
// Ensure an empty placeholder value exists for the block, if it provides a default blockGap value.
256+
// The real blockGap value to be used will be determined when the styles are rendered for output.
257+
$config['styles']['blocks'][ $block_name ]['spacing']['blockGap'] = null;
258+
}
259+
}
260+
261+
// Core here means it's the lower level part of the styles chain.
262+
// It can be a core or a third-party block.
263+
return new WP_Theme_JSON( $config, 'core' );
264+
}
265+
266+
/**
267+
* When given an array, this will remove any keys with the name `//`.
268+
*
269+
* @param array $array The array to filter.
270+
* @return array The filtered array.
271+
*/
272+
private static function remove_json_comments( $array ) {
273+
unset( $array['//'] );
274+
foreach ( $array as $k => $v ) {
275+
if ( is_array( $v ) ) {
276+
$array[ $k ] = static::remove_json_comments( $v );
277+
}
278+
}
279+
280+
return $array;
281+
}
282+
235283
/**
236284
* Returns the custom post type that contains the user's origin config
237285
* for the active theme or a void array if none are found.
@@ -370,6 +418,7 @@ public static function get_user_data() {
370418
* @since 5.8.0
371419
* @since 5.9.0 Added user data, removed the `$settings` parameter,
372420
* added the `$origin` parameter.
421+
* @since 6.1.0 Added block data.
373422
*
374423
* @param string $origin Optional. To what level should we merge data.
375424
* Valid values are 'theme' or 'custom'. Default 'custom'.
@@ -382,6 +431,7 @@ public static function get_merged_data( $origin = 'custom' ) {
382431

383432
$result = new WP_Theme_JSON();
384433
$result->merge( static::get_core_data() );
434+
$result->merge( static::get_block_data() );
385435
$result->merge( static::get_theme_data() );
386436

387437
if ( 'custom' === $origin ) {

0 commit comments

Comments
 (0)