@@ -42,539 +42,539 @@ |
||
| 42 | 42 | * @param int $depth Depth of menu item. Used for padding. |
| 43 | 43 | * @param stdClass $args An object of wp_nav_menu() arguments. |
| 44 | 44 | */ |
| 45 | - public function start_lvl(&$output, $depth = 0, $args = array()) |
|
| 46 | - { |
|
| 47 | - if (isset($args->item_spacing) && 'discard' === $args->item_spacing) { |
|
| 48 | - $t = ''; |
|
| 49 | - $n = ''; |
|
| 50 | - } else { |
|
| 51 | - $t = "\t"; |
|
| 52 | - $n = "\n"; |
|
| 53 | - } |
|
| 54 | - $indent = str_repeat($t, $depth); |
|
| 55 | - // Default class to add to the file. |
|
| 56 | - $classes = array( 'dropdown-menu' ); |
|
| 57 | - /** |
|
| 58 | - * Filters the CSS class(es) applied to a menu list element. |
|
| 59 | - * |
|
| 60 | - * @since WP 4.8.0 |
|
| 61 | - * |
|
| 62 | - * @param array $classes The CSS classes that are applied to the menu `<ul>` element. |
|
| 63 | - * @param stdClass $args An object of `wp_nav_menu()` arguments. |
|
| 64 | - * @param int $depth Depth of menu item. Used for padding. |
|
| 65 | - */ |
|
| 66 | - $class_names = join(' ', apply_filters('nav_menu_submenu_css_class', $classes, $args, $depth)); |
|
| 67 | - $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : ''; |
|
| 68 | - /** |
|
| 69 | - * The `.dropdown-menu` container needs to have a labelledby |
|
| 70 | - * attribute which points to it's trigger link. |
|
| 71 | - * |
|
| 72 | - * Form a string for the labelledby attribute from the the latest |
|
| 73 | - * link with an id that was added to the $output. |
|
| 74 | - */ |
|
| 75 | - $labelledby = ''; |
|
| 76 | - // find all links with an id in the output. |
|
| 77 | - preg_match_all('/(<a.*?id=\"|\')(.*?)\"|\'.*?>/im', $output, $matches); |
|
| 78 | - // with pointer at end of array check if we got an ID match. |
|
| 79 | - if (end($matches[2])) { |
|
| 80 | - // build a string to use as aria-labelledby. |
|
| 81 | - $labelledby = 'aria-labelledby="' . end($matches[2]) . '"'; |
|
| 82 | - } |
|
| 83 | - $output .= "{$n}{$indent}<ul$class_names $labelledby role=\"menu\">{$n}"; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Starts the element output. |
|
| 88 | - * |
|
| 89 | - * @since WP 3.0.0 |
|
| 90 | - * @since WP 4.4.0 The {@see 'nav_menu_item_args'} filter was added. |
|
| 91 | - * |
|
| 92 | - * @see Walker_Nav_Menu::start_el() |
|
| 93 | - * |
|
| 94 | - * @param string $output Used to append additional content (passed by reference). |
|
| 95 | - * @param WP_Post $item Menu item data object. |
|
| 96 | - * @param int $depth Depth of menu item. Used for padding. |
|
| 97 | - * @param stdClass $args An object of wp_nav_menu() arguments. |
|
| 98 | - * @param int $id Current item ID. |
|
| 99 | - */ |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) |
|
| 103 | - { |
|
| 104 | - if (isset($args->item_spacing) && 'discard' === $args->item_spacing) { |
|
| 105 | - $t = ''; |
|
| 106 | - $n = ''; |
|
| 107 | - } else { |
|
| 108 | - $t = "\t"; |
|
| 109 | - $n = "\n"; |
|
| 110 | - } |
|
| 111 | - $indent = ($depth) ? str_repeat($t, $depth) : ''; |
|
| 112 | - |
|
| 113 | - $classes = empty($item->classes) ? array() : (array) $item->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::seporate_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 | - { |
|
| 326 | - if (! $element) { |
|
| 327 | - return; |
|
| 328 | - } |
|
| 329 | - $id_field = $this->db_fields['id']; |
|
| 330 | - // Display this element. |
|
| 331 | - if (is_object($args[0])) { |
|
| 332 | - $args[0]->has_children = ! empty($children_elements[ $element->$id_field ]); |
|
| 333 | - } |
|
| 334 | - parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Menu Fallback |
|
| 340 | - * ============= |
|
| 341 | - * If this function is assigned to the wp_nav_menu's fallback_cb variable |
|
| 342 | - * and a menu has not been assigned to the theme location in the WordPress |
|
| 343 | - * menu manager the function with display nothing to a non-logged in user, |
|
| 344 | - * and will add a link to the WordPress menu manager if logged in as an admin. |
|
| 345 | - * |
|
| 346 | - * @param array $args passed from the wp_nav_menu function. |
|
| 347 | - */ |
|
| 348 | - public static function fallback($args) |
|
| 349 | - { |
|
| 350 | - if (current_user_can('edit_theme_options')) { |
|
| 351 | - |
|
| 352 | - /* Get Arguments. */ |
|
| 353 | - $container = $args['container']; |
|
| 354 | - $container_id = $args['container_id']; |
|
| 355 | - $container_class = $args['container_class']; |
|
| 356 | - $menu_class = $args['menu_class']; |
|
| 357 | - $menu_id = $args['menu_id']; |
|
| 358 | - |
|
| 359 | - // initialize var to store fallback html. |
|
| 360 | - $fallback_output = ''; |
|
| 361 | - |
|
| 362 | - if ($container) { |
|
| 363 | - $fallback_output .= '<' . esc_attr($container); |
|
| 364 | - if ($container_id) { |
|
| 365 | - $fallback_output .= ' id="' . esc_attr($container_id) . '"'; |
|
| 366 | - } |
|
| 367 | - if ($container_class) { |
|
| 368 | - $fallback_output .= ' class="' . esc_attr($container_class) . '"'; |
|
| 369 | - } |
|
| 370 | - $fallback_output .= '>'; |
|
| 371 | - } |
|
| 372 | - $fallback_output .= '<ul'; |
|
| 373 | - if ($menu_id) { |
|
| 374 | - $fallback_output .= ' id="' . esc_attr($menu_id) . '"'; |
|
| 375 | - } |
|
| 376 | - if ($menu_class) { |
|
| 377 | - $fallback_output .= ' class="' . esc_attr($menu_class) . '"'; |
|
| 378 | - } |
|
| 379 | - $fallback_output .= '>'; |
|
| 380 | - $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>'; |
|
| 381 | - $fallback_output .= '</ul>'; |
|
| 382 | - if ($container) { |
|
| 383 | - $fallback_output .= '</' . esc_attr($container) . '>'; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - // if $args has 'echo' key and it's true echo, otherwise return. |
|
| 387 | - if (array_key_exists('echo', $args) && $args['echo']) { |
|
| 388 | - echo $fallback_output; // WPCS: XSS OK. |
|
| 389 | - } else { |
|
| 390 | - return $fallback_output; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Find any custom linkmod or icon classes and store in their holder |
|
| 398 | - * arrays then remove them from the main classes array. |
|
| 399 | - * |
|
| 400 | - * Supported linkmods: .disabled, .dropdown-header, .dropdown-divider, .sr-only |
|
| 401 | - * Supported iconsets: Font Awesome 4/5, Glypicons |
|
| 402 | - * |
|
| 403 | - * NOTE: This accepts the linkmod and icon arrays by reference. |
|
| 404 | - * |
|
| 405 | - * @since 4.0.0 |
|
| 406 | - * |
|
| 407 | - * @param array $classes an array of classes currently assigned to the item. |
|
| 408 | - * @param array $linkmod_classes an array to hold linkmod classes. |
|
| 409 | - * @param array $icon_classes an array to hold icon classes. |
|
| 410 | - * @param integer $depth an integer holding current depth level. |
|
| 411 | - * |
|
| 412 | - * @return array $classes a maybe modified array of classnames. |
|
| 413 | - */ |
|
| 414 | - private function seporate_linkmods_and_icons_from_classes($classes, &$linkmod_classes, &$icon_classes, $depth) |
|
| 415 | - { |
|
| 416 | - // Loop through $classes array to find linkmod or icon classes. |
|
| 417 | - foreach ($classes as $key => $class) { |
|
| 418 | - // If any special classes are found, store the class in it's |
|
| 419 | - // holder array and and unset the item from $classes. |
|
| 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 | - // Test for .dropdown-header or .dropdown-divider and a |
|
| 426 | - // depth greater than 0 - IE inside a dropdown. |
|
| 427 | - $linkmod_classes[] = $class; |
|
| 428 | - unset($classes[ $key ]); |
|
| 429 | - } elseif (preg_match('/^fa-(\S*)?|^fa(s|r|l|b)?(\s?)?$/i', $class)) { |
|
| 430 | - // Font Awesome. |
|
| 431 | - $icon_classes[] = $class; |
|
| 432 | - unset($classes[ $key ]); |
|
| 433 | - } elseif (preg_match('/^glyphicon-(\S*)?|^glyphicon(\s?)$/i', $class)) { |
|
| 434 | - // Glyphicons. |
|
| 435 | - $icon_classes[] = $class; |
|
| 436 | - unset($classes[ $key ]); |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - return $classes; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Return a string containing a linkmod type and update $atts array |
|
| 446 | - * accordingly depending on the decided. |
|
| 447 | - * |
|
| 448 | - * @since 4.0.0 |
|
| 449 | - * |
|
| 450 | - * @param array $linkmod_classes array of any link modifier classes. |
|
| 451 | - * |
|
| 452 | - * @return string empty for default, a linkmod type string otherwise. |
|
| 453 | - */ |
|
| 454 | - private function get_linkmod_type($linkmod_classes = array()) |
|
| 455 | - { |
|
| 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 | - /** |
|
| 478 | - * Update the attributes of a nav item depending on the limkmod classes. |
|
| 479 | - * |
|
| 480 | - * @since 4.0.0 |
|
| 481 | - * |
|
| 482 | - * @param array $atts array of atts for the current link in nav item. |
|
| 483 | - * @param array $linkmod_classes an array of classes that modify link or nav item behaviors or displays. |
|
| 484 | - * |
|
| 485 | - * @return array maybe updated array of attributes for item. |
|
| 486 | - */ |
|
| 487 | - private function update_atts_for_linkmod_type($atts = array(), $linkmod_classes = array()) |
|
| 488 | - { |
|
| 489 | - if (! empty($linkmod_classes)) { |
|
| 490 | - foreach ($linkmod_classes as $link_class) { |
|
| 491 | - if (! empty($link_class)) { |
|
| 492 | - // update $atts with a space and the extra classname... |
|
| 493 | - // so long as it's not a sr-only class. |
|
| 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 | - /** |
|
| 515 | - * Wraps the passed text in a screen reader only class. |
|
| 516 | - * |
|
| 517 | - * @since 4.0.0 |
|
| 518 | - * |
|
| 519 | - * @param string $text the string of text to be wrapped in a screen reader class. |
|
| 520 | - * @return string the string wrapped in a span with the class. |
|
| 521 | - */ |
|
| 522 | - private function wrap_for_screen_reader($text = '') |
|
| 523 | - { |
|
| 524 | - if ($text) { |
|
| 525 | - $text = '<span class="sr-only">' . $text . '</span>'; |
|
| 526 | - } |
|
| 527 | - return $text; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * Returns the correct opening element and attributes for a linkmod. |
|
| 533 | - * |
|
| 534 | - * @since 4.0.0 |
|
| 535 | - * |
|
| 536 | - * @param string $linkmod_type a sting containing a linkmod type flag. |
|
| 537 | - * @param string $attributes a string of attributes to add to the element. |
|
| 538 | - * |
|
| 539 | - * @return string a string with the openign tag for the element with attribibutes added. |
|
| 540 | - */ |
|
| 541 | - private function linkmod_element_open($linkmod_type, $attributes = '') |
|
| 542 | - { |
|
| 543 | - $output = ''; |
|
| 544 | - if ('dropdown-item-text' === $linkmod_type) { |
|
| 545 | - $output .= '<span class="dropdown-item-text"' . $attributes . '>'; |
|
| 546 | - } elseif ('dropdown-header' === $linkmod_type) { |
|
| 547 | - // For a header use a span with the .h6 class instead of a real |
|
| 548 | - // header tag so that it doesn't confuse screen readers. |
|
| 549 | - $output .= '<span class="dropdown-header h6"' . $attributes . '>'; |
|
| 550 | - } elseif ('dropdown-divider' === $linkmod_type) { |
|
| 551 | - // this is a divider. |
|
| 552 | - $output .= '<div class="dropdown-divider"' . $attributes . '>'; |
|
| 553 | - } |
|
| 554 | - return $output; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * Return the correct closing tag for the linkmod element. |
|
| 560 | - * |
|
| 561 | - * @since 4.0.0 |
|
| 562 | - * |
|
| 563 | - * @param string $linkmod_type a string containing a special linkmod type. |
|
| 564 | - * |
|
| 565 | - * @return string a string with the closing tag for this linkmod type. |
|
| 566 | - */ |
|
| 567 | - private function linkmod_element_close($linkmod_type) |
|
| 568 | - { |
|
| 569 | - $output = ''; |
|
| 570 | - if ('dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type) { |
|
| 571 | - // For a header use a span with the .h6 class instead of a real |
|
| 572 | - // header tag so that it doesn't confuse screen readers. |
|
| 573 | - $output .= '</span>'; |
|
| 574 | - } elseif ('dropdown-divider' === $linkmod_type) { |
|
| 575 | - // this is a divider. |
|
| 576 | - $output .= '</div>'; |
|
| 577 | - } |
|
| 578 | - return $output; |
|
| 579 | - } |
|
| 45 | + public function start_lvl(&$output, $depth = 0, $args = array()) |
|
| 46 | + { |
|
| 47 | + if (isset($args->item_spacing) && 'discard' === $args->item_spacing) { |
|
| 48 | + $t = ''; |
|
| 49 | + $n = ''; |
|
| 50 | + } else { |
|
| 51 | + $t = "\t"; |
|
| 52 | + $n = "\n"; |
|
| 53 | + } |
|
| 54 | + $indent = str_repeat($t, $depth); |
|
| 55 | + // Default class to add to the file. |
|
| 56 | + $classes = array( 'dropdown-menu' ); |
|
| 57 | + /** |
|
| 58 | + * Filters the CSS class(es) applied to a menu list element. |
|
| 59 | + * |
|
| 60 | + * @since WP 4.8.0 |
|
| 61 | + * |
|
| 62 | + * @param array $classes The CSS classes that are applied to the menu `<ul>` element. |
|
| 63 | + * @param stdClass $args An object of `wp_nav_menu()` arguments. |
|
| 64 | + * @param int $depth Depth of menu item. Used for padding. |
|
| 65 | + */ |
|
| 66 | + $class_names = join(' ', apply_filters('nav_menu_submenu_css_class', $classes, $args, $depth)); |
|
| 67 | + $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : ''; |
|
| 68 | + /** |
|
| 69 | + * The `.dropdown-menu` container needs to have a labelledby |
|
| 70 | + * attribute which points to it's trigger link. |
|
| 71 | + * |
|
| 72 | + * Form a string for the labelledby attribute from the the latest |
|
| 73 | + * link with an id that was added to the $output. |
|
| 74 | + */ |
|
| 75 | + $labelledby = ''; |
|
| 76 | + // find all links with an id in the output. |
|
| 77 | + preg_match_all('/(<a.*?id=\"|\')(.*?)\"|\'.*?>/im', $output, $matches); |
|
| 78 | + // with pointer at end of array check if we got an ID match. |
|
| 79 | + if (end($matches[2])) { |
|
| 80 | + // build a string to use as aria-labelledby. |
|
| 81 | + $labelledby = 'aria-labelledby="' . end($matches[2]) . '"'; |
|
| 82 | + } |
|
| 83 | + $output .= "{$n}{$indent}<ul$class_names $labelledby role=\"menu\">{$n}"; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Starts the element output. |
|
| 88 | + * |
|
| 89 | + * @since WP 3.0.0 |
|
| 90 | + * @since WP 4.4.0 The {@see 'nav_menu_item_args'} filter was added. |
|
| 91 | + * |
|
| 92 | + * @see Walker_Nav_Menu::start_el() |
|
| 93 | + * |
|
| 94 | + * @param string $output Used to append additional content (passed by reference). |
|
| 95 | + * @param WP_Post $item Menu item data object. |
|
| 96 | + * @param int $depth Depth of menu item. Used for padding. |
|
| 97 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
|
| 98 | + * @param int $id Current item ID. |
|
| 99 | + */ |
|
| 100 | + |
|
| 101 | + |
|
| 102 | + public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) |
|
| 103 | + { |
|
| 104 | + if (isset($args->item_spacing) && 'discard' === $args->item_spacing) { |
|
| 105 | + $t = ''; |
|
| 106 | + $n = ''; |
|
| 107 | + } else { |
|
| 108 | + $t = "\t"; |
|
| 109 | + $n = "\n"; |
|
| 110 | + } |
|
| 111 | + $indent = ($depth) ? str_repeat($t, $depth) : ''; |
|
| 112 | + |
|
| 113 | + $classes = empty($item->classes) ? array() : (array) $item->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::seporate_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 | + { |
|
| 326 | + if (! $element) { |
|
| 327 | + return; |
|
| 328 | + } |
|
| 329 | + $id_field = $this->db_fields['id']; |
|
| 330 | + // Display this element. |
|
| 331 | + if (is_object($args[0])) { |
|
| 332 | + $args[0]->has_children = ! empty($children_elements[ $element->$id_field ]); |
|
| 333 | + } |
|
| 334 | + parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Menu Fallback |
|
| 340 | + * ============= |
|
| 341 | + * If this function is assigned to the wp_nav_menu's fallback_cb variable |
|
| 342 | + * and a menu has not been assigned to the theme location in the WordPress |
|
| 343 | + * menu manager the function with display nothing to a non-logged in user, |
|
| 344 | + * and will add a link to the WordPress menu manager if logged in as an admin. |
|
| 345 | + * |
|
| 346 | + * @param array $args passed from the wp_nav_menu function. |
|
| 347 | + */ |
|
| 348 | + public static function fallback($args) |
|
| 349 | + { |
|
| 350 | + if (current_user_can('edit_theme_options')) { |
|
| 351 | + |
|
| 352 | + /* Get Arguments. */ |
|
| 353 | + $container = $args['container']; |
|
| 354 | + $container_id = $args['container_id']; |
|
| 355 | + $container_class = $args['container_class']; |
|
| 356 | + $menu_class = $args['menu_class']; |
|
| 357 | + $menu_id = $args['menu_id']; |
|
| 358 | + |
|
| 359 | + // initialize var to store fallback html. |
|
| 360 | + $fallback_output = ''; |
|
| 361 | + |
|
| 362 | + if ($container) { |
|
| 363 | + $fallback_output .= '<' . esc_attr($container); |
|
| 364 | + if ($container_id) { |
|
| 365 | + $fallback_output .= ' id="' . esc_attr($container_id) . '"'; |
|
| 366 | + } |
|
| 367 | + if ($container_class) { |
|
| 368 | + $fallback_output .= ' class="' . esc_attr($container_class) . '"'; |
|
| 369 | + } |
|
| 370 | + $fallback_output .= '>'; |
|
| 371 | + } |
|
| 372 | + $fallback_output .= '<ul'; |
|
| 373 | + if ($menu_id) { |
|
| 374 | + $fallback_output .= ' id="' . esc_attr($menu_id) . '"'; |
|
| 375 | + } |
|
| 376 | + if ($menu_class) { |
|
| 377 | + $fallback_output .= ' class="' . esc_attr($menu_class) . '"'; |
|
| 378 | + } |
|
| 379 | + $fallback_output .= '>'; |
|
| 380 | + $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>'; |
|
| 381 | + $fallback_output .= '</ul>'; |
|
| 382 | + if ($container) { |
|
| 383 | + $fallback_output .= '</' . esc_attr($container) . '>'; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + // if $args has 'echo' key and it's true echo, otherwise return. |
|
| 387 | + if (array_key_exists('echo', $args) && $args['echo']) { |
|
| 388 | + echo $fallback_output; // WPCS: XSS OK. |
|
| 389 | + } else { |
|
| 390 | + return $fallback_output; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Find any custom linkmod or icon classes and store in their holder |
|
| 398 | + * arrays then remove them from the main classes array. |
|
| 399 | + * |
|
| 400 | + * Supported linkmods: .disabled, .dropdown-header, .dropdown-divider, .sr-only |
|
| 401 | + * Supported iconsets: Font Awesome 4/5, Glypicons |
|
| 402 | + * |
|
| 403 | + * NOTE: This accepts the linkmod and icon arrays by reference. |
|
| 404 | + * |
|
| 405 | + * @since 4.0.0 |
|
| 406 | + * |
|
| 407 | + * @param array $classes an array of classes currently assigned to the item. |
|
| 408 | + * @param array $linkmod_classes an array to hold linkmod classes. |
|
| 409 | + * @param array $icon_classes an array to hold icon classes. |
|
| 410 | + * @param integer $depth an integer holding current depth level. |
|
| 411 | + * |
|
| 412 | + * @return array $classes a maybe modified array of classnames. |
|
| 413 | + */ |
|
| 414 | + private function seporate_linkmods_and_icons_from_classes($classes, &$linkmod_classes, &$icon_classes, $depth) |
|
| 415 | + { |
|
| 416 | + // Loop through $classes array to find linkmod or icon classes. |
|
| 417 | + foreach ($classes as $key => $class) { |
|
| 418 | + // If any special classes are found, store the class in it's |
|
| 419 | + // holder array and and unset the item from $classes. |
|
| 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 | + // Test for .dropdown-header or .dropdown-divider and a |
|
| 426 | + // depth greater than 0 - IE inside a dropdown. |
|
| 427 | + $linkmod_classes[] = $class; |
|
| 428 | + unset($classes[ $key ]); |
|
| 429 | + } elseif (preg_match('/^fa-(\S*)?|^fa(s|r|l|b)?(\s?)?$/i', $class)) { |
|
| 430 | + // Font Awesome. |
|
| 431 | + $icon_classes[] = $class; |
|
| 432 | + unset($classes[ $key ]); |
|
| 433 | + } elseif (preg_match('/^glyphicon-(\S*)?|^glyphicon(\s?)$/i', $class)) { |
|
| 434 | + // Glyphicons. |
|
| 435 | + $icon_classes[] = $class; |
|
| 436 | + unset($classes[ $key ]); |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + return $classes; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Return a string containing a linkmod type and update $atts array |
|
| 446 | + * accordingly depending on the decided. |
|
| 447 | + * |
|
| 448 | + * @since 4.0.0 |
|
| 449 | + * |
|
| 450 | + * @param array $linkmod_classes array of any link modifier classes. |
|
| 451 | + * |
|
| 452 | + * @return string empty for default, a linkmod type string otherwise. |
|
| 453 | + */ |
|
| 454 | + private function get_linkmod_type($linkmod_classes = array()) |
|
| 455 | + { |
|
| 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 | + /** |
|
| 478 | + * Update the attributes of a nav item depending on the limkmod classes. |
|
| 479 | + * |
|
| 480 | + * @since 4.0.0 |
|
| 481 | + * |
|
| 482 | + * @param array $atts array of atts for the current link in nav item. |
|
| 483 | + * @param array $linkmod_classes an array of classes that modify link or nav item behaviors or displays. |
|
| 484 | + * |
|
| 485 | + * @return array maybe updated array of attributes for item. |
|
| 486 | + */ |
|
| 487 | + private function update_atts_for_linkmod_type($atts = array(), $linkmod_classes = array()) |
|
| 488 | + { |
|
| 489 | + if (! empty($linkmod_classes)) { |
|
| 490 | + foreach ($linkmod_classes as $link_class) { |
|
| 491 | + if (! empty($link_class)) { |
|
| 492 | + // update $atts with a space and the extra classname... |
|
| 493 | + // so long as it's not a sr-only class. |
|
| 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 | + /** |
|
| 515 | + * Wraps the passed text in a screen reader only class. |
|
| 516 | + * |
|
| 517 | + * @since 4.0.0 |
|
| 518 | + * |
|
| 519 | + * @param string $text the string of text to be wrapped in a screen reader class. |
|
| 520 | + * @return string the string wrapped in a span with the class. |
|
| 521 | + */ |
|
| 522 | + private function wrap_for_screen_reader($text = '') |
|
| 523 | + { |
|
| 524 | + if ($text) { |
|
| 525 | + $text = '<span class="sr-only">' . $text . '</span>'; |
|
| 526 | + } |
|
| 527 | + return $text; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * Returns the correct opening element and attributes for a linkmod. |
|
| 533 | + * |
|
| 534 | + * @since 4.0.0 |
|
| 535 | + * |
|
| 536 | + * @param string $linkmod_type a sting containing a linkmod type flag. |
|
| 537 | + * @param string $attributes a string of attributes to add to the element. |
|
| 538 | + * |
|
| 539 | + * @return string a string with the openign tag for the element with attribibutes added. |
|
| 540 | + */ |
|
| 541 | + private function linkmod_element_open($linkmod_type, $attributes = '') |
|
| 542 | + { |
|
| 543 | + $output = ''; |
|
| 544 | + if ('dropdown-item-text' === $linkmod_type) { |
|
| 545 | + $output .= '<span class="dropdown-item-text"' . $attributes . '>'; |
|
| 546 | + } elseif ('dropdown-header' === $linkmod_type) { |
|
| 547 | + // For a header use a span with the .h6 class instead of a real |
|
| 548 | + // header tag so that it doesn't confuse screen readers. |
|
| 549 | + $output .= '<span class="dropdown-header h6"' . $attributes . '>'; |
|
| 550 | + } elseif ('dropdown-divider' === $linkmod_type) { |
|
| 551 | + // this is a divider. |
|
| 552 | + $output .= '<div class="dropdown-divider"' . $attributes . '>'; |
|
| 553 | + } |
|
| 554 | + return $output; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * Return the correct closing tag for the linkmod element. |
|
| 560 | + * |
|
| 561 | + * @since 4.0.0 |
|
| 562 | + * |
|
| 563 | + * @param string $linkmod_type a string containing a special linkmod type. |
|
| 564 | + * |
|
| 565 | + * @return string a string with the closing tag for this linkmod type. |
|
| 566 | + */ |
|
| 567 | + private function linkmod_element_close($linkmod_type) |
|
| 568 | + { |
|
| 569 | + $output = ''; |
|
| 570 | + if ('dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type) { |
|
| 571 | + // For a header use a span with the .h6 class instead of a real |
|
| 572 | + // header tag so that it doesn't confuse screen readers. |
|
| 573 | + $output .= '</span>'; |
|
| 574 | + } elseif ('dropdown-divider' === $linkmod_type) { |
|
| 575 | + // this is a divider. |
|
| 576 | + $output .= '</div>'; |
|
| 577 | + } |
|
| 578 | + return $output; |
|
| 579 | + } |
|
| 580 | 580 | } |