]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/nav-menu-template.php
Wordpress 3.0
[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) {
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) {
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, $args) {
68                 global $wp_query;
69                 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
70
71                 $class_names = $value = '';
72
73                 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
74
75                 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
76                 $class_names = ' class="' . esc_attr( $class_names ) . '"';
77
78                 $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
79
80                 $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
81                 $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
82                 $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
83                 $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
84
85                 $item_output = $args->before;
86                 $item_output .= '<a'. $attributes .'>';
87                 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
88                 $item_output .= '</a>';
89                 $item_output .= $args->after;
90
91                 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
92         }
93
94         /**
95          * @see Walker::end_el()
96          * @since 3.0.0
97          *
98          * @param string $output Passed by reference. Used to append additional content.
99          * @param object $item Page data object. Not used.
100          * @param int $depth Depth of page. Not Used.
101          */
102         function end_el(&$output, $item, $depth) {
103                 $output .= "</li>\n";
104         }
105 }
106
107 /**
108  * Displays a navigation menu.
109  *
110  * Optional $args contents:
111  *
112  * menu - The menu that is desired.  Accepts (matching in order) id, slug, name. Defaults to blank.
113  * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
114  * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
115  * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
116  * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
117  * container_id - The ID that is applied to the container. Defaults to blank.
118  * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
119  * before - Text before the link text.
120  * after - Text after the link text.
121  * link_before - Text before the link.
122  * link_after - Text after the link.
123  * echo - Whether to echo the menu or return it. Defaults to echo.
124  * depth - how many levels of the hierarchy are to be included.  0 means all.  Defaults to 0.
125  * walker - allows a custom walker to be specified.
126  * 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.
127  *
128  * @since 3.0.0
129  *
130  * @param array $args Arguments
131  */
132 function wp_nav_menu( $args = array() ) {
133         static $menu_id_slugs = array();
134
135         $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
136         'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
137         'depth' => 0, 'walker' => '', 'theme_location' => '' );
138
139         $args = wp_parse_args( $args, $defaults );
140         $args = apply_filters( 'wp_nav_menu_args', $args );
141         $args = (object) $args;
142
143         // Get the nav menu based on the requested menu
144         $menu = wp_get_nav_menu_object( $args->menu );
145
146         // Get the nav menu based on the theme_location
147         if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
148                 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
149
150         // get the first menu that has items if we still can't find a menu
151         if ( ! $menu && !$args->theme_location ) {
152                 $menus = wp_get_nav_menus();
153                 foreach ( $menus as $menu_maybe ) {
154                         if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) {
155                                 $menu = $menu_maybe;
156                                 break;
157                         }
158                 }
159         }
160
161         // If the menu exists, get its items.
162         if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
163                 $menu_items = wp_get_nav_menu_items( $menu->term_id );
164
165         // If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
166         if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
167                 && ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) )
168                         return call_user_func( $args->fallback_cb, (array) $args );
169
170         // If no fallback function was specified and the menu doesn't exists, bail.
171         if ( !$menu || is_wp_error($menu) )
172                 return false;
173
174         $nav_menu = $items = '';
175
176         $show_container = false;
177         if ( $args->container ) {
178                 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
179                 if ( in_array( $args->container, $allowed_tags ) ) {
180                         $show_container = true;
181                         $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
182                         $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
183                         $nav_menu .= '<'. $args->container . $id . $class . '>';
184                 }
185         }
186
187         // Set up the $menu_item variables
188         _wp_menu_item_classes_by_context( $menu_items );
189
190         $sorted_menu_items = array();
191         foreach ( (array) $menu_items as $key => $menu_item )
192                 $sorted_menu_items[$menu_item->menu_order] = $menu_item;
193
194         unset($menu_items);
195
196         $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
197         unset($sorted_menu_items);
198
199         // Attributes
200         if ( ! empty( $args->menu_id ) ) {
201                 $slug = $args->menu_id;
202         } else {
203                 $slug = 'menu-' . $menu->slug;
204                 while ( in_array( $slug, $menu_id_slugs ) ) {
205                         if ( preg_match( '#-(\d+)$#', $slug, $matches ) )
206                                 $slug = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $slug);
207                         else
208                                 $slug = $slug . '-1';
209                 }
210         }
211         $menu_id_slugs[] = $slug;
212         $attributes = ' id="' . $slug . '"';
213         $attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';
214
215         $nav_menu .= '<ul'. $attributes .'>';
216
217         // Allow plugins to hook into the menu to add their own <li>'s
218         $items = apply_filters( 'wp_nav_menu_items', $items, $args );
219         $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
220         $nav_menu .= $items;
221         unset($items);
222
223         $nav_menu .= '</ul>';
224
225         if ( $show_container )
226                 $nav_menu .= '</' . $args->container . '>';
227
228         $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
229
230         if ( $args->echo )
231                 echo $nav_menu;
232         else
233                 return $nav_menu;
234 }
235
236 /**
237  * Add the class property classes for the current context, if applicable.
238  *
239  * @access private
240  * @since 3.0
241  *
242  * @param array $menu_items The current menu item objects to which to add the class property information.
243  */
244 function _wp_menu_item_classes_by_context( &$menu_items ) {
245         global $wp_query;
246
247         $queried_object = $wp_query->get_queried_object();
248         $queried_object_id = (int) $wp_query->queried_object_id;
249
250         $active_object = '';
251         $active_ancestor_item_ids = array();
252         $active_parent_item_ids = array();
253         $active_parent_object_ids = array();
254         $possible_taxonomy_ancestors = array();
255         $possible_object_parents = array();
256         $home_page_id = (int) get_option( 'page_for_posts' );
257
258         if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
259                 foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
260                         if ( is_taxonomy_hierarchical( $taxonomy ) ) {
261                                 $term_hierarchy = _get_term_hierarchy( $taxonomy );
262                                 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
263                                 if ( is_array( $terms ) ) {
264                                         $possible_object_parents = array_merge( $possible_object_parents, $terms );
265                                         $term_to_ancestor = array();
266                                         foreach ( (array) $term_hierarchy as $anc => $descs ) {
267                                                 foreach ( (array) $descs as $desc )
268                                                         $term_to_ancestor[ $desc ] = $anc;
269                                         }
270
271                                         foreach ( $terms as $desc ) {
272                                                 do {
273                                                         $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
274                                                         if ( isset( $term_to_ancestor[ $desc ] ) ) {
275                                                                 $_desc = $term_to_ancestor[ $desc ];
276                                                                 unset( $term_to_ancestor[ $desc ] );
277                                                                 $desc = $_desc;
278                                                         } else {
279                                                                 $desc = 0;
280                                                         }
281                                                 } while ( ! empty( $desc ) );
282                                         }
283                                 }
284                         }
285                 }
286         } elseif ( ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) ) {
287                 _get_post_ancestors( $queried_object );
288         } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
289                 $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
290                 $term_to_ancestor = array();
291                 foreach ( (array) $term_hierarchy as $anc => $descs ) {
292                         foreach ( (array) $descs as $desc )
293                                 $term_to_ancestor[ $desc ] = $anc;
294                 }
295                 $desc = $queried_object->term_id;
296                 do {
297                         $possible_taxonomy_ancestors[ $queried_object->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         $possible_object_parents = array_filter( $possible_object_parents );
309
310         foreach ( (array) $menu_items as $key => $menu_item ) {
311                 $classes = (array) $menu_item->classes;
312                 $classes[] = 'menu-item';
313                 $classes[] = 'menu-item-type-' . $menu_item->type;
314
315                 // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
316                 if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
317                         $active_parent_object_ids[] = (int) $menu_item->object_id;
318                         $active_parent_item_ids[] = (int) $menu_item->db_id;
319                         $active_object = $queried_object->post_type;
320
321                 // if the menu item corresponds to the currently-queried post or taxonomy object
322                 } elseif (
323                         $menu_item->object_id == $queried_object_id &&
324                         (
325                                 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
326                                 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
327                                 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) )
328                         )
329                 ) {
330                         $classes[] = 'current-menu-item';
331                         $_anc_id = (int) $menu_item->db_id;
332
333                         while(
334                                 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
335                                 ! in_array( $_anc_id, $active_ancestor_item_ids )
336                         ) {
337                                 $active_ancestor_item_ids[] = $_anc_id;
338                         }
339
340                         if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
341                                 // Back compat classes for pages to match wp_page_menu()
342                                 $classes[] = 'page_item';
343                                 $classes[] = 'page-item-' . $menu_item->object_id;
344                                 $classes[] = 'current_page_item';
345                         }
346                         $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
347                         $active_parent_object_ids[] = (int) $menu_item->post_parent;
348                         $active_object = $menu_item->object;
349
350                 // if the menu item corresponds to the currently-requested URL
351                 } elseif ( 'custom' == $menu_item->object ) {
352                         $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
353                         $item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
354                         if ( $item_url == $current_url ) {
355                                 $classes[] = 'current-menu-item';
356                                 $_anc_id = (int) $menu_item->db_id;
357
358                                 while(
359                                         ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
360                                         ! in_array( $_anc_id, $active_ancestor_item_ids )
361                                 ) {
362                                         $active_ancestor_item_ids[] = $_anc_id;
363                                 }
364
365                                 if ( untrailingslashit($current_url) == home_url() ) {
366                                         $classes[] = 'menu-item-home';
367                                         // Back compat for home limk to match wp_page_menu()
368                                         $classes[] = 'current_page_item';
369                                 }
370                                 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
371                                 $active_parent_object_ids[] = (int) $menu_item->post_parent;
372                                 $active_object = $menu_item->object;
373                         }
374                 }
375
376                 // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
377                 if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
378                         $classes[] = 'current_page_parent';
379
380                 $menu_items[$key]->classes = array_unique( $classes );
381         }
382         $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
383         $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
384         $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
385
386         // set parent's class
387         foreach ( (array) $menu_items as $key => $parent_item ) {
388                 $classes = (array) $parent_item->classes;
389
390                 if (
391                         isset( $parent_item->type ) &&
392                         (
393                                 // ancestral post object
394                                 (
395                                         'post_type' == $parent_item->type &&
396                                         ! empty( $queried_object->post_type ) &&
397                                         is_post_type_hierarchical( $queried_object->post_type ) &&
398                                         in_array( $parent_item->object_id, $queried_object->ancestors )
399                                 ) ||
400
401                                 // ancestral term
402                                 (
403                                         'taxonomy' == $parent_item->type &&
404                                         isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
405                                         in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] )
406                                 )
407                         )
408                 ) {
409                         $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
410                 }
411
412                 if ( in_array(  intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
413                         $classes[] = 'current-menu-ancestor';
414                 }
415                 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) )
416                         $classes[] = 'current-menu-parent';
417                 if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
418                         $classes[] = 'current-' . $active_object . '-parent';
419
420                 if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
421                         // Back compat classes for pages to match wp_page_menu()
422                         if ( in_array('current-menu-parent', $classes) )
423                                 $classes[] = 'current_page_parent';
424                         if ( in_array('current-menu-ancestor', $classes) )
425                                 $classes[] = 'current_page_ancestor';
426                 }
427
428                 $menu_items[$key]->classes = array_unique( $classes );
429         }
430 }
431
432 /**
433  * Retrieve the HTML list content for nav menu items.
434  *
435  * @uses Walker_Nav_Menu to create HTML list content.
436  * @since 2.1.0
437  * @see Walker::walk() for parameters and return description.
438  */
439 function walk_nav_menu_tree( $items, $depth, $r ) {
440         $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
441         $args = array( $items, $depth, $r );
442
443         return call_user_func_array( array(&$walker, 'walk'), $args );
444 }
445
446 ?>