Skip to content

Block Hooks: Allow returning null from hooked_block filter - #6143

Closed
ockham wants to merge 6 commits into
WordPress:trunkfrom
ockham:update/hooked-block-filter-allow-returning-null
Closed

ockham wants to merge 6 commits into
WordPress:trunkfrom
ockham:update/hooked-block-filter-allow-returning-null

Conversation

@ockham

@ockham ockham commented Feb 20, 2024

Copy link
Copy Markdown
Contributor

The hooked_block_types filter allows extenders to conditionally insert a block, based on the anchor block's ''type''.

However, it's currently not possible to base the criterion for insertion on the value of an attribute of the anchor block.

One fairly straight-forward way to implement this would be to allow the hooked_block (and hooked_block_{$hooked_block_type}) filter -- which has access to the entire parsed anchor block, i.e. including attributes -- to return null, which Block Hooks could then interpret as "do not render hooked block". (This would avoid changing the hooked_block_types filter's signature in an awkward way.)

Trac ticket: https://core.trac.wordpress.org/ticket/60580


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.

@ockham ockham self-assigned this Feb 20, 2024
@swissspidy

Copy link
Copy Markdown
Member

This would avoid changing the hooked_block_types filter's signature in an awkward way

You still need to change the signature to indicate the value can be null or an array

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ockham

ockham commented Feb 20, 2024

Copy link
Copy Markdown
Contributor Author

This would avoid changing the hooked_block_types filter's signature in an awkward way

You still need to change the signature to indicate the value can be null or an array

What I meant is I don't need to add a $parsed_anchor_block arg to hooked_block_types (when it already has $anchor_block_type).

It's true that I need to allow for hooked_block and hooked_block_{$hooked_block_type} to return null, but that's less of a problem -- they've only been introduced during the 6.5 cycle (unlike hooked_block_types, which has been around since 6.4).

@ockham
ockham requested a review from gziolo February 20, 2024 12:21
@ockham
ockham marked this pull request as ready for review February 20, 2024 12:23
@github-actions

github-actions Bot commented Feb 20, 2024 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props bernhard-reiter, swissspidy, tomjcafferkey.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ockham

ockham commented Feb 20, 2024

Copy link
Copy Markdown
Contributor Author

FYI @tjcafferkey -- if you'd like to give this a review 😄

@swissspidy

Copy link
Copy Markdown
Member
@param array|null $parsed_hooked_block The parsed block array for the given hooked block type or null to remove the block.

@ockham

ockham commented Feb 20, 2024

Copy link
Copy Markdown
Contributor Author
@param array|null $parsed_hooked_block The parsed block array for the given hooked block type or null to remove the block.

@swissspidy Thank you, good point! Added in 5566464. (I opted to write "to suppress the block" rather than "remove", as the latter would sound a bit like the block has already been inserted and is being removed. LMK if that's okay, or if you'd prefer "remove" after all.)

@ockham
ockham force-pushed the update/hooked-block-filter-allow-returning-null branch from 5566464 to 9df509d Compare February 20, 2024 13:25
@ockham

ockham commented Feb 20, 2024

Copy link
Copy Markdown
Contributor Author

Rebased.

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
@tjcafferkey

Copy link
Copy Markdown

FYI @tjcafferkey -- if you'd like to give this a review 😄

I'm going to try and review this today if required 😄

@swissspidy

Copy link
Copy Markdown
Member

@tjcafferkey If you'd like to, the sooner the better - beta 2 release party kicks off in around 2hrs.

Comment thread src/wp-includes/blocks.php Resolved

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code changes look good to me, but I would appreciate sanity check from @tjcafferkey.

@tjcafferkey tjcafferkey left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I've reviewed the code and had a quick catch up with @ockham and from my POV these LGTM. Thanks @ockham!

@ockham

ockham commented Feb 20, 2024

Copy link
Copy Markdown
Contributor Author

Committed to Core in https://core.trac.wordpress.org/changeset/57668.

@ockham ockham closed this Feb 20, 2024
@ockham
ockham deleted the update/hooked-block-filter-allow-returning-null branch February 20, 2024 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

4 participants