Skip to content

Commit

Permalink
Editor: Fix error handling of converting classic to block menus.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@56422 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Aug 21, 2023
1 parent f0d53f8 commit 17e9e69
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/wp-includes/class-wp-classic-to-block-menu-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand All @@ -34,7 +36,7 @@ public static function convert( $menu ) {
$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.
Expand Down
4 changes: 4 additions & 0 deletions src/wp-includes/class-wp-navigation-fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ private static function create_classic_menu_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.' ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,14 @@ public function test_does_not_convert_menu_items_with_non_publish_status() {
* @ticket 58557
* @covers WP_Classic_To_Block_Menu_Converter::convert
*/
public function test_returns_empty_array_for_menus_with_no_items() {
public function test_returns_empty_string_for_menus_with_no_items() {
$menu_id = wp_create_nav_menu( 'Empty Menu' );

$classic_nav_menu = wp_get_nav_menu_object( $menu_id );

$blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );

$this->assertEmpty( $blocks, 'Result should be empty.' );

$this->assertIsArray( $blocks, 'Result should be empty array.' );
$this->assertSame( '', $blocks, 'Result should be empty string.' );

wp_delete_nav_menu( $menu_id );
}
Expand Down

0 comments on commit 17e9e69

Please sign in to comment.