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