-
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: Stabilize border block support keys #64330
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 |
---|---|---|
|
@@ -43,3 +43,25 @@ function gutenberg_filter_block_type_metadata_settings_allow_variations_php_file | |
return $settings; | ||
} | ||
add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_allow_variations_php_file', 10, 2 ); | ||
|
||
/** | ||
* Filters the block type arguments during registration to stabilize experimental block supports. | ||
* | ||
* This is a temporary compatibility shim as the approach in core is for this to be handled | ||
* within the WP_Block_Type class rather than requiring a filter. | ||
* | ||
* @param array $args Array of arguments for registering a block type. | ||
* @return array Array of arguments for registering a block type. | ||
*/ | ||
function gutenberg_stabilize_experimental_block_supports( $args ) { | ||
|
||
if ( empty( $args['supports']['__experimentalBorder'] ) ) { | ||
return $args; | ||
} | ||
|
||
$args['supports']['border'] = $args['supports']['__experimentalBorder']; | ||
|
||
return $args; | ||
} | ||
|
||
add_filter( 'register_block_type_args', 'gutenberg_stabilize_experimental_block_supports', PHP_INT_MAX, 1 ); | ||
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. As this won't land until 6.8, we'll need to move this to the 6.8 compat folder. Given there's already the same filter equivalent for typography supports, we probably only need to move the border stabilization into that same filter function and rename. I believe there is a small cost with filters, so while I like the separation of concerns to have each support in its own filter, there could be a benefit to have a single function to stabilize block supports. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,34 @@ function mergeBlockVariations( | |
return result; | ||
} | ||
|
||
/** | ||
* Function to stabilize border supports. | ||
* This function needs to be merged once we have below PR merged. | ||
* | ||
* Reference from - https://github.com/WordPress/gutenberg/pull/63401/ | ||
* | ||
* | ||
* @param {Object} rawSupports Support for the block. | ||
* | ||
* @return {Object} Stabilized supports. | ||
*/ | ||
function stabilizeBorderSupports( 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. The typography stabilization PR introduced a |
||
if ( ! rawSupports ) { | ||
return rawSupports; | ||
} | ||
|
||
const supports = { ...rawSupports }; | ||
|
||
if ( | ||
supports?.__experimentalBorder && | ||
typeof supports.__experimentalBorder === 'object' | ||
) { | ||
supports.border = supports.__experimentalBorder; | ||
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 needs to merge with values already using the stabilized |
||
} | ||
|
||
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. | ||
|
@@ -102,13 +130,21 @@ export const processBlockType = | |
), | ||
}; | ||
|
||
blockType.supports = stabilizeBorderSupports( blockType.supports ); | ||
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 need some unit tests to cover all the stabilization, similar to those in #63401. |
||
|
||
const settings = applyFilters( | ||
'blocks.registerBlockType', | ||
blockType, | ||
name, | ||
null | ||
); | ||
|
||
// Re-stabilize any experimental supports after applying filters. | ||
// This ensures that any supports updated by filters are also stabilized. | ||
// Reference from - https://github.com/WordPress/gutenberg/pull/63401/ | ||
|
||
blockType.supports = stabilizeBorderSupports( blockType.supports ); | ||
|
||
if ( | ||
settings.description && | ||
typeof settings.description !== 'string' | ||
|
@@ -119,29 +155,40 @@ 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 ) => { | ||
// Stabilize any experimental supports before applying filters. | ||
deprecation.supports = stabilizeBorderSupports( | ||
deprecation.supports | ||
); | ||
|
||
const filteredDeprecation = // 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 | ||
); | ||
|
||
// Re-stabilize any experimental supports after applying filters. | ||
// This ensures that any supports updated by filters are also stabilized. | ||
filteredDeprecation.supports = stabilizeBorderSupports( | ||
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.
This would completely overwrite any prior config that already uses the stabilized key.