]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-walker-nav-menu.php
WordPress 4.7.1-scripts
[autoinstalls/wordpress.git] / wp-includes / class-walker-nav-menu.php
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 stdClass $args   An object of wp_nav_menu() arguments.
51          */
52         public function start_lvl( &$output, $depth = 0, $args = array() ) {
53                 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
54                         $t = '';
55                         $n = '';
56                 } else {
57                         $t = "\t";
58                         $n = "\n";
59                 }
60                 $indent = str_repeat( $t, $depth );
61                 $output .= "{$n}{$indent}<ul class=\"sub-menu\">{$n}";
62         }
63
64         /**
65          * Ends the list of after the elements are added.
66          *
67          * @since 3.0.0
68          *
69          * @see Walker::end_lvl()
70          *
71          * @param string   $output Passed by reference. Used to append additional content.
72          * @param int      $depth  Depth of menu item. Used for padding.
73          * @param stdClass $args   An object of wp_nav_menu() arguments.
74          */
75         public function end_lvl( &$output, $depth = 0, $args = array() ) {
76                 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
77                         $t = '';
78                         $n = '';
79                 } else {
80                         $t = "\t";
81                         $n = "\n";
82                 }
83                 $indent = str_repeat( $t, $depth );
84                 $output .= "$indent</ul>{$n}";
85         }
86
87         /**
88          * Starts the element output.
89          *
90          * @since 3.0.0
91          * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
92          *
93          * @see Walker::start_el()
94          *
95          * @param string   $output Passed by reference. Used to append additional content.
96          * @param WP_Post  $item   Menu item data object.
97          * @param int      $depth  Depth of menu item. Used for padding.
98          * @param stdClass $args   An object of wp_nav_menu() arguments.
99          * @param int      $id     Current item ID.
100          */
101         public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
102                 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
103                         $t = '';
104                         $n = '';
105                 } else {
106                         $t = "\t";
107                         $n = "\n";
108                 }
109                 $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
110
111                 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
112                 $classes[] = 'menu-item-' . $item->ID;
113
114                 /**
115                  * Filters the arguments for a single nav menu item.
116                  *
117                  * @since 4.4.0
118                  *
119                  * @param stdClass $args  An object of wp_nav_menu() arguments.
120                  * @param WP_Post  $item  Menu item data object.
121                  * @param int      $depth Depth of menu item. Used for padding.
122                  */
123                 $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
124
125                 /**
126                  * Filters the CSS class(es) applied to a menu item's list item element.
127                  *
128                  * @since 3.0.0
129                  * @since 4.1.0 The `$depth` parameter was added.
130                  *
131                  * @param array    $classes The CSS classes that are applied to the menu item's `<li>` element.
132                  * @param WP_Post  $item    The current menu item.
133                  * @param stdClass $args    An object of wp_nav_menu() arguments.
134                  * @param int      $depth   Depth of menu item. Used for padding.
135                  */
136                 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
137                 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
138
139                 /**
140                  * Filters the ID applied to a menu item's list item element.
141                  *
142                  * @since 3.0.1
143                  * @since 4.1.0 The `$depth` parameter was added.
144                  *
145                  * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
146                  * @param WP_Post  $item    The current menu item.
147                  * @param stdClass $args    An object of wp_nav_menu() arguments.
148                  * @param int      $depth   Depth of menu item. Used for padding.
149                  */
150                 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
151                 $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
152
153                 $output .= $indent . '<li' . $id . $class_names .'>';
154
155                 $atts = array();
156                 $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
157                 $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
158                 $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
159                 $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
160
161                 /**
162                  * Filters the HTML attributes applied to a menu item's anchor element.
163                  *
164                  * @since 3.6.0
165                  * @since 4.1.0 The `$depth` parameter was added.
166                  *
167                  * @param array $atts {
168                  *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
169                  *
170                  *     @type string $title  Title attribute.
171                  *     @type string $target Target attribute.
172                  *     @type string $rel    The rel attribute.
173                  *     @type string $href   The href attribute.
174                  * }
175                  * @param WP_Post  $item  The current menu item.
176                  * @param stdClass $args  An object of wp_nav_menu() arguments.
177                  * @param int      $depth Depth of menu item. Used for padding.
178                  */
179                 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
180
181                 $attributes = '';
182                 foreach ( $atts as $attr => $value ) {
183                         if ( ! empty( $value ) ) {
184                                 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
185                                 $attributes .= ' ' . $attr . '="' . $value . '"';
186                         }
187                 }
188
189                 /** This filter is documented in wp-includes/post-template.php */
190                 $title = apply_filters( 'the_title', $item->title, $item->ID );
191
192                 /**
193                  * Filters a menu item's title.
194                  *
195                  * @since 4.4.0
196                  *
197                  * @param string   $title The menu item's title.
198                  * @param WP_Post  $item  The current menu item.
199                  * @param stdClass $args  An object of wp_nav_menu() arguments.
200                  * @param int      $depth Depth of menu item. Used for padding.
201                  */
202                 $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
203
204                 $item_output = $args->before;
205                 $item_output .= '<a'. $attributes .'>';
206                 $item_output .= $args->link_before . $title . $args->link_after;
207                 $item_output .= '</a>';
208                 $item_output .= $args->after;
209
210                 /**
211                  * Filters a menu item's starting output.
212                  *
213                  * The menu item's starting output only includes `$args->before`, the opening `<a>`,
214                  * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
215                  * no filter for modifying the opening and closing `<li>` for a menu item.
216                  *
217                  * @since 3.0.0
218                  *
219                  * @param string   $item_output The menu item's starting HTML output.
220                  * @param WP_Post  $item        Menu item data object.
221                  * @param int      $depth       Depth of menu item. Used for padding.
222                  * @param stdClass $args        An object of wp_nav_menu() arguments.
223                  */
224                 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
225         }
226
227         /**
228          * Ends the element output, if needed.
229          *
230          * @since 3.0.0
231          *
232          * @see Walker::end_el()
233          *
234          * @param string   $output Passed by reference. Used to append additional content.
235          * @param WP_Post  $item   Page data object. Not used.
236          * @param int      $depth  Depth of page. Not Used.
237          * @param stdClass $args   An object of wp_nav_menu() arguments.
238          */
239         public function end_el( &$output, $item, $depth = 0, $args = array() ) {
240                 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
241                         $t = '';
242                         $n = '';
243                 } else {
244                         $t = "\t";
245                         $n = "\n";
246                 }
247                 $output .= "</li>{$n}";
248         }
249
250 } // Walker_Nav_Menu