-
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
Fix parsing in do_blocks() and rendering of blocks on frontend in the_content #1244
Conversation
Note to self. After this I want to follow up with def41d5 |
58db195
to
7817f8a
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.
Couple remarks, but looks good.
lib/blocks.php
Outdated
// do nothing if the block is not registered. | ||
$block_name = $matches['block_name'][ $index ][0]; | ||
|
||
$output = ''; | ||
if ( ! isset( $wp_registered_blocks[ $block_name ] ) ) { |
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 a guideline apparently, but in the case of conditions and ternaries, I find keeping positive form as the condition helps readability (reversing negative for else
is awkward to follow). What do you think? In this case also allows you to collapse nested if
:
if ( isset( $wp_registered_blocks[ $block_name ] ) ) {
$block_attributes_string = $matches['attributes'][ $index ][0];
$block_attributes = parse_block_attributes( $block_attributes_string );
// Call the block's render function to generate the dynamic output.
$output = call_user_func( $wp_registered_blocks[ $block_name ]['render'], $block_attributes );
} elseif ( isset( $matches['content'][ $index ][0] ) ) {
$output = $matches['content'][ $index ][0];
}
preg_match_all( $matcher, $content, $matches, PREG_OFFSET_CAPTURE ); | ||
|
||
$new_content = $content; | ||
$offset_differential = 0; |
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.
Curious if it would be simpler to implement as while ( preg_match( $matcher, $content, $match ) ) {
Maybe a performance hit by repeatedly matching the same string.
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.
@aduth good thought, but the problem is that preg_match
doesn't accept an offset, so it would have to keep running the regex and search from the beginning of the string. So yeah, it would be less efficient.
Regardless, all of this regex logic will be eliminated once a PEG is implemented in PHP (#1152).
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.
preg_match
does accept an offset:
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.
Oh, TIL. In any case, re-using the one set of results from preg_match_all
seems good.
Thanks for the fix @westonruter. |
So this is kinda breaking other things in new and interesting ways. By firing For example, we're experimenting with converting some shortcodes into blocks -- but we're hitting inconsistent results because the existing shortcodes fire after wpautop, and blocks fire before -- so rando Talked this over with @matias at the GM, possibly worth looking at seeing if we can do a fix in |
do_blocks
to run asthe_content
filter at priority 9 instead of 10, to ensure it applies beforewpautop
. Blocks were getting processed afterwpautop
was being applied, causing problems with extra paragraphs being added erroneously (see below).❌ Before:
✅ After: