| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
class Easyel_Menu_Walker extends \Walker_Nav_Menu { |
| 6 |
|
| 7 |
/** |
| 8 |
* Start element |
| 9 |
* |
| 10 |
* @since 1.3.0 |
| 11 |
* @param string $output Output HTML. |
| 12 |
* @param object $item Individual Menu item. |
| 13 |
* @param int $depth Depth. |
| 14 |
* @param array $args Arguments array. |
| 15 |
* @param int $id Menu ID. |
| 16 |
* @access public |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function start_el( &$output, $item, $depth = 0, $args = [], $id = 0 ) { |
| 20 |
|
| 21 |
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
| 22 |
$args = (object) $args; |
| 23 |
|
| 24 |
$class_names = ''; |
| 25 |
$value = ''; |
| 26 |
$rel_xfn = ''; |
| 27 |
$rel_blank = ''; |
| 28 |
|
| 29 |
// Ensure $classes is always an array |
| 30 |
$classes = []; |
| 31 |
if ( ! empty( $item->classes ) && is_array( $item->classes ) ) { |
| 32 |
$classes = $item->classes; |
| 33 |
} |
| 34 |
$submenu = $args->has_children ? ' eel-has-submenu' : ''; |
| 35 |
|
| 36 |
if ( 0 === $depth ) { |
| 37 |
array_push( $classes, 'parent' ); |
| 38 |
} |
| 39 |
|
| 40 |
// Add mobile-friendly classes for better compatibility |
| 41 |
if ( $args->has_children ) { |
| 42 |
array_push( $classes, 'menu-item-has-children' ); |
| 43 |
} |
| 44 |
|
| 45 |
// Check if this is the last menu item and if Button option is selected |
| 46 |
$is_last_item = false; |
| 47 |
$menu_last_item_setting = ''; |
| 48 |
|
| 49 |
// Get the menu last item setting from the widget |
| 50 |
if ( isset( $args->menu_last_item ) ) { |
| 51 |
$menu_last_item_setting = $args->menu_last_item; |
| 52 |
} |
| 53 |
|
| 54 |
// Check if this is the last top-level menu item |
| 55 |
if ( $depth === 0 ) { |
| 56 |
// Get all top-level menu items to check if this is the last one |
| 57 |
$menu_items = wp_get_nav_menu_items( $args->menu ); |
| 58 |
if ( $menu_items && is_array( $menu_items ) ) { |
| 59 |
$top_level_items = array_filter( $menu_items, function( $menu_item ) { |
| 60 |
return $menu_item->menu_item_parent == 0; |
| 61 |
} ); |
| 62 |
|
| 63 |
$last_top_level_item = end( $top_level_items ); |
| 64 |
if ( $last_top_level_item && $last_top_level_item->ID === $item->ID ) { |
| 65 |
$is_last_item = true; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// Add last-item class to li element if Button option is selected |
| 71 |
if ( $is_last_item && $menu_last_item_setting === 'cta' ) { |
| 72 |
array_push( $classes, 'last-item' ); |
| 73 |
} |
| 74 |
|
| 75 |
// Ensure we have a valid array before applying filters |
| 76 |
$filtered_classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ); |
| 77 |
if ( ! is_array( $filtered_classes ) ) { |
| 78 |
$filtered_classes = array_filter( $classes ); |
| 79 |
} |
| 80 |
$class_names = join( ' ', $filtered_classes ); |
| 81 |
$class_names = ' class="' . esc_attr( $class_names ) . $submenu . ' eel-creative-menu"'; |
| 82 |
$value = apply_filters( 'nav_menu_li_values', $value ); |
| 83 |
|
| 84 |
$output .= $indent . '<li id="menu-item-' . $item->ID . '"' . $value . $class_names . '>'; |
| 85 |
|
| 86 |
if ( isset( $item->target ) && '_blank' === $item->target && isset( $item->xfn ) && false === strpos( $item->xfn, 'noopener' ) ) { |
| 87 |
$rel_xfn = ' noopener'; |
| 88 |
} |
| 89 |
if ( isset( $item->target ) && '_blank' === $item->target && isset( $item->xfn ) && empty( $item->xfn ) ) { |
| 90 |
$rel_blank = 'rel="noopener"'; |
| 91 |
} |
| 92 |
|
| 93 |
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : ''; |
| 94 |
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : ''; |
| 95 |
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . $rel_xfn . '"' : '' . $rel_blank; |
| 96 |
$attributes .= ! empty( $item->url ) ? ' href="' . esc_url( $item->url ) . '"' : ''; |
| 97 |
|
| 98 |
// Enhanced accessibility: Add aria-haspopup and aria-expanded for menu items with children |
| 99 |
if ( $args->has_children ) { |
| 100 |
$attributes .= ' aria-haspopup="true" aria-expanded="false"'; |
| 101 |
} |
| 102 |
|
| 103 |
$atts = apply_filters( 'Easyel_Menu_Walker_nav_menu_attrs', $attributes ); |
| 104 |
|
| 105 |
$item_output = $args->has_children ? '<div class="eel-has-submenu-container">' : ''; |
| 106 |
$item_output .= $args->before; |
| 107 |
$item_output .= '<a' . $atts; |
| 108 |
if ( 0 === $depth ) { |
| 109 |
$anchor_class = 'eel-menu-item'; |
| 110 |
// Add elementor-button class to anchor if it's the last item and Button option is selected |
| 111 |
if ( $is_last_item && $menu_last_item_setting === 'cta' ) { |
| 112 |
$anchor_class .= ' elementor-button'; |
| 113 |
} |
| 114 |
$item_output .= ' class="' . $anchor_class . '"'; |
| 115 |
} else { |
| 116 |
$item_output .= in_array( 'current-menu-item', $classes ) ? ' class="eel-sub-menu-item eel-sub-menu-item-active"' : ' class="eel-sub-menu-item"'; |
| 117 |
} |
| 118 |
|
| 119 |
$item_output .= '>'; |
| 120 |
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; |
| 121 |
if ( $args->has_children ) { |
| 122 |
$item_output .= sprintf( |
| 123 |
'<span class="eel-menu-toggle sub-arrow eel-menu-child-%1$d" tabindex="0" role="button" aria-label="%2$s" aria-expanded="false" data-menu-item-id="%1$d"> |
| 124 |
<i class="unicon-chevron-down" aria-hidden="true"></i> |
| 125 |
</span>', |
| 126 |
$item->ID, |
| 127 |
esc_attr__('Toggle Submenu', 'easy-elements') |
| 128 |
); |
| 129 |
|
| 130 |
} |
| 131 |
$item_output .= '</a>'; |
| 132 |
|
| 133 |
$item_output .= $args->after; |
| 134 |
$item_output .= $args->has_children ? '</div>' : ''; |
| 135 |
|
| 136 |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Start level (for submenu containers) |
| 141 |
* |
| 142 |
* @since 1.3.0 |
| 143 |
* @param string $output Output HTML. |
| 144 |
* @param int $depth Depth. |
| 145 |
* @param array $args Arguments array. |
| 146 |
* @access public |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public function start_lvl( &$output, $depth = 0, $args = [] ) { |
| 150 |
$indent = str_repeat( "\t", $depth ); |
| 151 |
$submenu_class = 'sub-menu'; |
| 152 |
|
| 153 |
// Add mobile-friendly submenu classes |
| 154 |
if ( $depth > 0 ) { |
| 155 |
$submenu_class .= ' sub-sub-menu'; |
| 156 |
} |
| 157 |
|
| 158 |
$output .= "\n" . $indent . '<ul class="' . $submenu_class . '">' . "\n"; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* End level (for submenu containers) |
| 163 |
* |
| 164 |
* @since 1.3.0 |
| 165 |
* @param string $output Output HTML. |
| 166 |
* @param int $depth Depth. |
| 167 |
* @param array $args Arguments array. |
| 168 |
* @access public |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function end_lvl( &$output, $depth = 0, $args = [] ) { |
| 172 |
$indent = str_repeat( "\t", $depth ); |
| 173 |
$output .= $indent . "</ul>\n"; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Display element |
| 178 |
* |
| 179 |
* @since 1.3.0 |
| 180 |
* @param object $element Individual Menu element. |
| 181 |
* @param object $children_elements Child Elements. |
| 182 |
* @param int $max_depth Maximum Depth. |
| 183 |
* @param int $depth Depth. |
| 184 |
* @param array $args Arguments array. |
| 185 |
* @param string $output Output HTML. |
| 186 |
* @access public |
| 187 |
* @return (void | null) |
| 188 |
*/ |
| 189 |
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
| 190 |
|
| 191 |
$id_field = $this->db_fields['id']; |
| 192 |
|
| 193 |
if ( is_object( $args[0] ) ) { |
| 194 |
$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] ); |
| 195 |
} |
| 196 |
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 197 |
} |
| 198 |
} |