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

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

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