feat: update WP Bootstrap Navwalker

pull/620/head^2
Seaton Jiang 2024-01-16 18:24:42 +08:00
parent abc6b58026
commit 8b9fd365eb
No known key found for this signature in database
GPG Key ID: 1E85ACC90F7EB73E
1 changed files with 215 additions and 174 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* WP Bootstrap Navwalker * WP Bootstrap Navwalker
* *
@ -8,7 +9,7 @@
* Plugin Name: WP Bootstrap Navwalker * Plugin Name: WP Bootstrap Navwalker
* Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker * Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
* 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. * 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.
* Author: Edward McIntyre - @twittem, WP Bootstrap, William Patton - @pattonwebz * Author: Edward McIntyre - @twittem, WP Bootstrap, William Patton - @pattonwebz, IanDelMar - @IanDelMar
* Version: 4.3.0 * Version: 4.3.0
* Author URI: https://github.com/wp-bootstrap * Author URI: https://github.com/wp-bootstrap
* GitHub Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker * GitHub Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
@ -31,16 +32,33 @@ function attributes_filter($var)
add_filter('nav_menu_css_class', 'attributes_filter', 100, 1); add_filter('nav_menu_css_class', 'attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'attributes_filter', 100, 1); add_filter('nav_menu_item_id', 'attributes_filter', 100, 1);
add_filter('page_css_class', 'attributes_filter', 100, 1); add_filter('page_css_class', 'attributes_filter', 100, 1);
if (!class_exists('WP_Bootstrap_Navwalker')) :
if (!class_exists('WP_Bootstrap_Navwalker')) {
/** /**
* WP_Bootstrap_Navwalker class. * WP_Bootstrap_Navwalker class.
*
* @extends Walker_Nav_Menu
*/ */
class WP_Bootstrap_Navwalker extends Walker_Nav_Menu class WP_Bootstrap_Navwalker extends Walker_Nav_Menu
{ {
/**
* Whether the items_wrap contains schema microdata or not.
*
* @since 4.2.0
* @var boolean
*/
private $has_schema = false;
/**
* Ensure the items_wrap argument contains microdata.
*
* @since 4.2.0
*/
public function __construct()
{
if (!has_filter('wp_nav_menu_args', array($this, 'add_schema_to_navbar_ul'))) {
add_filter('wp_nav_menu_args', array($this, 'add_schema_to_navbar_ul'));
}
}
/** /**
* Starts the list before the elements are added. * Starts the list before the elements are added.
* *
@ -48,11 +66,11 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
* *
* @see Walker_Nav_Menu::start_lvl() * @see Walker_Nav_Menu::start_lvl()
* *
* @param string $output Used to append additional content (passed by reference). * @param string $output Used to append additional content (passed by reference).
* @param int $depth Depth of menu item. Used for padding. * @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments. * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments.
*/ */
public function start_lvl(&$output, $depth = 0, $args = array()) public function start_lvl(&$output, $depth = 0, $args = null)
{ {
if (isset($args->item_spacing) && 'discard' === $args->item_spacing) { if (isset($args->item_spacing) && 'discard' === $args->item_spacing) {
$t = ''; $t = '';
@ -77,12 +95,12 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : ''; $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
/* /*
* The `.dropdown-menu` container needs to have a labelledby * The `.dropdown-menu` container needs to have a labelledby
* attribute which points to it's trigger link. * attribute which points to it's trigger link.
* *
* Form a string for the labelledby attribute from the the latest * Form a string for the labelledby attribute from the the latest
* link with an id that was added to the $output. * link with an id that was added to the $output.
*/ */
$labelledby = ''; $labelledby = '';
// Find all links with an id in the output. // Find all links with an id in the output.
preg_match_all('/(<a.*?id=\"|\')(.*?)\"|\'.*?>/im', $output, $matches); preg_match_all('/(<a.*?id=\"|\')(.*?)\"|\'.*?>/im', $output, $matches);
@ -91,7 +109,7 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
// Build a string to use as aria-labelledby. // Build a string to use as aria-labelledby.
$labelledby = 'aria-labelledby="' . esc_attr(end($matches[2])) . '"'; $labelledby = 'aria-labelledby="' . esc_attr(end($matches[2])) . '"';
} }
$output .= "{$n}{$indent}<ul$class_names $labelledby role=\"menu\">{$n}"; $output .= "{$n}{$indent}<ul$class_names $labelledby>{$n}";
} }
/** /**
@ -102,13 +120,13 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
* *
* @see Walker_Nav_Menu::start_el() * @see Walker_Nav_Menu::start_el()
* *
* @param string $output Used to append additional content (passed by reference). * @param string $output Used to append additional content (passed by reference).
* @param WP_Post $item Menu item data object. * @param WP_Nav_Menu_Item $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding. * @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments. * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments.
* @param int $id Current item ID. * @param int $id Current item ID.
*/ */
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) public function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
{ {
if (isset($args->item_spacing) && 'discard' === $args->item_spacing) { if (isset($args->item_spacing) && 'discard' === $args->item_spacing) {
$t = ''; $t = '';
@ -119,22 +137,35 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
} }
$indent = ($depth) ? str_repeat($t, $depth) : ''; $indent = ($depth) ? str_repeat($t, $depth) : '';
if (false !== strpos($args->items_wrap, 'itemscope') && false === $this->has_schema) {
$this->has_schema = true;
$args->link_before = '<span itemprop="name">' . $args->link_before;
$args->link_after .= '</span>';
}
$classes = empty($item->classes) ? array() : (array) $item->classes; $classes = empty($item->classes) ? array() : (array) $item->classes;
/* // Updating the CSS classes of a menu item in the WordPress Customizer preview results in all classes defined
* Initialize some holder variables to store specially handled item // in that particular input box to come in as one big class string.
* wrappers and icons. $split_on_spaces = function ($class) {
*/ return preg_split('/\s+/', $class);
$linkmod_classes = array(); };
$icon_classes = array(); $classes = $this->flatten(array_map($split_on_spaces, $classes));
/* /*
* Get an updated $classes array without linkmod or icon classes. * Initialize some holder variables to store specially handled item
* * wrappers and icons.
* NOTE: linkmod and icon class arrays are passed by reference and */
* are maybe modified before being used later in this function. $linkmod_classes = array();
*/ $icon_classes = array();
$classes = self::separate_linkmods_and_icons_from_classes($classes, $linkmod_classes, $icon_classes, $depth);
/*
* Get an updated $classes array without linkmod or icon classes.
*
* NOTE: linkmod and icon class arrays are passed by reference and
* are maybe modified before being used later in this function.
*/
$classes = $this->separate_linkmods_and_icons_from_classes($classes, $linkmod_classes, $icon_classes, $depth);
// Join any icon classes plucked from $classes into a string. // Join any icon classes plucked from $classes into a string.
$icon_class_string = join(' ', $icon_classes); $icon_class_string = join(' ', $icon_classes);
@ -142,16 +173,18 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
/** /**
* Filters the arguments for a single nav menu item. * Filters the arguments for a single nav menu item.
* *
* WP 4.4.0 * @since WP 4.4.0
* *
* @param stdClass $args An object of wp_nav_menu() arguments. * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments.
* @param WP_Post $item Menu item data object. * @param WP_Nav_Menu_Item $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding. * @param int $depth Depth of menu item. Used for padding.
*
* @var WP_Nav_Menu_Args
*/ */
$args = apply_filters('nav_menu_item_args', $args, $item, $depth); $args = apply_filters('nav_menu_item_args', $args, $item, $depth);
// Add .dropdown or .active classes where they are needed. // Add .dropdown or .active classes where they are needed.
if (isset($args->has_children) && $args->has_children) { if ($this->has_children) {
$classes[] = 'dropdown'; $classes[] = 'dropdown';
} }
if (in_array('current-menu-item', $classes, true) || in_array('current-menu-parent', $classes, true)) { if (in_array('current-menu-item', $classes, true) || in_array('current-menu-parent', $classes, true)) {
@ -175,44 +208,38 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
* @since WP 3.0.1 * @since WP 3.0.1
* @since WP 4.1.0 The `$depth` parameter was added. * @since WP 4.1.0 The `$depth` parameter was added.
* *
* @param string $menu_id The ID that is applied to the menu item's `<li>` element. * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
* @param WP_Post $item The current menu item. * @param WP_Nav_Menu_Item $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments. * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding. * @param int $depth Depth of menu item. Used for padding.
*/ */
$id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth); $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth);
$id = $id ? ' id="' . esc_attr($id) . '"' : ''; $id = $id ? ' id="' . esc_attr($id) . '"' : '';
if ($args->has_children) { $output .= $indent . '<li ' . $id . $class_names . '>';
$output .= $indent . '<li' . $id . ' class="nav-item dropdown" ' . '>';
} else {
$output .= $indent . '<li' . $id . ' class="nav-item" ' . '>';
}
// Initialize array for holding the $atts for the link item. // Initialize array for holding the $atts for the link item.
$atts = array(); $atts = array();
$atts['title'] = !empty($item->attr_title) ? $item->attr_title : '';
/* $atts['target'] = !empty($item->target) ? $item->target : '';
* Set title from item to the $atts array - if title is empty then if ('_blank' === $item->target && empty($item->xfn)) {
* default to item title. $atts['rel'] = 'noopener noreferrer';
*/
if (empty($item->attr_title)) {
$atts['title'] = !empty($item->title) ? strip_tags($item->title) : '';
} else { } else {
$atts['title'] = $item->attr_title; $atts['rel'] = !empty($item->xfn) ? $item->xfn : '';
} }
$atts['target'] = !empty($item->target) ? $item->target : ''; // If the item has_children add atts to <a>.
$atts['rel'] = !empty($item->xfn) ? $item->xfn : ''; if ($this->has_children && 0 === $depth) {
// If the item has children, add atts to the <a>. $atts['href'] = '#';
if (isset($args->has_children) && $args->has_children && 0 === $depth && $args->depth > 1) { $atts['data-toggle'] = 'dropdown';
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['aria-haspopup'] = 'true';
$atts['aria-expanded'] = 'false'; $atts['aria-expanded'] = 'false';
$atts['class'] = 'dropdown-toggle nav-link'; $atts['class'] = 'dropdown-toggle nav-link';
$atts['id'] = 'menu-item-dropdown-' . $item->ID; $atts['id'] = 'menu-item-dropdown-' . $item->ID;
} else { } else {
if (true === $this->has_schema) {
$atts['itemprop'] = 'url';
}
$atts['href'] = !empty($item->url) ? $item->url : '#'; $atts['href'] = !empty($item->url) ? $item->url : '#';
// For items in dropdowns use .dropdown-item instead of .nav-link. // For items in dropdowns use .dropdown-item instead of .nav-link.
if ($depth > 0) { if ($depth > 0) {
@ -225,7 +252,8 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
$atts['aria-current'] = $item->current ? 'page' : ''; $atts['aria-current'] = $item->current ? 'page' : '';
// Update atts of this item based on any custom linkmod classes. // Update atts of this item based on any custom linkmod classes.
$atts = self::update_atts_for_linkmod_type($atts, $linkmod_classes); $atts = $this->update_atts_for_linkmod_type($atts, $linkmod_classes);
// Allow filtering of the $atts array before using it. // Allow filtering of the $atts array before using it.
$atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth); $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth);
@ -233,34 +261,34 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
$attributes = ''; $attributes = '';
foreach ($atts as $attr => $value) { foreach ($atts as $attr => $value) {
if (!empty($value)) { if (!empty($value)) {
$value = ('href' === $attr) ? esc_url($value) : esc_attr($value); $value = ('href' === $attr) ? esc_url($value) : esc_attr($value);
$attributes .= ' ' . $attr . '="' . $value . '"'; $attributes .= ' ' . $attr . '="' . $value . '"';
} }
} }
// Set a typeflag to easily test if this is a linkmod or not. // Set a typeflag to easily test if this is a linkmod or not.
$linkmod_type = self::get_linkmod_type($linkmod_classes); $linkmod_type = $this->get_linkmod_type($linkmod_classes);
// START appending the internal item contents to the output. // START appending the internal item contents to the output.
$item_output = isset($args->before) ? $args->before : ''; $item_output = isset($args->before) ? $args->before : '';
/* /*
* This is the start of the internal nav item. Depending on what * This is the start of the internal nav item. Depending on what
* kind of linkmod we have we may need different wrapper elements. * kind of linkmod we have we may need different wrapper elements.
*/ */
if ('' !== $linkmod_type) { if ('' !== $linkmod_type) {
// Is linkmod, output the required element opener. // Is linkmod, output the required element opener.
$item_output .= self::linkmod_element_open($linkmod_type, $attributes); $item_output .= $this->linkmod_element_open($linkmod_type, $attributes);
} else { } else {
// With no link mod type set this must be a standard <a> tag. // With no link mod type set this must be a standard <a> tag.
$item_output .= '<a' . $attributes . '>'; $item_output .= '<a' . $attributes . '>';
} }
/* /*
* Initiate empty icon var, then if we have a string containing any * Initiate empty icon var, then if we have a string containing any
* icon classes form the icon markup with an <i> element. This is * icon classes form the icon markup with an <i> element. This is
* output inside of the item before the $title (the link text). * output inside of the item before the $title (the link text).
*/ */
$icon_html = ''; $icon_html = '';
if (!empty($icon_class_string)) { if (!empty($icon_class_string)) {
// Append an <i> with the icon classes to what is output before links. // Append an <i> with the icon classes to what is output before links.
@ -275,16 +303,16 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
* *
* @since WP 4.4.0 * @since WP 4.4.0
* *
* @param string $title The menu item's title. * @param string $title The menu item's title.
* @param WP_Post $item The current menu item. * @param WP_Nav_Menu_Item $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments. * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding. * @param int $depth Depth of menu item. Used for padding.
*/ */
$title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth); $title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth);
// If the .sr-only class was set apply to the nav items text only. // If the .sr-only class was set apply to the nav items text only.
if (in_array('sr-only', $linkmod_classes, true)) { if (in_array('sr-only', $linkmod_classes, true)) {
$title = self::wrap_for_screen_reader($title); $title = $this->wrap_for_screen_reader($title);
$keys_to_unset = array_keys($linkmod_classes, 'sr-only', true); $keys_to_unset = array_keys($linkmod_classes, 'sr-only', true);
foreach ($keys_to_unset as $k) { foreach ($keys_to_unset as $k) {
unset($linkmod_classes[$k]); unset($linkmod_classes[$k]);
@ -295,12 +323,12 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
$item_output .= isset($args->link_before) ? $args->link_before . $icon_html . $title . $args->link_after : ''; $item_output .= isset($args->link_before) ? $args->link_before . $icon_html . $title . $args->link_after : '';
/* /*
* This is the end of the internal nav item. We need to close the * This is the end of the internal nav item. We need to close the
* correct element depending on the type of link or link mod. * correct element depending on the type of link or link mod.
*/ */
if ('' !== $linkmod_type) { if ('' !== $linkmod_type) {
// Is linkmod, output the required closing element. // Is linkmod, output the required closing element.
$item_output .= self::linkmod_element_close($linkmod_type); $item_output .= $this->linkmod_element_close($linkmod_type);
} else { } else {
// With no link mod type set this must be a standard <a> tag. // With no link mod type set this must be a standard <a> tag.
$item_output .= '</a>'; $item_output .= '</a>';
@ -310,94 +338,86 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
// END appending the internal item contents to the output. // END appending the internal item contents to the output.
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
} }
/** /**
* Traverse elements to create list from elements. * Menu fallback.
*
* Display one element if the element doesn't have any children otherwise,
* display the element and its children. Will only traverse up to the max
* depth and no ignore elements under that depth. It is possible to set the
* max depth to include all depths, see walk() method.
*
* This method should not be called directly, use the walk() method instead.
*
* @since WP 2.5.0
*
* @see Walker::start_lvl()
*
* @param object $element Data object.
* @param array $children_elements List of elements to continue traversing (passed by reference).
* @param int $max_depth Max depth to traverse.
* @param int $depth Depth of current element.
* @param array $args An array of arguments.
* @param string $output Used to append additional content (passed by reference).
*/
public function display_element($element, &$children_elements, $max_depth, $depth, $args, &$output)
{
if (!$element) {
return;}
$id_field = $this->db_fields['id'];
// Display this element.
if (is_object($args[0])) {
$args[0]->has_children = !empty($children_elements[$element->$id_field]);}
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
/**
* Menu Fallback.
* *
* If this function is assigned to the wp_nav_menu's fallback_cb variable * If this function is assigned to the wp_nav_menu's fallback_cb variable
* and a menu has not been assigned to the theme location in the WordPress * and a menu has not been assigned to the theme location in the WordPress
* menu manager the function with display nothing to a non-logged in user, * menu manager the function will display nothing to a non-logged in user,
* and will add a link to the WordPress menu manager if logged in as an admin. * and will add a link to the WordPress menu manager if logged in as an admin.
* *
* @param array $args passed from the wp_nav_menu function. * @param array $args passed from the wp_nav_menu function.
* @return string|void String when echo is false.
*/ */
public static function fallback($args) public static function fallback($args)
{ {
if (current_user_can('edit_theme_options')) { if (!current_user_can('edit_theme_options')) {
return;
}
// Get Arguments. // Initialize var to store fallback html.
$container = $args['container']; $fallback_output = '';
$container_id = $args['container_id'];
$container_class = $args['container_class'];
$menu_class = $args['menu_class'];
$menu_id = $args['menu_id'];
// Initialize var to store fallback html. // Menu container opening tag.
$fallback_output = ''; $show_container = false;
if ($args['container']) {
if ($container) { /**
$fallback_output .= '<' . esc_attr($container); * Filters the list of HTML tags that are valid for use as menu containers.
if ($container_id) { *
$fallback_output .= ' id="' . esc_attr($container_id) . '"'; * @since WP 3.0.0
} *
if ($container_class) { * @param array $tags The acceptable HTML tags for use as menu containers.
$fallback_output .= ' class="' . esc_attr($container_class) . '"'; * Default is array containing 'div' and 'nav'.
} */
$fallback_output .= '>'; $allowed_tags = apply_filters('wp_nav_menu_container_allowedtags', array('div', 'nav'));
} if (is_string($args['container']) && in_array($args['container'], $allowed_tags, true)) {
$fallback_output .= '<ul'; $show_container = true;
if ($menu_id) { $class = $args['container_class'] ? ' class="menu-fallback-container ' . esc_attr($args['container_class']) . '"' : ' class="menu-fallback-container"';
$fallback_output .= ' id="' . esc_attr($menu_id) . '"';} $id = $args['container_id'] ? ' id="' . esc_attr($args['container_id']) . '"' : '';
if ($menu_class) { $fallback_output .= '<' . $args['container'] . $id . $class . '>';
$fallback_output .= ' class="' . esc_attr($menu_class) . '"';}
$fallback_output .= '>';
$fallback_output .= '<li class="nav-item"><a href="' . esc_url(admin_url('nav-menus.php')) . '" class="nav-link">' . esc_attr__('添加导航', 'kratos') . '</a></li>';
$fallback_output .= '</ul>';
if ($container) {
$fallback_output .= '</' . esc_attr($container) . '>';
}
// If $args has 'echo' key and it's true echo, otherwise return.
if (array_key_exists('echo', $args) && $args['echo']) {
echo $fallback_output; // WPCS: XSS OK.
} else {
return $fallback_output;
} }
} }
// The fallback menu.
$class = $args['menu_class'] ? ' class="menu-fallback-menu ' . esc_attr($args['menu_class']) . '"' : ' class="menu-fallback-menu"';
$id = $args['menu_id'] ? ' id="' . esc_attr($args['menu_id']) . '"' : '';
$fallback_output .= '<ul' . $id . $class . '>';
$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>';
$fallback_output .= '</ul>';
// Menu container closing tag.
if ($show_container) {
$fallback_output .= '</' . $args['container'] . '>';
}
// if $args has 'echo' key and it's true echo, otherwise return.
if (array_key_exists('echo', $args) && $args['echo']) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $fallback_output;
} else {
return $fallback_output;
}
}
/**
* Filter to ensure the items_Wrap argument contains microdata.
*
* @since 4.2.0
*
* @param array $args The nav instance arguments.
* @return array $args The altered nav instance arguments.
*/
public function add_schema_to_navbar_ul($args)
{
if (isset($args['items_wrap'])) {
$wrap = $args['items_wrap'];
if (strpos($wrap, 'SiteNavigationElement') === false) {
$args['items_wrap'] = preg_replace('/(>).*>?\%3\$s/', ' itemscope itemtype="http://www.schema.org/SiteNavigationElement"$0', $wrap);
}
}
return $args;
} }
/** /**
@ -423,18 +443,18 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
// Loop through $classes array to find linkmod or icon classes. // Loop through $classes array to find linkmod or icon classes.
foreach ($classes as $key => $class) { foreach ($classes as $key => $class) {
/* /*
* If any special classes are found, store the class in it's * If any special classes are found, store the class in it's
* holder array and and unset the item from $classes. * holder array and and unset the item from $classes.
*/ */
if (preg_match('/^disabled|^sr-only/i', $class)) { if (preg_match('/^disabled|^sr-only/i', $class)) {
// Test for .disabled or .sr-only classes. // Test for .disabled or .sr-only classes.
$linkmod_classes[] = $class; $linkmod_classes[] = $class;
unset($classes[$key]); unset($classes[$key]);
} elseif (preg_match('/^dropdown-header|^dropdown-divider|^dropdown-item-text/i', $class) && $depth > 0) { } elseif (preg_match('/^dropdown-header|^dropdown-divider|^dropdown-item-text/i', $class) && $depth > 0) {
/* /*
* Test for .dropdown-header or .dropdown-divider and a * Test for .dropdown-header or .dropdown-divider and a
* depth greater than 0 - IE inside a dropdown. * depth greater than 0 - IE inside a dropdown.
*/ */
$linkmod_classes[] = $class; $linkmod_classes[] = $class;
unset($classes[$key]); unset($classes[$key]);
} elseif (preg_match('/^fa-(\S*)?|^fa(s|r|l|b)?(\s?)?$/i', $class)) { } elseif (preg_match('/^fa-(\S*)?|^fa(s|r|l|b)?(\s?)?$/i', $class)) {
@ -499,9 +519,9 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
foreach ($linkmod_classes as $link_class) { foreach ($linkmod_classes as $link_class) {
if (!empty($link_class)) { if (!empty($link_class)) {
/* /*
* Update $atts with a space and the extra classname * Update $atts with a space and the extra classname
* so long as it's not a sr-only class. * so long as it's not a sr-only class.
*/ */
if ('sr-only' !== $link_class) { if ('sr-only' !== $link_class) {
$atts['class'] .= ' ' . esc_attr($link_class); $atts['class'] .= ' ' . esc_attr($link_class);
} }
@ -554,9 +574,9 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
$output .= '<span class="dropdown-item-text"' . $attributes . '>'; $output .= '<span class="dropdown-item-text"' . $attributes . '>';
} elseif ('dropdown-header' === $linkmod_type) { } elseif ('dropdown-header' === $linkmod_type) {
/* /*
* For a header use a span with the .h6 class instead of a real * For a header use a span with the .h6 class instead of a real
* header tag so that it doesn't confuse screen readers. * header tag so that it doesn't confuse screen readers.
*/ */
$output .= '<span class="dropdown-header h6"' . $attributes . '>'; $output .= '<span class="dropdown-header h6"' . $attributes . '>';
} elseif ('dropdown-divider' === $linkmod_type) { } elseif ('dropdown-divider' === $linkmod_type) {
// This is a divider. // This is a divider.
@ -579,9 +599,9 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
$output = ''; $output = '';
if ('dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type) { if ('dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type) {
/* /*
* For a header use a span with the .h6 class instead of a real * For a header use a span with the .h6 class instead of a real
* header tag so that it doesn't confuse screen readers. * header tag so that it doesn't confuse screen readers.
*/ */
$output .= '</span>'; $output .= '</span>';
} elseif ('dropdown-divider' === $linkmod_type) { } elseif ('dropdown-divider' === $linkmod_type) {
// This is a divider. // This is a divider.
@ -589,5 +609,26 @@ if (!class_exists('WP_Bootstrap_Navwalker')) {
} }
return $output; return $output;
} }
/**
* Flattens a multidimensional array to a simple array.
*
* @param array $array a multidimensional array.
*
* @return array a simple array
*/
public function flatten($array)
{
$result = array();
foreach ($array as $element) {
if (is_array($element)) {
array_push($result, ...$this->flatten($element));
} else {
$result[] = $element;
}
}
return $result;
}
} }
}
endif;