-
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
Stabilize the __experimentalBorder support #65968
Changes from 3 commits
cace01d
344e635
5277b53
aae3411
97a8dd1
f9dbbfc
849a6d3
5f2cd17
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ function gutenberg_register_border_support( $block_type ) { | |
$block_type->attributes = array(); | ||
} | ||
|
||
if ( block_has_support( $block_type, array( '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) { | ||
if ( block_has_support( $block_type, array( 'border', '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) { | ||
$block_type->attributes['style'] = array( | ||
'type' => 'object', | ||
); | ||
|
@@ -52,7 +52,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { | |
if ( | ||
gutenberg_has_border_feature_support( $block_type, 'radius' ) && | ||
isset( $block_attributes['style']['border']['radius'] ) && | ||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) | ||
( ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) || | ||
! wp_should_skip_block_supports_serialization( $block_type, 'border', 'radius' ) ) | ||
) { | ||
$border_radius = $block_attributes['style']['border']['radius']; | ||
|
||
|
@@ -67,7 +68,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { | |
if ( | ||
gutenberg_has_border_feature_support( $block_type, 'style' ) && | ||
isset( $block_attributes['style']['border']['style'] ) && | ||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) | ||
( ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) || | ||
! wp_should_skip_block_supports_serialization( $block_type, 'border', 'style' ) ) | ||
) { | ||
$border_block_styles['style'] = $block_attributes['style']['border']['style']; | ||
} | ||
|
@@ -76,7 +78,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { | |
if ( | ||
$has_border_width_support && | ||
isset( $block_attributes['style']['border']['width'] ) && | ||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) | ||
( ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) || | ||
! wp_should_skip_block_supports_serialization( $block_type, 'border', 'width' ) ) | ||
) { | ||
$border_width = $block_attributes['style']['border']['width']; | ||
|
||
|
@@ -91,7 +94,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { | |
// Border color. | ||
if ( | ||
$has_border_color_support && | ||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) | ||
( ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) || | ||
! wp_should_skip_block_supports_serialization( $block_type, 'border', 'color' ) ) | ||
) { | ||
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null; | ||
$custom_border_color = $block_attributes['style']['border']['color'] ?? null; | ||
|
@@ -103,9 +107,9 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { | |
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { | ||
$border = $block_attributes['style']['border'][ $side ] ?? null; | ||
$border_side_values = array( | ||
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, | ||
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, | ||
'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, | ||
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, 'border', 'width' ) ? $border['width'] : null, | ||
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, 'border', 'color' ) ? $border['color'] : null, | ||
'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, 'border', 'style' ) ? $border['style'] : null, | ||
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. Does this need to handle the skip serialization for the 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. In order to make sure that, now I have added the skip serialization for the __experimentalBorder case too. |
||
); | ||
$border_block_styles[ $side ] = $border_side_values; | ||
} | ||
|
@@ -143,15 +147,16 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { | |
function gutenberg_has_border_feature_support( $block_type, $feature, $default_value = false ) { | ||
// Check if all border support features have been opted into via `"__experimentalBorder": true`. | ||
if ( $block_type instanceof WP_Block_Type ) { | ||
$block_type_supports_border = $block_type->supports['__experimentalBorder'] ?? $default_value; | ||
$block_type_supports_border = $block_type->supports['border'] ?? $block_type->supports['__experimentalBorder'] ?? $default_value; | ||
if ( true === $block_type_supports_border ) { | ||
return true; | ||
} | ||
} | ||
|
||
// Check if the specific feature has been opted into individually | ||
// via nested flag under `__experimentalBorder`. | ||
return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value ); | ||
return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value ) | ||
|| block_has_support( $block_type, array( 'border', $feature ), $default_value ); | ||
} | ||
|
||
// Register the block support. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,22 @@ function mergeBlockVariations( | |
|
||
return result; | ||
} | ||
function handleExperimentalBorder( rawSupports ) { | ||
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'll also need to handle the transformation for Typography at some point (which I worked on in #63401). Would it be worth renaming this to 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. Function name has been updated as you mentioned above. |
||
if ( ! rawSupports ) { | ||
return rawSupports; | ||
} | ||
|
||
const supports = { ...rawSupports }; | ||
|
||
if ( | ||
supports?.__experimentalBorder && | ||
typeof supports.__experimentalBorder === 'object' | ||
) { | ||
supports.border = supports.__experimentalBorder; | ||
} | ||
|
||
return supports; | ||
} | ||
/** | ||
* Takes the unprocessed block type settings, merges them with block type metadata | ||
* and applies all the existing filters for the registered block type. | ||
|
@@ -101,13 +116,15 @@ export const processBlockType = | |
: [] | ||
), | ||
}; | ||
blockType.supports = handleExperimentalBorder( blockType.supports ); | ||
|
||
const settings = applyFilters( | ||
'blocks.registerBlockType', | ||
blockType, | ||
name, | ||
null | ||
); | ||
blockType.supports = handleExperimentalBorder( blockType.supports ); | ||
|
||
if ( | ||
settings.description && | ||
|
@@ -119,29 +136,30 @@ export const processBlockType = | |
} | ||
|
||
if ( settings.deprecated ) { | ||
settings.deprecated = settings.deprecated.map( ( deprecation ) => | ||
Object.fromEntries( | ||
Object.entries( | ||
// Only keep valid deprecation keys. | ||
applyFilters( | ||
'blocks.registerBlockType', | ||
// Merge deprecation keys with pre-filter settings | ||
// so that filters that depend on specific keys being | ||
// present don't fail. | ||
{ | ||
// Omit deprecation keys here so that deprecations | ||
// can opt out of specific keys like "supports". | ||
...omit( blockType, DEPRECATED_ENTRY_KEYS ), | ||
...deprecation, | ||
}, | ||
blockType.name, | ||
deprecation | ||
) | ||
).filter( ( [ key ] ) => | ||
settings.deprecated = settings.deprecated.map( ( deprecation ) => { | ||
deprecation.supports = handleExperimentalBorder( | ||
deprecation.supports | ||
); | ||
|
||
const filteredDeprecation = applyFilters( | ||
'blocks.registerBlockType', | ||
{ | ||
...omit( blockType, DEPRECATED_ENTRY_KEYS ), | ||
...deprecation, | ||
}, | ||
blockType.name, | ||
deprecation | ||
); | ||
filteredDeprecation.supports = handleExperimentalBorder( | ||
filteredDeprecation.supports | ||
); | ||
|
||
return Object.fromEntries( | ||
Object.entries( filteredDeprecation ).filter( ( [ key ] ) => | ||
DEPRECATED_ENTRY_KEYS.includes( key ) | ||
) | ||
) | ||
); | ||
); | ||
} ); | ||
} | ||
|
||
if ( ! isPlainObject( settings ) ) { | ||
|
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.
I haven't tested this, but I think this needs to have two
block_has_supports
checks (one forborder
and one for__experimentalBorder
) as an array passed to this function is treated as a path, rather than an OR. I see there's a similar check down in line 158 that looks correct to me.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.
Thanks for your suggestions @andrewserong
I have updated this case in the recent commit.