PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/geyson/geyson
PHP | 694 lines | 326 code | 76 blank | 292 comment | 99 complexity | 5792f1c7fbe309c80c9788e332296454 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Navigation Menu template functions
  4. *
  5. * @package WordPress
  6. * @subpackage Nav_Menus
  7. * @since 3.0.0
  8. */
  9. /**
  10. * Create HTML list of nav menu items.
  11. *
  12. * @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. * @staticvar array $menu_id_slugs
  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 object|false|void 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();
  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 ( empty( $args->menu ) ) {
  264. $args->menu = $menu;
  265. }
  266. // If the menu exists, get its items.
  267. if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
  268. $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
  269. /*
  270. * If no menu was found:
  271. * - Fall back (if one was specified), or bail.
  272. *
  273. * If no menu items were found:
  274. * - Fall back, but only if no theme location was specified.
  275. * - Otherwise, bail.
  276. */
  277. if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
  278. && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) )
  279. return call_user_func( $args->fallback_cb, (array) $args );
  280. if ( ! $menu || is_wp_error( $menu ) )
  281. return false;
  282. $nav_menu = $items = '';
  283. $show_container = false;
  284. if ( $args->container ) {
  285. /**
  286. * Filter the list of HTML tags that are valid for use as menu containers.
  287. *
  288. * @since 3.0.0
  289. *
  290. * @param array $tags The acceptable HTML tags for use as menu containers.
  291. * Default is array containing 'div' and 'nav'.
  292. */
  293. $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
  294. if ( in_array( $args->container, $allowed_tags ) ) {
  295. $show_container = true;
  296. $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
  297. $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
  298. $nav_menu .= '<'. $args->container . $id . $class . '>';
  299. }
  300. }
  301. // Set up the $menu_item variables
  302. _wp_menu_item_classes_by_context( $menu_items );
  303. $sorted_menu_items = $menu_items_with_children = array();
  304. foreach ( (array) $menu_items as $menu_item ) {
  305. $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
  306. if ( $menu_item->menu_item_parent )
  307. $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
  308. }
  309. // Add the menu-item-has-children class where applicable
  310. if ( $menu_items_with_children ) {
  311. foreach ( $sorted_menu_items as &$menu_item ) {
  312. if ( isset( $menu_items_with_children[ $menu_item->ID ] ) )
  313. $menu_item->classes[] = 'menu-item-has-children';
  314. }
  315. }
  316. unset( $menu_items, $menu_item );
  317. /**
  318. * Filter the sorted list of menu item objects before generating the menu's HTML.
  319. *
  320. * @since 3.1.0
  321. *
  322. * @param array $sorted_menu_items The menu items, sorted by each menu item's menu order.
  323. * @param object $args An object containing wp_nav_menu() arguments.
  324. */
  325. $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
  326. $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
  327. unset($sorted_menu_items);
  328. // Attributes
  329. if ( ! empty( $args->menu_id ) ) {
  330. $wrap_id = $args->menu_id;
  331. } else {
  332. $wrap_id = 'menu-' . $menu->slug;
  333. while ( in_array( $wrap_id, $menu_id_slugs ) ) {
  334. if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
  335. $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
  336. else
  337. $wrap_id = $wrap_id . '-1';
  338. }
  339. }
  340. $menu_id_slugs[] = $wrap_id;
  341. $wrap_class = $args->menu_class ? $args->menu_class : '';
  342. /**
  343. * Filter the HTML list content for navigation menus.
  344. *
  345. * @since 3.0.0
  346. *
  347. * @see wp_nav_menu()
  348. *
  349. * @param string $items The HTML list content for the menu items.
  350. * @param object $args An object containing wp_nav_menu() arguments.
  351. */
  352. $items = apply_filters( 'wp_nav_menu_items', $items, $args );
  353. /**
  354. * Filter the HTML list content for a specific navigation menu.
  355. *
  356. * @since 3.0.0
  357. *
  358. * @see wp_nav_menu()
  359. *
  360. * @param string $items The HTML list content for the menu items.
  361. * @param object $args An object containing wp_nav_menu() arguments.
  362. */
  363. $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
  364. // Don't print any markup if there are no items at this point.
  365. if ( empty( $items ) )
  366. return false;
  367. $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
  368. unset( $items );
  369. if ( $show_container )
  370. $nav_menu .= '</' . $args->container . '>';
  371. /**
  372. * Filter the HTML content for navigation menus.
  373. *
  374. * @since 3.0.0
  375. *
  376. * @see wp_nav_menu()
  377. *
  378. * @param string $nav_menu The HTML content for the navigation menu.
  379. * @param object $args An object containing wp_nav_menu() arguments.
  380. */
  381. $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
  382. if ( $args->echo )
  383. echo $nav_menu;
  384. else
  385. return $nav_menu;
  386. }
  387. /**
  388. * Add the class property classes for the current context, if applicable.
  389. *
  390. * @access private
  391. * @since 3.0.0
  392. *
  393. * @global WP_Query $wp_query
  394. * @global WP_Rewrite $wp_rewrite
  395. *
  396. * @param array $menu_items The current menu item objects to which to add the class property information.
  397. */
  398. function _wp_menu_item_classes_by_context( &$menu_items ) {
  399. global $wp_query, $wp_rewrite;
  400. $queried_object = $wp_query->get_queried_object();
  401. $queried_object_id = (int) $wp_query->queried_object_id;
  402. $active_object = '';
  403. $active_ancestor_item_ids = array();
  404. $active_parent_item_ids = array();
  405. $active_parent_object_ids = array();
  406. $possible_taxonomy_ancestors = array();
  407. $possible_object_parents = array();
  408. $home_page_id = (int) get_option( 'page_for_posts' );
  409. if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
  410. foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
  411. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  412. $term_hierarchy = _get_term_hierarchy( $taxonomy );
  413. $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
  414. if ( is_array( $terms ) ) {
  415. $possible_object_parents = array_merge( $possible_object_parents, $terms );
  416. $term_to_ancestor = array();
  417. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  418. foreach ( (array) $descs as $desc )
  419. $term_to_ancestor[ $desc ] = $anc;
  420. }
  421. foreach ( $terms as $desc ) {
  422. do {
  423. $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
  424. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  425. $_desc = $term_to_ancestor[ $desc ];
  426. unset( $term_to_ancestor[ $desc ] );
  427. $desc = $_desc;
  428. } else {
  429. $desc = 0;
  430. }
  431. } while ( ! empty( $desc ) );
  432. }
  433. }
  434. }
  435. }
  436. } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
  437. $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
  438. $term_to_ancestor = array();
  439. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  440. foreach ( (array) $descs as $desc )
  441. $term_to_ancestor[ $desc ] = $anc;
  442. }
  443. $desc = $queried_object->term_id;
  444. do {
  445. $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
  446. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  447. $_desc = $term_to_ancestor[ $desc ];
  448. unset( $term_to_ancestor[ $desc ] );
  449. $desc = $_desc;
  450. } else {
  451. $desc = 0;
  452. }
  453. } while ( ! empty( $desc ) );
  454. }
  455. $possible_object_parents = array_filter( $possible_object_parents );
  456. $front_page_url = home_url();
  457. foreach ( (array) $menu_items as $key => $menu_item ) {
  458. $menu_items[$key]->current = false;
  459. $classes = (array) $menu_item->classes;
  460. $classes[] = 'menu-item';
  461. $classes[] = 'menu-item-type-' . $menu_item->type;
  462. $classes[] = 'menu-item-object-' . $menu_item->object;
  463. // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
  464. if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
  465. $active_parent_object_ids[] = (int) $menu_item->object_id;
  466. $active_parent_item_ids[] = (int) $menu_item->db_id;
  467. $active_object = $queried_object->post_type;
  468. // if the menu item corresponds to the currently-queried post or taxonomy object
  469. } elseif (
  470. $menu_item->object_id == $queried_object_id &&
  471. (
  472. ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
  473. ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
  474. ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
  475. )
  476. ) {
  477. $classes[] = 'current-menu-item';
  478. $menu_items[$key]->current = true;
  479. $_anc_id = (int) $menu_item->db_id;
  480. while(
  481. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  482. ! in_array( $_anc_id, $active_ancestor_item_ids )
  483. ) {
  484. $active_ancestor_item_ids[] = $_anc_id;
  485. }
  486. if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
  487. // Back compat classes for pages to match wp_page_menu()
  488. $classes[] = 'page_item';
  489. $classes[] = 'page-item-' . $menu_item->object_id;
  490. $classes[] = 'current_page_item';
  491. }
  492. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  493. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  494. $active_object = $menu_item->object;
  495. // if the menu item corresponds to the currently-requested URL
  496. } elseif ( 'custom' == $menu_item->object ) {
  497. $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
  498. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
  499. $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
  500. $item_url = set_url_scheme( untrailingslashit( $raw_item_url ) );
  501. $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
  502. if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
  503. $classes[] = 'current-menu-item';
  504. $menu_items[$key]->current = true;
  505. $_anc_id = (int) $menu_item->db_id;
  506. while(
  507. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  508. ! in_array( $_anc_id, $active_ancestor_item_ids )
  509. ) {
  510. $active_ancestor_item_ids[] = $_anc_id;
  511. }
  512. if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
  513. // Back compat for home link to match wp_page_menu()
  514. $classes[] = 'current_page_item';
  515. }
  516. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  517. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  518. $active_object = $menu_item->object;
  519. // give front page item current-menu-item class when extra query arguments involved
  520. } elseif ( $item_url == $front_page_url && is_front_page() ) {
  521. $classes[] = 'current-menu-item';
  522. }
  523. if ( untrailingslashit($item_url) == home_url() )
  524. $classes[] = 'menu-item-home';
  525. }
  526. // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
  527. if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
  528. $classes[] = 'current_page_parent';
  529. $menu_items[$key]->classes = array_unique( $classes );
  530. }
  531. $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
  532. $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
  533. $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
  534. // set parent's class
  535. foreach ( (array) $menu_items as $key => $parent_item ) {
  536. $classes = (array) $parent_item->classes;
  537. $menu_items[$key]->current_item_ancestor = false;
  538. $menu_items[$key]->current_item_parent = false;
  539. if (
  540. isset( $parent_item->type ) &&
  541. (
  542. // ancestral post object
  543. (
  544. 'post_type' == $parent_item->type &&
  545. ! empty( $queried_object->post_type ) &&
  546. is_post_type_hierarchical( $queried_object->post_type ) &&
  547. in_array( $parent_item->object_id, $queried_object->ancestors ) &&
  548. $parent_item->object != $queried_object->ID
  549. ) ||
  550. // ancestral term
  551. (
  552. 'taxonomy' == $parent_item->type &&
  553. isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  554. in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  555. (
  556. ! isset( $queried_object->term_id ) ||
  557. $parent_item->object_id != $queried_object->term_id
  558. )
  559. )
  560. )
  561. ) {
  562. $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
  563. }
  564. if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
  565. $classes[] = 'current-menu-ancestor';
  566. $menu_items[$key]->current_item_ancestor = true;
  567. }
  568. if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
  569. $classes[] = 'current-menu-parent';
  570. $menu_items[$key]->current_item_parent = true;
  571. }
  572. if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
  573. $classes[] = 'current-' . $active_object . '-parent';
  574. if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
  575. // Back compat classes for pages to match wp_page_menu()
  576. if ( in_array('current-menu-parent', $classes) )
  577. $classes[] = 'current_page_parent';
  578. if ( in_array('current-menu-ancestor', $classes) )
  579. $classes[] = 'current_page_ancestor';
  580. }
  581. $menu_items[$key]->classes = array_unique( $classes );
  582. }
  583. }
  584. /**
  585. * Retrieve the HTML list content for nav menu items.
  586. *
  587. * @uses Walker_Nav_Menu to create HTML list content.
  588. * @since 3.0.0
  589. *
  590. * @param array $items
  591. * @param int $depth
  592. * @param object $r
  593. * @return string
  594. */
  595. function walk_nav_menu_tree( $items, $depth, $r ) {
  596. $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
  597. $args = array( $items, $depth, $r );
  598. return call_user_func_array( array( $walker, 'walk' ), $args );
  599. }
  600. /**
  601. * Prevents a menu item ID from being used more than once.
  602. *
  603. * @since 3.0.1
  604. * @access private
  605. *
  606. * @staticvar array $used_ids
  607. * @param string $id
  608. * @param object $item
  609. * @return string
  610. */
  611. function _nav_menu_item_id_use_once( $id, $item ) {
  612. static $_used_ids = array();
  613. if ( in_array( $item->ID, $_used_ids ) ) {
  614. return '';
  615. }
  616. $_used_ids[] = $item->ID;
  617. return $id;
  618. }