Make WordPress Core

Opened 2 months ago

Last modified 4 weeks ago

#60769 new enhancement

Block Hooks: Consolidate approach to get the list of hooked blocks.

Reported by: tomjcafferkey's profile tomjcafferkey Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

Our current approach to getting hooked blocks has two parts to it:

  1. We use get_hooked_blocks() to get all of the hooked blocks which are hooked from the block.json file.
  2. In function insert_hooked_blocks we apply the filter callbacks (passing a derived value from step 1 as the default value) to get hooked blocks that are applied by the hooked_block_types filter.

Currently it's a bit confusing and without knowledge you'd assume get_hooked_blocks() would retrieve all of the hooked blocks. Additionally it feels like you have to run a few different pieces of code separately and combine their resulting values to get a complete picture which could result in some bugs or unnecessary complexities.

I feel there's room for improvement to provide a better API for getting all of the hooked blocks by consolidating these two approaches into a single function somehow.

How this to work we would also need to know the current context so we can pass that data to the filter callbacks.

Change History (1)

#1 @Bernhard Reiter
4 weeks ago

Agree, this is an architectural flaw/oversight on my part.

How this to work we would also need to know the current context so we can pass that data to the filter callbacks.

I think we'll have to add not only $context but also $anchor_block_type and $relative_position as arguments to get_hooked_blocks(), since we need all of them to apply the filters (see `hooked_block_types` and `hooked_block`.

We'd likely want the arguments in a different order though, probably get_hooked_blocks( $anchor_block_type, $relative_position, $context ). We'd then apply the filters inside of the function if those arguments are non-null.

This means that the return value should also look a bit different, since we no longer need to return a nested array (by anchor block and relative position) like we do now.

Makes me wonder if we should introduce a separate helper function for this (since it seems like it would be different enough from get_hooked_blocks()). get_hooked_blocks_for_block_hook() or so maybe. (If we assume that a block hook is defined by an anchor block and a relative position.)

Note: See TracTickets for help on using tickets.