Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

chriszarate
Copy link
Member

Description

Fixes #86.

Our current solution for extracting and parsing blocks from synced patterns is a bit too experimental. It hooks into the render cycle and attempts to detect when the block being rendered has a synced pattern parent block. It does this via a somewhat fragile inspection of WP_Block_Supports.

The issue with that approach is that it can't tell when the synced pattern is a direct parent block vs. an ancestor block, so it ends up duplicating blocks in nested trees. There's no way to make this determination reliably, so we need to abandon that approach.

Instead of hooking into the existing render cycle, we can just render synced patterns manually. This carries some of its own fragility, because we need to duplicate some code from Core to support synced pattern overrides. But it's an overall improvement and the duplicated code is just a few lines.

It does require making ContentParser#render_parsed_block a public method, which was the path I found that required the least refactoring.

@toshotosho Thanks for the failing test case. Can you check out this branch and make sure it resolves the issue for you? Feel free to contribute additional test cases.

@chriszarate chriszarate added the bug Something isn't working label Mar 20, 2025
@chriszarate chriszarate requested a review from a team as a code owner March 20, 2025 16:05
alecgeatches
alecgeatches previously approved these changes Mar 20, 2025
Copy link
Contributor

@alecgeatches alecgeatches left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix! I like that the core/block addition is actually smaller now, and we've doubled the size of the tests on synced patterns. The new tests cover a lot more.

It'd be great to have @toshotosho verify on real data first per the bug in #86, but I'm happy to merge when verified.

public function test_synced_pattern_with_override_in_nested_content() {
$synced_pattern_content = '
<!-- wp:group -->
<div class=k"wp-block-group">
Copy link
Contributor

@alecgeatches alecgeatches Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note, there's 3 k"s scattered in here that appear to be an accident, unless I'm missing something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5dbe308

#vimfail

'inner_blocks' => [],
'locked' => false,
];
if ( ! $post instanceof \WP_Post ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extreme nit - can we use use WP_Block; up top and this for consistency?

Suggested change
if ( ! $post instanceof \WP_Post ) {
if ( ! $post instanceof WP_Post ) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Fixed in fc24575

@toshotosho
Copy link

Thanks guys!

I just deployed the fix and it solves our issue without breaking anything else, so great work!

@alecgeatches alecgeatches merged commit 774808a into trunk Mar 21, 2025
4 checks passed
@alecgeatches alecgeatches deleted the fix/synced-pattern-nesting branch March 21, 2025 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested blocks in a synced pattern are listed multiple times

3 participants