Completed
Pull Request — master (#473)
by
unknown
01:14
created

WP_Bootstrap_Navwalker   F

Complexity

Total Complexity 86

Size/Duplication

Total Lines 547
Duplicated Lines 2.56 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 86
lcom 1
cbo 0
dl 14
loc 547
rs 2
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B start_lvl() 7 40 5
F start_el() 7 209 35
A display_element() 0 9 3
B fallback() 0 43 10
B separate_linkmods_and_icons_from_classes() 0 31 7
B get_linkmod_type() 0 20 7
B update_atts_for_linkmod_type() 0 26 9
A wrap_for_screen_reader() 0 6 2
A linkmod_element_open() 0 16 4
A linkmod_element_close() 0 14 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WP_Bootstrap_Navwalker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WP_Bootstrap_Navwalker, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * WP Bootstrap Navwalker
4
 *
5
 * @package WP-Bootstrap-Navwalker
6
 *
7
 * @wordpress-plugin
8
 * Plugin Name: WP Bootstrap Navwalker
9
 * Plugin URI:  https://github.com/wp-bootstrap/wp-bootstrap-navwalker
10
 * Description: A custom WordPress nav walker class to implement the Bootstrap 4 navigation style in a custom theme using the WordPress built in menu manager.
11
 * Author: Edward McIntyre - @twittem, WP Bootstrap, William Patton - @pattonwebz
12
 * Version: 4.3.0
13
 * Author URI: https://github.com/wp-bootstrap
14
 * GitHub Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
15
 * GitHub Branch: master
16
 * License: GPL-3.0+
17
 * License URI: http://www.gnu.org/licenses/gpl-3.0.txt
18
 */
19
20
// Check if Class Exists.
21
if ( ! class_exists( 'WP_Bootstrap_Navwalker' ) ) {
22
	/**
23
	 * WP_Bootstrap_Navwalker class.
24
	 *
25
	 * @extends Walker_Nav_Menu
26
	 */
27
	class WP_Bootstrap_Navwalker extends Walker_Nav_Menu {
28
29
		/**
30
		 * Starts the list before the elements are added.
31
		 *
32
		 * @since WP 3.0.0
33
		 *
34
		 * @see Walker_Nav_Menu::start_lvl()
35
		 *
36
		 * @param string   $output Used to append additional content (passed by reference).
37
		 * @param int      $depth  Depth of menu item. Used for padding.
38
		 * @param stdClass $args   An object of wp_nav_menu() arguments.
39
		 */
40
		public function start_lvl( &$output, $depth = 0, $args = array() ) {
41 View Code Duplication
			if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
42
				$t = '';
43
				$n = '';
44
			} else {
45
				$t = "\t";
46
				$n = "\n";
47
			}
48
			$indent = str_repeat( $t, $depth );
49
			// Default class to add to the file.
50
			$classes = array( 'dropdown-menu' );
51
			/**
52
			 * Filters the CSS class(es) applied to a menu list element.
53
			 *
54
			 * @since WP 4.8.0
55
			 *
56
			 * @param array    $classes The CSS classes that are applied to the menu `<ul>` element.
57
			 * @param stdClass $args    An object of `wp_nav_menu()` arguments.
58
			 * @param int      $depth   Depth of menu item. Used for padding.
59
			 */
60
			$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
61
			$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
62
63
			/*
64
			 * The `.dropdown-menu` container needs to have a labelledby
65
			 * attribute which points to it's trigger link.
66
			 *
67
			 * Form a string for the labelledby attribute from the the latest
68
			 * link with an id that was added to the $output.
69
			 */
70
			$labelledby = '';
71
			// Find all links with an id in the output.
72
			preg_match_all( '/(<a.*?id=\"|\')(.*?)\"|\'.*?>/im', $output, $matches );
73
			// With pointer at end of array check if we got an ID match.
74
			if ( end( $matches[2] ) ) {
75
				// Build a string to use as aria-labelledby.
76
				$labelledby = 'aria-labelledby="' . esc_attr( end( $matches[2] ) ) . '"';
77
			}
78
			$output .= "{$n}{$indent}<ul$class_names $labelledby role=\"menu\">{$n}";
79
		}
80
81
		/**
82
		 * Starts the element output.
83
		 *
84
		 * @since WP 3.0.0
85
		 * @since WP 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
86
		 *
87
		 * @see Walker_Nav_Menu::start_el()
88
		 *
89
		 * @param string   $output Used to append additional content (passed by reference).
90
		 * @param WP_Post  $item   Menu item data object.
91
		 * @param int      $depth  Depth of menu item. Used for padding.
92
		 * @param stdClass $args   An object of wp_nav_menu() arguments.
93
		 * @param int      $id     Current item ID.
94
		 */
95
		public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
96 View Code Duplication
			if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
97
				$t = '';
98
				$n = '';
0 ignored issues
show
Unused Code introduced by
$n 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...
99
			} else {
100
				$t = "\t";
101
				$n = "\n";
0 ignored issues
show
Unused Code introduced by
$n 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...
102
			}
103
			$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
104
105
			$classes = empty( $item->classes ) ? array() : (array) $item->classes;
106
107
			/*
108
			 * Initialize some holder variables to store specially handled item
109
			 * wrappers and icons.
110
			 */
111
			$linkmod_classes = array();
112
			$icon_classes    = array();
113
114
			/*
115
			 * Get an updated $classes array without linkmod or icon classes.
116
			 *
117
			 * NOTE: linkmod and icon class arrays are passed by reference and
118
			 * are maybe modified before being used later in this function.
119
			 */
120
			$classes = self::separate_linkmods_and_icons_from_classes( $classes, $linkmod_classes, $icon_classes, $depth );
121
122
			// Join any icon classes plucked from $classes into a string.
123
			$icon_class_string = join( ' ', $icon_classes );
124
125
			/**
126
			 * Filters the arguments for a single nav menu item.
127
			 *
128
			 *  WP 4.4.0
129
			 *
130
			 * @param stdClass $args  An object of wp_nav_menu() arguments.
131
			 * @param WP_Post  $item  Menu item data object.
132
			 * @param int      $depth Depth of menu item. Used for padding.
133
			 */
134
			$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
135
136
			// Add .dropdown or .active classes where they are needed.
137
			if ( isset( $args->has_children ) && $args->has_children ) {
138
				$classes[] = 'dropdown';
139
			}
140
			if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) {
141
				$classes[] = 'active';
142
			}
