Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b5aaa44
Don't set ignoredHookedBlocks metadata upon read
ockham Feb 12, 2024
2e28810
Don't pass anchor_block by reference
ockham Feb 12, 2024
40fa3fb
Update PHPDoc
ockham Feb 12, 2024
1f0d087
Add new set_ignored_hooked_blocks_metadata() function
ockham Feb 12, 2024
c434d75
Whitespace
ockham Feb 12, 2024
b63d367
Fix first batch of unit tests
ockham Feb 12, 2024
36c0fc3
Fix remaining unit tests
ockham Feb 12, 2024
2358241
There can never be enough callbacks
ockham Feb 12, 2024
0330473
Tweak set_ignored_hooked_blocks_metadata to match callback signature
ockham Feb 12, 2024
e6eea6e
Wire it all up
ockham Feb 12, 2024
eb1daa1
Whitespace
ockham Feb 13, 2024
d057ece
Bail early
ockham Feb 13, 2024
ab6a8c9
Actually update post
ockham Feb 13, 2024
998147f
Use correct action
ockham Feb 13, 2024
1296344
Add action for template parts
ockham Feb 13, 2024
27724fc
Add note on action vs filter
ockham Feb 13, 2024
012004b
Clarify comment
ockham Feb 13, 2024
27a2ec8
Fix more unit tests
ockham Feb 13, 2024
12ccd0d
Start adding test coverage for set_ignored_hooked_blocks_metadata
ockham Feb 13, 2024
1128b5f
Add more test coverage and a small fix
ockham Feb 13, 2024
5817b49
Add test coverage for hooked block added by filter
ockham Feb 13, 2024
60b2732
Add test to cover context-aware filter
ockham Feb 13, 2024
61847cb
Remove obsolete comment about post_filtered_content
ockham Feb 13, 2024
712260f
Move set_ignored_hooked_blocks_metadata function definition below ins…
ockham Feb 13, 2024
7c7f01e
Inline get_hooked_block_markup
ockham Feb 13, 2024
f4f856b
Coding Standards in test
ockham Feb 13, 2024
7d43f21
Add test coverage to verify hooked blocks aren't added if ignored
ockham Feb 13, 2024
3c9aa4b
Move set_ignored_hooked_blocks_metadata_upon_rest_insert to block-tem…
ockham Feb 13, 2024
51832fe
Rename to inject_ignored_hooked_blocks_metadata_attributes
ockham Feb 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bail early
  • Loading branch information
ockham committed Feb 13, 2024
commit d057ece8ce7f05a41d32802b862cd1ef4e6b6df7
31 changes: 16 additions & 15 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,22 +754,23 @@

// TODO: This should probably go somewhere else.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this function might be best placed in block-template-utils.php

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good call!

As an alternative, maybe it's worth putting directly into WP_REST_Templates_Controller after all? I was hesitant to have it directly inside a controller when I thought that templates were handled by WP_REST_Posts_Controller (as it seemed too specific to the wp_template and wp_template_part post types there), but realizing that we have a specialized controller, it might not feel so out of place there... 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is actually a better place IMO!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the function into block-template-utils.php in dfb6590 (but still using it as an action hooked into rest_after_insert_wp_template and rest_after_insert_wp_template_part, respectively).

Considering leaving as-is for now, as it's not as straight-forward to find the right spot inside the controller for running this logic. (Both create_item and update_item methods call prepare_item_for_database which does a lot of the heavy lifting, but the way it works is that it prepares a $changes object which is just a stdClass. There are some stages --- especially when first creating the template from a theme file for subsequent insertion into the DB -- where some fields are in an in-between state (e.g. still claiming to be a file-based template when we're already about to insert into the DB). This might give some misleading $context information to the filter).

function set_ignored_hooked_blocks_metadata_upon_rest_insert( $post ) {
$before_block_visitor = null;
$after_block_visitor = null;
$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
// At this point, the post has already been created.
// We need to build the corresponding `WP_Block_Template` object.
// To that end, we need to prevent hooked blocks insertion from running again.

// Suppress all hooked blocks getting inserted into the context.
add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 );
$template = _build_block_template_result_from_post( $post );
remove_filter( 'hooked_block_types', '__return_empty_array', 99999 );

$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );
$hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return;
}

// At this point, the post has already been created.
// We need to build the corresponding `WP_Block_Template` object.
// To that end, we need to prevent hooked blocks insertion from running again.

// Suppress all hooked blocks getting inserted into the context.
add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 );
$template = _build_block_template_result_from_post( $post );
remove_filter( 'hooked_block_types', '__return_empty_array', 99999 );

$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );

$blocks = parse_blocks( $template->content );
$post->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
}
Expand Down