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