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

Block Hooks: Add new hooked_block filter #6136

Closed
wants to merge 4 commits into from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Feb 19, 2024

In [57354] (#5835), a new hooked_block_{$block_type} filter was introduced that allowed extenders to specify attributes and inner blocks for hooked blocks.

@swissspidy pointed out that for filters with a dynamic name portion like this, it is customary to have a "non-dynamic" counterpart filter (i.e. hooked_block, in this case).

There are a few concerns around the implications for the function signatures for each filter. We can discuss them here (or on a PR that implements this feature) and decide how to proceed.

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


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 19, 2024
@ockham
Copy link
Contributor Author

ockham commented Feb 19, 2024

To quote my own concerns:

So the signature for the non-dynamic version would have to be something like

apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context )

which then raises the question if we also need to adjust the dynamic version to include the type (for consistency)?

apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context )

Note that this duplicates $hooked_block_type (which is already present in the filter name) in a somewhat ugly way; plus given the way that filters work, that arg ends up in the position after $parsed_hooked_block, which is also a bit awkward.

Copy link

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.

@swissspidy
Copy link
Member

Note that this duplicates $hooked_block_type (which is already present in the filter name)

"Duplicating" the variable again is the whole point of it :)

Just left this as a comment on the ticket as well, but about moving $hooked_block_type to the end of the arguments list? Would that be less awkward?

@ockham
Copy link
Contributor Author

ockham commented Feb 19, 2024

Just left this as a comment on the ticket as well, but about moving $hooked_block_type to the end of the arguments list? Would that be less awkward?

Given the choice, I think I'd swallow the pill and would put it in the second position (i.e. after $parsed_hooked_block, as per the current diff).

The chief reason being that substituting for parameters, it's at least somewhat natural to read:

..., $hooked_block_type, $relative_position, $parsed_anchor_block, ...

becomes e.g.

..., 'ockham/like-button`, `after`, `core/post-content`,...

which does exactly what it says: It inserts a Like Button block after the Post Content block.

(Furthermore, we have a convention of sorts that $context is the last argument in Block Hooks related functions and filters, e.g. in the hooked_block_types filter (introduced in 6.4).)

@ockham ockham marked this pull request as ready for review February 19, 2024 16:24
Copy link

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.

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

@swissspidy
Copy link
Member

Committed in https://core.trac.wordpress.org/changeset/57660

@swissspidy swissspidy closed this Feb 20, 2024
@ockham
Copy link
Contributor Author

ockham commented Feb 20, 2024

Thank you for landing this @swissspidy!

@ockham ockham deleted the add/hooked-block-filter branch February 20, 2024 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants