Completed
Push — master ( baabf5...12a59e )
by Warwick
03:22
created

LSX_Bootstrap_Navwalker::fallback()   B

Complexity

Conditions 8
Paths 41

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 41
nop 1
dl 0
loc 39
rs 8.0515
c 0
b 0
f 0
1
<?php
2
/**
3
 * LSX functions and definitions - Bootstrap Navigation Walker
4
 *
5
 * @package    lsx
6
 * @subpackage navigation
7
 * @category   bootstrap-navigation-walker
8
 */
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
if ( ! class_exists( 'Walker_Nav_Menu' ) ) {
15
	return;
16
}
17
18
if ( ! class_exists( 'LSX_Bootstrap_Navwalker' ) ) :
19
20
	/**
21
	 * Cleaner Bootstrap walker
22
	 *
23
	 * @package    lsx
24
	 * @subpackage navigation
25
	 * @category   bootstrap-navigation-walker
26
	 */
27
	class LSX_Bootstrap_Navwalker extends Walker_Nav_Menu {
28
29
		/**
30
		 * @see Walker::start_lvl()
31
		 * @since 3.0.0
32
		 *
33
		 * @param string $output Passed by reference. Used to append additional content.
34
		 * @param int $depth Depth of page. Used for padding.
35
		 */
36
		public function start_lvl( &$output, $depth = 0, $args = array() ) {
37
			$indent = str_repeat( "\t", $depth );
38
			$output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\">\n";
39
		}
40
41
		/**
42
		 * @param string $item Passed by reference. Used to append additional content.
43
		 */
44
		public function filter_default_pages( &$item ) {
45
			return $item;
46
		}
47
48
		/**
49
		 * @see Walker::start_el()
50
		 * @since 3.0.0
51
		 *
52
		 * @param string $output Passed by reference. Used to append additional content.
53
		 * @param object $item Menu item data object.
54
		 * @param int $depth Depth of menu item. Used for padding.
55
		 * @param int $current_page Menu item ID.
0 ignored issues
show
Bug introduced by
There is no parameter named $current_page. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
		 * @param object $args
57
		 */
58
		public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
59
			$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
60
61
			/**
62
			 * If this is a default menu being called we need to fix
63
			 * the item object thats coming through.
64
			 */
65
			if ( ! isset( $item->title ) ) {
66
				return;
67
			}
68
69
			/**
70
			 * Dividers, Headers or Disabled
71
			 * =============================
72
			 * Determine whether the item is a Divider, Header, Disabled or regular
73
			 * menu item. To prevent errors we use the strcasecmp() function to so a
74
			 * comparison that is not case sensitive. The strcasecmp() function returns
75
			 * a 0 if the strings are equal.
76
			 */
77
			if ( 0 == strcasecmp( $item->attr_title, 'divider' ) && 1 === $depth ) {
78
				$output .= $indent . '<li role="presentation" class="divider">';
79
			} elseif ( 0 == strcasecmp( $item->title, 'divider' ) && 1 === $depth ) {
80
				$output .= $indent . '<li role="presentation" class="divider">';
81
			} elseif ( 0 == strcasecmp( $item->attr_title, 'dropdown-header' ) && 1 === $depth ) {
82
				$output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
83
			} elseif ( 0 == strcasecmp( $item->attr_title, 'disabled' ) ) {
84
				$output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
85
			} else {
86
				$class_names = '';
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...
87
				$value       = '';
88
89
				$classes = empty( $item->classes ) ? array() : (array) $item->classes;
90
				$classes[] = 'menu-item-' . $item->ID;
91
92
				$classes = apply_filters( 'lsx_nav_menu_css_class', array_filter( $classes ), $item, $args , $depth );
93
94
				$class_names = join( ' ', $classes );
95
96
				if ( $args->has_children )
97
					$class_names .= ' dropdown';
98
99
				if ( in_array( 'current-menu-item', $classes ) )
100
					$class_names .= ' active';
101
102
				if ( in_array( 'current-menu-parent', $classes ) )
103
					$class_names .= ' active';
104
105
				//Check if this is ment to be a "social" type menu
106
				$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
107
108
				$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
109
				$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
110
111
				$output .= $indent . '<li' . $id . $value . $class_names . '>';
112
113
				$atts = array();
114
				$atts['title']  = ! empty( $item->title ) ? $item->title : '';
115
				$atts['target'] = ! empty( $item->target ) ? $item->target : '';
116
				$atts['rel']    = ! empty( $item->xfn ) ? $item->xfn : '';
117
118
				// If item has_children add atts to a.
119
				if ( $args->has_children ) {
120
					$atts['href']          = ! empty( $item->url ) ? $item->url : '';
121
					$atts['data-toggle']   = 'dropdown';
122
					$atts['class']         = 'dropdown-toggle';
123
					$atts['aria-haspopup'] = 'true';
124
				} else {
125
					$atts['href'] = ! empty( $item->url ) ? $item->url : '';
126
				}
127
128
				$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
129
130
				$attributes = '';
131
				foreach ( $atts as $attr => $value ) {
132
					if ( ! empty( $value ) ) {
133
						$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
134
						$attributes .= ' ' . $attr . '="' . $value . '"';
135
					}
136
				}
137
138
				$item_output = $args->before;
139
140
				$item_output .= '<a' . $attributes . '>';
141
				$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
142
				$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
143
				$item_output .= $args->after;
144
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
		 * @param object $element Data object
162
		 * @param array $children_elements List of elements to continue traversing.
163
		 * @param int $max_depth Max depth to traverse.
164
		 * @param int $depth Depth of current element.
165
		 * @param array $args
166
		 * @param string $output Passed by reference. Used to append additional content.
167
		 * @return null Null on failure with no changes to parameters.
168
		 */
169
		public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
170
			if ( ! $element ) {
171
				return;
172
			}
173
174
			$id_field = $this->db_fields['id'];
175
176
			if ( is_object( $args[0] ) ) {
177
				$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
178
			}
179
180
			parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
181
		}
182
183
		/**
184
		 * Menu Fallback
185
		 * =============
186
		 * If this function is assigned to the wp_nav_menu's fallback_cb variable
187
		 * and a manu has not been assigned to the theme location in the WordPress
188
		 * menu manager the function with display nothing to a non-logged in user,
189
		 * and will add a link to the WordPress menu manager if logged in as an admin.
190
		 *
191
		 * @param array $args passed from the wp_nav_menu function.
192
		 *
193
		 */
194
		public static function fallback( $args ) {
195
			if ( current_user_can( 'manage_options' ) ) {
196
				$fb_output = null;
197
198
				if ( $args['container'] ) {
199
					$fb_output = '<' . $args['container'];
200
201
					if ( $args['container_id'] ) {
202
						$fb_output .= ' id="' . $args['container_id'] . '"';
203
					}
204
205
					if ( $args['container_class'] ) {
206
						$fb_output .= ' class="' . $args['container_class'] . '"';
207
					}
208
209
					$fb_output .= '>';
210
				}
211
212
				$fb_output .= '<ul';
213
214
				if ( $args['menu_id'] ) {
215
					$fb_output .= ' id="' . $args['menu_id'] . '"';
216
				}
217
218
				if ( $args['menu_class'] ) {
219
					$fb_output .= ' class="' . $args['menu_class'] . '"';
220
				}
221
222
				$fb_output .= '>';
223
				$fb_output .= '<li><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">' . esc_html__( 'Add a menu', 'lsx' ) . '</a></li>';
224
				$fb_output .= '</ul>';
225
226
				if ( $args['container'] ) {
227
					$fb_output .= '</' . $args['container'] . '>';
228
				}
229
230
				echo wp_kses_post( $fb_output );
231
			}
232
		}
233
234
	}
235
236
endif;
237