143
144
			// Add some additional default classes to the item.
145
			$classes[] = 'menu-item-' . $item->ID;
146
			$classes[] = 'nav-item';
147
148
			// Allow filtering the classes.
149
			$classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth );
150
151
			// Form a string of classes in format: class="class_names".
152
			$class_names = join( ' ', $classes );
153
			$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
154
155
			/**
156
			 * Filters the ID applied to a menu item's list item element.
157
			 *
158
			 * @since WP 3.0.1
159
			 * @since WP 4.1.0 The `$depth` parameter was added.
160
			 *
161
			 * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
162
			 * @param WP_Post  $item    The current menu item.
163
			 * @param stdClass $args    An object of wp_nav_menu() arguments.
164
			 * @param int      $depth   Depth of menu item. Used for padding.
165
			 */
166
			$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
167
			$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
168
169
			$output .= $indent . '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement"' . $id . $class_names . '>';
170
171
			// Initialize array for holding the $atts for the link item.
172
			$atts = array();
173
174
			/*
175
			 * Set title from item to the $atts array - if title is empty then
176
			 * default to item title.
177
			 */
178
			if ( empty( $item->attr_title ) ) {
179
				$atts['title'] = ! empty( $item->title ) ? strip_tags( $item->title ) : '';
180
			} else {
181
				$atts['title'] = $item->attr_title;
182
			}
183
184
			$atts['target'] = ! empty( $item->target ) ? $item->target : '';
185
			$atts['rel']    = ! empty( $item->xfn ) ? $item->xfn : '';
186
			// If the item has children, add atts to the <a>.
187
			if ( isset( $args->has_children ) && $args->has_children && 0 === $depth && $args->depth > 1 ) {
188
				$atts['href']          = '#';
189
				$atts['data-toggle']   = 'dropdown';
190
				$atts['aria-haspopup'] = 'true';
191
				$atts['aria-expanded'] = 'false';
192
				$atts['class']         = 'dropdown-toggle nav-link';
193
				$atts['id']            = 'menu-item-dropdown-' . $item->ID;
194
			} else {
195
				$atts['href'] = ! empty( $item->url ) ? $item->url : '#';
196
				// For items in dropdowns use .dropdown-item instead of .nav-link.
197
				if ( $depth > 0 ) {
198
					$atts['class'] = 'dropdown-item';
199
				} else {
200
					$atts['class'] = 'nav-link';
201
				}
202
			}
203
204
			$atts['aria-current'] = $item->current ? 'page' : '';
205
206
			// Update atts of this item based on any custom linkmod classes.
207
			$atts = self::update_atts_for_linkmod_type( $atts, $linkmod_classes );
208
			// Allow filtering of the $atts array before using it.
209
			$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
210
211
			// Build a string of html containing all the atts for the item.
212
			$attributes = '';
213
			foreach ( $atts as $attr => $value ) {
214
				if ( ! empty( $value ) ) {
215
					$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
216
					$attributes .= ' ' . $attr . '="' . $value . '"';
217
				}
218
			}
219
220
			// Set a typeflag to easily test if this is a linkmod or not.
221
			$linkmod_type = self::get_linkmod_type( $linkmod_classes );
222
223
			// START appending the internal item contents to the output.
224
			$item_output = isset( $args->before ) ? $args->before : '';
225
226
			/*
227
			 * This is the start of the internal nav item. Depending on what
228
			 * kind of linkmod we have we may need different wrapper elements.
229
			 */
230
			if ( '' !== $linkmod_type ) {
231
				// Is linkmod, output the required element opener.
232
				$item_output .= self::linkmod_element_open( $linkmod_type, $attributes );
233
			} else {
234
				// With no link mod type set this must be a standard <a> tag.
235
				$item_output .= '<a' . $attributes . '>';
236
			}
237
238
			/*
239
			 * Initiate empty icon var, then if we have a string containing any
240
			 * icon classes form the icon markup with an <i> element. This is
241
			 * output inside of the item before the $title (the link text).
242
			 */
243
			$icon_html = '';
244
			if ( ! empty( $icon_class_string ) ) {
245
				// Append an <i> with the icon classes to what is output before links.
246
				$icon_html = '<i class="' . esc_attr( $icon_class_string ) . '" aria-hidden="true"></i> ';
247
			}
248
249
			/**
250
			 * Allows titles to be optionally escaped by setting 'escape_titles' => true/false in the wp_nav_menu() args.
251
			 * wp_nav_menu(array( 'escape_titles'  => false, 'walker' => new WP_Bootstrap_Navwalker() ));
252
			 *
253
			 * Not having escape_titles set, or setting escape_titles to true will result in escaped titles.
254
			 */
255
			if (property_exists($args, 'escape_titles') && false === $args->escape_titles) { // WPCS: loose comparison ok.
256
			    $title = apply_filters('the_title', $item->title, $item->ID);
257
			} else {
258
			    /** This filter is documented in wp-includes/post-template.php */
259
			    $title = apply_filters('the_title', esc_html($item->title), $item->ID);
260
			}
261
262
			/**
263
			 * Filters a menu item's title.
264
			 *
265
			 * @since WP 4.4.0
266
			 *
267
			 * @param string   $title The menu item's title.
268
			 * @param WP_Post  $item  The current menu item.
269
			 * @param stdClass $args  An object of wp_nav_menu() arguments.
270
			 * @param int      $depth Depth of menu item. Used for padding.
271
			 */
272
			$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
273
274
			// If the .sr-only class was set apply to the nav items text only.
275
			if ( in_array( 'sr-only', $linkmod_classes, true ) ) {
276
				$title         = self::wrap_for_screen_reader( $title );
277
				$keys_to_unset = array_keys( $linkmod_classes, 'sr-only', true );
278
				foreach ( $keys_to_unset as $k ) {
279
					unset( $linkmod_classes[ $k ] );
280
				}
281
			}
282
283
			// Put the item contents into $output.
284
			$item_output .= isset( $args->link_before ) ? $args->link_before . $icon_html . $title . $args->link_after : '';
285
286
			/*
287
			 * This is the end of the internal nav item. We need to close the
288
			 * correct element depending on the type of link or link mod.
289
			 */
290
			if ( '' !== $linkmod_type ) {
291
				// Is linkmod, output the required closing element.
292
				$item_output .= self::linkmod_element_close( $linkmod_type );
293
			} else {
294
				// With no link mod type set this must be a standard <a> tag.
295
				$item_output .= '</a>';
296
			}
297
298
			$item_output .= isset( $args->after ) ? $args->after : '';
299
300
			// END appending the internal item contents to the output.
301
			$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
302
303
		}
304
305
		/**
306
		 * Traverse elements to create list from elements.
307
		 *
308
		 * Display one element if the element doesn't have any children otherwise,
309
		 * display the element and its children. Will only traverse up to the max
310
		 * depth and no ignore elements under that depth. It is possible to set the
311
		 * max depth to include all depths, see walk() method.
312
		 *
313
		 * This method should not be called directly, use the walk() method instead.
314
		 *
315
		 * @since WP 2.5.0
316
		 *
317
		 * @see Walker::start_lvl()
318
		 *
319
		 * @param object $element           Data object.
320
		 * @param array  $children_elements List of elements to continue traversing (passed by reference).
321
		 * @param int    $max_depth         Max depth to traverse.
322
		 * @param int    $depth             Depth of current element.
323
		 * @param array  $args              An array of arguments.
324
		 * @param string $output            Used to append additional content (passed by reference).
325
		 */
326
		public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
327
			if ( ! $element ) {
328
				return; }
329
			$id_field = $this->db_fields['id'];
330
			// Display this element.
331
			if ( is_object( $args[0] ) ) {
332
				$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] ); }
333
			parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
334
		}
335
336
		/**
337
		 * Menu Fallback.
338
		 *
339
		 * If this function is assigned to the wp_nav_menu's fallback_cb variable
340
		 * and a menu has not been assigned to the theme location in the WordPress
341
		 * menu manager the function with display nothing to a non-logged in user,
342
		 * and will add a link to the WordPress menu manager if logged in as an admin.
343
		 *
344
		 * @param array $args passed from the wp_nav_menu function.
345
		 */
346
		public static function fallback( $args ) {
347
			if ( current_user_can( 'edit_theme_options' ) ) {
348
349
				// Get Arguments.
350
				$container       = $args['container'];
351
				$container_id    = $args['container_id'];
352
				$container_class = $args['container_class'];
353
				$menu_class      = $args['menu_class'];
354
				$menu_id         = $args['menu_id'];
355
356
				// Initialize var to store fallback html.
357
				$fallback_output = '';
358
359
				if ( $container ) {
360
					$fallback_output .= '<' . esc_attr( $container );
361
					if ( $container_id ) {
362
						$fallback_output .= ' id="' . esc_attr( $container_id ) . '"';
363
					}
364
					if ( $container_class ) {
365
						$fallback_output .= ' class="' . esc_attr( $container_class ) . '"';
366
					}
367
					$fallback_output .= '>';
368
				}
369
				$fallback_output .= '<ul';
370
				if ( $menu_id ) {
371
					$fallback_output .= ' id="' . esc_attr( $menu_id ) . '"'; }
372
				if ( $menu_class ) {
373
					$fallback_output .= ' class="' . esc_attr( $menu_class ) . '"'; }
374
				$fallback_output .= '>';
375
				$fallback_output .= '<li class="nav-item"><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '" class="nav-link" title="' . esc_attr__( 'Add a menu', 'wp-bootstrap-navwalker' ) . '">' . esc_html__( 'Add a menu', 'wp-bootstrap-navwalker' ) . '</a></li>';
376
				$fallback_output .= '</ul>';
377
				if ( $container ) {
378
					$fallback_output .= '</' . esc_attr( $container ) . '>';
379
				}
380
381
				// If $args has 'echo' key and it's true echo, otherwise return.
382
				if ( array_key_exists( 'echo', $args ) && $args['echo'] ) {
383
					echo $fallback_output; // WPCS: XSS OK.
384
				} else {
385
					return $fallback_output;
386
				}
387
			}
388
		}
389
390
		/**
391
		 * Find any custom linkmod or icon classes and store in their holder
392
		 * arrays then remove them from the main classes array.
393
		 *
394
		 * Supported linkmods: .disabled, .dropdown-header, .dropdown-divider, .sr-only
395
		 * Supported iconsets: Font Awesome 4/5, Glypicons
396
		 *
397
		 * NOTE: This accepts the linkmod and icon arrays by reference.
398
		 *
399
		 * @since 4.0.0
400
		 *
401
		 * @param array   $classes         an array of classes currently assigned to the item.
402
		 * @param array   $linkmod_classes an array to hold linkmod classes.
403
		 * @param array   $icon_classes    an array to hold icon classes.
404
		 * @param integer $depth           an integer holding current depth level.
405
		 *
406
		 * @return array  $classes         a maybe modified array of classnames.
407
		 */
408
		private function separate_linkmods_and_icons_from_classes( $classes, &$linkmod_classes, &$icon_classes, $depth ) {
409
			// Loop through $classes array to find linkmod or icon classes.
410
			foreach ( $classes as $key => $class ) {
411
				/*
412
				 * If any special classes are found, store the class in it's
413
				 * holder array and and unset the item from $classes.
414
				 */
415
				if ( preg_match( '/^disabled|^sr-only/i', $class ) ) {
416
					// Test for .disabled or .sr-only classes.
417
					$linkmod_classes[] = $class;
418
					unset( $classes[ $key ] );
419
				} elseif ( preg_match( '/^dropdown-header|^dropdown-divider|^dropdown-item-text/i', $class ) && $depth > 0 ) {
420
					/*
421
					 * Test for .dropdown-header or .dropdown-divider and a
422
					 * depth greater than 0 - IE inside a dropdown.
423
					 */
424
					$linkmod_classes[] = $class;
425
					unset( $classes[ $key ] );
426
				} elseif ( preg_match( '/^fa-(\S*)?|^fa(s|r|l|b)?(\s?)?$/i', $class ) ) {
427
					// Font Awesome.
428
					$icon_classes[] = $class;
429
					unset( $classes[ $key ] );
430
				} elseif ( preg_match( '/^glyphicon-(\S*)?|^glyphicon(\s?)$/i', $class ) ) {
431
					// Glyphicons.
432
					$icon_classes[] = $class;
433
					unset( $classes[ $key ] );
434
				}
435
			}
436
437
			return $classes;
438
		}
439
440
		/**
441
		 * Return a string containing a linkmod type and update $atts array
442
		 * accordingly depending on the decided.
443
		 *
444
		 * @since 4.0.0
445
		 *
446
		 * @param array $linkmod_classes array of any link modifier classes.
447
		 *
448
		 * @return string                empty for default, a linkmod type string otherwise.
449
		 */
450
		private function get_linkmod_type( $linkmod_classes = array() ) {
451
			$linkmod_type = '';
452
			// Loop through array of linkmod classes to handle their $atts.
453
			if ( ! empty( $linkmod_classes ) ) {
454
				foreach ( $linkmod_classes as $link_class ) {
455
					if ( ! empty( $link_class ) ) {
456
457
						// Check for special class types and set a flag for them.
458
						if ( 'dropdown-header' === $link_class ) {
459
							$linkmod_type = 'dropdown-header';
460
						} elseif ( 'dropdown-divider' === $link_class ) {
461
							$linkmod_type = 'dropdown-divider';
462
						} elseif ( 'dropdown-item-text' === $link_class ) {
463
							$linkmod_type = 'dropdown-item-text';
464
						}
465
					}
466
				}
467
			}
468
			return $linkmod_type;
469
		}
