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

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

https://bitbucket.org/praveen_excell/opshop
PHP | 491 lines | 299 code | 68 blank | 124 comment | 89 complexity | 0300272b30ff3731ec4799987c7e141d MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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. 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, $args ) );
  69. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  70. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  71. $id = $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 = 0, $args = array() ) {
  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'. Set to false for no fallback.
  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. * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders.
  117. *
  118. * @since 3.0.0
  119. *
  120. * @param array $args Arguments
  121. */
  122. function wp_nav_menu( $args = array() ) {
  123. static $menu_id_slugs = array();
  124. $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
  125. '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>',
  126. 'depth' => 0, 'walker' => '', 'theme_location' => '' );
  127. $args = wp_parse_args( $args, $defaults );
  128. $args = apply_filters( 'wp_nav_menu_args', $args );
  129. $args = (object) $args;
  130. // Get the nav menu based on the requested menu
  131. $menu = wp_get_nav_menu_object( $args->menu );
  132. // Get the nav menu based on the theme_location
  133. if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
  134. $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
  135. // get the first menu that has items if we still can't find a menu
  136. if ( ! $menu && !$args->theme_location ) {
  137. $menus = wp_get_nav_menus();
  138. foreach ( $menus as $menu_maybe ) {
  139. if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) {
  140. $menu = $menu_maybe;
  141. break;
  142. }
  143. }
  144. }
  145. // If the menu exists, get its items.
  146. if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
  147. $menu_items = wp_get_nav_menu_items( $menu->term_id );
  148. // If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
  149. if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
  150. && $args->fallback_cb && is_callable( $args->fallback_cb ) )
  151. return call_user_func( $args->fallback_cb, (array) $args );
  152. // If no fallback function was specified and the menu doesn't exists, bail.
  153. if ( !$menu || is_wp_error($menu) )
  154. return false;
  155. $nav_menu = $items = '';
  156. $show_container = false;
  157. if ( $args->container ) {
  158. $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
  159. if ( in_array( $args->container, $allowed_tags ) ) {
  160. $show_container = true;
  161. $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
  162. $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
  163. $nav_menu .= '<'. $args->container . $id . $class . '>';
  164. }
  165. }
  166. // Set up the $menu_item variables
  167. _wp_menu_item_classes_by_context( $menu_items );
  168. $sorted_menu_items = array();
  169. foreach ( (array) $menu_items as $key => $menu_item )
  170. $sorted_menu_items[$menu_item->menu_order] = $menu_item;
  171. unset($menu_items);
  172. $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
  173. $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
  174. unset($sorted_menu_items);
  175. // Attributes
  176. if ( ! empty( $args->menu_id ) ) {
  177. $wrap_id = $args->menu_id;
  178. } else {
  179. $wrap_id = 'menu-' . $menu->slug;
  180. while ( in_array( $wrap_id, $menu_id_slugs ) ) {
  181. if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
  182. $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
  183. else
  184. $wrap_id = $wrap_id . '-1';
  185. }
  186. }
  187. $menu_id_slugs[] = $wrap_id;
  188. $wrap_class = $args->menu_class ? $args->menu_class : '';
  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 .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
  193. unset( $items );
  194. if ( $show_container )
  195. $nav_menu .= '</' . $args->container . '>';
  196. $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
  197. if ( $args->echo )
  198. echo $nav_menu;
  199. else
  200. return $nav_menu;
  201. }
  202. /**
  203. * Add the class property classes for the current context, if applicable.
  204. *
  205. * @access private
  206. * @since 3.0
  207. *
  208. * @param array $menu_items The current menu item objects to which to add the class property information.
  209. */
  210. function _wp_menu_item_classes_by_context( &$menu_items ) {
  211. global $wp_query;
  212. $queried_object = $wp_query->get_queried_object();
  213. $queried_object_id = (int) $wp_query->queried_object_id;
  214. $active_object = '';
  215. $active_ancestor_item_ids = array();
  216. $active_parent_item_ids = array();
  217. $active_parent_object_ids = array();
  218. $possible_taxonomy_ancestors = array();
  219. $possible_object_parents = array();
  220. $home_page_id = (int) get_option( 'page_for_posts' );
  221. if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
  222. foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
  223. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  224. $term_hierarchy = _get_term_hierarchy( $taxonomy );
  225. $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
  226. if ( is_array( $terms ) ) {
  227. $possible_object_parents = array_merge( $possible_object_parents, $terms );
  228. $term_to_ancestor = array();
  229. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  230. foreach ( (array) $descs as $desc )
  231. $term_to_ancestor[ $desc ] = $anc;
  232. }
  233. foreach ( $terms as $desc ) {
  234. do {
  235. $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
  236. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  237. $_desc = $term_to_ancestor[ $desc ];
  238. unset( $term_to_ancestor[ $desc ] );
  239. $desc = $_desc;
  240. } else {
  241. $desc = 0;
  242. }
  243. } while ( ! empty( $desc ) );
  244. }
  245. }
  246. }
  247. }
  248. } elseif ( ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) ) {
  249. _get_post_ancestors( $queried_object );
  250. } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
  251. $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
  252. $term_to_ancestor = array();
  253. foreach ( (array) $term_hierarchy as $anc => $descs ) {
  254. foreach ( (array) $descs as $desc )
  255. $term_to_ancestor[ $desc ] = $anc;
  256. }
  257. $desc = $queried_object->term_id;
  258. do {
  259. $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
  260. if ( isset( $term_to_ancestor[ $desc ] ) ) {
  261. $_desc = $term_to_ancestor[ $desc ];
  262. unset( $term_to_ancestor[ $desc ] );
  263. $desc = $_desc;
  264. } else {
  265. $desc = 0;
  266. }
  267. } while ( ! empty( $desc ) );
  268. }
  269. $possible_object_parents = array_filter( $possible_object_parents );
  270. $front_page_url = home_url();
  271. foreach ( (array) $menu_items as $key => $menu_item ) {
  272. $menu_items[$key]->current = false;
  273. $classes = (array) $menu_item->classes;
  274. $classes[] = 'menu-item';
  275. $classes[] = 'menu-item-type-' . $menu_item->type;
  276. $classes[] = 'menu-item-object-' . $menu_item->object;
  277. // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
  278. if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
  279. $active_parent_object_ids[] = (int) $menu_item->object_id;
  280. $active_parent_item_ids[] = (int) $menu_item->db_id;
  281. $active_object = $queried_object->post_type;
  282. // if the menu item corresponds to the currently-queried post or taxonomy object
  283. } elseif (
  284. $menu_item->object_id == $queried_object_id &&
  285. (
  286. ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
  287. ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
  288. ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) )
  289. )
  290. ) {
  291. $classes[] = 'current-menu-item';
  292. $menu_items[$key]->current = true;
  293. $_anc_id = (int) $menu_item->db_id;
  294. while(
  295. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  296. ! in_array( $_anc_id, $active_ancestor_item_ids )
  297. ) {
  298. $active_ancestor_item_ids[] = $_anc_id;
  299. }
  300. if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
  301. // Back compat classes for pages to match wp_page_menu()
  302. $classes[] = 'page_item';
  303. $classes[] = 'page-item-' . $menu_item->object_id;
  304. $classes[] = 'current_page_item';
  305. }
  306. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  307. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  308. $active_object = $menu_item->object;
  309. // if the menu item corresponds to the currently-requested URL
  310. } elseif ( 'custom' == $menu_item->object ) {
  311. $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
  312. $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_root_relative_current;
  313. $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
  314. $item_url = untrailingslashit( $raw_item_url );
  315. $_indexless_current = untrailingslashit( preg_replace( '/index.php$/', '', $current_url ) );
  316. if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
  317. $classes[] = 'current-menu-item';
  318. $menu_items[$key]->current = true;
  319. $_anc_id = (int) $menu_item->db_id;
  320. while(
  321. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  322. ! in_array( $_anc_id, $active_ancestor_item_ids )
  323. ) {
  324. $active_ancestor_item_ids[] = $_anc_id;
  325. }
  326. if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
  327. // Back compat for home link to match wp_page_menu()
  328. $classes[] = 'current_page_item';
  329. }
  330. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  331. $active_parent_object_ids[] = (int) $menu_item->post_parent;
  332. $active_object = $menu_item->object;
  333. // give front page item current-menu-item class when extra query arguments involved
  334. } elseif ( $item_url == $front_page_url && is_front_page() ) {
  335. $classes[] = 'current-menu-item';
  336. }
  337. if ( untrailingslashit($item_url) == home_url() )
  338. $classes[] = 'menu-item-home';
  339. }
  340. // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
  341. if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
  342. $classes[] = 'current_page_parent';
  343. $menu_items[$key]->classes = array_unique( $classes );
  344. }
  345. $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
  346. $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
  347. $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
  348. // set parent's class
  349. foreach ( (array) $menu_items as $key => $parent_item ) {
  350. $classes = (array) $parent_item->classes;
  351. $menu_items[$key]->current_item_ancestor = false;
  352. $menu_items[$key]->current_item_parent = false;
  353. if (
  354. isset( $parent_item->type ) &&
  355. (
  356. // ancestral post object
  357. (
  358. 'post_type' == $parent_item->type &&
  359. ! empty( $queried_object->post_type ) &&
  360. is_post_type_hierarchical( $queried_object->post_type ) &&
  361. in_array( $parent_item->object_id, $queried_object->ancestors ) &&
  362. $parent_item->object != $queried_object->ID
  363. ) ||
  364. // ancestral term
  365. (
  366. 'taxonomy' == $parent_item->type &&
  367. isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  368. in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
  369. (
  370. ! isset( $queried_object->term_id ) ||
  371. $parent_item->object_id != $queried_object->term_id
  372. )
  373. )
  374. )
  375. ) {
  376. $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
  377. }
  378. if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
  379. $classes[] = 'current-menu-ancestor';
  380. $menu_items[$key]->current_item_ancestor = true;
  381. }
  382. if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
  383. $classes[] = 'current-menu-parent';
  384. $menu_items[$key]->current_item_parent = true;
  385. }
  386. if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
  387. $classes[] = 'current-' . $active_object . '-parent';
  388. if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
  389. // Back compat classes for pages to match wp_page_menu()
  390. if ( in_array('current-menu-parent', $classes) )
  391. $classes[] = 'current_page_parent';
  392. if ( in_array('current-menu-ancestor', $classes) )
  393. $classes[] = 'current_page_ancestor';
  394. }
  395. $menu_items[$key]->classes = array_unique( $classes );
  396. }
  397. }
  398. /**
  399. * Retrieve the HTML list content for nav menu items.
  400. *
  401. * @uses Walker_Nav_Menu to create HTML list content.
  402. * @since 3.0.0
  403. * @see Walker::walk() for parameters and return description.
  404. */
  405. function walk_nav_menu_tree( $items, $depth, $r ) {
  406. $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
  407. $args = array( $items, $depth, $r );
  408. return call_user_func_array( array(&$walker, 'walk'), $args );
  409. }
  410. /**
  411. * Prevents a menu item ID from being used more than once.
  412. *
  413. * @since 3.0.1
  414. * @access private
  415. */
  416. function _nav_menu_item_id_use_once( $id, $item ) {
  417. static $_used_ids = array();
  418. if ( in_array( $item->ID, $_used_ids ) )
  419. return '';
  420. $_used_ids[] = $item->ID;
  421. return $id;
  422. }
  423. add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );