[wp-trac] [WordPress Trac] #60754: Block Hooks: Incorrect context passed when setting ignored hooked blocks metadata

WordPress Trac noreply at wordpress.org
Tue Mar 12 11:17:36 UTC 2024


#60754: Block Hooks: Incorrect context passed when setting ignored hooked blocks
metadata
-----------------------------+-----------------------------
 Reporter:  Bernhard Reiter  |      Owner:  (none)
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  General          |    Version:  trunk
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 Since [57790], the `rest_pre_insert_wp_template` and
 `rest_pre_insert_wp_template_part` hooks in the Templates Controller are
 used to call `inject_ignored_hooked_blocks_metadata_attributes()`, which
 in turn sets the `ignoredHookedBlocks` metadata attribute on anchor blocks
 upon writing to the DB.

 The latter function applies the `hooked_block_types`, `hooked_block`, and
 `hooked_block_{$hooked_block_type}` filters, in order to inform which
 blocks should be hooked to a given anchor block. Each of these filters is
 given a `$context` argument, which in the case of templates (and template
 parts, respectively) is the containing `WP_Block_Template` object.

 Since the `rest_pre_insert_`-prefixed filters only pass a `stdClass`
 object for the relevant `$changes` that need to be made to the relevant
 template (or template part), `$context` is constructed by fetching the
 existing template (or part) via `get_block_template()` (using the
 template/part ID present in the network `$request`).

 This isn't quite how things should be: `hooked_block_types`,
 `hooked_block`, and `hooked_block_{$hooked_block_type}` should be given a
 `$context` object that reflects the template (part) that is about to be
 persisted to the database, ''not'' the one that's currently stored there.
 A filter might e.g. look for the presence of a certain block in the
 template's content; that block might not have been present in the previous
 version of a template (stored in the DB) but might've been added by the
 user in the editor (and will be part of the payload that's used to update
 the DB).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60754>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list