wpdreams / ajax-search-pro-development

Issues and dev tracking repository for Ajax Search Pro for WordPress plugin
0 stars 0 forks source link

Wordpress error, if WP menu is empty (handleMenuOutput) #102

Open butter-and-salt opened 5 months ago

butter-and-salt commented 5 months ago

PHP Fatal error: Uncaught TypeError: WPDRMS\ASP\NavMenu\Controller::handleMenuOutput(): Argument #1 ($item_output) must be of type string, null given, called in /XXX/wp-includes/class-wp-hook.php on line 326 and defined in /XXX/wp-content/plugins/ajax-search-pro/includes/classes/NavMenu/Controller.php:60

PHP Fatal error: Uncaught TypeError: WPDRMS\ASP\NavMenu\Controller::handleMenuOutput(): Return value must be of type string, null returned in /XXX/wp-content/plugins/ajax-search-pro/includes/classes/NavMenu/Controller.php:70

Fix:

    /**
     * @param string       $item_output
     * @param WP_Menu_Item $menu_item
     * @return string
     */
    public function handleMenuOutput($item_output, $menu_item ): string {
        if ( $menu_item->title === 'Ajax Search Pro' ) {
            $fields = $this->getCustomFields($menu_item->ID);
            if ( $fields['search_id'] > 0 ) {
                $item_output = do_shortcode(
                    "[wd_asp id={$fields['search_id']} prevent_events={$fields['prevent_events']}]"
                );
            }
        }

        return $item_output ?? '';
    }
ernestmarcinko commented 5 months ago

Hi,

Thanks for the report.

I'm afraid this is not a plugin related issue and your fix is not going to solve it. The function highlighted hooks linto the core walker_nav_menu_start_el, which is defined as:

apply_filters( 'walker_nav_menu_start_el', string $item_output, WP_Post $menu_item, int $depth, stdClass $args )

The second argument $item_output must be a string and string only. The issue here is that there is a null element in the menu, therefore PHP throws a type error.

I suggest checkin the menu items, there might be a malfunctioning one as well as all 3rd party hooks to walker_nav_menu_start_el, one of them is returning null instead of ''.

ernestmarcinko commented 5 months ago

Let me know if you have any updates on this.