| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Walkers; |
| 4 |
use ResponsiveMenu\Collections\OptionsCollection as OptionsCollection; |
| 5 |
|
| 6 |
class WpWalker extends \Walker_Nav_Menu |
| 7 |
{ |
| 8 |
|
| 9 |
private $curItem; |
| 10 |
|
| 11 |
public function __construct(OptionsCollection $options) |
| 12 |
{ |
| 13 |
$this->options = $options; |
| 14 |
} |
| 15 |
|
| 16 |
public function start_lvl( &$output, $depth = 0, $args = array()) |
| 17 |
{ |
| 18 |
if($this->options['auto_expand_all_submenus'] == 'on'): |
| 19 |
$class = 'responsive-menu-submenu-open'; |
| 20 |
elseif( |
| 21 |
($this->options['auto_expand_current_submenus'] == 'on') |
| 22 |
&& ($this->curItem->current_item_ancestor || $this->curItem->current_item_parent) |
| 23 |
): |
| 24 |
$class = 'responsive-menu-submenu-open'; |
| 25 |
else: |
| 26 |
$class = ''; |
| 27 |
endif; |
| 28 |
$output .= "<ul class='responsive-menu-submenu responsive-menu-submenu-depth-" . ($depth + 1) . " {$class}'>"; |
| 29 |
} |
| 30 |
|
| 31 |
public function end_lvl( &$output, $depth = 0, $args = array() ) |
| 32 |
{ |
| 33 |
$output .= "</ul>"; |
| 34 |
} |
| 35 |
|
| 36 |
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) |
| 37 |
{ |
| 38 |
$this->curItem = $item; |
| 39 |
$classes = empty( $item->classes ) ? array() : (array) $item->classes; |
| 40 |
$responsive_menu_classes = $classes; |
| 41 |
|
| 42 |
# Turn into our Responsive Menu Classes |
| 43 |
foreach($classes as $class): |
| 44 |
switch($class): |
| 45 |
case 'menu-item': $responsive_menu_classes[] = 'responsive-menu-item'; break; |
| 46 |
case 'current-menu-item': $responsive_menu_classes[] = 'responsive-menu-current-item'; break; |
| 47 |
case 'menu-item-has-children': $responsive_menu_classes[] = 'responsive-menu-item-has-children'; break; |
| 48 |
case 'current-menu-parent': $responsive_menu_classes[] = 'responsive-menu-item-current-parent'; break; |
| 49 |
case 'current-menu-ancestor': $responsive_menu_classes[] = 'responsive-menu-item-current-ancestor'; break; |
| 50 |
endswitch; |
| 51 |
endforeach; |
| 52 |
|
| 53 |
$class_names = join(' ', array_unique($responsive_menu_classes)); |
| 54 |
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
| 55 |
|
| 56 |
$id = ' id="responsive-menu-item-' . esc_attr( $item->ID ) . '"'; |
| 57 |
|
| 58 |
$output .= '<li' . $id . $class_names .'>'; |
| 59 |
|
| 60 |
$atts = array(); |
| 61 |
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
| 62 |
$atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
| 63 |
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
| 64 |
$atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
| 65 |
$atts['class'] = 'responsive-menu-item-link'; |
| 66 |
|
| 67 |
|
| 68 |
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); |
| 69 |
|
| 70 |
$attributes = ''; |
| 71 |
foreach ( $atts as $attr => $value ) { |
| 72 |
if ( ! empty( $value ) ) { |
| 73 |
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
| 74 |
$attributes .= ' ' . $attr . '="' . $value . '"'; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** This filter is documented in wp-includes/post-template.php */ |
| 79 |
$title = apply_filters( 'the_title', $item->title, $item->ID ); |
| 80 |
|
| 81 |
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth ); |
| 82 |
|
| 83 |
/* Calculate which arrow to show */ |
| 84 |
if(in_array('responsive-menu-item-has-children', $responsive_menu_classes)): |
| 85 |
$inactive_arrow = '<div class="responsive-menu-subarrow">' . $this->options->getInActiveArrow() . '</div>'; |
| 86 |
$active_arrow = '<div class="responsive-menu-subarrow responsive-menu-subarrow-active">' . $this->options->getActiveArrow() . '</div>'; |
| 87 |
if($this->options['auto_expand_all_submenus'] == 'on'): |
| 88 |
$initial_arrow = $active_arrow; |
| 89 |
elseif( |
| 90 |
$this->options['auto_expand_current_submenus'] == 'on' && (in_array('responsive-menu-item-current-parent', $responsive_menu_classes) |
| 91 |
|| in_array('responsive-menu-item-current-ancestor', $responsive_menu_classes))): |
| 92 |
$initial_arrow = $active_arrow; |
| 93 |
else: |
| 94 |
$initial_arrow = $inactive_arrow; |
| 95 |
endif; |
| 96 |
else: |
| 97 |
$initial_arrow = ''; |
| 98 |
endif; |
| 99 |
|
| 100 |
/* Clear Arrow if we are at the final depth level */ |
| 101 |
if($depth + 1 == $this->options['menu_depth']->getValue()) |
| 102 |
$initial_arrow = ''; |
| 103 |
|
| 104 |
$item_output = '<a'. $attributes .'>'; |
| 105 |
$item_output .= $title; |
| 106 |
$item_output .= $initial_arrow; |
| 107 |
$item_output .= '</a>'; |
| 108 |
|
| 109 |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
public function end_el( &$output, $item, $depth = 0, $args = array() ) |
| 114 |
{ |
| 115 |
$output .= "</li>"; |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|