WordPress Version: 6.5
/**
* Updates the post meta with the list of ignored hooked blocks when the navigation is created or updated via the REST API.
*
* @access private
* @since 6.5.0
*
* @param stdClass $post Post object.
* @return stdClass The updated post object.
*/
function block_core_navigation_update_ignore_hooked_blocks_meta($post)
{
/*
* 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 (empty($post->ID)) {
return $post;
}
/**
* Skip meta generation when consumers intentionally update specific Navigation fields
* and omit the content update.
*/
if (!isset($post->post_content)) {
return $post;
}
/*
* We run the Block Hooks mechanism to inject the `metadata.ignoredHookedBlocks` attribute into
* all anchor blocks. For the root level, we create a mock Navigation and extract them from there.
*/
$blocks = parse_blocks($post->post_content);
/*
* Block Hooks logic requires a `WP_Post` object (rather than the `stdClass` with the updates that
* we're getting from the `rest_pre_insert_wp_navigation` filter) as its second argument (to be
* used as context for hooked blocks insertion).
* We thus have to look it up from the DB,based on `$post->ID`.
*/
$markup = block_core_navigation_set_ignored_hooked_blocks_metadata($blocks, get_post($post->ID));
$root_nav_block = parse_blocks($markup)[0];
$ignored_hooked_blocks = isset($root_nav_block['attrs']['metadata']['ignoredHookedBlocks']) ? $root_nav_block['attrs']['metadata']['ignoredHookedBlocks'] : 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)) {
$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));
}
update_post_meta($post->ID, '_wp_ignored_hooked_blocks', json_encode($ignored_hooked_blocks));
}
$post->post_content = block_core_navigation_remove_serialized_parent_block($markup);
return $post;
}