wp_bootstrap_navwalker.php 7.2 KB

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