WordPress.org

Make WordPress Core

Opened 7 weeks ago

Last modified 10 days ago

#51565 new defect (bug)

PHP Notices from wp_setup_nav_menu_item when object_id doesn't exist anymore

Reported by: tristanleboss Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.5.1
Component: Menus Keywords:
Focuses: Cc:

Description

Hello,

The function wp_setup_nav_menu_item calls function get_post_states after trying to get the post linked to a menu item.

Unfortunately, no check is done of get_post return value and, if the post linked to a menu item doesn't exist anymore, the variable $menu_post will be null. The susequent call to get_post_states (which uses $menu_post as its first argument with the null value) will give 2 PHP warnings:

  • Notice: Trying to get property 'post_status' of non-object
  • Notice: Trying to get property 'ID' of non-object

Indeed, get_post_states don't check that its first argument is really a post object and directly tries to access its properties assuming it to be a valid object.

I think both functions should be corrected: wp_setup_nav_menu_item should check the return value of get_post and get_post_states should also check if its first argument is indeed a valid post object.

Culprit code from wp_setup_nav_menu_item:

<?php
                                if ( $object ) {
                                        $menu_item->type_label = $object->labels->singular_name;
                                        // Denote post states for special pages (only in the admin).
                                        if ( function_exists( 'get_post_states' ) ) {
                                                $menu_post   = get_post( $menu_item->object_id );
                                                $post_states = get_post_states( $menu_post );
                                                if ( $post_states ) {
                                                        $menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
                                                }
                                        }

Change History (2)

#1 @tristanleboss
7 weeks ago

  • Summary changed from HP warnings from wp_setup_nav_menu_item when object_id doesn't exist anymore to PHP Notices from wp_setup_nav_menu_item when object_id doesn't exist anymore

#2 @SergeyBiryukov
10 days ago

  • Component changed from General to Menus
Note: See TracTickets for help on using tickets.