PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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