Closed darylldoyle closed 1 year ago
wp_get_nav_menu_items() returns either false or an array() and is being used on the following line:
wp_get_nav_menu_items()
false
array()
https://github.com/thebatclaudio/wp-rest-api-v2-menus/blob/631d033253106e740388483af87478e464cd6895/wp-rest-api-v2-menus.php#L141
This is then processed in the method and finally passed to array_values() on the following line:
array_values()
https://github.com/thebatclaudio/wp-rest-api-v2-menus/blob/631d033253106e740388483af87478e464cd6895/wp-rest-api-v2-menus.php#L208
If wp_get_nav_menu_items() returned false, because no menu was assigned to the menu location it will throw a TypeError under PHP 8.
TypeError
To get around this, I suggest we check for false after fetching the menu items and return an empty array if that's true. E.G.
function wp_api_v2_menus_get_menu_items( $id ) { $menu_items = wp_get_nav_menu_items( $id ); if ( false === $menu_items ) { return []; } $all_menu_items = $menu_items; // ... }
Hello @darylldoyle, I just released a new plugin version with your bug fix. Thank you!
Thank you, @thebatclaudio, that looks to have resolved the issue!
wp_get_nav_menu_items()
returns eitherfalse
or anarray()
and is being used on the following line:https://github.com/thebatclaudio/wp-rest-api-v2-menus/blob/631d033253106e740388483af87478e464cd6895/wp-rest-api-v2-menus.php#L141
This is then processed in the method and finally passed to
array_values()
on the following line:https://github.com/thebatclaudio/wp-rest-api-v2-menus/blob/631d033253106e740388483af87478e464cd6895/wp-rest-api-v2-menus.php#L208
If
wp_get_nav_menu_items()
returnedfalse
, because no menu was assigned to the menu location it will throw aTypeError
under PHP 8.To get around this, I suggest we check for
false
after fetching the menu items and return an empty array if that's true. E.G.