PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/nav-menu-template.php

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