Passed
Push — development ( 7327cc...058d58 )
by Eric
03:29 queued 38s
created

SpursWPBootstrapNavwalker::display_element()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 6
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Adapted from Edward McIntyre's wp_bootstrap_navwalker class.
4
 * Removed support for glyphicon and added support for Font Awesome.
5
 *
6
 * @package spurs
7
 */
8
9
// Exit if accessed directly.
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
if ( ! class_exists( 'SpursWPBootstrapNavwalker' ) ) :
1 ignored issue
show
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
15
16
	/**
17
	 * Class WP_Bootstrap_Navwalker
18
	 * GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker
19
	 * Description: A custom WordPress nav walker class to implement the Bootstrap 4
20
	 * navigation style in a custom theme using the WordPress built in menu manager.
21
	 * Version: 2.0.4
22
	 * Author: Edward McIntyre - @twittem
23
	 * License: GPL-2.0+
24
	 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
25
	 */
26
	class SpursWPBootstrapNavwalker extends Walker_Nav_Menu {
27
		/**
28
		 * The starting level of the menu.
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
		 * @param mixed $args Rest of arguments.
36
		 */
37
		public function start_lvl( &$output, $depth = 0, $args = array() ) {
0 ignored issues
show
Coding Style Naming introduced by
The method start_lvl is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
38
			$indent = str_repeat( "\t", $depth );
39
			$output .= "\n$indent<ul class=\" dropdown-menu\" role=\"menu\">\n";
40
		}
41
42
		/**
43
		 * Open element.
44
		 *
45
		 * @see Walker::start_el()
46
		 * @since 3.0.0
47
		 *
48
		 * @param string $output Passed by reference. Used to append additional content.
49
		 * @param object $item Menu item data object.
50
		 * @param int $depth Depth of menu item. Used for padding.
51
		 * @param mixed $args Rest arguments.
52
		 * @param int $id Element's ID.
53
		 */
54
		public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
0 ignored issues
show
Complexity introduced by
This operation has 918750035 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
Coding Style Naming introduced by
The method start_el is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $class_names is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $item_output is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
55
			$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
56
			/**
57
			 * Dividers, Headers or Disabled
58
			 * =============================
59
			 * Determine whether the item is a Divider, Header, Disabled or regular
60
			 * menu item. To prevent errors we use the strcasecmp() function to so a
61
			 * comparison that is not case sensitive. The strcasecmp() function returns
62
			 * a 0 if the strings are equal.
63
			 */
64
			if ( 0 == strcasecmp( $item->attr_title, 'divider' ) && 1 === $depth ) {
65
				$output .= $indent . '<li class="dropdown-divider" role="presentation">';
66
			} else if ( 0 == strcasecmp( $item->title, 'divider' ) && 1 === $depth ) {
67
				$output .= $indent . '<li class="dropdown-divider" role="presentation">';
68
			} else if ( 0 == strcasecmp( $item->attr_title, 'dropdown-header' ) && 1 === $depth ) {
69
				$output .= $indent . '<li class="dropdown-header" role="presentation">' . esc_html( $item->title );
70
			} else if ( 0 == strcasecmp( $item->attr_title, 'disabled' ) ) {
71
				$output .= $indent . '<li class="disabled" role="presentation"><a href="#">' . esc_html( $item->title ) . '</a>';
72
			} else {
73
				$class_names = $value = '';
74
				$classes     = empty( $item->classes ) ? array() : ( array ) $item->classes;
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(array)" but found "( array )"
Loading history...
75
				$classes[]   = 'nav-item menu-item-' . $item->ID;
76
				$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
77
				/*
78
				if ( $args->has_children )
79
				  $class_names .= ' dropdown';
80
				*/
81
				if ( $args->has_children && 0 === $depth ) {
82
					$class_names .= ' dropdown';
83
				} elseif ( $args->has_children && $depth > 0 ) {
84
					$class_names .= ' dropdown-submenu';
85
				}
86
				if ( in_array( 'current-menu-item', $classes ) ) {
87
					$class_names .= ' active';
88
				}
89
				// remove Font Awesome icon from classes array and save the icon
90
				// we will add the icon back in via a <span> below so it aligns with
91
				// the menu item
92
				if ( in_array( 'fa', $classes ) ) {
93
					$key         = array_search( 'fa', $classes );
94
					$icon        = $classes[$key + 1];
95
					$class_names = str_replace( $classes[$key + 1], '', $class_names );
96
					$class_names = str_replace( $classes[$key], '', $class_names );
97
				}
98
99
				$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
100
				$id          = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $id. This often makes code more readable.
Loading history...
101
				$id          = $id ? ' id="' . esc_attr( $id ) . '"' : '';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $id. This often makes code more readable.
Loading history...
102
				$output .= $indent . '<li' . $id . $value . $class_names . '>';
103
				$atts        = array();
104
				if ( empty( $item->attr_title ) ) {
105
					$atts['title'] = ! empty( $item->title ) ? strip_tags( $item->title ) : '';
106
				} else {
107
					$atts['title'] = $item->attr_title;
108
				}
109
				$atts['target'] = ! empty( $item->target ) ? $item->target : '';
110
				$atts['rel']    = ! empty( $item->xfn ) ? $item->xfn : '';
111
				// If item has_children add atts to a.
112
113
				if ( $args->has_children && 0 === $depth ) {
114
					$atts['href']        = '#';
115
					$atts['data-toggle'] = 'dropdown';
116
					$atts['class']       = 'nav-link dropdown-toggle';
117
				} else {
118
					$atts['href']  = ! empty( $item->url ) ? $item->url : '';
119
					$atts['class'] = 'nav-link';
120
				}
121
				$atts       = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
122
				$attributes = '';
123
				foreach ( $atts as $attr => $value ) {
124
					if ( ! empty( $value ) ) {
125
						$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
126
						$attributes .= ' ' . $attr . '="' . $value . '"';
127
					}
128
				}
129
				$item_output = $args->before;
130
				// Font Awesome icons
131
				if ( ! empty( $icon ) ) {
132
					$item_output .= '<a' . $attributes . '><span class="fa ' . esc_attr( $icon ) . '"></span>&nbsp;';
133
				} else {
134
					$item_output .= '<a' . $attributes . '>';
135
				}
136
				$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
137
				$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
138
				$item_output .= $args->after;
139
				$output      .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
140
			}
141
		}
142
143
		/**
144
		 * Traverse elements to create list from elements.
145
		 *
146
		 * Display one element if the element doesn't have any children otherwise,
147
		 * display the element and its children. Will only traverse up to the max
148
		 * depth and no ignore elements under that depth.
149
		 *
150
		 * This method shouldn't be called directly, use the walk() method instead.
151
		 *
152
		 * @see Walker::start_el()
153
		 * @since 2.5.0
154
		 *
155
		 * @param object $element Data object
156
		 * @param array $children_elements List of elements to continue traversing.
157
		 * @param int $max_depth Max depth to traverse.
158
		 * @param int $depth Depth of current element.
159
		 * @param array $args
160
		 * @param string $output Passed by reference. Used to append additional content.
161
		 *
162
		 * @return null Null on failure with no changes to parameters.
163
		 */
164
		public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
0 ignored issues
show
Coding Style Naming introduced by
The method display_element is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $children_elements is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $max_depth is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $id_field is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $children_elements is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $max_depth is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
165
			if ( ! $element ) {
166
				return;
167
			}
168
			$id_field = $this->db_fields['id'];
169
			// Display this element.
170
			if ( is_object( $args[0] ) ) {
171
				$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
172
			}
173
			parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
174
		}
175
176
		/**
177
		 * Menu Fallback
178
		 * =============
179
		 * If this function is assigned to the wp_nav_menu's fallback_cb variable
180
		 * and a manu has not been assigned to the theme location in the WordPress
181
		 * menu manager the function with display nothing to a non-logged in user,
182
		 * and will add a link to the WordPress menu manager if logged in as an admin.
183
		 *
184
		 * @param array $args passed from the wp_nav_menu function.
185
		 *
186
		 */
187
		public static function fallback( $args ) {
0 ignored issues
show
Coding Style Naming introduced by
The variable $fb_output is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $container_class is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $container_id is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $menu_class is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $menu_id is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
188
			if ( current_user_can( 'manage_options' ) ) {
189
				extract( $args );
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
190
				$fb_output = null;
191
				if ( $container ) {
192
					$fb_output = '<' . $container;
193
					if ( $container_class ) {
194
						$fb_output .= ' class="' . $container_class . '"';
195
					}
196
					if ( $container_id ) {
197
						$fb_output .= ' id="' . $container_id . '"';
198
					}
199
					$fb_output .= '>';
200
				}
201
				$fb_output .= '<ul';
202
				if ( $menu_class ) {
203
					$fb_output .= ' class="' . $menu_class . '"';
204
				}
205
				if ( $menu_id ) {
206
					$fb_output .= ' id="' . $menu_id . '"';
207
				}
208
				$fb_output .= '>';
209
				$fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
210
				$fb_output .= '</ul>';
211
				if ( $container ) {
212
					$fb_output .= '</' . $container . '>';
213
				}
214
				echo $fb_output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$fb_output'
Loading history...
215
			}
216
		}
217
	}
218
219
endif; /* End if class exists */
220