Make WordPress Core

Opened 4 years ago

Last modified 14 months ago

#51565 new defect (bug)

PHP Notices from wp_setup_nav_menu_item when object_id doesn't exist anymore

Reported by: tristanleboss's profile tristanleboss Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.5.1
Component: Menus Keywords: has-patch
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 (5)

#1 @tristanleboss
4 years 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
4 years ago

  • Component changed from General to Menus

This ticket was mentioned in PR #1791 on WordPress/wordpress-develop by etoler1841.


3 years ago
#3

  • Keywords has-patch added

A check is added in wp_setup_nav_menu_item() to make sure the retrieved $menu_item is actually a post before running get_post_states().

Trac ticket: https://core.trac.wordpress.org/ticket/51565

#4 @sabernhardt
2 years ago

Related/duplicate: #58044

#5 @sabernhardt
14 months ago

#60251 was marked as a duplicate.

Note: See TracTickets for help on using tickets.