Skip to content
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

Add synced patterns support #75

Merged
merged 16 commits into from
Sep 3, 2024
Merged

Conversation

chriszarate
Copy link
Member

Description

Add support for synced patterns (previously known as "reusable blocks"). Synced patterns are implemented as a dynamic block (core/block) and currently result in a dead end in the API output:

{
  "blocks": [
    {
      "name": "core/block",
      "attributes": {
        "ref": 6
      }
    }
  ]
}

However that ref is a pointer to a wp_block post, where the blocks (and potential block data) is stored. This PR adds support for loading those blocks:

{
  "blocks": [
    {
      "name": "core/block",
      "attributes": {
        "ref": 6
      },
      "innerBlocks": [
        {
          "name": "core/paragraph",
          "attributes": {
            "content": "This is a block stored in a synced pattern!"
          }
        }
      ]
    }
  ]
}

Setting aside tests and comments, the diff here is only about 150 LOC. Its primary features are:

  • Add two new actions and one filter that allow the CoreBlock class to hook into the parser. These hooks are documented in the README.
  • Hook into the core render cycle so that we can capture the inner blocks of synced patterns and inject them as the parser walks the tree.
  • Provide support for important core features and use cases, including:
    • Multiple nested synced patterns
    • Block bindings inside synced patterns
    • Synced pattern overrides (itself a special kind of block binding)

The CoreBlock does some fairly advanced filtering but is well-documented in the comments. Likewise, the tests are fairly extensive but feel free to suggest additional cases.

@chriszarate chriszarate added the enhancement New feature or request label Aug 26, 2024
@chriszarate chriszarate requested a review from a team as a code owner August 26, 2024 20:41
ingeniumed
ingeniumed previously approved these changes Aug 28, 2024
Copy link
Contributor

@ingeniumed ingeniumed left a comment

Choose a reason for hiding this comment

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

Something I noticed that gutenberg does is that, it adds this paragraph block with the text This is a custom pattern to every block as an inner block. Should this be something worth filtering out, and offering a filter that could reverse it if someone doesn't want it? Ignore this, as it was me who added a paragraph saying that and forgot about it. Always check your blocks, folks.

It looks good to me. Would want to also get @alecgeatches's take on this PR.

Analytics::record_usage();

if ( isset( $filter_options['exclude'] ) && isset( $filter_options['include'] ) ) {
return new WP_Error( 'vip-block-data-api-invalid-params', 'Cannot provide blocks to exclude and include at the same time', [ 'status' => 400 ] );
}

$this->post_id = $post_id;
// Temporarily set global $post.
$previous_global_post = $post;
Copy link
Contributor

Choose a reason for hiding this comment

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

Making sure I understand this, why is this needed? Would be worth documenting the reasoning behind this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Improved the comment in 0c1b48a, thanks!

Base automatically changed from add/block-bindings-support to trunk August 30, 2024 20:55
@alecgeatches alecgeatches dismissed ingeniumed’s stale review August 30, 2024 20:55

The base branch was changed.

ingeniumed
ingeniumed previously approved these changes Sep 2, 2024
README.md Outdated
Comment on lines 1324 to 1328
if ( 'core/gallery' !== $block_name ) {
return $inner_blocks;
}

return [];
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is just an example and would work, but it feels backwards to me. How about the inverse ordering:

Suggested change
if ( 'core/gallery' !== $block_name ) {
return $inner_blocks;
}
return [];
if ( 'core/gallery' === $block_name ) {
return [];
}
return $inner_blocks;

@@ -1309,11 +1341,11 @@ Modify or add attributes to a block's output in the Block Data API.
*
* @param array $sourced_block An associative array of parsed block data with keys 'name' and 'attributes'.
* @param string $block_name The name of the parsed block, e.g. 'core/paragraph'.
* @param string $post_id The post ID associated with the parsed block.
* @param string $block The result of parse_blocks() for this block.
* @param int $post_id The post ID associated with the parsed block.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for fixing these!

// inside a dynamic block's render callback function.
//
// https://github.com/WordPress/WordPress/blob/6.6.1/wp-includes/class-wp-block.php#L517
$parent_block = WP_Block_Supports::$block_to_render;
Copy link
Contributor

@alecgeatches alecgeatches Sep 3, 2024

Choose a reason for hiding this comment

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

Can we wrap this in some sort of safety method in case it becomes private or changes names in the future? Like an isset()/property_exists() check. I think we can just bail if we don't see the variable we expect, and we'll see that synced pattern tests are failing.

alecgeatches
alecgeatches previously approved these changes Sep 3, 2024
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.

This works really well! I spent some time reviewing the code and debugging locally to watch how it works.

I left a couple of notes, but because there's nothing major I'll just fix those in this branch and merge. Thank you Chris!

@alecgeatches alecgeatches dismissed stale reviews from ingeniumed and themself via bc8ea11 September 3, 2024 16:45
@alecgeatches alecgeatches merged commit df0b7ab into trunk Sep 3, 2024
3 checks passed
@alecgeatches alecgeatches deleted the add/synced-patterns-support branch September 3, 2024 16:49
@alecgeatches alecgeatches mentioned this pull request Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants