Completed
Pull Request — master (#410)
by
unknown
01:23
created

class-wp-bootstrap-navwalker.php ➔ flatten()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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