You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

537 lines
20 KiB

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