PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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