-
Notifications
You must be signed in to change notification settings - Fork 25
/
bootstrap-navwalker.php
418 lines (365 loc) · 13.6 KB
/
bootstrap-navwalker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/**
* Bootstrap NavWalker
* Class Name: Bootstrap_NavWalker
* Author: Bishal Napit
* Author URI: https://napitwptech.com/
* GitHub URI: https://github.com/mebishalnapit/bootstrap-navwalker/
* Description: A custom WordPress nav walker class to implement the Bootstrap 4 navigation style in a custom WordPress
* Bootstrap based theme using the WordPress built in menu manager.
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
class Bootstrap_NavWalker extends Walker_Nav_Menu {
// Create the $current_menu_id_bootstrap variable for generating the current menu id
protected $current_menu_id_bootstrap;
/**
* Starts the list before the elements are added.
*
* @since 3.0.0
* @see Walker::start_lvl()
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
// Use the current menu id generated via start_el()
$current_menu_id = $this->current_menu_id_bootstrap;
// Assign the dynamic id for use inside the dropdown menu, ie, sub-menu for Bootstrap
$id = 'aria-labelledby="navbar-dropdown-menu-link-' . $current_menu_id->ID . '"';
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
/**
* Add the classes for the dropdown menu in WordPress
*
* 1. For WordPress default: '.sub-menu'
* 2. For Bootstrap Sub-Menu: '.dropdown-menu'
*/
$classes = array( 'sub-menu', 'dropdown-menu' );
/**
* Filters the CSS class(es) applied to a menu list element.
*
* @since 4.8.0
*
* @param array $classes The CSS classes that are applied to the menu `<ul>` element.
* @param stdClass $args An object of `wp_nav_menu()` arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
/**
* Change <ul> to <div> for Bootstrap Navigation
* Add the current menu id for the sub-menu toggle feature for Bootstrap
*/
$output .= "{$n}{$indent}<div $class_names $id>{$n}";
}
/**
* Ends the list of after the elements are added.
*
* @since 3.0.0
* @see Walker::end_lvl()
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
*/
public function end_lvl( &$output, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
/**
* Change </ul> to </div> for Bootstrap Navigation
*/
$output .= "$indent</div>{$n}";
}
/**
* Starts the element output.
*
* @since 3.0.0
* @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
* @see Walker::start_el()
*
* @param string $output Passed by reference. Used to append additional content.
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $id Current item ID.
*/
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
// Find the current menu item id to be used for start_lvl()
$this->current_menu_id_bootstrap = $item;
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
$classes = empty( $item->classes ) ? array() : ( array ) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
/**
* Add class '.nav-item' inside <li> tag for Bootstrap
*/
$classes[] = 'nav-item';
/**
* Add class '.active' inside <li> tag for Bootstrap active menu as well as for the parent menu, which have the active sub-menu
*/
if ( in_array( 'current-menu-item', $classes ) || in_array( 'current-menu-parent', $classes ) ) {
$classes[] = 'active';
}
/**
* Add class '.dropdown' inside <li> tag for Bootstrap dropdown menu, ie, <li> having sub-menu
*/
if ( in_array( 'menu-item-has-children', $classes ) ) {
$classes[] = 'dropdown';
}
/**
* Filters the arguments for a single nav menu item.
*
* @since 4.4.0
*
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
*/
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
/**
* Filters the CSS class(es) applied to a menu item's list item element.
*
* @since 3.0.0
* @since 4.1.0 The `$depth` parameter was added.
*
* @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
/**
* Filters the ID applied to a menu item's list item element.
*
* @since 3.0.1
* @since 4.1.0 The `$depth` parameter was added.
*
* @param string $menu_id The ID that is applied to the menu item's `<li>` element.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
/**
* <li> is required for parent menu only in Bootstrap
*/
if ( $depth === 0 ) {
$output .= $indent . '<li' . $id . $class_names . '>';
}
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
/**
* Add '.nav-link' class for <a> in parent menu for Bootstrap
*/
if ( $depth === 0 ) {
$atts['class'] = 'nav-link';
}
/**
* Add the attributes for <a> in parent menu
*
* 1. Add '.dropdown-toggle' class for <a> in parent menu if it has sub-menu as required for Bootstrap
* 2. Add '.dropdown' as 'data-toggle' attribute in <a> in parent menu if it has sub-menu as required for Bootstrap
* 3. Add the current menu id attribute to indicate the exact menu to toggle for set in sub-menu div
* 4. Add the attribute of 'true' for 'aria-haspopup' in parent menu to indicate it has sub-menus
* 5. Add the attribute of 'false' for 'aria-expanded' in parent menu to indicate the sub-menus is hidden by default
* 6. Add the '#' link in the <a> tag in the parent menu if it has sub-menu as required for Bootstrap
*/
if ( $depth === 0 && in_array( 'menu-item-has-children', $classes ) ) {
$atts['class'] .= ' dropdown-toggle';
$atts['data-toggle'] = 'dropdown';
$atts['id'] = 'navbar-dropdown-menu-link-' . $item->ID;
$atts['aria-haspopup'] = "true";
$atts['aria-expanded'] = "false";
$atts['href'] = '#';
}
/**
* Add the attributes for <a> in sub-menu
* 1. Add '.dropdown-item' class for <a> inside sub-menu for Bootstrap
* 2. Add the current menu id attribute if you want to style the menu differently
*/
if ( $depth > 0 ) {
$atts['class'] = 'dropdown-item';
$atts['id'] = 'menu-item-' . $item->ID;
}
/**
* Add '.active' class inside <a> in sub-menu for Bootstrap
*/
if ( in_array( 'current-menu-item', $item->classes ) ) {
$atts['class'] .= ' active';
}
/**
* Add '.disabled' class for <a> in menu for Bootstrap disabled class
*/
if ( in_array( 'disabled', $item->classes ) ) {
$atts['class'] .= ' disabled';
}
/**
* Filters the HTML attributes applied to a menu item's anchor element.
*
* @since 3.6.0
* @since 4.1.0 The `$depth` parameter was added.
*
* @param array $atts {
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
*
* @type string $title Title attribute.
* @type string $target Target attribute.
* @type string $rel The rel attribute.
* @type string $href The href attribute.
* }
*
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
/**
* If '.disabled' class is added to the menu, add the url of '#' in it
*/
if ( in_array( 'disabled', $item->classes ) ) {
$value = ( 'href' === $attr ) ? esc_url( '#' ) : esc_attr( $value );
}
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( 'the_title', $item->title, $item->ID );
/**
* Filters a menu item's title.
*
* @since 4.4.0
*
* @param string $title The menu item's title.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
/**
* Filters a menu item's starting output.
* The menu item's starting output only includes `$args->before`, the opening `<a>`,
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
* no filter for modifying the opening and closing `<li>` for a menu item.
*
* @since 3.0.0
*
* @param string $item_output The menu item's starting HTML output.
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
*/
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
/**
* Ends the element output, if needed.
*
* @since 3.0.0
* @see Walker::end_el()
*
* @param string $output Passed by reference. Used to append additional content.
* @param WP_Post $item Page data object. Not used.
* @param int $depth Depth of page. Not Used.
* @param stdClass $args An object of wp_nav_menu() arguments.
*/
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
/**
* <li> is required for parent menu only in Bootstrap
*/
if ( $depth === 0 ) {
$output .= "</li>{$n}";
}
}
/**
* Fallback menu
* If you assign the fallback menu for your custom menu setup via wp_nav_menu function, then, this function will be
* rendered if no menu is assigned to that menu location. You need to assign it via 'fallback_cb' array. Also, this
* will be only rendered to the logged in users pointing them to the menu manager url.
*
* @param $args Arrays passed from the wp_nav_menu function
*/
public static function fallback( $args ) {
if ( current_user_can( 'edit_theme_options' ) ) {
$container = $args['container'];
$container_id = $args['container_id'];
$container_class = $args['container_class'];
$menu_class = $args['menu_class'];
$menu_id = $args['menu_id'];
// If there is container render it
if ( $container ) {
echo '<' . esc_attr( $container );
// If container id is set render it
if ( $container_id ) {
echo ' id="' . esc_attr( $container_id ) . '"';
}
// If container class is set render it
if ( $container_class ) {
echo ' class="' . esc_attr( $container_class ) . '"';
}
echo '>';
}
// Default wrapper for menu is <ul>
echo '<ul';
// If menu id has been set render it
if ( $menu_id ) {
echo ' id="' . esc_attr( $menu_id ) . '"';
}
// If menu class has been set render it
if ( $menu_class ) {
echo ' class="' . esc_attr( $menu_class ) . '"';
}
// Close <ul> div wrapper for menu
echo '>';
// Display the link to Add New Menu
echo '<li class="nav-item active"><a class="nav-link" href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">';
esc_html_e( 'Add a menu', 'theme-textdomain' );
echo '</a></li>';
// Close the main <ul>
echo '</ul>';
// Close the main container div
if ( $container ) {
echo '</' . esc_attr( $container ) . '>';
}
}
}
}