PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

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