470
471
		/**
472
		 * Update the attributes of a nav item depending on the limkmod classes.
473
		 *
474
		 * @since 4.0.0
475
		 *
476
		 * @param array $atts            array of atts for the current link in nav item.
477
		 * @param array $linkmod_classes an array of classes that modify link or nav item behaviors or displays.
478
		 *
479
		 * @return array                 maybe updated array of attributes for item.
480
		 */
481
		private function update_atts_for_linkmod_type( $atts = array(), $linkmod_classes = array() ) {
482
			if ( ! empty( $linkmod_classes ) ) {
483
				foreach ( $linkmod_classes as $link_class ) {
484
					if ( ! empty( $link_class ) ) {
485
						/*
486
						 * Update $atts with a space and the extra classname
487
						 * so long as it's not a sr-only class.
488
						 */
489
						if ( 'sr-only' !== $link_class ) {
490
							$atts['class'] .= ' ' . esc_attr( $link_class );
491
						}
492
						// Check for special class types we need additional handling for.
493
						if ( 'disabled' === $link_class ) {
494
							// Convert link to '#' and unset open targets.
495
							$atts['href'] = '#';
496
							unset( $atts['target'] );
497
						} elseif ( 'dropdown-header' === $link_class || 'dropdown-divider' === $link_class || 'dropdown-item-text' === $link_class ) {
498
							// Store a type flag and unset href and target.
499
							unset( $atts['href'] );
500
							unset( $atts['target'] );
501
						}
502
					}
503
				}
504
			}
505
			return $atts;
506
		}
507
508
		/**
509
		 * Wraps the passed text in a screen reader only class.
510
		 *
511
		 * @since 4.0.0
512
		 *
513
		 * @param string $text the string of text to be wrapped in a screen reader class.
514
		 * @return string      the string wrapped in a span with the class.
515
		 */
516
		private function wrap_for_screen_reader( $text = '' ) {
517
			if ( $text ) {
518
				$text = '<span class="sr-only">' . $text . '</span>';
519
			}
520
			return $text;
521
		}
522
523
		/**
524
		 * Returns the correct opening element and attributes for a linkmod.
525
		 *
526
		 * @since 4.0.0
527
		 *
528
		 * @param string $linkmod_type a sting containing a linkmod type flag.
529
		 * @param string $attributes   a string of attributes to add to the element.
530
		 *
531
		 * @return string              a string with the openign tag for the element with attribibutes added.
532
		 */
533
		private function linkmod_element_open( $linkmod_type, $attributes = '' ) {
534
			$output = '';
535
			if ( 'dropdown-item-text' === $linkmod_type ) {
536
				$output .= '<span class="dropdown-item-text"' . $attributes . '>';
537
			} elseif ( 'dropdown-header' === $linkmod_type ) {
538
				/*
539
				 * For a header use a span with the .h6 class instead of a real
540
				 * header tag so that it doesn't confuse screen readers.
541
				 */
542
				$output .= '<span class="dropdown-header h6"' . $attributes . '>';
543
			} elseif ( 'dropdown-divider' === $linkmod_type ) {
544
				// This is a divider.
545
				$output .= '<div class="dropdown-divider"' . $attributes . '>';
546
			}
547
			return $output;
548
		}
549
550
		/**
551
		 * Return the correct closing tag for the linkmod element.
552
		 *
553
		 * @since 4.0.0
554
		 *
555
		 * @param string $linkmod_type a string containing a special linkmod type.
556
		 *
557
		 * @return string              a string with the closing tag for this linkmod type.
558
		 */
559
		private function linkmod_element_close( $linkmod_type ) {
560
			$output = '';
561
			if ( 'dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type ) {
562
				/*
563
				 * For a header use a span with the .h6 class instead of a real
564
				 * header tag so that it doesn't confuse screen readers.
565
				 */
566
				$output .= '</span>';
567
			} elseif ( 'dropdown-divider' === $linkmod_type ) {
568
				// This is a divider.
569
				$output .= '</div>';
570
			}
571
			return $output;
572
		}
573
	}
574
}
575