-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Patterns: Add the pattern name to pattern blocks when they are converted #10248
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
base: trunk
Are you sure you want to change the base?
Patterns: Add the pattern name to pattern blocks when they are converted #10248
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
b5812e5 to
d8f1d20
Compare
|
Is there other metadata we could/should add to patterns - some of them seem to also contain a longer description. It would be good to support as much metadata as possible IMO. |
Good question. What do you think the editor might need? There are also optional categories and block types, which could come in handy if the editor ever wanted to extend "change design" to patterns bundled with templates. I'm not sure. |
|
The description would be useful for WordPress/gutenberg#72574. Not sure about the others. |
d8f1d20 to
ed8d449
Compare
Done! |
ed8d449 to
dc0bc0a
Compare
ef3a7bc to
a640eaa
Compare
src/wp-includes/blocks.php
Outdated
| $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); | ||
|
|
||
| $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); |
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.
Should these lines be duplicated like this?
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.
WUT? No they shouldn't 😄
Thanks for spotting that. Human paste error.
src/wp-includes/blocks.php
Outdated
| $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); | ||
|
|
||
| $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); |
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.
| $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); | |
| $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); | |
| $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); |
src/wp-includes/blocks.php
Outdated
| * For single-root patterns, add the pattern name to make this a pattern instance in the editor. | ||
| * If the pattern has metadata, merge it with the existing metadata. | ||
| */ |
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.
| * For single-root patterns, add the pattern name to make this a pattern instance in the editor. | |
| * If the pattern has metadata, merge it with the existing metadata. | |
| */ | |
| * For single-root patterns, add the pattern name to make this a pattern instance in the editor. | |
| * If the pattern has metadata, merge it with the existing metadata. | |
| */ |
src/wp-includes/blocks.php
Outdated
| * Merge pattern metadata with existing block metadata. | ||
| * Pattern metadata takes precedence, but existing block metadata | ||
| * is preserved as a fallback when the pattern doesn't define that field. | ||
| * Only the defined fields (name, description, categories) are updated; | ||
| * other metadata keys are preserved. | ||
| */ |
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.
| * Merge pattern metadata with existing block metadata. | |
| * Pattern metadata takes precedence, but existing block metadata | |
| * is preserved as a fallback when the pattern doesn't define that field. | |
| * Only the defined fields (name, description, categories) are updated; | |
| * other metadata keys are preserved. | |
| */ | |
| * Merge pattern metadata with existing block metadata. | |
| * Pattern metadata takes precedence, but existing block metadata | |
| * is preserved as a fallback when the pattern doesn't define that field. | |
| * Only the defined fields (name, description, categories) are updated; | |
| * other metadata keys are preserved. | |
| */ |
src/wp-includes/blocks.php
Outdated
| ) as $key => $pattern_key ) { | ||
| $value = $pattern[ $pattern_key ] ?? $block_metadata[ $key ] ?? null; | ||
| if ( $value ) { | ||
| $block_metadata[ $key ] = 'categories' === $key && is_array( $value ) |
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.
Is the $key check needed here? Shouldn't array_map() always be used when the value is an array, regardless of the key?
| $block_metadata[ $key ] = 'categories' === $key && is_array( $value ) | |
| $block_metadata[ $key ] = is_array( $value ) |
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.
By jove, you're right! I just commit stuff when it works
…r single-root patterns, allowing the pattern name and title to be stored in the block attributes. Adds unit tests.
…cks` function Enhances the `resolve_pattern_blocks` function to include additional metadata attributes such as `description` and `categories` for single-root patterns. Updates unit tests to validate the new functionality, including sanitization of potentially harmful characters in pattern attributes.
Refines the logic for merging metadata in single-root patterns, ensuring that existing metadata is preserved and new attributes such as `name`, `description`, and `categories` are correctly integrated. Updates unit tests to validate the new behavior, including a test for preserving existing metadata.
…teardown in `resolvePatternBlocks` test suite.
Improves the handling of metadata for single-root patterns by ensuring that existing metadata is preserved while integrating new attributes such as `name`, `description`, and `categories`. Updates unit tests to reflect the changes in metadata structure, ensuring accurate representation of merged metadata.
…n single-pattern container blocks for version 7.0.0.
Introduces a new block pattern 'core/with-custom-metadata' with custom metadata keys. Updates the unit tests to ensure that custom metadata is preserved when resolving patterns, enhancing the test coverage for metadata handling in block patterns.
Improves the documentation within the `resolve_pattern_blocks` function by enhancing comments related to metadata handling for single-root patterns. This change aims to provide clearer guidance on the merging process of pattern metadata with existing block metadata.
a640eaa to
44c4c8f
Compare
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: ramonjd <[email protected]> Source: WordPress/gutenberg@ec6ec9e
Builds upon #10180
Props to @scruffian
Modifies the
resolve_pattern_blocksfunction to include metadata for single-root patterns, allowing the pattern name and title to be stored in the block attributes. The metadata contains thepatternNameandnameattributes.This enables identification of patterns inside templates and allows for special treatment in the editor.
This will be used to test contentOnly editing (WordPress/gutenberg#71517), so that themes that use patterns automatically opt into this behaviour.
Trac ticket: https://core.trac.wordpress.org/ticket/64123
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.