Completed
Push — master ( 8990f9...572472 )
by Brandon
03:51 queued 01:46
created

WP_Bootstrap_Navwalker::fallback()   C

Complexity

Conditions 8
Paths 41

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 24
nc 41
nop 1
1
<?php
2
/**
3
 * WP Bootstrap Navwalker
4
 *
5
 * @package WP-Bootstrap-Navwalker
6
 */
7
8
/**
9
 * Class Name: WP_Bootstrap_Navwalker
10
 * Plugin Name: WP Bootstrap Navwalker
11
 * Plugin URI:  https://github.com/wp-bootstrap/wp-bootstrap-navwalker
12
 * 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.
13
 * Author: Edward McIntyre - @twittem, WP Bootstrap
14
 * Version: 2.0.5
15
 * Author URI: https://github.com/wp-bootstrap
16
 * GitHub Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
17
 * GitHub Branch: master
18
 * License: GPL-3.0+
19
 * License URI: https://www.gnu.org/licenses/gpl-3.0.txt
20
 */
21
22
/* Check if Class Exists. */
23
if ( ! class_exists( 'WP_Bootstrap_Navwalker' ) ) {
24
	/**
25
	 * WP_Bootstrap_Navwalker class.
26
	 *
27
	 * @extends Walker_Nav_Menu
28
	 */
29
	class WP_Bootstrap_Navwalker extends Walker_Nav_Menu {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
31
		/**
32
		 * Start Level.
33
		 *
34
		 * @see Walker::start_lvl()
35
		 * @since 3.0.0
36
		 *
37
		 * @access public
38
		 * @param mixed $output Passed by reference. Used to append additional content.
39
		 * @param int   $depth (default: 0) Depth of page. Used for padding.
40
		 * @param array $args (default: array()) Arguments.
41
		 * @return void
42
		 */
43
		public function start_lvl( &$output, $depth = 0, $args = array() ) {
44
			$indent = str_repeat( "\t", $depth );
45
			$output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\" >\n";
46
		}
47
48
		/**
49
		 * Start El.
50
		 *
51
		 * @see Walker::start_el()
52
		 * @since 3.0.0
53
		 *
54
		 * @access public
55
		 * @param mixed $output Passed by reference. Used to append additional content.
56
		 * @param mixed $item Menu item data object.
57
		 * @param int   $depth (default: 0) Depth of menu item. Used for padding.
58
		 * @param array $args (default: array()) Arguments.
59
		 * @param int   $id (default: 0) Menu item ID.
60
		 * @return void
61
		 */
62
		public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
63
			$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
64
65
			/**
66
			 * Dividers, Headers or Disabled
67
			 * =============================
68
			 * Determine whether the item is a Divider, Header, Disabled or regular
69
			 * menu item. To prevent errors we use the strcasecmp() function to so a
70
			 * comparison that is not case sensitive. The strcasecmp() function returns
71
			 * a 0 if the strings are equal.
72
			 */
73
			if ( 0 === strcasecmp( $item->attr_title, 'divider' ) && 1 === $depth ) {
74
				$output .= $indent . '<li role="presentation" class="divider">';
75
			} elseif ( 0 === strcasecmp( $item->title, 'divider' ) && 1 === $depth ) {
76
				$output .= $indent . '<li role="presentation" class="divider">';
77
			} elseif ( 0 === strcasecmp( $item->attr_title, 'dropdown-header' ) && 1 === $depth ) {
78
				$output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
79
			} elseif ( 0 === strcasecmp( $item->attr_title, 'disabled' ) ) {
80
				$output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
81
			} else {
82
				$value = '';
83
				$class_names = $value;
0 ignored issues
show
Unused Code introduced by
$class_names is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
				$classes = empty( $item->classes ) ? array() : (array) $item->classes;
85
				$classes[] = 'menu-item-' . $item->ID;
86
				$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
87
				if ( $args->has_children ) {
88
					$class_names .= ' dropdown';
89
				}
90
				if ( in_array( 'current-menu-item', $classes, true ) ) {
91
					$class_names .= ' active';
92
				}
93
				$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
94
				$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
95
				$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
96
				$output .= $indent . '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement"' . $id . $value . $class_names . '>';
97
				$atts = array();
98
99
				if ( empty( $item->attr_title ) ) {
100
					$atts['title']  = ! empty( $item->title )   ? strip_tags( $item->title ) : '';
101
				} else {
102
					$atts['title'] = $item->attr_title;
103
				}
104
105
				$atts['target'] = ! empty( $item->target ) ? $item->target : '';
106
				$atts['rel']    = ! empty( $item->xfn )    ? $item->xfn    : '';
107
				// If item has_children add atts to a.
108
				if ( $args->has_children && 0 === $depth ) {
109
					$atts['href']           = '#';
110
					$atts['data-toggle']    = 'dropdown';
111
					$atts['class']          = 'dropdown-toggle';
112
					$atts['aria-haspopup']  = 'true';
113
				} else {
114
					$atts['href'] = ! empty( $item->url ) ? $item->url : '';
115
				}
116
				$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
117
				$attributes = '';
118
				foreach ( $atts as $attr => $value ) {
119
					if ( ! empty( $value ) ) {
120
						$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
121
						$attributes .= ' ' . $attr . '="' . $value . '"';
122
					}
123
				}
124
				$item_output = $args->before;
125
126
				/*
127
				 * Glyphicons/Font-Awesome
128
				 * ===========
129
				 * Since the the menu item is NOT a Divider or Header we check the see
130
				 * if there is a value in the attr_title property. If the attr_title
131
				 * property is NOT null we apply it as the class name for the glyphicon.
132
				 */
133
				if ( ! empty( $item->attr_title ) ) {
134
					$pos = strpos( esc_attr( $item->attr_title ), 'glyphicon' );
135
					if ( false !== $pos ) {
136
						$item_output .= '<a' . $attributes . '><span class="glyphicon ' . esc_attr( $item->attr_title ) . '" aria-hidden="true"></span>&nbsp;';
137
					} else {
138
						$item_output .= '<a' . $attributes . '><i class="fa ' . esc_attr( $item->attr_title ) . '" aria-hidden="true"></i>&nbsp;';
139
					}
140
				} else {
141
					$item_output .= '<a' . $attributes . '>';
142
				}
143
				$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
144
				$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
145
				$item_output .= $args->after;
146
				$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
147
			} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
148
		}
149
150
		/**
151
		 * Traverse elements to create list from elements.
152
		 *
153
		 * Display one element if the element doesn't have any children otherwise,
154
		 * display the element and its children. Will only traverse up to the max
155
		 * depth and no ignore elements under that depth.
156
		 *
157
		 * This method shouldn't be called directly, use the walk() method instead.
158
		 *
159
		 * @see Walker::start_el()
160
		 * @since 2.5.0
161
		 *
162
		 * @access public
163
		 * @param mixed $element Data object.
164
		 * @param mixed $children_elements List of elements to continue traversing.
165
		 * @param mixed $max_depth Max depth to traverse.
166
		 * @param mixed $depth Depth of current element.
167
		 * @param mixed $args Arguments.
168
		 * @param mixed $output Passed by reference. Used to append additional content.
169
		 * @return null Null on failure with no changes to parameters.
170
		 */
171
		public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
172
			if ( ! $element ) {
173
				return; }
174
			$id_field = $this->db_fields['id'];
175
			// Display this element.
176
			if ( is_object( $args[0] ) ) {
177
				$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] ); }
178
			parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
179
		}
180
181
		/**
182
		 * Menu Fallback
183
		 * =============
184
		 * If this function is assigned to the wp_nav_menu's fallback_cb variable
185
		 * and a menu has not been assigned to the theme location in the WordPress
186
		 * menu manager the function with display nothing to a non-logged in user,
187
		 * and will add a link to the WordPress menu manager if logged in as an admin.
188
		 *
189
		 * @param array $args passed from the wp_nav_menu function.
190
		 */
191
		public static function fallback( $args ) {
192
			if ( current_user_can( 'edit_theme_options' ) ) {
193
194
				/* Get Arguments. */
195
				$container = $args['container'];
196
				$container_id = $args['container_id'];
197
				$container_class = $args['container_class'];
198
				$menu_class = $args['menu_class'];
199
				$menu_id = $args['menu_id'];
200
201
				if ( $container ) {
202
					echo '<' . esc_attr( $container );
203
					if ( $container_id ) {
204
						echo ' id="' . esc_attr( $container_id ) . '"';
205
					}
206
					if ( $container_class ) {
207
						echo ' class="' . sanitize_html_class( $container_class ) . '"'; }
208
					echo '>';
209
				}
210
				echo '<ul';
211
				if ( $menu_id ) {
212
					echo ' id="' . esc_attr( $menu_id ) . '"'; }
213
				if ( $menu_class ) {
214
					echo ' class="' . esc_attr( $menu_class ) . '"'; }
215
				echo '>';
216
				echo '<li><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '" title="">' . esc_attr( 'Add a menu', '' ) . '</a></li>';
217
				echo '</ul>';
218
				if ( $container ) {
219
					echo '</' . esc_attr( $container ) . '>'; }
220
			}
221
		}
222
	}
223
} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
224