PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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