PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/theshipswakecreative/psw
PHP | 678 lines | 323 code | 75 blank | 280 comment | 97 complexity | 764fdb61b6fa4644f13a9f95a74973c2 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-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. * @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 object $walker Instance of a custom walker class. 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( array( 'orderby' => 'name' ) );
  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. * @param object $args An object containing wp_nav_menu() arguments.
  321. */
  322. $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
  323. $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
  324. unset($sorted_menu_items);
  325. // Attributes
  326. if ( ! empty( $args->menu_id ) ) {
  327. $wrap_id = $args->menu_id;
  328. } else {
  329. $wrap_id = 'menu-' . $menu->slug;
  330. while ( in_array( $wrap_id, $menu_id_slugs ) ) {
  331. if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
  332. $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
  333. else
  334. $wrap_id = $wrap_id . '-1';
  335. }
  336. }
  337. $menu_id_slugs[] = $wrap_id;
  338. $wrap_class = $args->menu_class ? $args->menu_class : '';
  339. /**
  340. * Filter the HTML list content for navigation menus.
  341. *
  342. * @since 3.0.0
  343. *
  344. * @see wp_nav_menu()
  345. *
  346. * @param string $items The HTML list content for the menu items.
  347. * @param object $args An object containing wp_nav_menu() arguments.
  348. */
  349. $items = apply_filters( 'wp_nav_menu_items', $items, $args );
  350. /**
  351. * Filter the HTML list content for a specific navigation menu.
  352. *
  353. * @since 3.0.0
  354. *
  355. * @see wp_nav_menu()
  356. *
  357. * @param string $items The HTML list content for the menu items.
  358. * @param object $args An object containing wp_nav_menu() arguments.
  359. */
  360. $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
  361. // Don't print any markup if there are no items at this point.
  362. if ( empty( $items ) )
  363. return false;
  364. $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
  365. unset( $items );
  366. if ( $show_container )
  367. $nav_menu .= '</' . $args->container . '>';
  368. /**
  369. * Filter the HTML content for navigation menus.
  370. *
  371. * @since 3.0.0
  372. *
  373. * @see wp_nav_menu()
  374. *
  375. * @param string $nav_menu The HTML content for the navigation menu.
  376. * @param object $args An object containing wp_nav_menu() arguments.
  377. */
  378. $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
  379. if ( $args->echo )
  380. echo $nav_menu;
  381. else
  382. return $nav_menu;
  383. }
  384. /**
  385. * Add the class property classes for the current context, if applicable.
  386. *
  387. * @access private
  388. * @since 3.0.0
  389. *
  390. * @param array $menu_items The current menu item objects to which to add the class property information.
  391. */
  392. function _wp_menu_item_classes_by_context( &$menu_items ) {
  393. global $wp_query, $wp_rewrite;
  394. $queried_object = $wp_query->get_queried_object();
  395. $queried_object_id = (int) $wp_query->queried_object_id;
  396. $active_object = '';
  397. $active_ancestor_item_ids = array();
  398. $active_parent_item_ids = array();
  399. $active_parent_object_ids = array();
  400. $possible_taxonomy_ancestors = array();
  401. $possible_object_parents = array();
  402. $home_page_id = (int) get_option( 'page_for_posts' );
  403. if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
  404. foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
  405. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  406. $term_hierarchy = _get_term_hierarchy( $taxonomy );
  407. $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
  408. if ( is_array( $terms ) ) {
  409. $possible_object_parents = array_merge( $possible_object_parents, $terms );
  410. $term_to_ancestor = array();
  411. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  412. foreach ( (array) $descs as $desc )
  413. $term_to_ancestor[ $desc ] = $anc;
  414. }
  415. foreach ( $terms as $desc ) {
  416. do {
  417. $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
  418. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  419. $_desc = $term_to_ancestor[ $desc ];
  420. unset( $term_to_ancestor[ $desc ] );
  421. $desc = $_desc;
  422. } else {
  423. $desc = 0;
  424. }
  425. } while ( ! empty( $desc ) );
  426. }
  427. }
  428. }
  429. }
  430. } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
  431. $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
  432. $term_to_ancestor = array();
  433. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  434. foreach ( (array) $descs as $desc )
  435. $term_to_ancestor[ $desc ] = $anc;
  436. }
  437. $desc = $queried_object->term_id;
  438. do {
  439. $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
  440. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  441. $_desc = $term_to_ancestor[ $desc ];
  442. unset( $term_to_ancestor[ $desc ] );
  443. $desc = $_desc;
  444. } else {
  445. $desc = 0;
  446. }
  447. } while ( ! empty( $desc ) );
  448. }
  449. $possible_object_parents = array_filter( $possible_object_parents );
  450. $front_page_url = home_url();
  451. foreach ( (array) $menu_items as $key => $menu_item ) {
  452. $menu_items[$key]->current = false;
  453. $classes = (array) $menu_item->classes;
  454. $classes[] = 'menu-item';
  455. $classes[] = 'menu-item-type-' . $menu_item->type;
  456. $classes[] = 'menu-item-object-' . $menu_item->object;
  457. // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
  458. if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
  459. $active_parent_object_ids[] = (int) $menu_item->object_id;
  460. $active_parent_item_ids[] = (int) $menu_item->db_id;
  461. $active_object = $queried_object->post_type;
  462. // if the menu item corresponds to the currently-queried post or taxonomy object
  463. } elseif (
  464. $menu_item->object_id == $queried_object_id &&
  465. (
  466. ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
  467. ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
  468. ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
  469. )
  470. ) {
  471. $classes[] = 'current-menu-item';
  472. $menu_items[$key]->current = true;
  473. $_anc_id = (int) $menu_item->db_id;
  474. while(
  475. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  476. ! in_array( $_anc_id, $active_ancestor_item_ids )
  477. ) {
  478. $active_ancestor_item_ids[] = $_anc_id;
  479. }
  480. if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
  481. // Back compat classes for pages to match wp_page_menu()
  482. $classes[] = 'page_item';
  483. $classes[] = 'page-item-' . $menu_item->object_id;
  484. $classes[] = 'current_page_item';
  485. }
  486. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  487. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  488. $active_object = $menu_item->object;
  489. // if the menu item corresponds to the currently-requested URL
  490. } elseif ( 'custom' == $menu_item->object ) {
  491. $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
  492. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
  493. $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
  494. $item_url = untrailingslashit( $raw_item_url );
  495. $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
  496. if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
  497. $classes[] = 'current-menu-item';
  498. $menu_items[$key]->current = true;
  499. $_anc_id = (int) $menu_item->db_id;
  500. while(
  501. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  502. ! in_array( $_anc_id, $active_ancestor_item_ids )
  503. ) {
  504. $active_ancestor_item_ids[] = $_anc_id;
  505. }
  506. if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
  507. // Back compat for home link to match wp_page_menu()
  508. $classes[] = 'current_page_item';
  509. }
  510. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  511. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  512. $active_object = $menu_item->object;
  513. // give front page item current-menu-item class when extra query arguments involved
  514. } elseif ( $item_url == $front_page_url && is_front_page() ) {
  515. $classes[] = 'current-menu-item';
  516. }
  517. if ( untrailingslashit($item_url) == home_url() )
  518. $classes[] = 'menu-item-home';
  519. }
  520. // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
  521. if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
  522. $classes[] = 'current_page_parent';
  523. $menu_items[$key]->classes = array_unique( $classes );
  524. }
  525. $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
  526. $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
  527. $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
  528. // set parent's class
  529. foreach ( (array) $menu_items as $key => $parent_item ) {
  530. $classes = (array) $parent_item->classes;
  531. $menu_items[$key]->current_item_ancestor = false;
  532. $menu_items[$key]->current_item_parent = false;
  533. if (
  534. isset( $parent_item->type ) &&
  535. (
  536. // ancestral post object
  537. (
  538. 'post_type' == $parent_item->type &&
  539. ! empty( $queried_object->post_type ) &&
  540. is_post_type_hierarchical( $queried_object->post_type ) &&
  541. in_array( $parent_item->object_id, $queried_object->ancestors ) &&
  542. $parent_item->object != $queried_object->ID
  543. ) ||
  544. // ancestral term
  545. (
  546. 'taxonomy' == $parent_item->type &&
  547. isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  548. in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  549. (
  550. ! isset( $queried_object->term_id ) ||
  551. $parent_item->object_id != $queried_object->term_id
  552. )
  553. )
  554. )
  555. ) {
  556. $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
  557. }
  558. if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
  559. $classes[] = 'current-menu-ancestor';
  560. $menu_items[$key]->current_item_ancestor = true;
  561. }
  562. if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
  563. $classes[] = 'current-menu-parent';
  564. $menu_items[$key]->current_item_parent = true;
  565. }
  566. if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
  567. $classes[] = 'current-' . $active_object . '-parent';
  568. if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
  569. // Back compat classes for pages to match wp_page_menu()
  570. if ( in_array('current-menu-parent', $classes) )
  571. $classes[] = 'current_page_parent';
  572. if ( in_array('current-menu-ancestor', $classes) )
  573. $classes[] = 'current_page_ancestor';
  574. }
  575. $menu_items[$key]->classes = array_unique( $classes );
  576. }
  577. }
  578. /**
  579. * Retrieve the HTML list content for nav menu items.
  580. *
  581. * @uses Walker_Nav_Menu to create HTML list content.
  582. * @since 3.0.0
  583. * @see Walker::walk() for parameters and return description.
  584. */
  585. function walk_nav_menu_tree( $items, $depth, $r ) {
  586. $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
  587. $args = array( $items, $depth, $r );
  588. return call_user_func_array( array($walker, 'walk'), $args );
  589. }
  590. /**
  591. * Prevents a menu item ID from being used more than once.
  592. *
  593. * @since 3.0.1
  594. * @access private
  595. */
  596. function _nav_menu_item_id_use_once( $id, $item ) {
  597. static $_used_ids = array();
  598. if ( in_array( $item->ID, $_used_ids ) )
  599. return '';
  600. $_used_ids[] = $item->ID;
  601. return $id;
  602. }
  603. add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );