Skip to content
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

Block Hooks: Try to make insertion into Navigation block work on WP 6.4 #58508

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 48 additions & 0 deletions lib/compat/wordpress-6.5/block-hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Block hooks compatibility layer.
*
* @package gutenberg
*/

function respect_ignored_hooked_blocks_post_meta_on_wp_navigation_post( $hooked_block_types, $relative_position, $anchor_block_type, $context ) {

Check failure on line 8 in lib/compat/wordpress-6.5/block-hooks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

The "respect_ignored_hooked_blocks_post_meta_on_wp_navigation_post()" function should be guarded against redeclaration.
// Only apply this filter if $context is a wp_navigation post object.
if ( ! $context instanceof WP_Post || 'wp_navigation' !== $context->post_type ) {
return $hooked_block_types;
}

// Only apply this filter if the anchor block is a Navigation block.
if (
'core/navigation' !== $anchor_block_type || (
'first_child' !== $relative_position &&
'last_child' !== $relative_position
)
) {
return $hooked_block_types;
}

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

if ( ! isset( $ignored_hooked_blocks ) || ! is_array( $ignored_hooked_blocks ) ) {
$ignored_hooked_blocks = array();
}

$hooked_block_types = array_diff( $hooked_block_types, $ignored_hooked_blocks );

// FIXME. The problem is that we don't have any way of telling if this is being invoked
// upon read or write access. As a consequence, we're always writing the post meta, even
// if we're only reading. That means that the ignoredHookedBlocks meta will be set
// when first loading the frontend, and will cause hooked blocks to be omitted when loading
// it for the second time :(
$ignored_hooked_blocks = array_merge( $ignored_hooked_blocks, $hooked_block_types );
update_post_meta( $context->ID, '_wp_ignored_hooked_blocks', json_encode( $ignored_hooked_blocks ) );

return $hooked_block_types;
}

if ( version_compare( get_bloginfo( 'version' ), '6.5', '<' ) ) {
add_filter( 'hooked_block_types', 'respect_ignored_hooked_blocks_post_meta_on_wp_navigation_post', 10, 4 );
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function gutenberg_is_experiment_enabled( $name ) {

// WordPress 6.5 compat.
require __DIR__ . '/compat/wordpress-6.5/blocks.php';
require __DIR__ . '/compat/wordpress-6.5/block-hooks.php';
require __DIR__ . '/compat/wordpress-6.5/block-patterns.php';
require __DIR__ . '/compat/wordpress-6.5/kses.php';
require __DIR__ . '/compat/wordpress-6.5/interactivity-api/class-wp-interactivity-api.php';
Expand Down
16 changes: 8 additions & 8 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static function get_inner_blocks_from_navigation_post( $attributes ) {
// it encounters whitespace. This code strips it.
$blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );

if ( function_exists( 'get_hooked_block_markup' ) ) {
if ( function_exists( 'get_hooked_blocks' ) ) {
// Run Block Hooks algorithm to inject hooked blocks.
$markup = block_core_navigation_insert_hooked_blocks( $blocks, $navigation_post );
$root_nav_block = parse_blocks( $markup )[0];
Expand Down Expand Up @@ -987,7 +987,7 @@ function block_core_navigation_get_fallback_blocks() {
// In this case default to the (Page List) fallback.
$fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;

if ( function_exists( 'get_hooked_block_markup' ) ) {
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 = block_core_navigation_insert_hooked_blocks( $fallback_blocks, $navigation_post );
Expand Down Expand Up @@ -1413,9 +1413,9 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) {
}
}

// 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.
if ( function_exists( 'get_hooked_block_markup' ) ) {
// Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.4
// that are not present in Gutenberg's WP 6.4 compatibility layer.
if ( function_exists( 'get_hooked_blocks' ) ) {
add_action( 'rest_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta', 10, 3 );
}

Expand Down Expand Up @@ -1445,8 +1445,8 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons
return $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.
if ( function_exists( 'get_hooked_block_markup' ) ) {
// Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.4
// that are not present in Gutenberg's WP 6.4 compatibility layer.
if ( function_exists( 'get_hooked_blocks' ) ) {
add_filter( 'rest_prepare_wp_navigation', 'block_core_navigation_insert_hooked_blocks_into_rest_response', 10, 3 );
}
Loading