~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/nav-menu-template.php

  • Committer: Barry Price
  • Date: 2016-08-17 04:50:12 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: barry.price@canonical.com-20160817045012-qfui81zhqnqv2ba9
Merge WP4.6 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
/**
3
 
 * Navigation Menu template functions
 
3
 * Nav Menu API: Template functions
4
4
 *
5
5
 * @package WordPress
6
6
 * @subpackage Nav_Menus
7
7
 * @since 3.0.0
8
8
 */
9
9
 
10
 
/**
11
 
 * Create HTML list of nav menu items.
12
 
 *
13
 
 * @since 3.0.0
14
 
 * @uses Walker
15
 
 */
16
 
class Walker_Nav_Menu extends Walker {
17
 
        /**
18
 
         * What the class handles.
19
 
         *
20
 
         * @see Walker::$tree_type
21
 
         * @since 3.0.0
22
 
         * @var string
23
 
         */
24
 
        public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
25
 
 
26
 
        /**
27
 
         * Database fields to use.
28
 
         *
29
 
         * @see Walker::$db_fields
30
 
         * @since 3.0.0
31
 
         * @todo Decouple this.
32
 
         * @var array
33
 
         */
34
 
        public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
35
 
 
36
 
        /**
37
 
         * Starts the list before the elements are added.
38
 
         *
39
 
         * @see Walker::start_lvl()
40
 
         *
41
 
         * @since 3.0.0
42
 
         *
43
 
         * @param string $output Passed by reference. Used to append additional content.
44
 
         * @param int    $depth  Depth of menu item. Used for padding.
45
 
         * @param array  $args   An array of arguments. @see wp_nav_menu()
46
 
         */
47
 
        public function start_lvl( &$output, $depth = 0, $args = array() ) {
48
 
                $indent = str_repeat("\t", $depth);
49
 
                $output .= "\n$indent<ul class=\"sub-menu\">\n";
50
 
        }
51
 
 
52
 
        /**
53
 
         * Ends the list of after the elements are added.
54
 
         *
55
 
         * @see Walker::end_lvl()
56
 
         *
57
 
         * @since 3.0.0
58
 
         *
59
 
         * @param string $output Passed by reference. Used to append additional content.
60
 
         * @param int    $depth  Depth of menu item. Used for padding.
61
 
         * @param array  $args   An array of arguments. @see wp_nav_menu()
62
 
         */
63
 
        public function end_lvl( &$output, $depth = 0, $args = array() ) {
64
 
                $indent = str_repeat("\t", $depth);
65
 
                $output .= "$indent</ul>\n";
66
 
        }
67
 
 
68
 
        /**
69
 
         * Start the element output.
70
 
         *
71
 
         * @see Walker::start_el()
72
 
         *
73
 
         * @since 3.0.0
74
 
         * @since 4.4.0 'nav_menu_item_args' filter was added.
75
 
         *
76
 
         * @param string $output Passed by reference. Used to append additional content.
77
 
         * @param object $item   Menu item data object.
78
 
         * @param int    $depth  Depth of menu item. Used for padding.
79
 
         * @param array  $args   An array of arguments. @see wp_nav_menu()
80
 
         * @param int    $id     Current item ID.
81
 
         */
82
 
        public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
83
 
                $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
84
 
 
85
 
                $classes = empty( $item->classes ) ? array() : (array) $item->classes;
86
 
                $classes[] = 'menu-item-' . $item->ID;
87
 
 
88
 
                /**
89
 
                 * Filter the arguments for a single nav menu item.
90
 
                 *
91
 
                 * @since 4.4.0
92
 
                 *
93
 
                 * @param array  $args  An array of arguments.
94
 
                 * @param object $item  Menu item data object.
95
 
                 * @param int    $depth Depth of menu item. Used for padding.
96
 
                 */
97
 
                $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
98
 
 
99
 
