]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/nav-menu-template.php
WordPress 3.6.1
[autoinstalls/wordpress.git] / wp-includes / nav-menu-template.php
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  * @package WordPress
14  * @since 3.0.0
15  * @uses Walker
16  */
17 class Walker_Nav_Menu extends Walker {
18         /**
19          * @see Walker::$tree_type
20          * @since 3.0.0
21          * @var string
22          */
23         var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
24
25         /**
26          * @see Walker::$db_fields
27          * @since 3.0.0
28          * @todo Decouple this.
29          * @var array
30          */
31         var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
32
33         /**
34          * @see Walker::start_lvl()
35          * @since 3.0.0
36          *
37          * @param string $output Passed by reference. Used to append additional content.
38          * @param int $depth Depth of page. Used for padding.
39          */
40         function start_lvl( &$output, $depth = 0, $args = array() ) {
41                 $indent = str_repeat("\t", $depth);
42                 $output .= "\n$indent<ul class=\"sub-menu\">\n";
43         }
44
45         /**
46          * @see Walker::end_lvl()
47          * @since 3.0.0
48          *
49          * @param string $output Passed by reference. Used to append additional content.
50          * @param int $depth Depth of page. Used for padding.
51          */
52         function end_lvl( &$output, $depth = 0, $args = array() ) {
53                 $indent = str_repeat("\t", $depth);
54                 $output .= "$indent</ul>\n";
55         }
56
57         /**
58          * @see Walker::start_el()
59          * @since 3.0.0
60          *
61          * @param string $output Passed by reference. Used to append additional content.
62          * @param object $item Menu item data object.
63          * @param int $depth Depth of menu item. Used for padding.
64          * @param int $current_page Menu item ID.
65          * @param object $args
66          */
67         function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
68                 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
69
70                 $class_names = $value = '';
71
72                 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
73                 $classes[] = 'menu-item-' . $item->ID;
74
75                 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
76                 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
77
78                 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
79                 $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
80
81                 $output .= $indent . '<li' . $id . $value . $class_names .'>';
82
83                 $atts = array();
84                 $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
85                 $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
86                 $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
87                 $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
88
89                 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
90
91                 $attributes = '';
92                 foreach ( $atts as $attr => $value ) {
93                         if ( ! empty( $value ) ) {
94                                 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
95                                 $attributes .= ' ' . $attr . '="' . $value . '"';
96                         }
97                 }
98
99                 $item_output = $args->before;
100                 $item_output .= '<a'. $attributes .'>';
101                 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
102                 $item_output .= '</a>';
103                 $item_output .= $args->after;
104
105                 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
106         }
107
108         /**
109          * @see Walker::end_el()
110          * @since 3.0.0
111          *
112          * @param string $output Passed by reference. Used to append additional content.
113          * @param object $item Page data object. Not used.
114          * @param int $depth Depth of page. Not Used.
115          */
116         function end_el( &$output, $item, $depth = 0, $args = array() ) {
117                 $output .= "</li>\n";
118         }
119 }
120
121 /**
122  * Displays a navigation menu.
123  *
124  * Optional $args contents:
125  *
126  * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank.
127  * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
128  * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
129  * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
130  * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
131  * container_id - The ID that is applied to the container. Defaults to blank.
132  * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'. Set to false for no fallback.
133  * before - Text before the link text.
134  * after - Text after the link text.
135  * link_before - Text before the link.
136  * link_after - Text after the link.
137  * echo - Whether to echo the menu or return it. Defaults to echo.
138  * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0.
139  * walker - allows a custom walker to be specified.
140  * theme_location - the location in the theme to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
141  * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders.
142  *
143  * @since 3.0.0
144  *
145  * @param array $args Arguments
146  */
147 function wp_nav_menu( $args = array() ) {
148         static $menu_id_slugs = array();
149
150         $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
151         '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>',
152         'depth' => 0, 'walker' => '', 'theme_location' => '' );
153
154         $args = wp_parse_args( $args, $defaults );
155         $args = apply_filters( 'wp_nav_menu_args', $args );
156         $args = (object) $args;
157
158         // Get the nav menu based on the requested menu
159         $menu = wp_get_nav_menu_object( $args->menu );
160
161         // Get the nav menu based on the theme_location
162         if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
163                 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
164
165         // get the first menu that has items if we still can't find a menu
166         if ( ! $menu && !$args->theme_location ) {
167                 $menus = wp_get_nav_menus();
168                 foreach ( $menus as $menu_maybe ) {
169                         if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
170                                 $menu = $menu_maybe;
171                                 break;
172                         }
173                 }
174         }
175
176         // If the menu exists, get its items.
177         if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
178                 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
179
180         /*
181          * If no menu was found:
182          *  - Fall back (if one was specified), or bail.
183          *
184          * If no menu items were found:
185          *  - Fall back, but only if no theme location was specified.
186          *  - Otherwise, bail.
187          */
188         if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
189                 && $args->fallback_cb && is_callable( $args->fallback_cb ) )
190                         return call_user_func( $args->fallback_cb, (array) $args );
191
192         if ( ! $menu || is_wp_error( $menu ) )
193                 return false;
194
195         $nav_menu = $items = '';
196
197         $show_container = false;
198         if ( $args->container ) {
199                 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
200                 if ( in_array( $args->container, $allowed_tags ) ) {
201                         $show_container = true;
202                         $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
203                         $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
204                         $nav_menu .= '<'. $args->container . $id . $class . '>';
205                 }
206         }
207
208         // Set up the $menu_item variables
209         _wp_menu_item_classes_by_context( $menu_items );
210
211         $sorted_menu_items = array();
212         foreach ( (array) $menu_items as $key => $menu_item )
213                 $sorted_menu_items[$menu_item->menu_order] = $menu_item;
214
215         unset($menu_items);
216
217         $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
218
219         $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
220         unset($sorted_menu_items);
221
222         // Attributes
223         if ( ! empty( $args->menu_id ) ) {
224                 $wrap_id = $args->menu_id;
225         } else {
226                 $wrap_id = 'menu-' . $menu->slug;
227                 while ( in_array( $wrap_id, $menu_id_slugs ) ) {
228                         if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
229                                 $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
230                         else
231                                 $wrap_id = $wrap_id . '-1';
232                 }
233         }
234         $menu_id_slugs[] = $wrap_id;
235
236         $wrap_class = $args->menu_class ? $args->menu_class : '';
237
238         // Allow plugins to hook into the menu to add their own <li>'s
239         $items = apply_filters( 'wp_nav_menu_items', $items, $args );
240         $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
241
242         // Don't print any markup if there are no items at this point.
243         if ( empty( $items ) )
244                 return false;
245
246         $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
247         unset( $items );
248
249         if ( $show_container )
250                 $nav_menu .= '</' . $args->container . '>';
251
252         $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
253
254         if ( $args->echo )
255                 echo $nav_menu;
256         else
257                 return $nav_menu;
258 }
259
260 /**
261  * Add the class property classes for the current context, if applicable.
262  *
263  * @access private
264  * @since 3.0
265  *
266  * @param array $menu_items The current menu item objects to which to add the class property information.
267  */
268 function _wp_menu_item_classes_by_context( &$menu_items ) {
269         global $wp_query, $wp_rewrite;
270
271         $queried_object = $wp_query->get_queried_object();
272         $queried_object_id = (int) $wp_query->queried_object_id;
273
274         $active_object = '';
275         $active_ancestor_item_ids = array();
276         $active_parent_item_ids = array();
277         $active_parent_object_ids = array();
278         $possible_taxonomy_ancestors = array();
279         $possible_object_parents = array();
280         $home_page_id = (int) get_option( 'page_for_posts' );
281
282         if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
283                 foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
284                         if ( is_taxonomy_hierarchical( $taxonomy ) ) {
285                                 $term_hierarchy = _get_term_hierarchy( $taxonomy );
286                                 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
287                                 if ( is_array( $terms ) ) {
288                                         $possible_object_parents = array_merge( $possible_object_parents, $terms );
289                                         $term_to_ancestor = array();
290                                         foreach ( (array) $term_hierarchy as $anc => $descs ) {
291                                                 foreach ( (array) $descs as $desc )
292                                                         $term_to_ancestor[ $desc ] = $anc;
293                                         }
294
295                                         foreach ( $terms as $desc ) {
296                                                 do {
297                                                         $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
298                                                         if ( isset( $term_to_ancestor[ $desc ] ) ) {
299                                                                 $_desc = $term_to_ancestor[ $desc ];
300                                                                 unset( $term_to_ancestor[ $desc ] );
301                                                                 $desc = $_desc;
302                                                         } else {
303                                                                 $desc = 0;
304                                                         }
305                                                 } while ( ! empty( $desc ) );
306                                         }
307                                 }
308                         }
309                 }
310         } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
311                 $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
312                 $term_to_ancestor = array();
313                 foreach ( (array) $term_hierarchy as $anc => $descs ) {
314                         foreach ( (array) $descs as $desc )
315                                 $term_to_ancestor[ $desc ] = $anc;
316                 }
317                 $desc = $queried_object->term_id;
318                 do {
319                         $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
320                         if ( isset( $term_to_ancestor[ $desc ] ) ) {
321                                 $_desc = $term_to_ancestor[ $desc ];
322                                 unset( $term_to_ancestor[ $desc ] );
323                                 $desc = $_desc;
324                         } else {
325                                 $desc = 0;
326                         }
327                 } while ( ! empty( $desc ) );
328         }
329
330         $possible_object_parents = array_filter( $possible_object_parents );
331
332         $front_page_url = home_url();
333
334         foreach ( (array) $menu_items as $key => $menu_item ) {
335
336                 $menu_items[$key]->current = false;
337
338                 $classes = (array) $menu_item->classes;
339                 $classes[] = 'menu-item';
340                 $classes[] = 'menu-item-type-' . $menu_item->type;
341                 $classes[] = 'menu-item-object-' . $menu_item->object;
342
343                 // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
344                 if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
345                         $active_parent_object_ids[] = (int) $menu_item->object_id;
346                         $active_parent_item_ids[] = (int) $menu_item->db_id;
347                         $active_object = $queried_object->post_type;
348
349                 // if the menu item corresponds to the currently-queried post or taxonomy object
350                 } elseif (
351                         $menu_item->object_id == $queried_object_id &&
352                         (
353                                 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
354                                 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
355                                 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
356                         )
357                 ) {
358                         $classes[] = 'current-menu-item';
359                         $menu_items[$key]->current = true;
360                         $_anc_id = (int) $menu_item->db_id;
361
362                         while(
363                                 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
364                                 ! in_array( $_anc_id, $active_ancestor_item_ids )
365                         ) {
366                                 $active_ancestor_item_ids[] = $_anc_id;
367                         }
368
369                         if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
370                                 // Back compat classes for pages to match wp_page_menu()
371                                 $classes[] = 'page_item';
372                                 $classes[] = 'page-item-' . $menu_item->object_id;
373                                 $classes[] = 'current_page_item';
374                         }
375                         $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
376                         $active_parent_object_ids[] = (int) $menu_item->post_parent;
377                         $active_object = $menu_item->object;
378
379                 // if the menu item corresponds to the currently-requested URL
380                 } elseif ( 'custom' == $menu_item->object ) {
381                         $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
382                         $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
383                         $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
384                         $item_url = untrailingslashit( $raw_item_url );
385                         $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
386
387                         if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
388                                 $classes[] = 'current-menu-item';
389                                 $menu_items[$key]->current = true;
390                                 $_anc_id = (int) $menu_item->db_id;
391
392                                 while(
393                                         ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
394                                         ! in_array( $_anc_id, $active_ancestor_item_ids )
395                                 ) {
396                                         $active_ancestor_item_ids[] = $_anc_id;
397                                 }
398
399                                 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
400                                         // Back compat for home link to match wp_page_menu()
401                                         $classes[] = 'current_page_item';
402                                 }
403                                 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
404                                 $active_parent_object_ids[] = (int) $menu_item->post_parent;
405                                 $active_object = $menu_item->object;
406
407                         // give front page item current-menu-item class when extra query arguments involved
408                         } elseif ( $item_url == $front_page_url && is_front_page() ) {
409                                 $classes[] = 'current-menu-item';
410                         }
411
412                         if ( untrailingslashit($item_url) == home_url() )
413                                 $classes[] = 'menu-item-home';
414                 }
415
416                 // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
417                 if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
418                         $classes[] = 'current_page_parent';
419
420                 $menu_items[$key]->classes = array_unique( $classes );
421         }
422         $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
423         $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
424         $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
425
426         // set parent's class
427         foreach ( (array) $menu_items as $key => $parent_item ) {
428                 $classes = (array) $parent_item->classes;
429                 $menu_items[$key]->current_item_ancestor = false;
430                 $menu_items[$key]->current_item_parent = false;
431
432                 if (
433                         isset( $parent_item->type ) &&
434                         (
435                                 // ancestral post object
436                                 (
437                                         'post_type' == $parent_item->type &&
438                                         ! empty( $queried_object->post_type ) &&
439                                         is_post_type_hierarchical( $queried_object->post_type ) &&
440                                         in_array( $parent_item->object_id, $queried_object->ancestors ) &&
441                                         $parent_item->object != $queried_object->ID
442                                 ) ||
443
444                                 // ancestral term
445                                 (
446                                         'taxonomy' == $parent_item->type &&
447                                         isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
448                                         in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
449                                         (
450                                                 ! isset( $queried_object->term_id ) ||
451                                                 $parent_item->object_id != $queried_object->term_id
452                                         )
453                                 )
454                         )
455                 ) {
456                         $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
457                 }
458
459                 if ( in_array(  intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
460                         $classes[] = 'current-menu-ancestor';
461                         $menu_items[$key]->current_item_ancestor = true;
462                 }
463                 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
464                         $classes[] = 'current-menu-parent';
465                         $menu_items[$key]->current_item_parent = true;
466                 }
467                 if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
468                         $classes[] = 'current-' . $active_object . '-parent';
469
470                 if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
471                         // Back compat classes for pages to match wp_page_menu()
472                         if ( in_array('current-menu-parent', $classes) )
473                                 $classes[] = 'current_page_parent';
474                         if ( in_array('current-menu-ancestor', $classes) )
475                                 $classes[] = 'current_page_ancestor';
476                 }
477
478                 $menu_items[$key]->classes = array_unique( $classes );
479         }
480 }
481
482 /**
483  * Retrieve the HTML list content for nav menu items.
484  *
485  * @uses Walker_Nav_Menu to create HTML list content.
486  * @since 3.0.0
487  * @see Walker::walk() for parameters and return description.
488  */
489 function walk_nav_menu_tree( $items, $depth, $r ) {
490         $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
491         $args = array( $items, $depth, $r );
492
493         return call_user_func_array( array($walker, 'walk'), $args );
494 }
495
496 /**
497  * Prevents a menu item ID from being used more than once.
498  *
499  * @since 3.0.1
500  * @access private
501  */
502 function _nav_menu_item_id_use_once( $id, $item ) {
503         static $_used_ids = array();
504         if ( in_array( $item->ID, $_used_ids ) )
505                 return '';
506         $_used_ids[] = $item->ID;
507         return $id;
508 }
509 add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );