-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add Block Hooks (a.k.a. Auto-inserting Blocks) #5158
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b926b86
Add callback arg to serialize_blocks
ockham f7465b9
Inject theme attribute during serialization
ockham 7fda6f5
Add callback arg to serialize_blocks (plural)
ockham 3ad1fd7
Add block_hooks property
ockham cdcfbcc
Implement get_hooked_blocks()
ockham c897d86
Implement insert_hooked_block and insert_hooked_blocks
ockham 5013547
Insert hooked blocks during serialization
ockham c8115dc
Remove stray echo
ockham 37496a0
Map camelCased position string to snake_case
ockham 600e55d
Auto-insert into block pattern
ockham b1a82fe
Also apply to get_all_registered (needed for REST API)
ockham 25b1ed4
Use third-person singular verb in PHPDoc
ockham ea1f53f
Use multiline comment format for inline comment
ockham 8fc29bc
Adjust alignment and add Default value to PHPDoc for `serialize_block`
ockham 5cd2ded
Adjust alignment and add Default value to PHPDoc for `serialize_blocks`.
ockham 0267cc9
Don't allow null value for block_hooks
ockham 67e6a34
Placate linter
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Insert hooked blocks during serialization
- Loading branch information
commit 501354789fb49ded8ed65e30c80a2698955ac000
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -514,23 +514,41 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Injects the active theme's stylesheet as a `theme` attribute | ||||||
| * into a given template part block. | ||||||
| * Returns a function that... | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| * @access private | ||||||
| * | ||||||
| * @param array $block a parsed block. | ||||||
| * @return array Updated block. | ||||||
| * @param WP_Block_Template $block_template a block template. | ||||||
| * @return callable A function that returns a block. | ||||||
| */ | ||||||
| function _inject_theme_attribute_in_template_part_block( $block ) { | ||||||
| if ( | ||||||
| 'core/template-part' === $block['blockName'] && | ||||||
| ! isset( $block['attrs']['theme'] ) | ||||||
| ) { | ||||||
| $block['attrs']['theme'] = get_stylesheet(); | ||||||
| } | ||||||
| return $block; | ||||||
| function _parsed_block_visitor( $block_template ) { | ||||||
| return function( $block ) use ( $block_template ) { | ||||||
| if ( | ||||||
| 'core/template-part' === $block['blockName'] && | ||||||
| ! isset( $block['attrs']['theme'] ) | ||||||
| ) { | ||||||
| $block['attrs']['theme'] = get_stylesheet(); | ||||||
| } | ||||||
|
|
||||||
| $hooked_blocks = get_hooked_blocks( $block['blockName'] ); | ||||||
| foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) { | ||||||
| $hooked_block = array( | ||||||
| 'blockName' => $hooked_block_type, | ||||||
| 'attrs' => array(), | ||||||
| 'innerHTML' => '', | ||||||
| 'innerContent' => array(), | ||||||
| 'innerBlocks' => array(), | ||||||
| ); | ||||||
| // Need to pass full current block, (potentially its parent -- for sibiling insertion), relative position, and hooked_block. | ||||||
| $block = insert_hooked_block( $hooked_block, $relative_position, $block ); | ||||||
| /** | ||||||
| * This filter allows modifiying the auto-inserting behavior... | ||||||
| */ | ||||||
| $block = apply_filters( 'insert_hooked_block', $block, $hooked_blocks[ $hooked_block_type ], $block_template ); | ||||||
|
Contributor
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. Might use separate filters for template and pattern insertion
Suggested change
|
||||||
| } | ||||||
| return $block; | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -609,7 +627,8 @@ function _build_block_template_result_from_file( $template_file, $template_type | |||||
| } | ||||||
|
|
||||||
| $blocks = parse_blocks( $template_content ); | ||||||
| $template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' ); | ||||||
| $visitor = _parsed_block_visitor( $template ); | ||||||
| $template->content = serialize_blocks( $blocks, $visitor ); | ||||||
|
|
||||||
| return $template; | ||||||
| } | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.