PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/chimbien/mekongtest
PHP | 509 lines | 307 code | 71 blank | 131 comment | 92 complexity | 76165c9336909ac314a7b14fdf78bffb MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.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. * @package WordPress
  13. * @since 3.0.0
  14. * @uses Walker
  15. */
  16. class Walker_Nav_Menu extends Walker {
  17. /**
  18. * @see Walker::$tree_type
  19. * @since 3.0.0
  20. * @var string
  21. */
  22. var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
  23. /**
  24. * @see Walker::$db_fields
  25. * @since 3.0.0
  26. * @todo Decouple this.
  27. * @var array
  28. */
  29. var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
  30. /**
  31. * @see Walker::start_lvl()
  32. * @since 3.0.0
  33. *
  34. * @param string $output Passed by reference. Used to append additional content.
  35. * @param int $depth Depth of page. Used for padding.
  36. */
  37. function start_lvl( &$output, $depth = 0, $args = array() ) {
  38. $indent = str_repeat("\t", $depth);
  39. $output .= "\n$indent<ul class=\"sub-menu\">\n";
  40. }
  41. /**
  42. * @see Walker::end_lvl()
  43. * @since 3.0.0
  44. *
  45. * @param string $output Passed by reference. Used to append additional content.
  46. * @param int $depth Depth of page. Used for padding.
  47. */
  48. function end_lvl( &$output, $depth = 0, $args = array() ) {
  49. $indent = str_repeat("\t", $depth);
  50. $output .= "$indent</ul>\n";
  51. }
  52. /**
  53. * @see Walker::start_el()
  54. * @since 3.0.0
  55. *
  56. * @param string $output Passed by reference. Used to append additional content.
  57. * @param object $item Menu item data object.
  58. * @param int $depth Depth of menu item. Used for padding.
  59. * @param int $current_page Menu item ID.
  60. * @param object $args
  61. */
  62. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  63. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  64. $class_names = $value = '';
  65. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  66. $classes[] = 'menu-item-' . $item->ID;
  67. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  68. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  69. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  70. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  71. $output .= $indent . '<li' . $id . $value . $class_names .'>';
  72. $atts = array();
  73. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  74. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  75. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  76. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  77. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  78. $attributes = '';
  79. foreach ( $atts as $attr => $value ) {
  80. if ( ! empty( $value ) ) {
  81. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  82. $attributes .= ' ' . $attr . '="' . $value . '"';
  83. }
  84. }
  85. $item_output = $args->before;
  86. $item_output .= '<a'. $attributes .'>';
  87. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  88. $item_output .= '</a>';
  89. $item_output .= $args->after;
  90. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  91. }
  92. /**
  93. * @see Walker::end_el()
  94. * @since 3.0.0
  95. *
  96. * @param string $output Passed by reference. Used to append additional content.
  97. * @param object $item Page data object. Not used.
  98. * @param int $depth Depth of page. Not Used.
  99. */
  100. function end_el( &$output, $item, $depth = 0, $args = array() ) {
  101. $output .= "</li>\n";
  102. }
  103. }
  104. /**
  105. * Displays a navigation menu.
  106. *
  107. * Optional $args contents:
  108. *
  109. * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank.
  110. * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
  111. * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
  112. * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
  113. * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
  114. * container_id - The ID that is applied to the container. Defaults to blank.
  115. * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'. Set to false for no fallback.
  116. * before - Text before the link text.
  117. * after - Text after the link text.
  118. * link_before - Text before the link.
  119. * link_after - Text after the link.
  120. * echo - Whether to echo the menu or return it. Defaults to echo.
  121. * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0.
  122. * walker - allows a custom walker to be specified.
  123. * theme_location - the location in the theme to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
  124. * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders.
  125. *
  126. * @since 3.0.0
  127. *
  128. * @param array $args Arguments
  129. */
  130. function wp_nav_menu( $args = array() ) {
  131. static $menu_id_slugs = array();
  132. $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
  133. '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>',
  134. 'depth' => 0, 'walker' => '', 'theme_location' => '' );
  135. $args = wp_parse_args( $args, $defaults );
  136. $args = apply_filters( 'wp_nav_menu_args', $args );
  137. $args = (object) $args;
  138. // Get the nav menu based on the requested menu
  139. $menu = wp_get_nav_menu_object( $args->menu );
  140. // Get the nav menu based on the theme_location
  141. if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
  142. $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
  143. // get the first menu that has items if we still can't find a menu
  144. if ( ! $menu && !$args->theme_location ) {
  145. $menus = wp_get_nav_menus();
  146. foreach ( $menus as $menu_maybe ) {
  147. if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
  148. $menu = $menu_maybe;
  149. break;
  150. }
  151. }
  152. }
  153. // If the menu exists, get its items.
  154. if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
  155. $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
  156. /*
  157. * If no menu was found:
  158. * - Fall back (if one was specified), or bail.
  159. *
  160. * If no menu items were found:
  161. * - Fall back, but only if no theme location was specified.
  162. * - Otherwise, bail.
  163. */
  164. if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
  165. && $args->fallback_cb && is_callable( $args->fallback_cb ) )
  166. return call_user_func( $args->fallback_cb, (array) $args );
  167. if ( ! $menu || is_wp_error( $menu ) )
  168. return false;
  169. $nav_menu = $items = '';
  170. $show_container = false;
  171. if ( $args->container ) {
  172. $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
  173. if ( in_array( $args->container, $allowed_tags ) ) {
  174. $show_container = true;
  175. $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
  176. $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
  177. $nav_menu .= '<'. $args->container . $id . $class . '>';
  178. }
  179. }
  180. // Set up the $menu_item variables
  181. _wp_menu_item_classes_by_context( $menu_items );
  182. $sorted_menu_items = array();
  183. foreach ( (array) $menu_items as $key => $menu_item )
  184. $sorted_menu_items[$menu_item->menu_order] = $menu_item;
  185. unset($menu_items);
  186. $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
  187. $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
  188. unset($sorted_menu_items);
  189. // Attributes
  190. if ( ! empty( $args->menu_id ) ) {
  191. $wrap_id = $args->menu_id;
  192. } else {
  193. $wrap_id = 'menu-' . $menu->slug;
  194. while ( in_array( $wrap_id, $menu_id_slugs ) ) {
  195. if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
  196. $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
  197. else
  198. $wrap_id = $wrap_id . '-1';
  199. }
  200. }
  201. $menu_id_slugs[] = $wrap_id;
  202. $wrap_class = $args->menu_class ? $args->menu_class : '';
  203. // Allow plugins to hook into the menu to add their own <li>'s
  204. $items = apply_filters( 'wp_nav_menu_items', $items, $args );
  205. $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
  206. // Don't print any markup if there are no items at this point.
  207. if ( empty( $items ) )
  208. return false;
  209. $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
  210. unset( $items );
  211. if ( $show_container )
  212. $nav_menu .= '</' . $args->container . '>';
  213. $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
  214. if ( $args->echo )
  215. echo $nav_menu;
  216. else
  217. return $nav_menu;
  218. }
  219. /**
  220. * Add the class property classes for the current context, if applicable.
  221. *
  222. * @access private
  223. * @since 3.0
  224. *
  225. * @param array $menu_items The current menu item objects to which to add the class property information.
  226. */
  227. function _wp_menu_item_classes_by_context( &$menu_items ) {
  228. global $wp_query, $wp_rewrite;
  229. $queried_object = $wp_query->get_queried_object();
  230. $queried_object_id = (int) $wp_query->queried_object_id;
  231. $active_object = '';
  232. $active_ancestor_item_ids = array();
  233. $active_parent_item_ids = array();
  234. $active_parent_object_ids = array();
  235. $possible_taxonomy_ancestors = array();
  236. $possible_object_parents = array();
  237. $home_page_id = (int) get_option( 'page_for_posts' );
  238. if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
  239. foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
  240. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  241. $term_hierarchy = _get_term_hierarchy( $taxonomy );
  242. $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
  243. if ( is_array( $terms ) ) {
  244. $possible_object_parents = array_merge( $possible_object_parents, $terms );
  245. $term_to_ancestor = array();
  246. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  247. foreach ( (array) $descs as $desc )
  248. $term_to_ancestor[ $desc ] = $anc;
  249. }
  250. foreach ( $terms as $desc ) {
  251. do {
  252. $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
  253. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  254. $_desc = $term_to_ancestor[ $desc ];
  255. unset( $term_to_ancestor[ $desc ] );
  256. $desc = $_desc;
  257. } else {
  258. $desc = 0;
  259. }
  260. } while ( ! empty( $desc ) );
  261. }
  262. }
  263. }
  264. }
  265. } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
  266. $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
  267. $term_to_ancestor = array();
  268. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  269. foreach ( (array) $descs as $desc )
  270. $term_to_ancestor[ $desc ] = $anc;
  271. }
  272. $desc = $queried_object->term_id;
  273. do {
  274. $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
  275. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  276. $_desc = $term_to_ancestor[ $desc ];
  277. unset( $term_to_ancestor[ $desc ] );
  278. $desc = $_desc;
  279. } else {
  280. $desc = 0;
  281. }
  282. } while ( ! empty( $desc ) );
  283. }
  284. $possible_object_parents = array_filter( $possible_object_parents );
  285. $front_page_url = home_url();
  286. foreach ( (array) $menu_items as $key => $menu_item ) {
  287. $menu_items[$key]->current = false;
  288. $classes = (array) $menu_item->classes;
  289. $classes[] = 'menu-item';
  290. $classes[] = 'menu-item-type-' . $menu_item->type;
  291. $classes[] = 'menu-item-object-' . $menu_item->object;
  292. // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
  293. if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
  294. $active_parent_object_ids[] = (int) $menu_item->object_id;
  295. $active_parent_item_ids[] = (int) $menu_item->db_id;
  296. $active_object = $queried_object->post_type;
  297. // if the menu item corresponds to the currently-queried post or taxonomy object
  298. } elseif (
  299. $menu_item->object_id == $queried_object_id &&
  300. (
  301. ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
  302. ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
  303. ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
  304. )
  305. ) {
  306. $classes[] = 'current-menu-item';
  307. $menu_items[$key]->current = true;
  308. $_anc_id = (int) $menu_item->db_id;
  309. while(
  310. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  311. ! in_array( $_anc_id, $active_ancestor_item_ids )
  312. ) {
  313. $active_ancestor_item_ids[] = $_anc_id;
  314. }
  315. if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
  316. // Back compat classes for pages to match wp_page_menu()
  317. $classes[] = 'page_item';
  318. $classes[] = 'page-item-' . $menu_item->object_id;
  319. $classes[] = 'current_page_item';
  320. }
  321. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  322. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  323. $active_object = $menu_item->object;
  324. // if the menu item corresponds to the currently-requested URL
  325. } elseif ( 'custom' == $menu_item->object ) {
  326. $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
  327. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
  328. $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
  329. $item_url = untrailingslashit( $raw_item_url );
  330. $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
  331. if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
  332. $classes[] = 'current-menu-item';
  333. $menu_items[$key]->current = true;
  334. $_anc_id = (int) $menu_item->db_id;
  335. while(
  336. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  337. ! in_array( $_anc_id, $active_ancestor_item_ids )
  338. ) {
  339. $active_ancestor_item_ids[] = $_anc_id;
  340. }
  341. if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
  342. // Back compat for home link to match wp_page_menu()
  343. $classes[] = 'current_page_item';
  344. }
  345. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  346. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  347. $active_object = $menu_item->object;
  348. // give front page item current-menu-item class when extra query arguments involved
  349. } elseif ( $item_url == $front_page_url && is_front_page() ) {
  350. $classes[] = 'current-menu-item';
  351. }
  352. if ( untrailingslashit($item_url) == home_url() )
  353. $classes[] = 'menu-item-home';
  354. }
  355. // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
  356. if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
  357. $classes[] = 'current_page_parent';
  358. $menu_items[$key]->classes = array_unique( $classes );
  359. }
  360. $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
  361. $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
  362. $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
  363. // set parent's class
  364. foreach ( (array) $menu_items as $key => $parent_item ) {
  365. $classes = (array) $parent_item->classes;
  366. $menu_items[$key]->current_item_ancestor = false;
  367. $menu_items[$key]->current_item_parent = false;
  368. if (
  369. isset( $parent_item->type ) &&
  370. (
  371. // ancestral post object
  372. (
  373. 'post_type' == $parent_item->type &&
  374. ! empty( $queried_object->post_type ) &&
  375. is_post_type_hierarchical( $queried_object->post_type ) &&
  376. in_array( $parent_item->object_id, $queried_object->ancestors ) &&
  377. $parent_item->object != $queried_object->ID
  378. ) ||
  379. // ancestral term
  380. (
  381. 'taxonomy' == $parent_item->type &&
  382. isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  383. in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  384. (
  385. ! isset( $queried_object->term_id ) ||
  386. $parent_item->object_id != $queried_object->term_id
  387. )
  388. )
  389. )
  390. ) {
  391. $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
  392. }
  393. if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
  394. $classes[] = 'current-menu-ancestor';
  395. $menu_items[$key]->current_item_ancestor = true;
  396. }
  397. if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
  398. $classes[] = 'current-menu-parent';
  399. $menu_items[$key]->current_item_parent = true;
  400. }
  401. if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
  402. $classes[] = 'current-' . $active_object . '-parent';
  403. if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
  404. // Back compat classes for pages to match wp_page_menu()
  405. if ( in_array('current-menu-parent', $classes) )
  406. $classes[] = 'current_page_parent';
  407. if ( in_array('current-menu-ancestor', $classes) )
  408. $classes[] = 'current_page_ancestor';
  409. }
  410. $menu_items[$key]->classes = array_unique( $classes );
  411. }
  412. }
  413. /**
  414. * Retrieve the HTML list content for nav menu items.
  415. *
  416. * @uses Walker_Nav_Menu to create HTML list content.
  417. * @since 3.0.0
  418. * @see Walker::walk() for parameters and return description.
  419. */
  420. function walk_nav_menu_tree( $items, $depth, $r ) {
  421. $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
  422. $args = array( $items, $depth, $r );
  423. return call_user_func_array( array($walker, 'walk'), $args );
  424. }
  425. /**
  426. * Prevents a menu item ID from being used more than once.
  427. *
  428. * @since 3.0.1
  429. * @access private
  430. */
  431. function _nav_menu_item_id_use_once( $id, $item ) {
  432. static $_used_ids = array();
  433. if ( in_array( $item->ID, $_used_ids ) )
  434. return '';
  435. $_used_ids[] = $item->ID;
  436. return $id;
  437. }
  438. add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );