[wp-trac] [WordPress Trac] #60587: Block hooks: Allow filtering of block data on traversal
WordPress Trac
noreply at wordpress.org
Tue Feb 20 23:32:27 UTC 2024
#60587: Block hooks: Allow filtering of block data on traversal
-------------------------+-----------------------------
Reporter: joshuaflow | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Editor | Version:
Severity: normal | Keywords:
Focuses: |
-------------------------+-----------------------------
When parsing blocks, it would be useful to be able to update block
attributes, add metadata, or even potentially update a block name in a
template for use in the client.
In the WooCommerce product editor, block templates are used "statically"
in that the product form template is provided to the merchant without the
ability to add/remove/reorder blocks. If static templates are a valid use
case for blocks, I believe many static template blocks will need to be
editable to allow third parties some level of configuration. In this
case, we need to allow adding of metadata to blocks to control their
visibility, but this can also be useful for updating the titles of blocks
or adding block bindings to the templates.
{{{#!php
<?php
$block_to_modify = array(
'blockName' => 'woocommerce/product-tab',
'attrs' => array(
'id' => 'pricing',
'title' => 'Pricing', // Extensions may want to update this
property.
'metadata' => array(), // Need the ability to add product type
association, block bindings, and block hiding conditions here.
),
);
}}}
Templates and template parts that are parsed and retrieved via the REST
API pass through the
[traverse_and_serialize_blocks](https://github.com/WordPress/wordpress-
develop/blob/bd78247617da4e23c02876423f400030eb0c75a3/src/wp-
includes/blocks.php#L1356) function. Adding a hook to the top of this
function would allow flexibility in not only static templates, but also
traditional persisted templates where an extension wants to replace block
types with newly added blocks.
{{{#!php
<?php
$block = apply_filters( 'pre_traverse_and_serialize_block', $block );
}}}
Note that this is already possible by hooking into `get_block_templates`
and re-traversing the tree but is less convenient and means poorer
performance since all templates will have to be traversed at least twice.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/60587>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list