1 <?php
2
3 4 5 6 7
8
9 namespace nk2580\wordsmith\Utillities\NavWalkers;
10
11 12 13 14 15
16 class Bootstrap extends \Walker_Nav_Menu {
17
18 19 20 21 22 23 24
25 public function start_lvl( &$output, $depth = 0, $args = array() ) {
26 $indent = str_repeat( "\t", $depth );
27 $output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\">\n";
28 }
29
30 31 32 33 34 35 36 37 38 39
40 public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
41 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
42
43 44 45 46 47 48 49 50
51 if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
52 $output .= $indent . '<li role="presentation" class="divider">';
53 } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
54 $output .= $indent . '<li role="presentation" class="divider">';
55 } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
56 $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
57 } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
58 $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
59 } else {
60
61 $class_names = $value = '';
62
63 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
64 $classes[] = 'menu-item-' . $item->ID;
65
66 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
67
68 if ( $args->has_children )
69 $class_names .= ' dropdown';
70
71 if ( in_array( 'current-menu-item', $classes ) )
72 $class_names .= ' active';
73
74 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
75
76 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
77 $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
78
79 $output .= $indent . '<li' . $id . $value . $class_names .'>';
80
81 $atts = array();
82 $atts['title'] = ! empty( $item->title ) ? $item->title : '';
83 $atts['target'] = ! empty( $item->target ) ? $item->target : '';
84 $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
85
86
87 if ( $args->has_children && $depth === 0 ) {
88 $atts['href'] = '#';
89 $atts['data-toggle'] = 'dropdown';
90 $atts['class'] = 'dropdown-toggle';
91 $atts['aria-haspopup'] = 'true';
92 } else {
93 $atts['href'] = ! empty( $item->url ) ? $item->url : '';
94 }
95
96 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
97
98 $attributes = '';
99 foreach ( $atts as $attr => $value ) {
100 if ( ! empty( $value ) ) {
101 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
102 $attributes .= ' ' . $attr . '="' . $value . '"';
103 }
104 }
105
106 $item_output = $args->before;
107
108 109 110 111 112 113 114
115 if ( ! empty( $item->attr_title ) )
116 $item_output .= '<a'. $attributes .'><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span> ';
117 else
118 $item_output .= '<a'. $attributes .'>';
119
120 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
121 $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
122 $item_output .= $args->after;
123
124 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
125 }
126 }
127
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
148 public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
149 if ( ! $element )
150 return;
151
152 $id_field = $this->db_fields['id'];
153
154
155 if ( is_object( $args[0] ) )
156 $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
157
158 parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
159 }
160
161 162 163 164 165 166 167 168 169 170 171
172 public static function fallback( $args ) {
173 if ( current_user_can( 'manage_options' ) ) {
174
175 extract( $args );
176
177 $fb_output = null;
178
179 if ( $container ) {
180 $fb_output = '<' . $container;
181
182 if ( $container_id )
183 $fb_output .= ' id="' . $container_id . '"';
184
185 if ( $container_class )
186 $fb_output .= ' class="' . $container_class . '"';
187
188 $fb_output .= '>';
189 }
190
191 $fb_output .= '<ul';
192
193 if ( $menu_id )
194 $fb_output .= ' id="' . $menu_id . '"';
195
196 if ( $menu_class )
197 $fb_output .= ' class="' . $menu_class . '"';
198
199 $fb_output .= '>';
200 $fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
201 $fb_output .= '</ul>';
202
203 if ( $container )
204 $fb_output .= '</' . $container . '>';
205
206 echo $fb_output;
207 }
208 }
209
210 }
211