0
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Navigation Menu template functions |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Nav_Menus |
|
7 |
* @since 3.0.0 |
|
8 |
*/ |
|
9 |
|
|
10 |
/** |
|
11 |
* Create HTML list of nav menu items. |
|
12 |
* |
|
13 |
* @since 3.0.0 |
|
14 |
* @uses Walker |
|
15 |
*/ |
|
16 |
class Walker_Nav_Menu extends Walker { |
|
17 |
/** |
|
18 |
* What the class handles. |
|
19 |
* |
|
20 |
* @see Walker::$tree_type |
|
21 |
* @since 3.0.0 |
|
22 |
* @var string |
|
23 |
*/ |
5
|
24 |
public $tree_type = array( 'post_type', 'taxonomy', 'custom' ); |
0
|
25 |
|
|
26 |
/** |
|
27 |
* Database fields to use. |
|
28 |
* |
|
29 |
* @see Walker::$db_fields |
|
30 |
* @since 3.0.0 |
|
31 |
* @todo Decouple this. |
|
32 |
* @var array |
|
33 |
*/ |
5
|
34 |
public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' ); |
0
|
35 |
|
|
36 |
/** |
|
37 |
* Starts the list before the elements are added. |
|
38 |
* |
|
39 |
* @see Walker::start_lvl() |
|
40 |
* |
|
41 |
* @since 3.0.0 |
|
42 |
* |
|
43 |
* @param string $output Passed by reference. Used to append additional content. |
|
44 |
* @param int $depth Depth of menu item. Used for padding. |
|
45 |
* @param array $args An array of arguments. @see wp_nav_menu() |
|
46 |
*/ |
5
|
47 |
public function start_lvl( &$output, $depth = 0, $args = array() ) { |
0
|
48 |
$indent = str_repeat("\t", $depth); |
|
49 |
$output .= "\n$indent<ul class=\"sub-menu\">\n"; |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* Ends the list of after the elements are added. |
|
54 |
* |
|
55 |
* @see Walker::end_lvl() |
|
56 |
* |
|
57 |
* @since 3.0.0 |
|
58 |
* |
|
59 |
* @param string $output Passed by reference. Used to append additional content. |
|
60 |
* @param int $depth Depth of menu item. Used for padding. |
|
61 |
* @param array $args An array of arguments. @see wp_nav_menu() |
|
62 |
*/ |
5
|
63 |
public function end_lvl( &$output, $depth = 0, $args = array() ) { |
0
|
64 |
$indent = str_repeat("\t", $depth); |
|
65 |
$output .= "$indent</ul>\n"; |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* Start the element output. |
|
70 |
* |
|
71 |
* @see Walker::start_el() |
|
72 |
* |
|
73 |
* @since 3.0.0 |
|
74 |
* |
|
75 |
* @param string $output Passed by reference. Used to append additional content. |
|
76 |
* @param object $item Menu item data object. |
|
77 |
* @param int $depth Depth of menu item. Used for padding. |
|
78 |
* @param array $args An array of arguments. @see wp_nav_menu() |
|
79 |
* @param int $id Current item ID. |
|
80 |
*/ |
5
|
81 |
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
0
|
82 |
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
|
83 |
|
|
84 |
$classes = empty( $item->classes ) ? array() : (array) $item->classes; |
|
85 |
$classes[] = 'menu-item-' . $item->ID; |
|
86 |
|
|
87 |
/** |
5
|
88 |
* Filter the CSS class(es) applied to a menu item's list item element. |
0
|
89 |
* |
|
90 |
* @since 3.0.0 |
5
|
91 |
* @since 4.1.0 The `$depth` parameter was added. |
0
|
92 |
* |
5
|
93 |
* @param array $classes The CSS classes that are applied to the menu item's `<li>` element. |
0
|
94 |
* @param object $item The current menu item. |
5
|
95 |
* @param array $args An array of {@see wp_nav_menu()} arguments. |
|
96 |
* @param int $depth Depth of menu item. Used for padding. |
0
|
97 |
*/ |
5
|
98 |
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) ); |
0
|
99 |
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
|
100 |
|
|
101 |
/** |
5
|
102 |
* Filter the ID applied to a menu item's list item element. |
0
|
103 |
* |
|
104 |
* @since 3.0.1 |
5
|
105 |
* @since 4.1.0 The `$depth` parameter was added. |
0
|
106 |
* |
5
|
107 |
* @param string $menu_id The ID that is applied to the menu item's `<li>` element. |
|
108 |
* @param object $item The current menu item. |
|
109 |
* @param array $args An array of {@see wp_nav_menu()} arguments. |
|
110 |
* @param int $depth Depth of menu item. Used for padding. |
0
|
111 |
*/ |
5
|
112 |
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth ); |
0
|
113 |
$id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
|
114 |
|
5
|
115 |
$output .= $indent . '<li' . $id . $class_names .'>'; |
0
|
116 |
|
|
117 |
$atts = array(); |
|
118 |
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
|
119 |
$atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
|
120 |
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
|
121 |
$atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
|
122 |
|
|
123 |
/** |
5
|
124 |
* Filter the HTML attributes applied to a menu item's anchor element. |
0
|
125 |
* |
|
126 |
* @since 3.6.0 |
5
|
127 |
* @since 4.1.0 The `$depth` parameter was added. |
0
|
128 |
* |
|
129 |
* @param array $atts { |
5
|
130 |
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. |
0
|
131 |
* |
5
|
132 |
* @type string $title Title attribute. |
|
133 |
* @type string $target Target attribute. |
0
|
134 |
* @type string $rel The rel attribute. |
|
135 |
* @type string $href The href attribute. |
|
136 |
* } |
5
|
137 |
* @param object $item The current menu item. |
|
138 |
* @param array $args An array of {@see wp_nav_menu()} arguments. |
|
139 |
* @param int $depth Depth of menu item. Used for padding. |
0
|
140 |
*/ |
5
|
141 |
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); |
0
|
142 |
|
|
143 |
$attributes = ''; |
|
144 |
foreach ( $atts as $attr => $value ) { |
|
145 |
if ( ! empty( $value ) ) { |
|
146 |
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
|
147 |
$attributes .= ' ' . $attr . '="' . $value . '"'; |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
$item_output = $args->before; |
|
152 |
$item_output .= '<a'. $attributes .'>'; |
|
153 |
/** This filter is documented in wp-includes/post-template.php */ |
|
154 |
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; |
|
155 |
$item_output .= '</a>'; |
|
156 |
$item_output .= $args->after; |
|
157 |
|
|
158 |
/** |
|
159 |
* Filter a menu item's starting output. |
|
160 |
* |
5
|
161 |
* The menu item's starting output only includes `$args->before`, the opening `<a>`, |
|
162 |
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is |
|
163 |
* no filter for modifying the opening and closing `<li>` for a menu item. |
0
|
164 |
* |
|
165 |
* @since 3.0.0 |
|
166 |
* |
|
167 |
* @param string $item_output The menu item's starting HTML output. |
|
168 |
* @param object $item Menu item data object. |
|
169 |
* @param int $depth Depth of menu item. Used for padding. |
5
|
170 |
* @param array $args An array of {@see wp_nav_menu()} arguments. |
0
|
171 |
*/ |
|
172 |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
|
173 |
} |
|
174 |
|
|
175 |
/** |
|
176 |
* Ends the element output, if needed. |
|
177 |
* |
|
178 |
* @see Walker::end_el() |
|
179 |
* |
|
180 |
* @since 3.0.0 |
|
181 |
* |
|
182 |
* @param string $output Passed by reference. Used to append additional content. |
|
183 |
* @param object $item Page data object. Not used. |
|
184 |
* @param int $depth Depth of page. Not Used. |
|
185 |
* @param array $args An array of arguments. @see wp_nav_menu() |
|
186 |
*/ |
5
|
187 |
public function end_el( &$output, $item, $depth = 0, $args = array() ) { |
0
|
188 |
$output .= "</li>\n"; |
|
189 |
} |
|
190 |
|
|
191 |
} // Walker_Nav_Menu |
|
192 |
|
|
193 |
/** |
|
194 |
* Displays a navigation menu. |
|
195 |
* |
|
196 |
* @since 3.0.0 |
|
197 |
* |
5
|
198 |
* @param array $args { |
|
199 |
* Optional. Array of nav menu arguments. |
|
200 |
* |
|
201 |
* @type string $menu Desired menu. Accepts (matching in order) id, slug, name. Default empty. |
|
202 |
* @type string $menu_class CSS class to use for the ul element which forms the menu. Default 'menu'. |
|
203 |
* @type string $menu_id The ID that is applied to the ul element which forms the menu. |
|
204 |
* Default is the menu slug, incremented. |
|
205 |
* @type string $container Whether to wrap the ul, and what to wrap it with. Default 'div'. |
|
206 |
* @type string $container_class Class that is applied to the container. Default 'menu-{menu slug}-container'. |
|
207 |
* @type string $container_id The ID that is applied to the container. Default empty. |
|
208 |
* @type callback|bool $fallback_cb If the menu doesn't exists, a callback function will fire. |
|
209 |
* Default is 'wp_page_menu'. Set to false for no fallback. |
|
210 |
* @type string $before Text before the link text. Default empty. |
|
211 |
* @type string $after Text after the link text. Default empty. |
|
212 |
* @type string $link_before Text before the link. Default empty. |
|
213 |
* @type string $link_after Text after the link. Default empty. |
|
214 |
* @type bool $echo Whether to echo the menu or return it. Default true. |
|
215 |
* @type int $depth How many levels of the hierarchy are to be included. 0 means all. Default 0. |
|
216 |
* @type object $walker Instance of a custom walker class. Default empty. |
|
217 |
* @type string $theme_location Theme location to be used. Must be registered with register_nav_menu() |
|
218 |
* in order to be selectable by the user. |
|
219 |
* @type string $items_wrap How the list items should be wrapped. Default is a ul with an id and class. |
|
220 |
* Uses printf() format with numbered placeholders. |
|
221 |
* } |
|
222 |
* @return mixed Menu output if $echo is false, false if there are no items or no menu was found. |
0
|
223 |
*/ |
|
224 |
function wp_nav_menu( $args = array() ) { |
|
225 |
static $menu_id_slugs = array(); |
|
226 |
|
|
227 |
$defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', |
|
228 |
'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', |
|
229 |
'depth' => 0, 'walker' => '', 'theme_location' => '' ); |
|
230 |
|
|
231 |
$args = wp_parse_args( $args, $defaults ); |
|
232 |
/** |
|
233 |
* Filter the arguments used to display a navigation menu. |
|
234 |
* |
|
235 |
* @since 3.0.0 |
|
236 |
* |
5
|
237 |
* @see wp_nav_menu() |
|
238 |
* |
|
239 |
* @param array $args Array of wp_nav_menu() arguments. |
0
|
240 |
*/ |
|
241 |
$args = apply_filters( 'wp_nav_menu_args', $args ); |
|
242 |
$args = (object) $args; |
|
243 |
|
5
|
244 |
/** |
|
245 |
* Filter whether to short-circuit the wp_nav_menu() output. |
|
246 |
* |
|
247 |
* Returning a non-null value to the filter will short-circuit |
|
248 |
* wp_nav_menu(), echoing that value if $args->echo is true, |
|
249 |
* returning that value otherwise. |
|
250 |
* |
|
251 |
* @since 3.9.0 |
|
252 |
* |
|
253 |
* @see wp_nav_menu() |
|
254 |
* |
|
255 |
* @param string|null $output Nav menu output to short-circuit with. Default null. |
|
256 |
* @param object $args An object containing wp_nav_menu() arguments. |
|
257 |
*/ |
|
258 |
$nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args ); |
|
259 |
|
|
260 |
if ( null !== $nav_menu ) { |
|
261 |
if ( $args->echo ) { |
|
262 |
echo $nav_menu; |
|
263 |
return; |
|
264 |
} |
|
265 |
|
|
266 |
return $nav_menu; |
|
267 |
} |
|
268 |
|
0
|
269 |
// Get the nav menu based on the requested menu |
|
270 |
$menu = wp_get_nav_menu_object( $args->menu ); |
|
271 |
|
|
272 |
// Get the nav menu based on the theme_location |
|
273 |
if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) ) |
|
274 |
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); |
|
275 |
|
|
276 |
// get the first menu that has items if we still can't find a menu |
|
277 |
if ( ! $menu && !$args->theme_location ) { |
|
278 |
$menus = wp_get_nav_menus(); |
|
279 |
foreach ( $menus as $menu_maybe ) { |
|
280 |
if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) { |
|
281 |
$menu = $menu_maybe; |
|
282 |
break; |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
|
|
287 |
// If the menu exists, get its items. |
|
288 |
if ( $menu && ! is_wp_error($menu) && !isset($menu_items) ) |
|
289 |
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); |
|
290 |
|
|
291 |
/* |
|
292 |
* If no menu was found: |
|
293 |
* - Fall back (if one was specified), or bail. |
|
294 |
* |
|
295 |
* If no menu items were found: |
|
296 |
* - Fall back, but only if no theme location was specified. |
|
297 |
* - Otherwise, bail. |
|
298 |
*/ |
|
299 |
if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) ) |
|
300 |
&& $args->fallback_cb && is_callable( $args->fallback_cb ) ) |
|
301 |
return call_user_func( $args->fallback_cb, (array) $args ); |
|
302 |
|
|
303 |
if ( ! $menu || is_wp_error( $menu ) ) |
|
304 |
return false; |
|
305 |
|
|
306 |
$nav_menu = $items = ''; |
|
307 |
|
|
308 |
$show_container = false; |
|
309 |
if ( $args->container ) { |
|
310 |
/** |
|
311 |
* Filter the list of HTML tags that are valid for use as menu containers. |
|
312 |
* |
|
313 |
* @since 3.0.0 |
|
314 |
* |
5
|
315 |
* @param array $tags The acceptable HTML tags for use as menu containers. |
|
316 |
* Default is array containing 'div' and 'nav'. |
0
|
317 |
*/ |
|
318 |
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); |
|
319 |
if ( in_array( $args->container, $allowed_tags ) ) { |
|
320 |
$show_container = true; |
|
321 |
$class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"'; |
|
322 |
$id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : ''; |
|
323 |
$nav_menu .= '<'. $args->container . $id . $class . '>'; |
|
324 |
} |
|
325 |
} |
|
326 |
|
|
327 |
// Set up the $menu_item variables |
|
328 |
_wp_menu_item_classes_by_context( $menu_items ); |
|
329 |
|
|
330 |
$sorted_menu_items = $menu_items_with_children = array(); |
|
331 |
foreach ( (array) $menu_items as $menu_item ) { |
|
332 |
$sorted_menu_items[ $menu_item->menu_order ] = $menu_item; |
|
333 |
if ( $menu_item->menu_item_parent ) |
|
334 |
$menu_items_with_children[ $menu_item->menu_item_parent ] = true; |
|
335 |
} |
|
336 |
|
|
337 |
// Add the menu-item-has-children class where applicable |
|
338 |
if ( $menu_items_with_children ) { |
|
339 |
foreach ( $sorted_menu_items as &$menu_item ) { |
|
340 |
if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) |
|
341 |
$menu_item->classes[] = 'menu-item-has-children'; |
|
342 |
} |
|
343 |
} |
|
344 |
|
|
345 |
unset( $menu_items, $menu_item ); |
|
346 |
|
|
347 |
/** |
|
348 |
* Filter the sorted list of menu item objects before generating the menu's HTML. |
|
349 |
* |
|
350 |
* @since 3.1.0 |
|
351 |
* |
5
|
352 |
* @param array $sorted_menu_items The menu items, sorted by each menu item's menu order. |
|
353 |
* @param object $args An object containing wp_nav_menu() arguments. |
0
|
354 |
*/ |
|
355 |
$sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args ); |
|
356 |
|
|
357 |
$items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args ); |
|
358 |
unset($sorted_menu_items); |
|
359 |
|
|
360 |
// Attributes |
|
361 |
if ( ! empty( $args->menu_id ) ) { |
|
362 |
$wrap_id = $args->menu_id; |
|
363 |
} else { |
|
364 |
$wrap_id = 'menu-' . $menu->slug; |
|
365 |
while ( in_array( $wrap_id, $menu_id_slugs ) ) { |
|
366 |
if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) |
|
367 |
$wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id ); |
|
368 |
else |
|
369 |
$wrap_id = $wrap_id . '-1'; |
|
370 |
} |
|
371 |
} |
|
372 |
$menu_id_slugs[] = $wrap_id; |
|
373 |
|
|
374 |
$wrap_class = $args->menu_class ? $args->menu_class : ''; |
|
375 |
|
|
376 |
/** |
|
377 |
* Filter the HTML list content for navigation menus. |
|
378 |
* |
|
379 |
* @since 3.0.0 |
|
380 |
* |
5
|
381 |
* @see wp_nav_menu() |
|
382 |
* |
0
|
383 |
* @param string $items The HTML list content for the menu items. |
5
|
384 |
* @param object $args An object containing wp_nav_menu() arguments. |
0
|
385 |
*/ |
|
386 |
$items = apply_filters( 'wp_nav_menu_items', $items, $args ); |
|
387 |
/** |
|
388 |
* Filter the HTML list content for a specific navigation menu. |
|
389 |
* |
|
390 |
* @since 3.0.0 |
|
391 |
* |
5
|
392 |
* @see wp_nav_menu() |
|
393 |
* |
0
|
394 |
* @param string $items The HTML list content for the menu items. |
5
|
395 |
* @param object $args An object containing wp_nav_menu() arguments. |
0
|
396 |
*/ |
|
397 |
$items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); |
|
398 |
|
|
399 |
// Don't print any markup if there are no items at this point. |
|
400 |
if ( empty( $items ) ) |
|
401 |
return false; |
|
402 |
|
|
403 |
$nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items ); |
|
404 |
unset( $items ); |
|
405 |
|
|
406 |
if ( $show_container ) |
|
407 |
$nav_menu .= '</' . $args->container . '>'; |
|
408 |
|
|
409 |
/** |
|
410 |
* Filter the HTML content for navigation menus. |
|
411 |
* |
|
412 |
* @since 3.0.0 |
|
413 |
* |
5
|
414 |
* @see wp_nav_menu() |
|
415 |
* |
0
|
416 |
* @param string $nav_menu The HTML content for the navigation menu. |
5
|
417 |
* @param object $args An object containing wp_nav_menu() arguments. |
0
|
418 |
*/ |
|
419 |
$nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args ); |
|
420 |
|
|
421 |
if ( $args->echo ) |
|
422 |
echo $nav_menu; |
|
423 |
else |
|
424 |
return $nav_menu; |
|
425 |
} |
|
426 |
|
|
427 |
/** |
|
428 |
* Add the class property classes for the current context, if applicable. |
|
429 |
* |
|
430 |
* @access private |
5
|
431 |
* @since 3.0.0 |
0
|
432 |
* |
|
433 |
* @param array $menu_items The current menu item objects to which to add the class property information. |
|
434 |
*/ |
|
435 |
function _wp_menu_item_classes_by_context( &$menu_items ) { |
|
436 |
global $wp_query, $wp_rewrite; |
|
437 |
|
|
438 |
$queried_object = $wp_query->get_queried_object(); |
|
439 |
$queried_object_id = (int) $wp_query->queried_object_id; |
|
440 |
|
|
441 |
$active_object = ''; |
|
442 |
$active_ancestor_item_ids = array(); |
|
443 |
$active_parent_item_ids = array(); |
|
444 |
$active_parent_object_ids = array(); |
|
445 |
$possible_taxonomy_ancestors = array(); |
|
446 |
$possible_object_parents = array(); |
|
447 |
$home_page_id = (int) get_option( 'page_for_posts' ); |
|
448 |
|
|
449 |
if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) { |
|
450 |
foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) { |
|
451 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
|
452 |
$term_hierarchy = _get_term_hierarchy( $taxonomy ); |
|
453 |
$terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) ); |
|
454 |
if ( is_array( $terms ) ) { |
|
455 |
$possible_object_parents = array_merge( $possible_object_parents, $terms ); |
|
456 |
$term_to_ancestor = array(); |
|
457 |
foreach ( (array) $term_hierarchy as $anc => $descs ) { |
|
458 |
foreach ( (array) $descs as $desc ) |
|
459 |
$term_to_ancestor[ $desc ] = $anc; |
|
460 |
} |
|
461 |
|
|
462 |
foreach ( $terms as $desc ) { |
|
463 |
do { |
|
464 |
$possible_taxonomy_ancestors[ $taxonomy ][] = $desc; |
|
465 |
if ( isset( $term_to_ancestor[ $desc ] ) ) { |
|
466 |
$_desc = $term_to_ancestor[ $desc ]; |
|
467 |
unset( $term_to_ancestor[ $desc ] ); |
|
468 |
$desc = $_desc; |
|
469 |
} else { |
|
470 |
$desc = 0; |
|
471 |
} |
|
472 |
} while ( ! empty( $desc ) ); |
|
473 |
} |
|
474 |
} |
|
475 |
} |
|
476 |
} |
|
477 |
} elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) { |
|
478 |
$term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy ); |
|
479 |
$term_to_ancestor = array(); |
|
480 |
foreach ( (array) $term_hierarchy as $anc => $descs ) { |
|
481 |
foreach ( (array) $descs as $desc ) |
|
482 |
$term_to_ancestor[ $desc ] = $anc; |
|
483 |
} |
|
484 |
$desc = $queried_object->term_id; |
|
485 |
do { |
|
486 |
$possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc; |
|
487 |
if ( isset( $term_to_ancestor[ $desc ] ) ) { |
|
488 |
$_desc = $term_to_ancestor[ $desc ]; |
|
489 |
unset( $term_to_ancestor[ $desc ] ); |
|
490 |
$desc = $_desc; |
|
491 |
} else { |
|
492 |
$desc = 0; |
|
493 |
} |
|
494 |
} while ( ! empty( $desc ) ); |
|
495 |
} |
|
496 |
|
|
497 |
$possible_object_parents = array_filter( $possible_object_parents ); |
|
498 |
|
|
499 |
$front_page_url = home_url(); |
|
500 |
|
|
501 |
foreach ( (array) $menu_items as $key => $menu_item ) { |
|
502 |
|
|
503 |
$menu_items[$key]->current = false; |
|
504 |
|
|
505 |
$classes = (array) $menu_item->classes; |
|
506 |
$classes[] = 'menu-item'; |
|
507 |
$classes[] = 'menu-item-type-' . $menu_item->type; |
|
508 |
$classes[] = 'menu-item-object-' . $menu_item->object; |
|
509 |
|
|
510 |
// if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object |
|
511 |
if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) { |
|
512 |
$active_parent_object_ids[] = (int) $menu_item->object_id; |
|
513 |
$active_parent_item_ids[] = (int) $menu_item->db_id; |
|
514 |
$active_object = $queried_object->post_type; |
|
515 |
|
|
516 |
// if the menu item corresponds to the currently-queried post or taxonomy object |
|
517 |
} elseif ( |
|
518 |
$menu_item->object_id == $queried_object_id && |
|
519 |
( |
|
520 |
( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) || |
|
521 |
( 'post_type' == $menu_item->type && $wp_query->is_singular ) || |
|
522 |
( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object ) |
|
523 |
) |
|
524 |
) { |
|
525 |
$classes[] = 'current-menu-item'; |
|
526 |
$menu_items[$key]->current = true; |
|
527 |
$_anc_id = (int) $menu_item->db_id; |
|
528 |
|
|
529 |
while( |
|
530 |
( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && |
|
531 |
! in_array( $_anc_id, $active_ancestor_item_ids ) |
|
532 |
) { |
|
533 |
$active_ancestor_item_ids[] = $_anc_id; |
|
534 |
} |
|
535 |
|
|
536 |
if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) { |
|
537 |
// Back compat classes for pages to match wp_page_menu() |
|
538 |
$classes[] = 'page_item'; |
|
539 |
$classes[] = 'page-item-' . $menu_item->object_id; |
|
540 |
$classes[] = 'current_page_item'; |
|
541 |
} |
|
542 |
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent; |
|
543 |
$active_parent_object_ids[] = (int) $menu_item->post_parent; |
|
544 |
$active_object = $menu_item->object; |
|
545 |
|
|
546 |
// if the menu item corresponds to the currently-requested URL |
|
547 |
} elseif ( 'custom' == $menu_item->object ) { |
|
548 |
$_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] ); |
|
549 |
$current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current ); |
|
550 |
$raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url; |
|
551 |
$item_url = untrailingslashit( $raw_item_url ); |
|
552 |
$_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) ); |
|
553 |
|
|
554 |
if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) { |
|
555 |
$classes[] = 'current-menu-item'; |
|
556 |
$menu_items[$key]->current = true; |
|
557 |
$_anc_id = (int) $menu_item->db_id; |
|
558 |
|
|
559 |
while( |
|
560 |
( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && |
|
561 |
! in_array( $_anc_id, $active_ancestor_item_ids ) |
|
562 |
) { |
|
563 |
$active_ancestor_item_ids[] = $_anc_id; |
|
564 |
} |
|
565 |
|
|
566 |
if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) { |
|
567 |
// Back compat for home link to match wp_page_menu() |
|
568 |
$classes[] = 'current_page_item'; |
|
569 |
} |
|
570 |
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent; |
|
571 |
$active_parent_object_ids[] = (int) $menu_item->post_parent; |
|
572 |
$active_object = $menu_item->object; |
|
573 |
|
|
574 |
// give front page item current-menu-item class when extra query arguments involved |
|
575 |
} elseif ( $item_url == $front_page_url && is_front_page() ) { |
|
576 |
$classes[] = 'current-menu-item'; |
|
577 |
} |
|
578 |
|
|
579 |
if ( untrailingslashit($item_url) == home_url() ) |
|
580 |
$classes[] = 'menu-item-home'; |
|
581 |
} |
|
582 |
|
|
583 |
// back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query |
|
584 |
if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id ) |
|
585 |
$classes[] = 'current_page_parent'; |
|
586 |
|
|
587 |
$menu_items[$key]->classes = array_unique( $classes ); |
|
588 |
} |
|
589 |
$active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) ); |
|
590 |
$active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) ); |
|
591 |
$active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) ); |
|
592 |
|
|
593 |
// set parent's class |
|
594 |
foreach ( (array) $menu_items as $key => $parent_item ) { |
|
595 |
$classes = (array) $parent_item->classes; |
|
596 |
$menu_items[$key]->current_item_ancestor = false; |
|
597 |
$menu_items[$key]->current_item_parent = false; |
|
598 |
|
|
599 |
if ( |
|
600 |
isset( $parent_item->type ) && |
|
601 |
( |
|
602 |
// ancestral post object |
|
603 |
( |
|
604 |
'post_type' == $parent_item->type && |
|
605 |
! empty( $queried_object->post_type ) && |
|
606 |
is_post_type_hierarchical( $queried_object->post_type ) && |
|
607 |
in_array( $parent_item->object_id, $queried_object->ancestors ) && |
|
608 |
$parent_item->object != $queried_object->ID |
|
609 |
) || |
|
610 |
|
|
611 |
// ancestral term |
|
612 |
( |
|
613 |
'taxonomy' == $parent_item->type && |
|
614 |
isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) && |
|
615 |
in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) && |
|
616 |
( |
|
617 |
! isset( $queried_object->term_id ) || |
|
618 |
$parent_item->object_id != $queried_object->term_id |
|
619 |
) |
|
620 |
) |
|
621 |
) |
|
622 |
) { |
|
623 |
$classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor'; |
|
624 |
} |
|
625 |
|
|
626 |
if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) { |
|
627 |
$classes[] = 'current-menu-ancestor'; |
|
628 |
$menu_items[$key]->current_item_ancestor = true; |
|
629 |
} |
|
630 |
if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) { |
|
631 |
$classes[] = 'current-menu-parent'; |
|
632 |
$menu_items[$key]->current_item_parent = true; |
|
633 |
} |
|
634 |
if ( in_array( $parent_item->object_id, $active_parent_object_ids ) ) |
|
635 |
$classes[] = 'current-' . $active_object . '-parent'; |
|
636 |
|
|
637 |
if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) { |
|
638 |
// Back compat classes for pages to match wp_page_menu() |
|
639 |
if ( in_array('current-menu-parent', $classes) ) |
|
640 |
$classes[] = 'current_page_parent'; |
|
641 |
if ( in_array('current-menu-ancestor', $classes) ) |
|
642 |
$classes[] = 'current_page_ancestor'; |
|
643 |
} |
|
644 |
|
|
645 |
$menu_items[$key]->classes = array_unique( $classes ); |
|
646 |
} |
|
647 |
} |
|
648 |
|
|
649 |
/** |
|
650 |
* Retrieve the HTML list content for nav menu items. |
|
651 |
* |
|
652 |
* @uses Walker_Nav_Menu to create HTML list content. |
|
653 |
* @since 3.0.0 |
|
654 |
* @see Walker::walk() for parameters and return description. |
|
655 |
*/ |
|
656 |
function walk_nav_menu_tree( $items, $depth, $r ) { |
|
657 |
$walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker; |
|
658 |
$args = array( $items, $depth, $r ); |
|
659 |
|
|
660 |
return call_user_func_array( array($walker, 'walk'), $args ); |
|
661 |
} |
|
662 |
|
|
663 |
/** |
|
664 |
* Prevents a menu item ID from being used more than once. |
|
665 |
* |
|
666 |
* @since 3.0.1 |
|
667 |
* @access private |
|
668 |
*/ |
|
669 |
function _nav_menu_item_id_use_once( $id, $item ) { |
|
670 |
static $_used_ids = array(); |
|
671 |
if ( in_array( $item->ID, $_used_ids ) ) |
|
672 |
return ''; |
|
673 |
$_used_ids[] = $item->ID; |
|
674 |
return $id; |
|
675 |
} |