Skip to content

Conversation

@ockham
Copy link
Contributor

@ockham ockham commented May 30, 2024

In a similar vein as @tjcafferkey's #6604, this introduces a new insert_hooked_blocks_into_rest_response filter and adds it to the rest_prepare_wp_navigation hook.

This is part of an effort to move code from the Navigation block into Core. Specifically, insert_hooked_blocks_into_rest_response is based on block_core_navigation_insert_hooked_blocks_into_rest_response.

Testing instructions:

Preparation: Make sure you're using a Block Theme to test, ideally TT4. Add the following code to your site (e.g. to your theme's functions.php).

function register_logout_block_as_navigation_last_child( $hooked_blocks, $position, $anchor_block, $context ) {
	if ( $anchor_block === 'core/navigation' && $position === 'last_child' ) {
		$hooked_blocks[] = 'core/loginout';
	}

	return $hooked_blocks;
}

add_filter( 'hooked_block_types', 'register_logout_block_as_navigation_last_child', 10, 4 );
  1. If there's an existing wp_navigation post object for you Navigation menu in your database, make sure that it doesn't have any _wp_ignored_hooked_blocks post meta set. (You can check the latter via npm run wp-env run cli wp post meta list <postId>.) Otherwise, you'll need to delete said post meta.
  2. Go to the site editor and verify that the Login/out button block is added to the Navigation menu. You will probably see two copies. The reason is that the Navigation block code still keeps inserting hooked blocks on its own.
  3. To avoid the latter, apply the following patch, and reload the editor. You should now see only one copy of the Login/out block added.
diff --git a/src/wp-includes/blocks/navigation.php b/src/wp-includes/blocks/navigation.php
index ba1644cf60..bbc924328b 100644
--- a/src/wp-includes/blocks/navigation.php
+++ b/src/wp-includes/blocks/navigation.php
@@ -1567,9 +1567,15 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons
 $rest_prepare_wp_navigation_core_callback = 'block_core_navigation_' . 'insert_hooked_blocks_into_rest_response';
 
 /*
- * Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.5
- * that are not present in Gutenberg's WP 6.5 compatibility layer.
+ * Do not add the `block_core_navigation_insert_hooked_blocks_into_rest_response` filter in the following cases:
+ * - If Core has added the `insert_hooked_blocks_into_rest_response` filter already (WP >= 6.6);
+ * - or if the `set_ignored_hooked_blocks_metadata` function is unavailable (which is required for the filter to work. It was introduced by WP 6.5 but is not present in Gutenberg's WP 6.5 compatibility layer);
+ * - or if the `$rest_prepare_wp_navigation_core_callback` filter has already been added.
  */
-if ( function_exists( 'set_ignored_hooked_blocks_metadata' ) && ! has_filter( 'rest_prepare_wp_navigation', $rest_prepare_wp_navigation_core_callback ) ) {
+if (
+       ! has_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response' ) &&
+       function_exists( 'set_ignored_hooked_blocks_metadata' ) &&
+       ! has_filter( 'rest_prepare_wp_navigation', $rest_prepare_wp_navigation_core_callback )
+) {
        add_filter( 'rest_prepare_wp_navigation', 'block_core_navigation_insert_hooked_blocks_into_rest_response', 10, 3 );
 }

Here's a Gutenberg PR for that diff: WordPress/gutenberg#62134. That PR will have to get merged and sync'd to Core before we can land this change.

Trac ticket: https://core.trac.wordpress.org/ticket/60759


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ockham ockham force-pushed the add/insert-hooked-blocks-into-rest-response branch from b4c13c2 to b1855b0 Compare June 3, 2024 12:07
@ockham ockham marked this pull request as ready for review June 3, 2024 12:08
@github-actions
Copy link

github-actions bot commented Jun 3, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props bernhard-reiter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

github-actions bot commented Jun 3, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ockham
Copy link
Contributor Author

ockham commented Jun 3, 2024

Committed to Core in https://core.trac.wordpress.org/changeset/58292.

@ockham ockham closed this Jun 3, 2024
@ockham ockham deleted the add/insert-hooked-blocks-into-rest-response branch June 3, 2024 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant