thebatclaudio / wp-rest-api-v2-menus

Adding menus endpoints on WP REST API v2
47 stars 23 forks source link

array_values() throwing a TypeError #41

Closed darylldoyle closed 1 year ago

darylldoyle commented 1 year ago

wp_get_nav_menu_items() returns either false or an array() 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() returned false, because no menu was assigned to the menu location it will throw a TypeError 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.

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;

    // ...
}
thebatclaudio commented 1 year ago

Hello @darylldoyle, I just released a new plugin version with your bug fix. Thank you!

darylldoyle commented 1 year ago

Thank you, @thebatclaudio, that looks to have resolved the issue!