~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/class-walker-nav-menu.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
<?php
 
2
/**
 
3
 * Nav Menu API: Walker_Nav_Menu class
 
4
 *
 
5
 * @package WordPress
 
6
 * @subpackage Nav_Menus
 
7
 * @since 4.6.0
 
8
 */
 
9
 
 
10
/**
 
11
 * Core class used to implement an HTML list of nav menu items.
 
12
 *
 
13
 * @since 3.0.0
 
14
 *
 
15
 * @see Walker
 
16
 */
 
17
class Walker_Nav_Menu extends Walker {
 
18
        /**
 
19
         * What the class handles.
 
20
         *
 
21
         * @since 3.0.0
 
22
         * @access public
 
23
         * @var string
 
24
         *
 
25
         * @see Walker::$tree_type
 
26
         */
 
27
        public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
 
28
 
 
29
        /**
 
30
         * Database fields to use.
 
31
         *
 
32
         * @since 3.0.0
 
33
         * @access public
 
34
         * @todo Decouple this.
 
35
         * @var array
 
36
         *
 
37
         * @see Walker::$db_fields
 
38
         */
 
39
        public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
 
40
 
 
41
        /**
 
42
         * Starts the list before the elements are added.
 
43
         *
 
44
         * @since 3.0.0
 
45
         *
 
46
         * @see Walker::start_lvl()
 
47
         *
 
48
         * @param string $output Passed by reference. Used to append additional content.
 
49
         * @param int    $depth  Depth of menu item. Used for padding.
 
50
         * @param array  $args   An array of wp_nav_menu() arguments.
 
51
         */
 
52
        public function start_lvl( &$output, $depth = 0, $args = array() ) {
 
53
                $indent = str_repeat("\t", $depth);
 
54
                $output .= "\n$indent<ul class=\"sub-menu\">\n";
 
55
        }
 
56
 
 
57
        /**
 
58
         * Ends the list of after the elements are added.
 
59
         *
 
60
         * @since 3.0.0
 
61
         *
 
62
         * @see Walker::end_lvl()
 
63
         *
 
64
         * @param string $output Passed by reference. Used to append additional content.
 
65
         * @param int    $depth  Depth of menu item. Used for padding.
 
66
         * @param array  $args   An array of wp_nav_menu() arguments.
 
67
         */
 
68
        public function end_lvl( &$output, $depth = 0, $args = array() ) {
 
69
                $indent = str_repeat("\t", $depth);
 
70
                $output .= "$indent</ul>\n";
 
71
        }
 
72
 
 
73
        /**
 
74
         * Starts the element output.
 
75
         *
 
76
         * @since 3.0.0
 
77
         * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
 
78
         *
 
79
         * @see Walker::start_el()
 
80
         *
 
81
         * @param string $output Passed by reference. Used to append additional content.
 
82
         * @param object $item   Menu item data object.
 
83
         * @param int    $depth  Depth of menu item. Used for padding.
 
84
         * @param array  $args   An array of wp_nav_menu() arguments.
 
85
         * @param int    $id     Current item ID.
 
86
         */
 
87
        public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
 
88
                $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
 
89
 
 
90
                $classes = empty( $item->classes ) ? array() : (array) $item->classes;
 
91
                $classes[] = 'menu-item-' . $item->ID;
 
92
 
 
93
                /**
 
94
                 * Filters the arguments for a single nav menu item.
 
95
                 *
 
96
                 * @since 4.4.0
 
97
                 *
 
98
                 * @param array  $args  An array of arguments.
 
99
                 * @param object $item  Menu item data object.
 
100
                 * @param int    $depth Depth of menu item. Used for padding.
 
101
                 */
 
102
                $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
 
103
 
 
104
                /**
 
105
                 * Filters the CSS class(es) applied to a menu item's list item element.
 
106
                 *
 
107
                 * @since 3.0.0
 
108
                 * @since 4.1.0 The `$depth` parameter was added.
 
109
                 *
 
110
                 * @param array  $classes The CSS classes that are applied to the menu item's `<li>` element.
 
111
                 * @param object $item    The current menu item.
 
112
                 * @param array  $args    An array of wp_nav_menu() arguments.
 
113
                 * @param int    $depth   Depth of menu item. Used for padding.
 
114
                 */
 
115
                $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
 
116
                $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
 
117
 
 
118
                /**
 
119
                 * Filters the ID applied to a menu item's list item element.
 
120
                 *
 
121
                 * @since 3.0.1
 
122
                 * @since 4.1.0 The `$depth` parameter was added.
 
123
                 *
 
124
                 * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
 
125
                 * @param object $item    The current menu item.
 
126
                 * @param array  $args    An array of wp_nav_menu() arguments.
 
127
                 * @param int    $depth   Depth of menu item. Used for padding.
 
128
                 */
 
129
                $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
 
130
                $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
 
131
 
 
132
                $output .= $indent . '<li' . $id . $class_names .'>';
 
133
 
 
134
                $atts = array();
 
135
                $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
 
136
                $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
 
137
                $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
 
138
                $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
 
139
 
 
140
                /**
 
141
                 * Filters the HTML attributes applied to a menu item's anchor element.
 
142
                 *
 
143
                 * @since 3.6.0
 
144
                 * @since 4.1.0 The `$depth` parameter was added.
 
145
                 *
 
146
                 * @param array $atts {
 
147
                 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
 
148
                 *
 
149
                 *     @type string $title  Title attribute.
 
150
                 *     @type string $target Target attribute.
 
151
                 *     @type string $rel    The rel attribute.
 
152
                 *     @type string $href   The href attribute.
 
153
                 * }
 
154
                 * @param object $item  The current menu item.
 
155
                 * @param array  $args  An array of wp_nav_menu() arguments.
 
156
                 * @param int    $depth Depth of menu item. Used for padding.
 
157
                 */
 
158
                $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
 
159
 
 
160
                $attributes = '';
 
161
                foreach ( $atts as $attr => $value ) {
 
162
                        if ( ! empty( $value ) ) {
 
163
                                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
 
164
                                $attributes .= ' ' . $attr . '="' . $value . '"';
 
165
                        }
 
166
                }
 
167
 
 
168
                /** This filter is documented in wp-includes/post-template.php */
 
169
                $title = apply_filters( 'the_title', $item->title, $item->ID );
 
170
 
 
171
                /**
 
172
                 * Filters a menu item's title.
 
173
                 *
 
174
                 * @since 4.4.0
 
175
                 *
 
176
                 * @param string $title The menu item's title.
 
177
                 * @param object $item  The current menu item.
 
178
                 * @param array  $args  An array of wp_nav_menu() arguments.
 
179
                 * @param int    $depth Depth of menu item. Used for padding.
 
180
                 */
 
181
                $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
 
182
 
 
183
                $item_output = $args->before;
 
184
                $item_output .= '<a'. $attributes .'>';
 
185
                $item_output .= $args->link_before . $title . $args->link_after;
 
186
                $item_output .= '</a>';
 
187
                $item_output .= $args->after;
 
188
 
 
189
                /**
 
190
                 * Filters a menu item's starting output.
 
191
                 *
 
192
                 * The menu item's starting output only includes `$args->before`, the opening `<a>`,
 
193
                 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
 
194
                 * no filter for modifying the opening and closing `<li>` for a menu item.
 
195
                 *
 
196
                 * @since 3.0.0
 
197
                 *
 
198
                 * @param string $item_output The menu item's starting HTML output.
 
199
                 * @param object $item        Menu item data object.
 
200
                 * @param int    $depth       Depth of menu item. Used for padding.
 
201
                 * @param array  $args        An array of wp_nav_menu() arguments.
 
202
                 */
 
203
                $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
 
204
        }
 
205
 
 
206
        /**
 
207
         * Ends the element output, if needed.
 
208
         *
 
209
         * @since 3.0.0
 
210
         *
 
211
         * @see Walker::end_el()
 
212
         *
 
213
         * @param string $output Passed by reference. Used to append additional content.
 
214
         * @param object $item   Page data object. Not used.
 
215
         * @param int    $depth  Depth of page. Not Used.
 
216
         * @param array  $args   An array of wp_nav_menu() arguments.
 
217
         */
 
218
        public function end_el( &$output, $item, $depth = 0, $args = array() ) {
 
219
                $output .= "</li>\n";
 
220
        }
 
221
 
 
222
} // Walker_Nav_Menu