forked from macdonaldr93/yamm-nav-walker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamm-nav-walker.php
256 lines (205 loc) · 7.57 KB
/
yamm-nav-walker.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
<?php
/**
* Class Name: Yamm_Nav_Walker & Yamm_Fw_Nav_Walker
* GitHub URI: https://github.com/macdonaldr93/yamm-nav-walker
* Description: A custom WordPress nav walker class to implement the Yamm 3 bootstrap mega menu navigation style in a custom theme using the WordPress built in menu manager.
* Version: 1.0.0
* Author: Ryan Macdonald - @macdonaldr93
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
class Yamm_Nav_Walker extends Walker_Nav_Menu
{
function check_current($classes)
{
return preg_match('/(current[-_])|active|dropdown/', $classes);
}
function start_lvl(&$output, $depth = 0, $args = array())
{
$output .= ($depth == 0) ? "\n<ul class=\"dropdown-menu\">\n" . "\n<div class=\"yamm-content\">\n" . "\n<div class=\"row\">\n" : "\n<ul class=\"elementy-ul yamm-fw\">\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
{
$item_html = '';
parent::start_el($item_html, $item, $depth, $args);
if ($item->is_dropdown && ($depth === 0))
{
$item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html);
$item_html = str_replace('</a>', ' <b class="caret"></b></a>', $item_html);
}
elseif (stristr($item_html, 'li class="divider'))
{
$item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html);
}
elseif (stristr($item_html, 'li class="dropdown-header'))
{
$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html);
}
$item_html = apply_filters('roots_wp_nav_menu_item', $item_html);
$output .= $item_html;
}
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
{
$element->is_dropdown = ((!empty($children_elements[$element->ID]) && (($depth + 1) < $max_depth || ($max_depth === 0))));
if ($element->is_dropdown)
{
$element->classes[] = 'dropdown';
}
if ($element && ($depth === 1))
{
$element->classes[] = 'col-sm-4 menu-col';
}
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
}
/**
* Remove the id="" on nav menu items
* Return 'menu-slug' for nav menu classes
*/
function yamm_roots_nav_menu_css_class($classes, $item)
{
$slug = sanitize_title($item->title);
$classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes);
$classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);
$classes[] = 'menu-' . $slug;
$classes = array_unique($classes);
return array_filter($classes);
}
add_filter('nav_menu_css_class', 'yamm_roots_nav_menu_css_class', 10, 2);
add_filter('nav_menu_item_id', '__return_null');
/**
* Clean up wp_nav_menu_args
*
* Remove the container
* Use Yamm_Nav_Walker() by default
*/
function yamm_roots_nav_menu_args($args = '')
{
$yamm_roots_nav_menu_args['container'] = false;
if (!$args['items_wrap'])
{
$yamm_roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
}
if (current_theme_supports('bootstrap-top-navbar') && !$args['depth'])
{
$yamm_roots_nav_menu_args['depth'] = 3;
}
if (!$args['walker'])
{
$yamm_roots_nav_menu_args['walker'] = new Yamm_Nav_Walker();
}
return array_merge($args, $yamm_roots_nav_menu_args);
}
add_filter('wp_nav_menu_args', 'yamm_roots_nav_menu_args');
/**
* Fallback to simple list
*/
function Yamm_Nav_Walker_menu_fallback()
{
$fb_output = '<ul class="nav navbar-nav">';
$fb_output .= '<li><a href="' . esc_url(home_url()) . '">' . __('Home', THEMETD) . '</a></li>';
if (current_user_can('manage_options'))
{
$fb_output .= '<li><a href="' . admin_url('nav-menus.php') . '">' . __('Add a menu', THEMETD) . '</a></li>';
}
$fb_output .= '</ul>';
echo $fb_output;
}
class Yamm_Fw_Nav_Walker extends Walker_Nav_Menu
{
function check_current($classes)
{
return preg_match('/(current[-_])|active|dropdown/', $classes);
}
function start_lvl(&$output, $depth = 0, $args = array())
{
$output .= ($depth == 0) ? "\n<ul class=\"dropdown-menu\">\n" . "\n<div class=\"yamm-content\">\n" . "\n<div class=\"row\">\n" : "\n<ul class=\"elementy-ul yamm-fw\">\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
{
$item_html = '';
parent::start_el($item_html, $item, $depth, $args);
if ($item->is_dropdown && ($depth === 0))
{
$item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html);
$item_html = str_replace('</a>', ' <b class="caret"></b></a>', $item_html);
}
elseif (stristr($item_html, 'li class="divider'))
{
$item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html);
}
elseif (stristr($item_html, 'li class="dropdown-header'))
{
$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html);
}
$item_html = apply_filters('roots_wp_nav_menu_item', $item_html);
$output .= $item_html;
}
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
{
$element->is_dropdown = ((!empty($children_elements[$element->ID]) && (($depth + 1) < $max_depth || ($max_depth === 0))));
if ($element->is_dropdown)
{
$element->classes[] = 'dropdown yamm-fw';
}
if ($element && ($depth === 1))
{
$element->classes[] = 'col-sm-4 menu-col';
}
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
}
/**
* Remove the id="" on nav menu items
* Return 'menu-slug' for nav menu classes
*/
function yamm_fw_roots_nav_menu_css_class($classes, $item)
{
$slug = sanitize_title($item->title);
$classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes);
$classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);
$classes[] = 'menu-' . $slug;
$classes = array_unique($classes);
return array_filter($classes);
}
add_filter('nav_menu_css_class', 'yamm_fw_roots_nav_menu_css_class', 10, 2);
add_filter('nav_menu_item_id', '__return_null');
/**
* Clean up wp_nav_menu_args
*
* Remove the container
* Use Yamm_Fw_Nav_Walker() by default
*/
function yamm_fw_roots_nav_menu_args($args = '')
{
$yamm_fw_roots_nav_menu_args['container'] = false;
if (!$args['items_wrap'])
{
$yamm_fw_roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
}
if (current_theme_supports('bootstrap-top-navbar') && !$args['depth'])
{
$yamm_fw_roots_nav_menu_args['depth'] = 3;
}
if (!$args['walker'])
{
$yamm_fw_roots_nav_menu_args['walker'] = new Yamm_Fw_Nav_Walker();
}
return array_merge($args, $yamm_fw_roots_nav_menu_args);
}
add_filter('wp_nav_menu_args', 'yamm_fw_roots_nav_menu_args');
/**
* Fallback to simple list
*/
function Yamm_Fw_Nav_Walker_menu_fallback()
{
$fb_output = '<ul class="nav navbar-nav">';
$fb_output .= '<li><a href="' . esc_url(home_url()) . '">' . __('Home', THEMETD) . '</a></li>';
if (current_user_can('manage_options'))
{
$fb_output .= '<li><a href="' . admin_url('nav-menus.php') . '">' . __('Add a menu', THEMETD) . '</a></li>';
}
$fb_output .= '</ul>';
echo $fb_output;
}
?>