Completed
Pull Request — master (#216)
by
unknown
01:51
created

wp-bootstrap-navwalker.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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: http://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
			$args->top_level_follow = isset($args->top_level_follow) ? $args->top_level_follow : false;
66
67
			/**
68
			* Dividers, Headers or Disabled
69
			* =============================
70
			* Determine whether the item is a Divider, Header, Disabled or regular
71
			* menu item. To prevent errors we use the strcasecmp() function to so a
72
			* comparison that is not case sensitive. The strcasecmp() function returns
73
			* a 0 if the strings are equal.
74
		 	*/
75
			if ( 0 === strcasecmp( $item->attr_title, 'divider' ) && 1 === $depth ) {
76
				$output .= $indent . '<li role="presentation" class="divider">';
77
			} elseif ( 0 === strcasecmp( $item->title, 'divider' ) && 1 === $depth ) {
78
				$output .= $indent . '<li role="presentation" class="divider">';
79
			} elseif ( 0 === strcasecmp( $item->attr_title, 'dropdown-header' ) && 1 === $depth ) {
80
				$output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
81
			} elseif ( 0 === strcasecmp( $item->attr_title, 'disabled' ) ) {
82
				$output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
83
			} else {
84
				$class_names = $value = '';
0 ignored issues
show
$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...
85
				$classes = empty( $item->classes ) ? array() : (array) $item->classes;
86
				$classes[] = 'menu-item-' . $item->ID;
87
				$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
88
				if ( $args->has_children ) {
89
					$class_names .= ' dropdown'; }
90
				if ( in_array( 'current-menu-item', $classes, true ) ) {
91
					$class_names .= ' active'; }
92
				$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
93
				$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
94
				$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
95
				$output .= $indent . '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement"' . $id . $value . $class_names . '>';
96
				$atts = array();
97
98
				if ( empty( $item->attr_title ) ) {
99
					$atts['title']  = ! empty( $item->title )   ? strip_tags( $item->title ) : '';
100
				} else {
101
					$atts['title'] = $item->attr_title;
102
				}
103
104
				$atts['target'] = ! empty( $item->target )	? $item->target	: '';
105
				$atts['rel']    = ! empty( $item->xfn )		? $item->xfn	: '';
106
				// If item has_children add atts to a.
107
				if ( $args->has_children && $depth === 0 && !$args->top_level_follow ) {
108
					$atts['href']			= '#';
109
					$atts['data-toggle']	= 'dropdown';
110
					$atts['class']			= 'dropdown-toggle';
111
					$atts['aria-haspopup']	= 'true';
112
				} else {
113
					$atts['href'] = ! empty( $item->url ) ? $item->url : '';
114
				}
115
				$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
116
				$attributes = '';
117
				foreach ( $atts as $attr => $value ) {
118
					if ( ! empty( $value ) ) {
119
						$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
120
						$attributes .= ' ' . $attr . '="' . $value . '"';
121
					}
122
				}
123
				$item_output = $args->before;
124
125
				/*
126
				 * Glyphicons/Font-Awesome
127
				 * ===========
128
				 * Since the the menu item is NOT a Divider or Header we check the see
129
				 * if there is a value in the attr_title property. If the attr_title
130
				 * property is NOT null we apply it as the class name for the glyphicon.
131
				 */
132
				if ( ! empty( $item->attr_title ) ) :
133
								$pos = strpos( esc_attr( $item->attr_title ), 'glyphicon' );
134
					if ( false !== $pos ) :
135
						$item_output .= '<a' . $attributes . '><span class="glyphicon ' . esc_attr( $item->attr_title ) . '" aria-hidden="true"></span>&nbsp;';
136
								else :
137
									$item_output .= '<a' . $attributes . '><i class="fa ' . esc_attr( $item->attr_title ) . '" aria-hidden="true"></i>&nbsp;';
138
											endif;
139
				else :
140
					$item_output .= '<a' . $attributes . '>';
141
				endif;
142
				$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
143
				$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
144
				$item_output .= $args->after;
145
				$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
146
			}
147
		}
148
149
		/**
150
		 * Traverse elements to create list from elements.
151
		 *
152
		 * Display one element if the element doesn't have any children otherwise,
153
		 * display the element and its children. Will only traverse up to the max
154
		 * depth and no ignore elements under that depth.
155
		 *
156
		 * This method shouldn't be called directly, use the walk() method instead.
157
		 *
158
		 * @see Walker::start_el()
159
		 * @since 2.5.0
160
		 *
161
		 * @access public
162
		 * @param mixed $element Data object.
163
		 * @param mixed $children_elements List of elements to continue traversing.
164
		 * @param mixed $max_depth Max depth to traverse.
165
		 * @param mixed $depth Depth of current element.
166
		 * @param mixed $args Arguments.
167
		 * @param mixed $output Passed by reference. Used to append additional content.
168
		 * @return null Null on failure with no changes to parameters.
169
		 */
170
		public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
171
			if ( ! $element ) {
172
				return; }
173
			$id_field = $this->db_fields['id'];
174
			// Display this element.
175
			if ( is_object( $args[0] ) ) {
176
				$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] ); }
177
			parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
178
		}
179
180
		/**
181
		 * Menu Fallback
182
		 * =============
183
		 * If this function is assigned to the wp_nav_menu's fallback_cb variable
184
		 * and a menu has not been assigned to the theme location in the WordPress
185
		 * menu manager the function with display nothing to a non-logged in user,
186
		 * and will add a link to the WordPress menu manager if logged in as an admin.
187
		 *
188
		 * @param array $args passed from the wp_nav_menu function.
189
		 */
190
		public static function fallback( $args ) {
191
			if ( current_user_can( 'edit_theme_options' ) ) {
192
193
				/* Get Arguments. */
194
				$container = $args['container'];
195
				$container_id = $args['container_id'];
196
				$container_class = $args['container_class'];
197
				$menu_class = $args['menu_class'];
198
				$menu_id = $args['menu_id'];
199
200
				if ( $container ) {
201
					echo '<' . esc_attr( $container );
202
					if ( $container_id ) {
203
						echo ' id="' . esc_attr( $container_id ) . '"';
204
					}
205
					if ( $container_class ) {
206
						echo ' class="' . sanitize_html_class( $container_class ) . '"'; }
207
					echo '>';
208
				}
209
				echo '<ul';
210
				if ( $menu_id ) {
211
					echo ' id="' . esc_attr( $menu_id ) . '"'; }
212
				if ( $menu_class ) {
213
					echo ' class="' . esc_attr( $menu_class ) . '"'; }
214
				echo '>';
215
				echo '<li><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '" title="">' . esc_attr( 'Add a menu', '' ) . '</a></li>';
216
				echo '</ul>';
217
				if ( $container ) {
218
					echo '</' . esc_attr( $container ) . '>'; }
219
			}
220
		}
221
	}
222
}
223