|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Adapted from Edward McIntyre's wp_bootstrap_navwalker class. |
|
4
|
|
|
* Removed support for glyphicon and added support for Font Awesome. |
|
5
|
|
|
* |
|
6
|
|
|
* @package spurs |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
// Exit if accessed directly. |
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
11
|
|
|
exit; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
if ( ! class_exists( 'SpursWPBootstrapNavwalker' ) ) : |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class WP_Bootstrap_Navwalker |
|
18
|
|
|
* GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker |
|
19
|
|
|
* Description: A custom WordPress nav walker class to implement the Bootstrap 4 |
|
20
|
|
|
* navigation style in a custom theme using the WordPress built in menu manager. |
|
21
|
|
|
* Version: 2.0.4 |
|
22
|
|
|
* Author: Edward McIntyre - @twittem |
|
23
|
|
|
* License: GPL-2.0+ |
|
24
|
|
|
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
|
25
|
|
|
*/ |
|
26
|
|
|
class SpursWPBootstrapNavwalker extends Walker_Nav_Menu { |
|
27
|
|
|
/** |
|
28
|
|
|
* The starting level of the menu. |
|
29
|
|
|
* |
|
30
|
|
|
* @see Walker::start_lvl() |
|
31
|
|
|
* @since 3.0.0 |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $output Passed by reference. Used to append additional content. |
|
34
|
|
|
* @param int $depth Depth of page. Used for padding. |
|
35
|
|
|
* @param mixed $args Rest of arguments. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function start_lvl( &$output, $depth = 0, $args = array() ) { |
|
|
|
|
|
|
38
|
|
|
$indent = str_repeat( "\t", $depth ); |
|
39
|
|
|
$output .= "\n$indent<ul class=\" dropdown-menu\" role=\"menu\">\n"; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Open element. |
|
44
|
|
|
* |
|
45
|
|
|
* @see Walker::start_el() |
|
46
|
|
|
* @since 3.0.0 |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $output Passed by reference. Used to append additional content. |
|
49
|
|
|
* @param object $item Menu item data object. |
|
50
|
|
|
* @param int $depth Depth of menu item. Used for padding. |
|
51
|
|
|
* @param mixed $args Rest arguments. |
|
52
|
|
|
* @param int $id Element's ID. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
|
|
|
|
|
|
55
|
|
|
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
|
56
|
|
|
/** |
|
57
|
|
|
* Dividers, Headers or Disabled |
|
58
|
|
|
* ============================= |
|
59
|
|
|
* Determine whether the item is a Divider, Header, Disabled or regular |
|
60
|
|
|
* menu item. To prevent errors we use the strcasecmp() function to so a |
|
61
|
|
|
* comparison that is not case sensitive. The strcasecmp() function returns |
|
62
|
|
|
* a 0 if the strings are equal. |
|
63
|
|
|
*/ |
|
64
|
|
|
if ( 0 == strcasecmp( $item->attr_title, 'divider' ) && 1 === $depth ) { |
|
65
|
|
|
$output .= $indent . '<li class="dropdown-divider" role="presentation">'; |
|
66
|
|
|
} else if ( 0 == strcasecmp( $item->title, 'divider' ) && 1 === $depth ) { |
|
67
|
|
|
$output .= $indent . '<li class="dropdown-divider" role="presentation">'; |
|
68
|
|
|
} else if ( 0 == strcasecmp( $item->attr_title, 'dropdown-header' ) && 1 === $depth ) { |
|
69
|
|
|
$output .= $indent . '<li class="dropdown-header" role="presentation">' . esc_html( $item->title ); |
|
70
|
|
|
} else if ( 0 == strcasecmp( $item->attr_title, 'disabled' ) ) { |
|
71
|
|
|
$output .= $indent . '<li class="disabled" role="presentation"><a href="#">' . esc_html( $item->title ) . '</a>'; |
|
72
|
|
|
} else { |
|
73
|
|
|
$class_names = $value = ''; |
|
74
|
|
|
$classes = empty( $item->classes ) ? array() : ( array ) $item->classes; |
|
|
|
|
|
|
75
|
|
|
$classes[] = 'nav-item menu-item-' . $item->ID; |
|
76
|
|
|
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) ); |
|
77
|
|
|
/* |
|
78
|
|
|
if ( $args->has_children ) |
|
79
|
|
|
$class_names .= ' dropdown'; |
|
80
|
|
|
*/ |
|
81
|
|
|
if ( $args->has_children && 0 === $depth ) { |
|
82
|
|
|
$class_names .= ' dropdown'; |
|
83
|
|
|
} elseif ( $args->has_children && $depth > 0 ) { |
|
84
|
|
|
$class_names .= ' dropdown-submenu'; |
|
85
|
|
|
} |
|
86
|
|
|
if ( in_array( 'current-menu-item', $classes ) ) { |
|
87
|
|
|
$class_names .= ' active'; |
|
88
|
|
|
} |
|
89
|
|
|
// remove Font Awesome icon from classes array and save the icon |
|
90
|
|
|
// we will add the icon back in via a <span> below so it aligns with |
|
91
|
|
|
// the menu item |
|
92
|
|
|
if ( in_array( 'fa', $classes ) ) { |
|
93
|
|
|
$key = array_search( 'fa', $classes ); |
|
94
|
|
|
$icon = $classes[$key + 1]; |
|
95
|
|
|
$class_names = str_replace( $classes[$key + 1], '', $class_names ); |
|
96
|
|
|
$class_names = str_replace( $classes[$key], '', $class_names ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
|
100
|
|
|
$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args ); |
|
|
|
|
|
|
101
|
|
|
$id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
|
|
|
|
|
|
102
|
|
|
$output .= $indent . '<li' . $id . $value . $class_names . '>'; |
|
103
|
|
|
$atts = array(); |
|
104
|
|
|
if ( empty( $item->attr_title ) ) { |
|
105
|
|
|
$atts['title'] = ! empty( $item->title ) ? strip_tags( $item->title ) : ''; |
|
106
|
|
|
} else { |
|
107
|
|
|
$atts['title'] = $item->attr_title; |
|
108
|
|
|
} |
|
109
|
|
|
$atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
|
110
|
|
|
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
|
111
|
|
|
// If item has_children add atts to a. |
|
112
|
|
|
|
|
113
|
|
|
if ( $args->has_children && 0 === $depth ) { |
|
114
|
|
|
$atts['href'] = '#'; |
|
115
|
|
|
$atts['data-toggle'] = 'dropdown'; |
|
116
|
|
|
$atts['class'] = 'nav-link dropdown-toggle'; |
|
117
|
|
|
} else { |
|
118
|
|
|
$atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
|
119
|
|
|
$atts['class'] = 'nav-link'; |
|
120
|
|
|
} |
|
121
|
|
|
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args ); |
|
122
|
|
|
$attributes = ''; |
|
123
|
|
|
foreach ( $atts as $attr => $value ) { |
|
124
|
|
|
if ( ! empty( $value ) ) { |
|
125
|
|
|
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
|
126
|
|
|
$attributes .= ' ' . $attr . '="' . $value . '"'; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
$item_output = $args->before; |
|
130
|
|
|
// Font Awesome icons |
|
131
|
|
|
if ( ! empty( $icon ) ) { |
|
132
|
|
|
$item_output .= '<a' . $attributes . '><span class="fa ' . esc_attr( $icon ) . '"></span> '; |
|
133
|
|
|
} else { |
|
134
|
|
|
$item_output .= '<a' . $attributes . '>'; |
|
135
|
|
|
} |
|
136
|
|
|
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; |
|
137
|
|
|
$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>'; |
|
138
|
|
|
$item_output .= $args->after; |
|
139
|
|
|
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Traverse elements to create list from elements. |
|
145
|
|
|
* |
|
146
|
|
|
* Display one element if the element doesn't have any children otherwise, |
|
147
|
|
|
* display the element and its children. Will only traverse up to the max |
|
148
|
|
|
* depth and no ignore elements under that depth. |
|
149
|
|
|
* |
|
150
|
|
|
* This method shouldn't be called directly, use the walk() method instead. |
|
151
|
|
|
* |
|
152
|
|
|
* @see Walker::start_el() |
|
153
|
|
|
* @since 2.5.0 |
|
154
|
|
|
* |
|
155
|
|
|
* @param object $element Data object |
|
156
|
|
|
* @param array $children_elements List of elements to continue traversing. |
|
157
|
|
|
* @param int $max_depth Max depth to traverse. |
|
158
|
|
|
* @param int $depth Depth of current element. |
|
159
|
|
|
* @param array $args |
|
160
|
|
|
* @param string $output Passed by reference. Used to append additional content. |
|
161
|
|
|
* |
|
162
|
|
|
* @return null Null on failure with no changes to parameters. |
|
163
|
|
|
*/ |
|
164
|
|
|
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
|
|
|
|
|
|
165
|
|
|
if ( ! $element ) { |
|
166
|
|
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
$id_field = $this->db_fields['id']; |
|
169
|
|
|
// Display this element. |
|
170
|
|
|
if ( is_object( $args[0] ) ) { |
|
171
|
|
|
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] ); |
|
172
|
|
|
} |
|
173
|
|
|
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Menu Fallback |
|
178
|
|
|
* ============= |
|
179
|
|
|
* If this function is assigned to the wp_nav_menu's fallback_cb variable |
|
180
|
|
|
* and a manu has not been assigned to the theme location in the WordPress |
|
181
|
|
|
* menu manager the function with display nothing to a non-logged in user, |
|
182
|
|
|
* and will add a link to the WordPress menu manager if logged in as an admin. |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $args passed from the wp_nav_menu function. |
|
185
|
|
|
* |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function fallback( $args ) { |
|
|
|
|
|
|
188
|
|
|
if ( current_user_can( 'manage_options' ) ) { |
|
189
|
|
|
extract( $args ); |
|
|
|
|
|
|
190
|
|
|
$fb_output = null; |
|
191
|
|
|
if ( $container ) { |
|
192
|
|
|
$fb_output = '<' . $container; |
|
193
|
|
|
if ( $container_class ) { |
|
194
|
|
|
$fb_output .= ' class="' . $container_class . '"'; |
|
195
|
|
|
} |
|
196
|
|
|
if ( $container_id ) { |
|
197
|
|
|
$fb_output .= ' id="' . $container_id . '"'; |
|
198
|
|
|
} |
|
199
|
|
|
$fb_output .= '>'; |
|
200
|
|
|
} |
|
201
|
|
|
$fb_output .= '<ul'; |
|
202
|
|
|
if ( $menu_class ) { |
|
203
|
|
|
$fb_output .= ' class="' . $menu_class . '"'; |
|
204
|
|
|
} |
|
205
|
|
|
if ( $menu_id ) { |
|
206
|
|
|
$fb_output .= ' id="' . $menu_id . '"'; |
|
207
|
|
|
} |
|
208
|
|
|
$fb_output .= '>'; |
|
209
|
|
|
$fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>'; |
|
210
|
|
|
$fb_output .= '</ul>'; |
|
211
|
|
|
if ( $container ) { |
|
212
|
|
|
$fb_output .= '</' . $container . '>'; |
|
213
|
|
|
} |
|
214
|
|
|
echo $fb_output; |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
endif; /* End if class exists */ |
|
220
|
|
|
|