                /**
100
 
                 * Filter the CSS class(es) applied to a menu item's list item element.
101
 
                 *
102
 
                 * @since 3.0.0
103
 
                 * @since 4.1.0 The `$depth` parameter was added.
104
 
                 *
105
 
                 * @param array  $classes The CSS classes that are applied to the menu item's `<li>` element.
106
 
                 * @param object $item    The current menu item.
107
 
                 * @param array  $args    An array of {@see wp_nav_menu()} arguments.
108
 
                 * @param int    $depth   Depth of menu item. Used for padding.
109
 
                 */
110
 
                $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
111
 
                $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
112
 
 
113
 
                /**
114
 
                 * Filter the ID applied to a menu item's list item element.
115
 
                 *
116
 
                 * @since 3.0.1
117
 
                 * @since 4.1.0 The `$depth` parameter was added.
118
 
                 *
119
 
                 * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
120
 
                 * @param object $item    The current menu item.
121
 
                 * @param array  $args    An array of {@see wp_nav_menu()} arguments.
122
 
                 * @param int    $depth   Depth of menu item. Used for padding.
123
 
                 */
124
 
                $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
125
 
                $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
126
 
 
127
 
                $output .= $indent . '<li' . $id . $class_names .'>';
128
 
 
129
 
                $atts = array();
130
 
                $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
131
 
                $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
132
 
                $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
133
 
                $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
134
 
 
135
 
                /**
136
 
                 * Filter the HTML attributes applied to a menu item's anchor element.
137
 
                 *
138
 
                 * @since 3.6.0
139
 
                 * @since 4.1.0 The `$depth` parameter was added.
140
 
                 *
141
 
                 * @param array $atts {
142
 
                 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
143
 
                 *
144
 
                 *     @type string $title  Title attribute.
145
 
                 *     @type string $target Target attribute.
146
 
                 *     @type string $rel    The rel attribute.
147
 
                 *     @type string $href   The href attribute.
148
 
                 * }
149
 
                 * @param object $item  The current menu item.
150
 
                 * @param array  $args  An array of {@see wp_nav_menu()} arguments.
151
 
                 * @param int    $depth Depth of menu item. Used for padding.
152
 
                 */
153
 
                $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
154
 
 
155
 
                $attributes = '';
156
 
                foreach ( $atts as $attr => $value ) {
157
 
                        if ( ! empty( $value ) ) {
158
 
                                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
159
 
                                $attributes .= ' ' . $attr . '="' . $value . '"';
160
 
                        }
161
 
                }
162
 
 
163
 
                /** This filter is documented in wp-includes/post-template.php */
164
 
                $title = apply_filters( 'the_title', $item->title, $item->ID );
165
 
 
166
 
                /**
167
 
                 * Filter a menu item's title.
168
 
                 *
169
 
                 * @since 4.4.0
170
 
                 *
171
 
                 * @param string $title The menu item's title.
172
 
                 * @param object $item  The current menu item.
173
 
                 * @param array  $args  An array of {@see wp_nav_menu()} arguments.
174
 
                 * @param int    $depth Depth of menu item. Used for padding.
175
 
                 */
176
 
                $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
177
 
 
178
 
                $item_output = $args->before;
179
 
                $item_output .= '<a'. $attributes .'>';
180
 
                $item_output .= $args->link_before . $title . $args->link_after;
181
 
                $item_output .= '</a>';
182
 
                $item_output .= $args->after;
183
 
 
184
 
                /**
185
 
                 * Filter a menu item's starting output.
186
 
                 *
187
 
                 * The menu item's starting output only includes `$args->before`, the opening `<a>`,
188
 
                 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
189
 
                 * no filter for modifying the opening and closing `<li>` for a menu item.
190
 
                 *
191
 
                 * @since 3.0.0
192
 
                 *
193
 
                 * @param string $item_output The menu item's starting HTML output.
194
 
                 * @param object $item        Menu item data object.
195
 
                 * @param int    $depth       Depth of menu item. Used for padding.
196
 
                 * @param array  $args        An array of {@see wp_nav_menu()} arguments.
197
 
                 */
198
 
                $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
199
 
        }
