Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b020b06
Update template utils to accommodate first_child and last_child hooke…
tjcafferkey Jun 20, 2024
6b78fcd
Insert ignoredHookedBlocks postmeta via rest_pre_insert_wp_template_p…
tjcafferkey Jun 24, 2024
1dccc50
Update comment
tjcafferkey Jun 24, 2024
87462d5
Update tests
tjcafferkey Jun 25, 2024
8978b80
Add assertion, change anchor block
ockham Jun 25, 2024
d5f6c30
Update tests
tjcafferkey Jun 26, 2024
0ed6e89
Remove duplciate get_post_meta call
tjcafferkey Jun 26, 2024
83040a6
Build correct context for the block hooks API when the post type is a…
tjcafferkey Jun 26, 2024
5e4876c
Test coverage
tjcafferkey Jun 27, 2024
1e3e362
Ensure that for template files _inject_theme_attribute_in_template_pa…
tjcafferkey Jun 27, 2024
f4062ad
Update comments
tjcafferkey Jun 28, 2024
6c95919
Remove update_ignored_hooked_blocks_postmeta from rest_pre_insert_wp_…
ockham Jul 1, 2024
0ceb66b
Add extract_serialized_parent_block helper
ockham Jul 1, 2024
5489657
Handle template-part first_child/last_child insertion in inject_ignor…
ockham Jul 1, 2024
46c73e0
Revert changes to update_ignored_hooked_blocks_postmeta
ockham Jul 1, 2024
9fa3ee7
Add note about _make_mock_parsed_block being internal use only
ockham Jul 1, 2024
e676f4b
Add test coverage
ockham Jul 1, 2024
560dcce
Revert changes to updateIgnoredHookedBlocksPostMeta test
ockham Jul 1, 2024
58c6689
Clean up post meta
ockham Jul 1, 2024
e7db202
Start fixing tests
ockham Jul 1, 2024
adacaae
Polish test
ockham Jul 1, 2024
b3377b3
Fix other tests
ockham Jul 1, 2024
97d4624
Add ticket numbers
ockham Jul 1, 2024
f324f96
Single quotes
ockham Jul 1, 2024
83cfa72
Polish other tests
ockham Jul 1, 2024
1ddf9d3
Add PHPDoc descriptions to _make_mock_parsed_block
ockham Jul 1, 2024
40464ce
Wrap a line
ockham Jul 1, 2024
5df1860
Revert "Wrap a line"
ockham Jul 1, 2024
1517c44
Rewrite to remove _make_mock_parsed_block
ockham Jul 1, 2024
b679595
Variable names
ockham Jul 1, 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
Revert changes to update_ignored_hooked_blocks_postmeta
  • Loading branch information
ockham committed Jul 2, 2024
commit 46c73e04535df9bf6f505b097ec43561f2002a14
63 changes: 23 additions & 40 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,76 +1084,57 @@ function _make_mock_parsed_block( $block_name, $attrs = array(), $inner_blocks =

/**
* Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
* Currently only supports `wp_navigation` and `wp_template_part` post types.
* Currently only supports `wp_navigation` post types.
*
* @since 6.6.0
* @since 6.7.0 Support `wp_template_part` post type.
* @access private
*
* @param stdClass $post Post object.
* @return stdClass The updated post object.
*/
function update_ignored_hooked_blocks_postmeta( $post ) {
/*
* Skip meta generation when consumers intentionally update specific Navigation fields
* and omit the content update.
* In this scenario the user has likely tried to create a navigation via the REST API.
* In which case we won't have a post ID to work with and store meta against.
*/
if ( ! isset( $post->post_content ) ) {
if ( empty( $post->ID ) ) {
return $post;
}

/*
* We only hook into filters for the `wp_navigation` and `wp_template_part` post types.
* So if the post type is not set we can assume it's a `wp_template_part`.
* Skip meta generation when consumers intentionally update specific Navigation fields
* and omit the content update.
*/
$post_type = isset( $post->post_type ) ? $post->post_type : 'wp_template_part';
$post_type_to_block_name_map = array(
'wp_navigation' => 'core/navigation',
'wp_template_part' => 'core/template-part',
);
if ( ! isset( $post->post_content ) ) {
return $post;
}

/*
* Skip meta generation when the post content is not in the above map.
* Skip meta generation when the post content is not a navigation block.
*/
if ( ! isset( $post_type_to_block_name_map[ $post_type ] ) ) {
if ( ! isset( $post->post_type ) || 'wp_navigation' !== $post->post_type ) {
return $post;
}

$parent_block_name = $post_type_to_block_name_map[ $post_type ];
$attributes = array();
$existing_ignored_hooked_blocks = isset( $post->ID ) ? get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ) : '';
$attributes = array();

if ( ! empty( $existing_ignored_hooked_blocks ) ) {
$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
if ( ! empty( $ignored_hooked_blocks ) ) {
$ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true );
$attributes['metadata'] = array(
'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ),
'ignoredHookedBlocks' => $ignored_hooked_blocks,
);
}

$markup = get_comment_delimited_block_content(
$parent_block_name,
'core/navigation',
$attributes,
$post->post_content
);

/**
* We need to merge incoming changes with existing data so the block hooks API gets the correct context.
* For `wp_template_part` post type, we need to convert the post object to a block template object.
*/
$existing_post = isset( $post->ID ) ? get_post( $post->ID ) : array();
$context = (object) array_merge( (array) $existing_post, (array) $post );
// Given a post object ($context), get_post will return a WP_Post object.
$context = get_post( $context );
if ( 'wp_template_part' === $post_type ) {
// Convert the $context WP_Post object to a WP_Block_Template object.
if ( isset( $post->ID ) ) {
$context = _build_block_template_result_from_post( $context );
} else {
$meta = isset( $context->meta_input ) ? $context->meta_input : array();
$terms = isset( $context->tax_input ) ? $context->tax_input : array();
$context = _build_block_template_object_from_post_object( get_post( $context ), $terms, $meta );
}
}

$existing_post = get_post( $post->ID );
// Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.
$context = (object) array_merge( (array) $existing_post, (array) $post );
$serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
$root_block = parse_blocks( $serialized_block )[0];

Expand All @@ -1162,8 +1143,10 @@ function update_ignored_hooked_blocks_postmeta( $post ) {
: array();

if ( ! empty( $ignored_hooked_blocks ) ) {
$existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
if ( ! empty( $existing_ignored_hooked_blocks ) ) {
$ignored_hooked_blocks = array_unique( array_merge( $ignored_hooked_blocks, json_decode( $existing_ignored_hooked_blocks, true ) ) );
$existing_ignored_hooked_blocks = json_decode( $existing_ignored_hooked_blocks, true );
$ignored_hooked_blocks = array_unique( array_merge( $ignored_hooked_blocks, $existing_ignored_hooked_blocks ) );
}

if ( ! isset( $post->meta_input ) ) {
Expand Down