| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Walkers; |
| 4 |
use ResponsiveMenu\Collections\OptionsCollection; |
| 5 |
|
| 6 |
class Walker extends \Walker_Nav_Menu { |
| 7 |
|
| 8 |
private $current_item; |
| 9 |
|
| 10 |
public function __construct(OptionsCollection $options) { |
| 11 |
$this->options = $options; |
| 12 |
} |
| 13 |
|
| 14 |
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { |
| 15 |
|
| 16 |
$this->setCurrentItem($item); |
| 17 |
|
| 18 |
$classes = empty($item->classes) ? array() : (array) $item->classes; |
| 19 |
$responsive_menu_classes = $classes; |
| 20 |
|
| 21 |
# Turn into our Responsive Menu Classes |
| 22 |
foreach($classes as $class): |
| 23 |
switch($class): |
| 24 |
case 'menu-item': $responsive_menu_classes[] = 'responsive-menu-item'; break; |
| 25 |
case 'current-menu-item': $responsive_menu_classes[] = 'responsive-menu-current-item'; break; |
| 26 |
case 'menu-item-has-children': $responsive_menu_classes[] = 'responsive-menu-item-has-children'; break; |
| 27 |
case 'current-menu-parent': $responsive_menu_classes[] = 'responsive-menu-item-current-parent'; break; |
| 28 |
case 'current-menu-ancestor': $responsive_menu_classes[] = 'responsive-menu-item-current-ancestor'; break; |
| 29 |
endswitch; |
| 30 |
endforeach; |
| 31 |
|
| 32 |
/* Clear child class if we are at the final depth level */ |
| 33 |
if(isset($responsive_menu_classes)): |
| 34 |
if($depth + 1 == $this->options['menu_depth'] && ($key = array_search('responsive-menu-item-has-children', $responsive_menu_classes)) !== false) { |
| 35 |
unset($responsive_menu_classes[$key]); |
| 36 |
} |
| 37 |
endif; |
| 38 |
|
| 39 |
$class_names = join(' ', array_unique($responsive_menu_classes)); |
| 40 |
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
| 41 |
|
| 42 |
$id = ' id="responsive-menu-item-' . esc_attr( $item->ID ) . '"'; |
| 43 |
|
| 44 |
$output .= '<li' . $id . $class_names .' role="none">'; |
| 45 |
|
| 46 |
$atts = array(); |
| 47 |
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
| 48 |
$atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
| 49 |
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
| 50 |
$atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
| 51 |
$atts['class'] = 'responsive-menu-item-link'; |
| 52 |
$atts['tabindex'] = 1; |
| 53 |
$atts['role']= "menuitem"; |
| 54 |
$atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth); |
| 55 |
|
| 56 |
$attributes = ''; |
| 57 |
foreach ($atts as $attr => $value) { |
| 58 |
if (!empty( $value)) { |
| 59 |
$value = ('href' === $attr ) ? esc_url( $value) : esc_attr($value); |
| 60 |
$attributes .= ' ' . $attr . '="' . $value . '"'; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$title = apply_filters('the_title', $item->title, $item->ID); |
| 65 |
$title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth); |
| 66 |
|
| 67 |
/* Calculate which arrow to show */ |
| 68 |
if(in_array('responsive-menu-item-has-children', $responsive_menu_classes)): |
| 69 |
$inactive_arrow = '<div class="responsive-menu-subarrow">' . $this->options->getInActiveArrow() . '</div>'; |
| 70 |
$active_arrow = '<div class="responsive-menu-subarrow responsive-menu-subarrow-active">' . $this->options->getActiveArrow() . '</div>'; |
| 71 |
if($this->options['auto_expand_all_submenus'] == 'on'): |
| 72 |
$initial_arrow = $active_arrow; |
| 73 |
elseif( |
| 74 |
$this->options['auto_expand_current_submenus'] == 'on' && (in_array('responsive-menu-item-current-parent', $responsive_menu_classes) |
| 75 |
|| in_array('responsive-menu-item-current-ancestor', $responsive_menu_classes))): |
| 76 |
$initial_arrow = $active_arrow; |
| 77 |
else: |
| 78 |
$initial_arrow = $inactive_arrow; |
| 79 |
endif; |
| 80 |
else: |
| 81 |
$initial_arrow = ''; |
| 82 |
endif; |
| 83 |
|
| 84 |
/* Clear Arrow if we are at the final depth level */ |
| 85 |
if($depth + 1 == $this->options['menu_depth']) |
| 86 |
$initial_arrow = ''; |
| 87 |
|
| 88 |
$item_output = '<a'. $attributes .'>'; |
| 89 |
$item_output .= $title; |
| 90 |
$item_output .= $initial_arrow; |
| 91 |
|
| 92 |
if(isset($item->description) && $item->description && $this->options['submenu_descriptions_on']) |
| 93 |
$item_output .= '<span class="responsive-menu-item-description">' . $item->description . '</span>'; |
| 94 |
|
| 95 |
$item_output .= '</a>'; |
| 96 |
|
| 97 |
// Theme support for twenty twenty one. |
| 98 |
if ( function_exists( 'twenty_twenty_one_add_sub_menu_toggle' ) ) { |
| 99 |
remove_filter( 'walker_nav_menu_start_el', 'twenty_twenty_one_add_sub_menu_toggle', 10 ); |
| 100 |
remove_filter( 'walker_nav_menu_start_el', 'twenty_twenty_one_nav_menu_social_icons', 10 ); |
| 101 |
} |
| 102 |
|
| 103 |
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
public function start_lvl(&$output, $depth = 0, $args = array()) { |
| 108 |
$output .= "<ul aria-label='". esc_attr( $this->current_item->title) ."' role='menu' class='responsive-menu-submenu responsive-menu-submenu-depth-" . ($depth + 1) . $this->getSubmenuClassOpenOrNot() . "'>"; |
| 109 |
} |
| 110 |
|
| 111 |
public function end_el(&$output, $item, $depth = 0, $args = array()) { |
| 112 |
$output .= "</li>"; |
| 113 |
} |
| 114 |
|
| 115 |
public function end_lvl(&$output, $depth = 0, $args = array()) { |
| 116 |
$output .= "</ul>"; |
| 117 |
} |
| 118 |
|
| 119 |
public function setCurrentItem($item) { |
| 120 |
$this->current_item = $item; |
| 121 |
} |
| 122 |
|
| 123 |
public function getCurrentItem() { |
| 124 |
return $this->current_item; |
| 125 |
} |
| 126 |
|
| 127 |
public function getSubmenuClassOpenOrNot() { |
| 128 |
return $this->expandAllSubmenuOptionsIsOn() || $this->expandCurrentSubmenuOnAndItemIsParent() ? ' responsive-menu-submenu-open' : ''; |
| 129 |
} |
| 130 |
|
| 131 |
public function expandAllSubmenuOptionsIsOn() { |
| 132 |
return $this->options['auto_expand_all_submenus'] == 'on'; |
| 133 |
} |
| 134 |
|
| 135 |
public function expandCurrentSubmenuOnAndItemIsParent() { |
| 136 |
return ($this->options['auto_expand_current_submenus'] == 'on') |
| 137 |
&& ($this->getCurrentItem()->current_item_ancestor || $this->getCurrentItem()->current_item_parent); |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|