Completed
Pull Request — master (#446)
by
unknown
01:19
created

WP_Bootstrap_Navwalker   F

Complexity

Total Complexity 86

Size/Duplication

Total Lines 532
Duplicated Lines 2.63 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A display_element() 0 9 3
B start_lvl() 7 39 5
F start_el() 7 201 33
B separate_linkmods_and_icons_from_classes() 0 27 7
B get_linkmod_type() 0 20 7
B update_atts_for_linkmod_type() 0 24 9
A wrap_for_screen_reader() 0 6 2
A linkmod_element_open() 0 14 4
A linkmod_element_close() 0 12 4
C fallback() 0 46 12

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