-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add __default binding for pattern overrides
#60694
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
Changes from all commits
9d45054
a099f03
bbca6e9
e89587a
8acdc99
f897e0f
77d7b20
afee53f
c6b1c4f
e5b834f
9b2dce8
bb2280e
50822b7
29a65a4
869d53d
b07b346
1cd20dd
5638bcd
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,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/6694 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/60694 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
| /** | ||
| * Temporary compatibility shims for block APIs present in Gutenberg. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Replace the `__default` block bindings attribute with the full list of supported | ||
| * attribute names for pattern overrides. | ||
| * | ||
| * @param array $parsed_block The full block, including name and attributes. | ||
| * | ||
| * @return string The parsed block with default binding replace. | ||
| */ | ||
| function gutenberg_replace_pattern_override_default_binding( $parsed_block ) { | ||
| $supported_block_attrs = array( | ||
| 'core/paragraph' => array( 'content' ), | ||
| 'core/heading' => array( 'content' ), | ||
| 'core/image' => array( 'id', 'url', 'title', 'alt' ), | ||
| 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), | ||
| ); | ||
|
|
||
| $bindings = $parsed_block['attrs']['metadata']['bindings'] ?? array(); | ||
| if ( | ||
| isset( $bindings['__default']['source'] ) && | ||
| 'core/pattern-overrides' === $bindings['__default']['source'] | ||
| ) { | ||
| $updated_bindings = array(); | ||
|
|
||
| // Build an binding array of all supported attributes. | ||
| // Note that this also omits the `__default` attribute from the | ||
| // resulting array. | ||
| foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) { | ||
| // Retain any non-pattern override bindings that might be present. | ||
| $updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] ) | ||
| ? $bindings[ $attribute_name ] | ||
| : array( 'source' => 'core/pattern-overrides' ); | ||
| } | ||
| $parsed_block['attrs']['metadata']['bindings'] = $updated_bindings; | ||
| } | ||
|
|
||
| return $parsed_block; | ||
| } | ||
|
|
||
| add_filter( 'render_block_data', 'gutenberg_replace_pattern_override_default_binding', 10, 1 ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,25 +88,41 @@ export function convertLegacyBlockNameAndAttributes( name, attributes ) { | |
| ( name === 'core/paragraph' || | ||
| name === 'core/heading' || | ||
| name === 'core/image' || | ||
| name === 'core/button' ) | ||
| name === 'core/button' ) && | ||
| newAttributes.metadata.bindings.__default?.source !== | ||
| 'core/pattern-overrides' | ||
| ) { | ||
| const bindings = [ | ||
| 'content', | ||
| 'url', | ||
| 'title', | ||
| 'id', | ||
|
Contributor
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. To do this strictly correctly, we should use That said, IMO the strictly correct way seems overkill for the purpose of this branch. We could just loop over all keys of
Contributor
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. I agree we should probably just iterate through the bindings and remove the ones with source |
||
| 'alt', | ||
| 'text', | ||
| 'linkTarget', | ||
| ]; | ||
| // Delete any existing individual bindings and add a default binding. | ||
| // It was only possible to add all the default attributes through the UI, | ||
| // So as soon as we find an attribute, we can assume all default attributes are overridable. | ||
| let hasPatternOverrides = false; | ||
| bindings.forEach( ( binding ) => { | ||
| if ( | ||
| newAttributes.metadata.bindings[ binding ]?.source?.name === | ||
| 'pattern_attributes' | ||
| newAttributes.metadata.bindings[ binding ]?.source === | ||
| 'core/pattern-overrides' | ||
| ) { | ||
| newAttributes.metadata.bindings[ binding ].source = | ||
| 'core/pattern-overrides'; | ||
| hasPatternOverrides = true; | ||
| newAttributes.metadata = { | ||
| ...newAttributes.metadata, | ||
| bindings: { ...newAttributes.metadata.bindings }, | ||
| }; | ||
| delete newAttributes.metadata.bindings[ binding ]; | ||
| } | ||
| } ); | ||
| if ( hasPatternOverrides ) { | ||
| newAttributes.metadata.bindings.__default = { | ||
| source: 'core/pattern-overrides', | ||
| }; | ||
| } | ||
|
Comment on lines
+121
to
+125
Contributor
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 works ok in the site editor, but for some reason isn't working in the post editor when navigating to the original pattern. It must be that
Member
Author
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.
Could you clarify the testing instructions for this? I'm seeing a consistent result on my end. Kapture.2024-04-23.at.14.59.09.mp4It won't automatically change from |
||
| } | ||
| } | ||
| return [ name, newAttributes ]; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.