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

Incorrect error handling when converting classic to block menus #52481

Closed
dlh01 opened this issue Jul 10, 2023 · 5 comments
Closed

Incorrect error handling when converting classic to block menus #52481

dlh01 opened this issue Jul 10, 2023 · 5 comments
Labels
[Block] Navigation Affects the Navigation Block Needs Testing Needs further testing to be confirmed. [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. [Type] Bug An existing feature does not function as intended

Comments

@dlh01
Copy link
Contributor

dlh01 commented Jul 10, 2023

Description

Some errors that can occur when Gutenberg_Navigation_Fallback::create_classic_menu_fallback() calls Gutenberg_Classic_To_Block_Menu_Converter::convert() aren't handled as accurately as they could be.

  1. ::convert() can return a WP_Error object, but this error object won't be detected by the empty() check in ::create_classic_menu_fallback(). The WP_Error return type is also missing from the docs.
  2. ::convert() returns an array instead of a string when no menu items are returned by wp_get_nav_menu_items().

See here:

private static function create_classic_menu_fallback() {
// See if we have a classic menu.
$classic_nav_menu = static::get_fallback_classic_menu();
if ( ! $classic_nav_menu ) {
return new WP_Error( 'no_classic_menus', __( 'No Classic Menus found.', 'gutenberg' ) );
}
// If there is a classic menu then convert it to blocks.
$classic_nav_menu_blocks = Gutenberg_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );
if ( empty( $classic_nav_menu_blocks ) ) {
return new WP_Error( 'cannot_convert_classic_menu', __( 'Unable to convert Classic Menu to blocks.', 'gutenberg' ) );
}
// Create a new navigation menu from the classic menu.
$classic_menu_fallback = wp_insert_post(
array(
'post_content' => $classic_nav_menu_blocks,
'post_title' => $classic_nav_menu->name,
'post_name' => $classic_nav_menu->slug,
'post_status' => 'publish',
'post_type' => 'wp_navigation',
),
true // So that we can check whether the result is an error.
);
return $classic_menu_fallback;
}

and here:

/**
* Converts a Classic Menu to blocks.
*
* @param WP_Term $menu The Menu term object of the menu to convert.
* @return string the serialized and normalized parsed blocks.
*/
public static function convert( $menu ) {
if ( ! is_nav_menu( $menu ) ) {
return new WP_Error(
'invalid_menu',
__( 'The menu provided is not a valid menu.', 'gutenberg' )
);
}
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
if ( empty( $menu_items ) ) {
return array();
}
// Set up the $menu_item variables.
// Adds the class property classes for the current context, if applicable.
_wp_menu_item_classes_by_context( $menu_items );
$menu_items_by_parent_id = static::group_by_parent_id( $menu_items );
$first_menu_item = isset( $menu_items_by_parent_id[0] )
? $menu_items_by_parent_id[0]
: array();
$inner_blocks = static::to_blocks(
$first_menu_item,
$menu_items_by_parent_id
);
return serialize_blocks( $inner_blocks );
}

Step-by-step reproduction instructions

n/a

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress trunk
  • Gutenberg trunk

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

Yes

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

No

@jordesign jordesign added [Type] Bug An existing feature does not function as intended Needs Testing Needs further testing to be confirmed. [Block] Navigation Affects the Navigation Block labels Jul 11, 2023
@mrfoxtalbot
Copy link

Could this cause a fatal error? I had a slightly old version of the Gutenberg plugin on my site (15.8.1) and ran into this error:

Screenshot_2023-08-10-01-16-26-21_40deb401b9ffe8e1df2f1cc5ba480b12.jpg

An error of type E_COMPILE_ERROR was caused in line 14 of the file /srv/htdocs/wp-content/plugins/gutenberg/lib/experimental/class-wp-classic-to-block-menu-converter.php. Error message: Cannot declare class WP_Classic_To_Block_Menu_Converter, because the name is already in use

@getdave
Copy link
Contributor

getdave commented Aug 10, 2023

The redeclare class warning is likely because both Core and Gutenberg have a class with the same name. @ramonjd did a fix for this I believe so perhaps it needs backporting to older versions of Gutenberg?

@getdave
Copy link
Contributor

getdave commented Aug 10, 2023

This Issue will be resolved by merging WordPress/wordpress-develop#4858.

@github-actions
Copy link

Hi,
This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps.
Thanks for helping out.

@github-actions github-actions bot added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Sep 10, 2023
@dlh01
Copy link
Contributor Author

dlh01 commented Sep 10, 2023

Resolved as described previously.

@dlh01 dlh01 closed this as completed Sep 10, 2023
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 Needs Testing Needs further testing to be confirmed. [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. [Type] Bug An existing feature does not function as intended
4 participants