200
 
 
201
 
        /**
202
 
         * Ends the element output, if needed.
203
 
         *
204
 
         * @see Walker::end_el()
205
 
         *
206
 
         * @since 3.0.0
207
 
         *
208
 
         * @param string $output Passed by reference. Used to append additional content.
209
 
         * @param object $item   Page data object. Not used.
210
 
         * @param int    $depth  Depth of page. Not Used.
211
 
         * @param array  $args   An array of arguments. @see wp_nav_menu()
212
 
         */
213
 
        public function end_el( &$output, $item, $depth = 0, $args = array() ) {
214
 
                $output .= "</li>\n";
215
 
        }
216
 
 
217
 
} // Walker_Nav_Menu
 
10
/** Walker_Nav_Menu class */
 
11
require_once ABSPATH . WPINC . '/class-walker-nav-menu.php';
218
12
 
219
13
/**
220
14
 * Displays a navigation menu.
235
29
 *     @type string        $container_id    The ID that is applied to the container. Default empty.
236
30
 *     @type callable|bool $fallback_cb     If the menu doesn't exists, a callback function will fire.
237
31
 *                                          Default is 'wp_page_menu'. Set to false for no fallback.
238
 
 *     @type string        $before          Text before the link text. Default empty.
239
 
 *     @type string        $after           Text after the link text. Default empty.
240
 
 *     @type string        $link_before     Text before the link. Default empty.
241
 
 *     @type string        $link_after      Text after the link. Default empty.
 
32
 *     @type string        $before          Text before the link markup. Default empty.
 
33
 *     @type string        $after           Text after the link markup. Default empty.
 
34
 *     @type string        $link_before     Text before the link text. Default empty.
 
35
 *     @type string        $link_after      Text after the link text. Default empty.
242
36
 *     @type bool          $echo            Whether to echo the menu or return it. Default true.
243
37
 *     @type int           $depth           How many levels of the hierarchy are to be included. 0 means all. Default 0.
244
38
 *     @type object        $walker          Instance of a custom walker class. Default empty.
258
52
 
259
53
        $args = wp_parse_args( $args, $defaults );
260
54
        /**
261
 
         * Filter the arguments used to display a navigation menu.
 
55
         * Filters the arguments used to display a navigation menu.
262
56
         *
263
57
         * @since 3.0.0
264
58
         *
270
64
        $args = (object) $args;
271
65
 
272
66
        /**
273
 
         * Filter whether to short-circuit the wp_nav_menu() output.
 
67
         * Filters whether to short-circuit the wp_nav_menu() output.
274
68
         *
275
69
         * Returning a non-null value to the filter will short-circuit
276
70
         * wp_nav_menu(), echoing that value if $args->echo is true,
340
134
        $show_container = false;
341
135
        if ( $args->container ) {
342
136
                /**
343
 
                 * Filter the list of HTML tags that are valid for use as menu containers.
 
137
                 * Filters the list of HTML tags that are valid for use as menu containers.
344
138
                 *
345
139
                 * @since 3.0.0
346
140
                 *
377
171
        unset( $menu_items, $menu_item );
378
172
 
379
173
        /**
380
 
         * Filter the sorted list of menu item objects before generating the menu's HTML.
 
174
         * Filters the sorted list of menu item objects before generating the menu's HTML.
381
175
         *
382
176
         * @since 3.1.0
383
177
         *
406
200
        $wrap_class = $args->menu_class ? $args->menu_class : '';
407
201
 
408
202
        /**
409
 
         * Filter the HTML list content for navigation menus.
 
203
         * Filters the HTML list content for navigation menus.
410
204
         *
411
205
         * @since 3.0.0
412
206
         *
417
211
         */
418
212
        $items = apply_filters( 'wp_nav_menu_items', $items, $args );
419
213
        /**
420
 
         * Filter the HTML list content for a specific navigation menu.
 
214
         * Filters the HTML list content for a specific navigation menu.
421
215
         *
422
216
         * @since 3.0.0
423
217
         *
439
233
                $nav_menu .= '</' . $args->container . '>';
440
234
 
441
235
        /**
442
 
         * Filter the HTML content for navigation menus.
 
236
         * Filters the HTML content for navigation menus.
443
237
         *
444
238
         * @since 3.0.0
445
239
         *