WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/06/2016 09:05:45 AM (5 years ago)
Author:
peterwilsoncc
Message:

Menus: Add white space option to wp_nav_menu() and wp_list_pages().

Adds an item_spacing option to the arguments array for the functions wp_nav_menu(), wp_list_pages(), and wp_page_menu(). item_spacing is a boolean accepting either preserve or discard.

Previously, certain CSS choices could result in a site's layout changing if wp_nav_menu() fell back to the default wp_list_pages() due to differences in the whitespace within the HTML. The new argument ensures a function outputs consistant HTML while maintaining backward compatibility.

Fixes #35206.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-template.php

    r38371 r38523  
    11391139 *     @type string $title_li     List heading. Passing a null or empty value will result in no heading, and the list
    11401140 *                                will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
     1141 *     @type string $item_spacing Whether whitespace format the menu's HTML: 'discard' or 'preserve' (default).
    11411142 *     @type Walker $walker       Walker instance to use for listing pages. Default empty (Walker_Page).
    11421143 * }
     
    11501151        'title_li' => __( 'Pages' ), 'echo' => 1,
    11511152        'authors' => '', 'sort_column' => 'menu_order, post_title',
    1152         'link_before' => '', 'link_after' => '', 'walker' => '',
     1153        'link_before' => '', 'link_after' => '', 'item_spacing' => 'preserve', 'walker' => '',
    11531154    );
    11541155
    11551156    $r = wp_parse_args( $args, $defaults );
     1157
     1158    if ( ! in_array( $r['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
     1159        // invalid value, fall back to default.
     1160        $r['item_spacing'] = $defaults['item_spacing'];
     1161    }
    11561162
    11571163    $output = '';
     
    12311237 *     Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
    12321238 *
    1233  *     @type string          $sort_column How to short the list of pages. Accepts post column names.
    1234  *                                        Default 'menu_order, post_title'.
    1235  *     @type string          $menu_id     ID for the div containing the page list. Default is empty string.
    1236  *     @type string          $menu_class  Class to use for the element containing the page list. Default 'menu'.
    1237  *     @type string          $container   Element to use for the element containing the page list. Default 'div'.
    1238  *     @type bool            $echo        Whether to echo the list or return it. Accepts true (echo) or false (return).
    1239  *                                        Default true.
    1240  *     @type int|bool|string $show_home   Whether to display the link to the home page. Can just enter the text
    1241  *                                        you'd like shown for the home link. 1|true defaults to 'Home'.
    1242  *     @type string          $link_before The HTML or text to prepend to $show_home text. Default empty.
    1243  *     @type string          $link_after  The HTML or text to append to $show_home text. Default empty.
    1244  *     @type string          $before      The HTML or text to prepend to the menu. Default is '<ul>'.
    1245  *     @type string          $after       The HTML or text to append to the menu. Default is '</ul>'.
    1246  *     @type Walker          $walker      Walker instance to use for listing pages. Default empty (Walker_Page).
     1239 *     @type string          $sort_column  How to short the list of pages. Accepts post column names.
     1240 *                                         Default 'menu_order, post_title'.
     1241 *     @type string          $menu_id      ID for the div containing the page list. Default is empty string.
     1242 *     @type string          $menu_class   Class to use for the element containing the page list. Default 'menu'.
     1243 *     @type string          $container    Element to use for the element containing the page list. Default 'div'.
     1244 *     @type bool            $echo         Whether to echo the list or return it. Accepts true (echo) or false (return).
     1245 *                                         Default true.
     1246 *     @type int|bool|string $show_home    Whether to display the link to the home page. Can just enter the text
     1247 *                                         you'd like shown for the home link. 1|true defaults to 'Home'.
     1248 *     @type string          $link_before  The HTML or text to prepend to $show_home text. Default empty.
     1249 *     @type string          $link_after   The HTML or text to append to $show_home text. Default empty.
     1250 *     @type string          $before       The HTML or text to prepend to the menu. Default is '<ul>'.
     1251 *     @type string          $after        The HTML or text to append to the menu. Default is '</ul>'.
     1252 *     @type string          $item_spacing Whether whitespace format the menu's HTML: 'discard' or 'preserve' (default).
     1253 *     @type Walker          $walker       Walker instance to use for listing pages. Default empty (Walker_Page).
    12471254 * }
    12481255 * @return string|void HTML menu
     
    12501257function wp_page_menu( $args = array() ) {
    12511258    $defaults = array(
    1252         'sort_column' => 'menu_order, post_title',
    1253         'menu_id'     => '',
    1254         'menu_class'  => 'menu',
    1255         'container'   => 'div',
    1256         'echo'        => true,
    1257         'link_before' => '',
    1258         'link_after'  => '',
    1259         'before'      => '<ul>',
    1260         'after'       => '</ul>',
    1261         'walker'      => '',
     1259        'sort_column'  => 'menu_order, post_title',
     1260        'menu_id'      => '',
     1261        'menu_class'   => 'menu',
     1262        'container'    => 'div',
     1263        'echo'         => true,
     1264        'link_before'  => '',
     1265        'link_after'   => '',
     1266        'before'       => '<ul>',
     1267        'after'        => '</ul>',
     1268        'item_spacing' => 'discard',
     1269        'walker'       => '',
    12621270    );
    12631271    $args = wp_parse_args( $args, $defaults );
     1272
     1273    if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ) ) ) {
     1274        // invalid value, fall back to default.
     1275        $args['item_spacing'] = $defaults['item_spacing'];
     1276    }
     1277
     1278    if ( 'preserve' === $args['item_spacing'] ) {
     1279        $t = "\t";
     1280        $n = "\n";
     1281    } else {
     1282        $t = '';
     1283        $n = '';
     1284    }
    12641285
    12651286    /**
     
    13011322    $list_args['echo'] = false;
    13021323    $list_args['title_li'] = '';
    1303     $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
     1324    $menu .= wp_list_pages( $list_args );
    13041325
    13051326    $container = sanitize_text_field( $args['container'] );
     
    13161337            'wp_page_menu' === $args['fallback_cb'] &&
    13171338            'ul' !== $container ) {
    1318             $args['before'] = '<ul>';
     1339            $args['before'] = "<ul>{$n}";
    13191340            $args['after'] = '</ul>';
    13201341        }
     
    13321353    }
    13331354
    1334     $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>\n";
     1355    $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}";
    13351356
    13361357    /**
Note: See TracChangeset for help on using the changeset viewer.