wp_bootstrap_navwalker.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Class Name: wp_bootstrap_navwalker
  4. * GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker
  5. * Description: A custom WordPress nav walker class to implement the Bootstrap 3 navigation style in a custom theme using the WordPress built in menu manager.
  6. * Version: 2.0.4
  7. * Author: Edward McIntyre - @twittem
  8. * License: GPL-2.0+
  9. * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
  10. */
  11. class wp_bootstrap_navwalker extends Walker_Nav_Menu {
  12. /**
  13. * @see Walker::start_lvl()
  14. * @since 3.0.0
  15. *
  16. * @param string $output Passed by reference. Used to append additional content.
  17. * @param int $depth Depth of page. Used for padding.
  18. */
  19. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  20. $indent = str_repeat( "\t", $depth );
  21. $output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\">\n";
  22. }
  23. /**
  24. * @see Walker::start_el()
  25. * @since 3.0.0
  26. *
  27. * @param string $output Passed by reference. Used to append additional content.
  28. * @param object $item Menu item data object.
  29. * @param int $depth Depth of menu item. Used for padding.
  30. * @param int $current_page Menu item ID.
  31. * @param object $args
  32. */
  33. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  34. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  35. /**
  36. * Dividers, Headers or Disabled
  37. * =============================
  38. * Determine whether the item is a Divider, Header, Disabled or regular
  39. * menu item. To prevent errors we use the strcasecmp() function to so a
  40. * comparison that is not case sensitive. The strcasecmp() function returns
  41. * a 0 if the strings are equal.
  42. */
  43. if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
  44. $output .= $indent . '<li role="presentation" class="divider">';
  45. } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
  46. $output .= $indent . '<li role="presentation" class="divider">';
  47. } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
  48. $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
  49. } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
  50. $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
  51. } else {
  52. $class_names = $value = '';
  53. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  54. $classes[] = 'menu-item-' . $item->ID;
  55. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  56. if ( $args->has_children )
  57. $class_names .= ' dropdown';
  58. if ( in_array( 'current-menu-item', $classes ) )
  59. $class_names .= ' active';
  60. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  61. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  62. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  63. $output .= $indent . '<li' . $id . $value . $class_names .'>';
  64. $atts = array();
  65. $atts['title'] = ! empty( $item->title ) ? $item->title : '';
  66. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  67. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  68. // If item has_children add atts to a.
  69. if ( $args->has_children && $depth === 0 ) {
  70. $atts['href'] = '#';
  71. $atts['data-toggle'] = 'dropdown';
  72. $atts['class'] = 'dropdown-toggle';
  73. $atts['aria-haspopup'] = 'true';
  74. } else {
  75. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  76. }
  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. /*
  87. * Glyphicons
  88. * ===========
  89. * Since the the menu item is NOT a Divider or Header we check the see
  90. * if there is a value in the attr_title property. If the attr_title
  91. * property is NOT null we apply it as the class name for the glyphicon.
  92. */
  93. if ( ! empty( $item->attr_title ) )
  94. $item_output .= '<a'. $attributes .'><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span>&nbsp;';
  95. else
  96. $item_output .= '<a'. $attributes .'>';
  97. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  98. $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
  99. $item_output .= $args->after;
  100. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  101. }
  102. }
  103. /**
  104. * Traverse elements to create list from elements.
  105. *
  106. * Display one element if the element doesn't have any children otherwise,
  107. * display the element and its children. Will only traverse up to the max
  108. * depth and no ignore elements under that depth.
  109. *
  110. * This method shouldn't be called directly, use the walk() method instead.
  111. *
  112. * @see Walker::start_el()
  113. * @since 2.5.0
  114. *
  115. * @param object $element Data object
  116. * @param array $children_elements List of elements to continue traversing.
  117. * @param int $max_depth Max depth to traverse.
  118. * @param int $depth Depth of current element.
  119. * @param array $args
  120. * @param string $output Passed by reference. Used to append additional content.
  121. * @return null Null on failure with no changes to parameters.
  122. */
  123. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  124. if ( ! $element )
  125. return;
  126. $id_field = $this->db_fields['id'];
  127. // Display this element.
  128. if ( is_object( $args[0] ) )
  129. $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
  130. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  131. }
  132. /**
  133. * Menu Fallback
  134. * =============
  135. * If this function is assigned to the wp_nav_menu's fallback_cb variable
  136. * and a manu has not been assigned to the theme location in the WordPress
  137. * menu manager the function with display nothing to a non-logged in user,
  138. * and will add a link to the WordPress menu manager if logged in as an admin.
  139. *
  140. * @param array $args passed from the wp_nav_menu function.
  141. *
  142. */
  143. public static function fallback( $args ) {
  144. if ( current_user_can( 'manage_options' ) ) {
  145. extract( $args );
  146. $fb_output = null;
  147. if ( $container ) {
  148. $fb_output = '<' . $container;
  149. if ( $container_id )
  150. $fb_output .= ' id="' . $container_id . '"';
  151. if ( $container_class )
  152. $fb_output .= ' class="' . $container_class . '"';
  153. $fb_output .= '>';
  154. }
  155. $fb_output .= '<ul';
  156. if ( $menu_id )
  157. $fb_output .= ' id="' . $menu_id . '"';
  158. if ( $menu_class )
  159. $fb_output .= ' class="' . $menu_class . '"';
  160. $fb_output .= '>';
  161. $fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
  162. $fb_output .= '</ul>';
  163. if ( $container )
  164. $fb_output .= '</' . $container . '>';
  165. echo $fb_output;
  166. }
  167. }
  168. }