bobbingwide / fizzie

Fizzie theme - a Full Site Editing theme using Gutenberg blocks
9 stars 1 forks source link

Navigation menus not appearing in front-end with WordPress 5.6-RC1 #42

Closed bobbingwide closed 3 years ago

bobbingwide commented 3 years ago

Originally raised as #38, I'm raising this as a new issue since I think it's better to track it down now if it's a core problem rather than a Gutenberg problem. It would be easier if it's a Gutenberg problem because I'll be able to fix it.

Problem is that the navigation menus don't appear at all.

bobbingwide commented 3 years ago

OK, this is a problem with wp-includes\class-wp-block.php. The code has changed between Gutenberg 9.2.0 and Gutenberg 9.4.0 WordPress loads the 9.2 version, so Gutenberg doesn't load it. WordPress 5.6-RC1 populates $block->parsed_block['innerBlocks'] Gutenberg 9.4 expects the data in $block->inner_blocks. Nothing's there so the block returns '';

It looks like Gutenberg has to implement some additional shimming.

bobbingwide commented 3 years ago

I wrote a message on #core-editor Slack

Morning. Has anyone raised an issue regarding incompatibility of Gutenberg 9.4.x with WordPress 5.6-RC1 with regards to inner blocks. Gutenberg 9.4 expects $block->inner_blocks to be set. WordPress's version of the WP_Block class is passing $block->parsed_block['innerBlocks']. Blocks such as Navigation return '' when inner_blocks isn't set. ie My FSE navigation menus don't work anymore. It looks like some extra shimming is needed.

bobbingwide commented 3 years ago

Temporary fix

I wrote a function to populate $block->inner_blocks from $block->parsed_block['innerBlocks']. It's basically the the same code as in WP_blocks::get_inner_blocks()

function fizzie_shim_inner_blocks( $block ) {

    bw_trace( $block );

    $child_context = $block->available_context;

    if ( ! empty( $block->block_type->provides_context ) ) {
        foreach ( $block->block_type->provides_context as $context_name => $attribute_name ) {
            if ( array_key_exists( $attribute_name, $block->attributes ) ) {
                $child_context[ $context_name ] = $block->attributes[ $attribute_name ];
            }
        }
    }

    $registry = WP_Block_Type_Registry::get_instance();
    return new WP_Block_List(
            $block->parsed_block['innerBlocks'],
            $child_context,
            $registry
    );

}
bobbingwide commented 3 years ago

I don't know if the logic will work for nested menu items. Nor have I checked why core/query-loop works without any changes. Maybe core/navigation hadn't been updated.

bobbingwide commented 3 years ago

core/query-loop works differently since it uses the innerBlocks in $block->parse_block. So an alternative solution is to change the core/navigation block to render each of the blocks in $block->parse_block['innerBlocks'] I didn't check whether or not the $child_context is actually needed.

The loop to create $inner_blocks_html becomes

$inner_blocks_html = '';
    foreach ( $block->parsed_block['innerBlocks'] as $inner_block ) {
        $block = new WP_Block($inner_block, $child_context);
        $inner_blocks_html .= $block->render();
    }

Both solutions work. Since I don't use nested menu items yet I'll leave that to when I attempt to document the blocks.

bobbingwide commented 3 years ago

I don't believe the shim's still required with WordPress 5.6.1 or 5.7-beta3