[wp-trac] [WordPress Trac] #51565: HP warnings from wp_setup_nav_menu_item when object_id doesn't exist anymore

WordPress Trac noreply at wordpress.org
Mon Oct 19 08:49:56 UTC 2020


#51565: HP warnings from wp_setup_nav_menu_item when object_id doesn't exist
anymore
---------------------------+-----------------------------
 Reporter:  tristanleboss  |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  General        |    Version:  5.5.1
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 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
 <?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 )
 );
                                                 }
                                         }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51565>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list