-
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
[Experiment] Patterns: Allow the heading block and the button block to be partially synced #56704
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 |
---|---|---|
|
@@ -100,14 +100,17 @@ function gutenberg_render_block_connections( $block_content, $block, $block_inst | |
// Allowlist of blocks that support block connections. | ||
// Currently, we only allow the following blocks and attributes: | ||
// - Paragraph: content. | ||
// - Heading: content. | ||
// - Button: text. | ||
// - Image: url. | ||
$blocks_attributes_allowlist = array( | ||
'core/paragraph' => array( 'content' ), | ||
'core/heading' => array( 'content' ), | ||
'core/button' => array( 'text' ), | ||
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. Unfortunately, button blocks don't seem to be working correctly. When they have an override they're missing the It's because the code below does this: $tag_name = $tags->get_tag();
$markup = "<$tag_name>$custom_value</$tag_name>";
$updated_tags = new WP_HTML_Tag_Processor( $markup );
$updated_tags->next_tag();
return $updated_tags->get_updated_html(); So it considers the updated html of the block to just be the tag that it's updating and disregards any markup before or after that. Instead it needs to update the It is mentioned in the block bindings tracking issue - #54536.
I guess we need to drop button support until then. Image also doesn't work, as the code only updates tag content, not attribute values, so you end up with 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. @SantosGuillamot opened #57249 that updates the PHP implementation for block bindings that should fix the handling for the Heading and Button blocks. It was extracted from the full prototype for Custom Fields: #56867. |
||
'core/image' => array( 'url' ), | ||
); | ||
|
||
// Whitelist of the block types that support block connections. | ||
// Currently, we only allow the Paragraph and Image blocks to use block connections. | ||
if ( ! in_array( $block['blockName'], array_keys( $blocks_attributes_allowlist ), true ) ) { | ||
return $block_content; | ||
} | ||
|
@@ -168,14 +171,14 @@ function gutenberg_render_block_connections( $block_content, $block, $block_inst | |
continue; | ||
} | ||
|
||
$tags = new WP_HTML_Tag_Processor( $block_content ); | ||
$found = $tags->next_tag( | ||
array( | ||
// TODO: In the future, when blocks other than Paragraph and Image are | ||
// supported, we should build the full query from CSS selector. | ||
'tag_name' => $block_type->attributes[ $attribute_name ]['selector'], | ||
) | ||
); | ||
$selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ); | ||
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. It'd be good to add a comment above this saying something like:
I guess it's obvious in the context of this PR given it mentions the various heading block tags, but it might not be so obvious if you're just looking at this file on its own wondering why it works this way. |
||
$found = false; | ||
while ( ! $found && count( $selectors ) > 0 ) { | ||
$tags = new WP_HTML_Tag_Processor( $block_content ); | ||
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. Similarly, it might be worth a comment saying something like:
|
||
// TODO: In the future, to support connecting more block attributes, | ||
// we should build the full query from CSS selector. | ||
$found = $tags->next_tag( trim( array_shift( $selectors ) ) ); | ||
} | ||
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 could invert this loop and avoid the confusion @talldan wants to clarify, while improving performance.
this assumes that there are likely more tags than selectors, many more, and so the cost of checking each selector at each tag will be cheaper than checking every tag at each selector. additionally, we should make it pretty explicit here in a comment that the only selectors we currently support are tag names. $selectors = array();
foreach ( explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ) as $tag_name ) {
$selectors[] = trim( strtoupper( $tag_name ) );
}
$tags = new WP_HTML_Tag_Processor( $block_content );
while ( $tags->next_tag() ) {
$tag_name = $tags->get_tag();
if ( ! in_array( $tag_name, $selectors, true ) ) {
continue;
}
$markup = "<$tag_name>$custom_value</$tag_name>";
$updated_tags = new WP_HTML_Tag_Processor( $markup );
$updated_tags->next_tag();
// Get all the attributes from the original block and add them to the new markup.
$names = $tags->get_attribute_names_with_prefix( '' );
foreach ( $names as $name ) {
$updated_tags->set_attribute( $name, $tags->get_attribute( $name ) );
}
return $updated_tags->get_updated_html();
}
return $block_content; |
||
if ( ! $found ) { | ||
return $block_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.
Not really sure these comments are necessary, seeing how the same thing is listed below in an easy to read format. 😄