[wp-trac] [WordPress Trac] #40090: Walker::display_element does not populate $args[0]->has_children when it is cast as an Object

WordPress Trac noreply at wordpress.org
Thu Mar 9 23:39:11 UTC 2017


#40090: Walker::display_element does not populate $args[0]->has_children when it is
cast as an Object
--------------------------+-----------------------------
 Reporter:  JoelStransky  |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  4.7.3
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When using the various hooks in {{{Walker_Nav_Menu}}}, the {{{$args}}}
 argument is expected to have a {{{has_children}}} object property.

 Walker::display_element only creates and populates this property if
 {{{$args[0]}}} exists and is an array.
 {{{#!php
 <?php
 //display this element
 $this->has_children = ! empty( $children_elements[ $id ] );
 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
         $args[0]['has_children'] = $this->has_children; // Back-compat.
 }
 }}}

 But {{{wp_nav_menu()}}} casts $args as an object ensuring that this
 assignment will always fail.
 = Suggested Fix =
 Add an additional check for object type and assign appropriately.
 {{{#!php
 <?php
 //display this element
 $this->has_children = ! empty( $children_elements[ $id ] );
 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
         $args[0]['has_children'] = $this->has_children; // Back-compat.
 } else if ( isset( $args[0] ) && is_object( $args[0] ) ) {
         $args[0]->has_children = $this->has_children;
 }
 }}}

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


More information about the wp-trac mailing list