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

Navigation Block: __unstableLocation can trigger a fatal in block_core_navigation_get_post_ids() #56600

Closed
tommusrhodus opened this issue Nov 28, 2023 · 4 comments · Fixed by #56629
Assignees
Labels
[Block] Navigation Affects the Navigation Block [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@tommusrhodus
Copy link

tommusrhodus commented Nov 28, 2023

Description

Stack trace:

PHP Fatal error: Uncaught TypeError:
iterator_to_array(): Argument #1 ($iterator) must be of type Traversable|array, string given in /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php:376


Stack trace:
#0 /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php(376): iterator_to_array('')
#1 /srv/htdocs/wp-content/plugins/gutenberg/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php(273): gutenberg_block_core_navigation_get_post_ids('')
#2 /srv/htdocs/wp-content/plugins/gutenberg/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php(628): WP_Navigation_Block_Renderer::get_inner_blocks(Array, Object(WP_Block))
#3 /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php(413): WP_Navigation_Block_Renderer::render(Array, '', Object(WP_Block))
#4 /wordpress/core/6.4.1/wp-includes/class-wp-block.php(258): gutenberg_render_block_core_navigation(Array, '', Object(WP_Block))
#5 /wordpress/core/6.4.1/wp-includes/class-wp-block.php(244): WP_Block->render()
#6 /wordpress/core/6.4.1/wp-includes/blocks.php(1484): WP_Block->render()
#7 /wordpress/core/6.4.1/wp-includes/blocks.php(1522): render_block(Array)
#8 /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/template-part.php(144): do_blocks('<!-- wp:group {...')
#9 /srv/htdocs/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/BlockTemplatesController.php(171): gutenberg_render_block_core_template_part(Array)
#10 /wordpress/core/6.4.1/wp-includes/class-wp-block.php(258): Automattic\WooCommerce\Blocks\BlockTemplatesController->render_woocommerce_template_part(Array, '', Object(WP_Block))
#11 /wordpress/core/6.4.1/wp-includes/blocks.php(1484): WP_Block->render()
#12 /wordpress/core/6.4.1/wp-includes/blocks.php(1522): render_block(Array)
#13 /wordpress/core/6.4.1/wp-includes/block-template.php(263): do_blocks('<!-- wp:templat...')
#14 /wordpress/core/6.4.1/wp-includes/template-canvas.php(12): get_the_block_template_html()
#15 /wordpress/core/6.4.1/wp-includes/template-loader.php(106): include('/wordpress/core...')
#16 /wordpress/core/6.4.1/wp-blog-header.php(19): require_once('/wordpress/core...')
#17 /wordpress/core/6.4.1/index.php(17): require('/wordpress/core...')
#18 {main} thrown in /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php on line 376

TL:DR We have a site in the latest Gutenberg release (17.1.3) that's throwing a fatal through the use of __unstableLocation in a navigation block in a template part.

Here's the block markup:

<!-- wp:navigation {"isResponsive":true,"__unstableLocation":"primary","style":{"typography":{"fontStyle":"normal","fontWeight":"500"}}} /-->

Investigation so far:

I appreciate this is quite a vague report that I cannot currently re-produce on a standard theme, any insight appreciated.

This PR seems related to a similar area, but not the root cause of this issue: #56459

Step-by-step reproduction instructions

I can't currently reproduce this issue.

Screenshots, screen recording, code snippet

Current theme patch that's resolving this issue:

/**
 * Fixes a fatal error if inner_blocks is empty.
 *
 * This function addresses a potential issue where empty inner_blocks can cause a fatal error. It ensures that the inner_blocks parameter always returns a valid array.
 *
 * @param array $inner_blocks The array of inner blocks provided to the navigation block. These are the blocks nested within the navigation block.
 *
 * @return array Returns an empty array if $inner_blocks is empty, or the unmodified $inner_blocks array otherwise.
 */
function prefix_navigation_render_inner_blocks( $inner_blocks ) {
	if ( empty( $inner_blocks ) ) {
		return array();
	}

	return $inner_blocks;
}
add_filter( 'block_core_navigation_render_inner_blocks', 'prefix_navigation_render_inner_blocks', 10 );

Environment info

  • WordPress 6.4.1
  • Gutenberg Plugin 17.1.3
  • Custom FSE child theme of blockbase

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@tommusrhodus tommusrhodus added the [Type] Bug An existing feature does not function as intended label Nov 28, 2023
@Mamaduka Mamaduka added the [Block] Navigation Affects the Navigation Block label Nov 28, 2023
@talldan talldan self-assigned this Nov 29, 2023
@talldan talldan added the [Status] In Progress Tracking issues with work in progress label Nov 29, 2023
@talldan
Copy link
Contributor

talldan commented Nov 29, 2023

I believe the issue is caused here

That looks like it! I think I spotted a few other places with the same issue. It looks like the code was refactored recently, and the empty string previously meant the navigation block should show no content.

Since being refactored, this code should return something iterable, as the calling code expects a block list.

I've put together a bug fix in #56629. Now I just need to figure out some reproduction steps. I think it might happen if you have an empty menu or maybe a location with no menu assigned.

@talldan
Copy link
Contributor

talldan commented Nov 29, 2023

@tommusrhodus From what I can tell it takes quite a set of circumstances for this error to trigger:

  • No wp_navigation posts can be present
  • The core/page-list block needs to be unregistered on the site
  • An empty menu or no menu assigned to the location / or no classic menus at all

Does that sound like it might be the situation on your site?

For me it has been quite hard to trigger as the fallback that adds the page list is quite effective, and that only fails to work when there's no page list block registered, but I guess you may have unregisted of that block so that you can use __unstableLocation.

@getdave
Copy link
Contributor

getdave commented Nov 30, 2023

It should be possible to test Dan's PR at

http://gutenberg.run/56629

@tommusrhodus
Copy link
Author

Does that sound like it might be the situation on your site?

@talldan Confirming we have no wp_navigation posts, but core/page-list is available.

However, our first classic menu is titled login/logout and contains two items that I believe may be filtered out (need to review plugins on the site)

I think it might happen if you have an empty menu or maybe a location with no menu assigned.

Yes, we do have a location called from __unstableLocation with no menu assigned.

Your PR changes what I suspect is the cause, still can't quite replicate how to get there on the site that originally triggered the fatal though.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants