Completed
Push — master ( 7227c9...e3af96 )
by William
01:31 queued 11s
created

WP_Bootstrap_Navwalker::fallback()   C

Complexity

Conditions 12
Paths 97

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 6.9666
c 0
b 0
f 0
cc 12
nc 97
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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