[wp-trac] [WordPress Trac] #47720: Walker_Nav_Menu filter nav_menu_link_attributes checks $atts argument via empty() and not isset(), causing confusion with legitimate values that evaluate to boolean false

WordPress Trac noreply at wordpress.org
Wed Jul 17 10:52:47 UTC 2019


#47720: Walker_Nav_Menu filter nav_menu_link_attributes checks $atts argument via
empty() and not isset(), causing confusion with legitimate values that
evaluate to boolean false
--------------------------+-----------------------------
 Reporter:  nevma         |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  5.2.2
 Severity:  normal        |   Keywords:  needs-patch
  Focuses:  template      |
--------------------------+-----------------------------
 In file `wp-includes/class-walker-nav-menu.php`, line `206`, class
 function `start_el()`, where the `nav_menu_link_attributes` filter is
 applied, the current code checks the supplied attributes via PHP `empty()`
 function.

 This is the current code:

 {{{
 foreach ( $atts as $attr => $value ) {
     if ( ! empty( $value ) ) {
         $value       = ( 'href' === $attr ) ? esc_url( $value ) :
 esc_attr( $value );
         $attributes .= ' ' . $attr . '="' . $value . '"';
     }
 }
 }}}

 If an attribute with a legitimate value, which evaluates to something that
 equals to boolean false is passed, then the if clause will not be
 executed. However, there are many cases where such values are useful.

 For instance an HTML data attribute with a value of zero (0) could be
 passed as an attribute in the `$atts` array, but its value would then be
 equal to boolean false (as far as PHP `empty()` is concerned and thus the
 value would be erroneously ignored.

 I believe that this check would be better done with the PHP `isset()`
 function like this:

 {{{
 foreach ( $atts as $attr => $value ) {
     if ( ! isset( $value ) ) {
         $value       = ( 'href' === $attr ) ? esc_url( $value ) :
 esc_attr( $value );
         $attributes .= ' ' . $attr . '="' . $value . '"';
     }
 }
 }}}

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


More information about the wp-trac mailing list