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

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

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