Completed
Push — master ( 1070ce...7d21d0 )
by William
01:17
created

WP_Bootstrap_Navwalker::get_linkmod_type()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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