Make WordPress Core

Opened 12 months ago

Closed 11 months ago

Last modified 11 months ago

#59669 closed defect (bug) (fixed)

Block: Give `traverse_and_serialize_block(s)`' post-callback access to block instance

Reported by: bernhard-reiter's profile Bernhard Reiter Owned by: bernhard-reiter's profile Bernhard Reiter
Milestone: 6.4 Priority: normal
Severity: normal Version: 6.4
Component: General Keywords: has-patch has-unit-tests commit dev-reviewed
Focuses: Cc:

Description (last modified by Bernhard Reiter)

Both the $pre_ and $post_callback functions that are given as arguments to traverse_and_serialize_block(s) receive a reference to the current block as their first argument. However, while any changes that the "pre" callback makes to the block are actually respected during serialization, the same isn't true for the "post" callback: Any changes that it makes are only applied after the block has already been serialized. For applications like #59646, we probably want to change that.

Change History (12)

This ticket was mentioned in PR #5525 on WordPress/wordpress-develop by @Bernhard Reiter.


12 months ago
#1

  • Keywords has-patch has-unit-tests added

Both the $pre_ and $post_callback functions that are given as arguments to traverse_and_serialize_block(s) receive a reference to the current block as its first argument. However, while any changes that the "pre" callback makes to the block are actually respected during serialization, the same isn't true for the "post" callback: Any changes that it makes are only applied after the block has already been serialized. For applications like #59412, we probably want to change that.

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

#2 @Bernhard Reiter
12 months ago

  • Description modified (diff)

#3 @Bernhard Reiter
12 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 56970:

Blocks: During traversal, allow post callback to modify block.

Both the $pre_callback and $post_callback functions that are given as arguments to traverse_and_serialize_block(s) receive a reference to the current block as their first argument. However, while any changes that the "pre" callback makes to the block are reflected by the serialized markup, the same wasn't true for the "post" callback: Any changes that it made were only applied after the block had already been serialized.

This commit changes the behavior such that $post_callback's changes to the current block are also reflected in the serialized markup.

See #59646.
Props gziolo.
Fixes #59669.

#5 @Bernhard Reiter
11 months ago

  • Keywords dev-feedback added
  • Milestone changed from 6.5 to 6.4
  • Resolution fixed deleted
  • Status changed from closed to reopened

Re-opening for potential inclusion in 6.4. Requesting another committer's feedback/approval :)

I forgot to backport this to the 6.4 branch after committing to trunk. It might not be a "huge" bug (doesn't seem to affect Block Hooks in Core), but it might pose problems for third parties that want to use traverse_and_serialize_block(s) with a $post_callback that modifies the current block (as that won't work, without the fix). One use case could be the Gutenberg plugin, if we want to e.g. implement something like #59646 there first.

FWIW, traverse_and_serialize_block(s) was only introduced during the 6.4 cycle, so I believe the fix qualifies for backporting during RC.

This ticket was mentioned in Slack in #core by bernhardreiter. View the logs.


11 months ago

#7 @Bernhard Reiter
11 months ago

  • Type changed from enhancement to defect (bug)

Looks like the Type was set to enhancement. I've now changed that to defect (bug) because it actually is one :)

#8 @hellofromTonya
11 months ago

  • Keywords commit dev-reviewed added; dev-feedback removed
  • Version set to trunk

it might pose problems for third parties that want to use traverse_and_serialize_block(s) with a $post_callback that modifies the current block (as that won't work, without the fix).

Good catch @bernhard-reiter flagging this fix.

As the bug (and functions) was(were) introduced in 6.4, [56970] LGTM for backport to the 6.4 branch.

#9 @Bernhard Reiter
11 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 57043:

Blocks: During traversal, allow post callback to modify block.

Both the $pre_callback and $post_callback functions that are given as arguments to traverse_and_serialize_block(s) receive a reference to the current block as their first argument. However, while any changes that the "pre" callback makes to the block are reflected by the serialized markup, the same wasn't true for the "post" callback: Any changes that it made were only applied after the block had already been serialized.

This commit changes the behavior such that $post_callback's changes to the current block are also reflected in the serialized markup.

Reviewed by hellofromTonya.
Merges [56970] to the 6.4 branch.

See #59646.
Props gziolo.
Fixes #59669.

#11 @azaozz
11 months ago

Just curious, why callbacks (as function params) and not filters? The (basic) difference between callbacks as params and filters is that with filters many plugins can apply callbacks at the same time, i.e. work in sync. With separate callbacks all plugins will have to rerun the same functions again and again.

Looking ast gthe code here, these callbacks seem like an anti-pattern in WP?

Version 0, edited 11 months ago by azaozz (next)

#12 @hellofromTonya
11 months ago

Looping back here. Today, there was a discussion in Make/Core slack about @azaozz (and my) concerns.

tl;dr

  • The new functionality and functions are low-level and intended for internal Core-only usage.
  • Architectural changes will be discussed in a separate ticket for a future release, as it's too late in the 6.4 cycle for new filters or architectural redesign to encapsulate this internal functionality.
  • #59783 is now open to better document the intent of Core-only.
Note: See TracTickets for help on using tickets.