-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Navigation block to render hooked inner blocks #57754
Merged
ockham
merged 4 commits into
WordPress:trunk
from
tjcafferkey:experimental/update-wp-navigation-block-renderer-with-block-hooks
Jan 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
// Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.4 | ||
// that have not yet been included in Gutenberg's compatibility layer (lib/compat/wordpress-6.4/block-hooks.php). | ||
if ( function_exists( 'get_hooked_blocks' ) ) { | ||
add_filter( 'rest_prepare_wp_navigation', 'gutenberg_hook_blocks_into_core_navigation_rest_response', 10, 3 ); | ||
add_action( 'rest_insert_wp_navigation', 'gutenberg_update_ignore_hooked_blocks_meta_core_navigation', 10, 3 ); | ||
} | ||
|
||
/** | ||
* Updates the post meta with the list of ignored hooked blocks when the navigation is created or updated via the REST API. | ||
* | ||
* @param WP_Post $post Post object. | ||
*/ | ||
function gutenberg_update_ignore_hooked_blocks_meta_core_navigation( $post ) { | ||
if ( ! isset( $post->ID ) ) { | ||
return; | ||
} | ||
|
||
// We run the Block Hooks mechanism so it will return the list of ignored hooked blocks | ||
// in the mock root Navigation block's metadata attribute. | ||
// We ignore the rest of the returned `$markup`; `$post->post_content` already has the hooked | ||
// blocks inserted, whereas `$markup` will have them inserted twice. | ||
$blocks = parse_blocks( $post->post_content ); | ||
$markup = gutenberg_insert_hooked_blocks_into_navigation_block( $blocks, $post ); | ||
$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 ) ); | ||
} | ||
} | ||
|
||
/** | ||
* Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks. | ||
* | ||
* @param WP_REST_Response $response The response object. | ||
* @param WP_Post $post Post object. | ||
* @param WP_REST_Request $request Request object. | ||
* @return WP_REST_Response The response object. | ||
*/ | ||
function gutenberg_hook_blocks_into_core_navigation_rest_response( $response, $post ) { | ||
if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) { | ||
return $response; | ||
} | ||
$parsed_blocks = parse_blocks( $response->data['content']['raw'] ); | ||
$content = gutenberg_insert_hooked_blocks_into_navigation_block( $parsed_blocks, $post ); | ||
|
||
// Remove mock Navigation block wrapper. | ||
$start = strpos( $content, '-->' ) + strlen( '-->' ); | ||
$end = strrpos( $content, '<!--' ); | ||
$content = substr( $content, $start, $end - $start ); | ||
|
||
$response->data['content']['raw'] = $content; | ||
$response->data['content']['rendered'] = apply_filters( 'the_content', $content ); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Insert hooked blocks into a Navigation block. | ||
* | ||
* Given a Navigation block's inner blocks and its corresponding `wp_navigation` post object, | ||
* this function inserts hooked blocks into it, and returns the serialized inner blocks in a | ||
* mock Navigation block wrapper. | ||
* | ||
* If there are any hooked blocks that need to be inserted as the Navigation block's first or last | ||
* children, the `wp_navigation` post's `_wp_ignored_hooked_blocks` meta is checked to see if any | ||
* of those hooked blocks should be exempted from insertion. | ||
* | ||
* @param array $inner_blocks Parsed inner blocks of a Navigation block. | ||
* @param WP_Post $post `wp_navigation` post object corresponding to the block. | ||
* @return string Serialized inner blocks in mock Navigation block wrapper, with hooked blocks inserted, if any. | ||
*/ | ||
function gutenberg_insert_hooked_blocks_into_navigation_block( $inner_blocks, $post = null ) { | ||
$before_block_visitor = null; | ||
$after_block_visitor = null; | ||
$hooked_blocks = get_hooked_blocks(); | ||
$attributes = array(); | ||
|
||
if ( isset( $post->ID ) ) { | ||
$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' => $ignored_hooked_blocks, | ||
); | ||
} | ||
} | ||
|
||
$mock_anchor_parent_block = array( | ||
'blockName' => 'core/navigation', | ||
'attrs' => $attributes, | ||
'innerBlocks' => $inner_blocks, | ||
'innerContent' => array_fill( 0, count( $inner_blocks ), null ), | ||
); | ||
$before_block_visitor = null; | ||
$after_block_visitor = null; | ||
|
||
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { | ||
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $post ); | ||
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $post ); | ||
} | ||
|
||
return traverse_and_serialize_block( $mock_anchor_parent_block, $before_block_visitor, $after_block_visitor ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,11 +190,19 @@ private static function get_inner_blocks_from_navigation_post( $attributes ) { | |
|
||
// 'parse_blocks' includes a null block with '\n\n' as the content when | ||
// it encounters whitespace. This code strips it. | ||
$compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); | ||
$blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); | ||
|
||
if ( function_exists( 'get_hooked_blocks' ) ) { | ||
// Run Block Hooks algorithm to inject hooked blocks. | ||
$markup = gutenberg_insert_hooked_blocks_into_navigation_block( $blocks, $navigation_post ); | ||
$root_nav_block = parse_blocks( $markup )[0]; | ||
|
||
$blocks = isset( $root_nav_block['innerBlocks'] ) ? $root_nav_block['innerBlocks'] : $blocks; | ||
} | ||
|
||
// TODO - this uses the full navigation block attributes for the | ||
// context which could be refined. | ||
return new WP_Block_List( $compacted_blocks, $attributes ); | ||
return new WP_Block_List( $blocks, $attributes ); | ||
} | ||
} | ||
|
||
|
@@ -983,6 +991,17 @@ function block_core_navigation_get_fallback_blocks() { | |
// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks. | ||
// In this case default to the (Page List) fallback. | ||
$fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks; | ||
|
||
if ( function_exists( 'get_hooked_blocks' ) ) { | ||
// Run Block Hooks algorithm to inject hooked blocks. | ||
// We have to run it here because we need the post ID of the Navigation block to track ignored hooked blocks. | ||
$markup = gutenberg_insert_hooked_blocks_into_navigation_block( $fallback_blocks, $navigation_post ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
$blocks = parse_blocks( $markup ); | ||
|
||
if ( isset( $blocks[0]['innerBlocks'] ) ) { | ||
$fallback_blocks = $blocks[0]['innerBlocks']; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -994,7 +1013,7 @@ function block_core_navigation_get_fallback_blocks() { | |
* | ||
* @since 5.9.0 | ||
* | ||
* @param array[] default fallback blocks provided by the default block mechanic. | ||
* @param array[] $fallback_blocks default fallback blocks provided by the default block mechanic. | ||
*/ | ||
return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks ); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to change this function name: Dynamic blocks PHP -- i.e. pretty much any
packages/block-library/src/*/*.php
files -- cannot contain anygutenberg_
prefixed functions, as they are synced verbatim from GB to Core via npm package sync.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2211001