-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Block Hooks: Make work with modified templates/parts/patterns #5523
Block Hooks: Make work with modified templates/parts/patterns #5523
Conversation
Right, that would be necessary here. Unless we reuse In that case, we would need to make So whatever works for you best 😀 |
1de1b5e
to
8ef9462
Compare
I've added some more comments to the Trac issue; the tl;dr is:
|
On the note of the global Edit: Also related: WordPress/gutenberg#55194 |
8ef9462
to
d7837be
Compare
Update: With the attribute renamed to diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php
index 93fad3ee95..d28274ab3a 100644
--- a/src/wp-includes/blocks.php
+++ b/src/wp-includes/blocks.php
@@ -781,12 +781,12 @@ function insert_hooked_blocks( &$block, $relative_position, $anchor_block_type,
) {
$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' );
}
- if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
+ //if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
if ( ! isset( $block['attrs']['metadata']['ignoredHookedBlocks'] ) ) {
$block['attrs']['metadata']['ignoredHookedBlocks'] = array();
}
$block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type;
- }
+ //}
}
return $markup;
} now successfully adds the information to the block in the editor 🎉
|
Also working for <!-- wp:comment-template -->
<!-- wp:columns {"metadata":{"ignoredHookedBlocks":["ockham/like-button"]},"style":{"spacing":{"margin":{"bottom":"var:preset|spacing|40"}}}} -->
<div class="wp-block-columns" style="margin-bottom:var(--wp--preset--spacing--40)"><!-- wp:column {"width":"40px"} -->
<div class="wp-block-column" style="flex-basis:40px"><!-- wp:avatar {"size":40,"style":{"border":{"radius":"20px"}}} /--></div>
<!-- /wp:column --> |
Better now: <!-- wp:comment-template {"metadata":{"ignoredHookedBlocks":["ockham/like-button"]}} --> (Also, no more patching needed -- the PR now Just Works™️) |
2ad2683
to
f5e4b6f
Compare
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 is shaping up nicely. I'm surprised how little changes are necessary to open the logic for templates and template parts modified by the site editor's user.
src/wp-includes/blocks.php
Outdated
@@ -757,6 +757,44 @@ function get_hooked_blocks() { | |||
return $hooked_blocks; | |||
} | |||
|
|||
function insert_hooked_blocks( $relative_position, &$anchor_block, $hooked_blocks, $context ) { |
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.
Minor: &$anchor_block
might go as a first param as it's the only one that can get modified. It's probably something we can discuss in #5609.
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.
Good point, thank you!
@@ -894,6 +894,14 @@ function _build_block_template_result_from_post( $post ) { | |||
} | |||
} | |||
|
|||
$hooked_blocks = get_hooked_blocks(); |
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.
That would be quite convenient for all templates stored in the database 👍🏻
|
||
// TODO: The following is only needed for the REST API endpoint. | ||
// Ideally, the code should thus be moved into the controller. | ||
if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { |
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 there any issue with running it always? It's a simple operation, so I don't expect it would impact the front end. I would be in favor of keeping it simple unless I miss something.
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.
No issue, other than it adds a few bytes per block (and it might be somewhat confusing for people). I just thought if we revisit #5514 (which requires different treatment of frontend and REST API), we might also look into this. Definitely okay to leave it in though.
496f2ff
to
39c69a3
Compare
Some notes:
|
839ee3b
to
f29ba55
Compare
Closing in favor or #5712. |
From a pair-programming session with @gziolo.
The biggest tradeoff we made in the implementation of Block Hooks was that we had to limit them to templates, template parts, and patterns that didn't have any user modifications (see
#59313
for the reason). We would like to remove this limitation, so they’ll also work with user-modified templates, parts, and patterns.The crucial problem we need to solve is to acknowledge if a user has opted to remove or persist a hooked block, so that the auto-insertion mechanism won't run again and inject an extraneous hooked block on the frontend when none is solicited.
TODO:
traverse_and_serialize_block(s)
' post-callback access to block instance #5525hookedBlocks
global attribute default is set correctly (to empty array).hookedBlocks
attribute for first and last child cases.ignoredHookedBlocks
is added to the correct block.Consider this PR experimental; we might want to break it down into smaller pieces to land invididually:
_build_block_template_result_from_post
gutenberg#55811metadata
global attribute #5608insert_hooked_blocks()
function #5609Trac ticket: https://core.trac.wordpress.org/ticket/59646
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.