From 87aa505a94ecb15c9384ce10905d921029c659fb Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Mon, 21 Aug 2023 17:51:19 +0000 Subject: [PATCH] Editor: Fix error handling of converting classic to block menus. Fixes the error handling for when `WP_Classic_To_Block_Menu_Converter::convert()` returns an instance of `WP_Error`. `WP_Navigation_Fallback::create_classic_menu_fallback()` now checks for `is_wp_error()` and if `true`, returns the error. And the `@return` type is updated to `string|WP_Error`. Also includes a fix in the return type in `WP_Classic_To_Block_Menu_Converter::convert()` to return an empty string instead of an array instead, i.e. when bailing out for no menu items returned by `wp_get_nav_menu_items()`. The return type is clearly documented as a `string`. Follow-up to [56052]. Props dlh, get_dave, antonvlasenko, hellofromTonya. Fixes #58823. Built from https://develop.svn.wordpress.org/trunk@56422 git-svn-id: http://core.svn.wordpress.org/trunk@55934 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-classic-to-block-menu-converter.php | 6 ++++-- wp-includes/class-wp-navigation-fallback.php | 4 ++++ wp-includes/version.php | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/wp-includes/class-wp-classic-to-block-menu-converter.php b/wp-includes/class-wp-classic-to-block-menu-converter.php index 3aeb865302..6430aab6fa 100644 --- a/wp-includes/class-wp-classic-to-block-menu-converter.php +++ b/wp-includes/class-wp-classic-to-block-menu-converter.php @@ -20,7 +20,9 @@ class WP_Classic_To_Block_Menu_Converter { * @since 6.3.0 * * @param WP_Term $menu The Menu term object of the menu to convert. - * @return string the serialized and normalized parsed blocks. + * @return string|WP_Error The serialized and normalized parsed blocks on success, + * an empty string when there are no menus to convert, + * or WP_Error on invalid menu. */ public static function convert( $menu ) { @@ -34,7 +36,7 @@ class WP_Classic_To_Block_Menu_Converter { $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); if ( empty( $menu_items ) ) { - return array(); + return ''; } // Set up the $menu_item variables. diff --git a/wp-includes/class-wp-navigation-fallback.php b/wp-includes/class-wp-navigation-fallback.php index 70ddf11e4e..fb97c643bb 100644 --- a/wp-includes/class-wp-navigation-fallback.php +++ b/wp-includes/class-wp-navigation-fallback.php @@ -105,6 +105,10 @@ class WP_Navigation_Fallback { // If there is a classic menu then convert it to blocks. $classic_nav_menu_blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu ); + if ( is_wp_error( $classic_nav_menu_blocks ) ) { + return $classic_nav_menu_blocks; + } + if ( empty( $classic_nav_menu_blocks ) ) { return new WP_Error( 'cannot_convert_classic_menu', __( 'Unable to convert Classic Menu to blocks.' ) ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 964f84b9dd..1993e99af9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56421'; +$wp_version = '6.4-alpha-56422'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.