Completed
Push — master ( 6d51e4...54bfbf )
by William
01:32 queued 11s
created

WP_Bootstrap_Navwalker::add_schema_to_navbar_ul()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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