| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
class Easyel_Menu_Walker extends \Walker_Nav_Menu { |
| 7 |
|
| 8 |
/** |
| 9 |
* Start element |
| 10 |
*/ |
| 11 |
public function start_el( &$output, $item, $depth = 0, $args = [], $id = 0 ) { |
| 12 |
|
| 13 |
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
| 14 |
$args = (object) $args; |
| 15 |
|
| 16 |
$class_names = ''; |
| 17 |
$value = ''; |
| 18 |
$rel_xfn = ''; |
| 19 |
$rel_blank = ''; |
| 20 |
|
| 21 |
$classes = ! empty( $item->classes ) && is_array( $item->classes ) ? $item->classes : []; |
| 22 |
$submenu = $args->has_children ? ' eel-has-submenu' : ''; |
| 23 |
|
| 24 |
if ( 0 === $depth ) array_push( $classes, 'parent' ); |
| 25 |
if ( $args->has_children ) array_push( $classes, 'menu-item-has-children' ); |
| 26 |
|
| 27 |
// last-item logic |
| 28 |
$is_last_item = false; |
| 29 |
$menu_last_item_setting = isset( $args->menu_last_item ) ? $args->menu_last_item : ''; |
| 30 |
if ( $depth === 0 ) { |
| 31 |
$menu_items = wp_get_nav_menu_items( $args->menu ); |
| 32 |
if ( $menu_items && is_array( $menu_items ) ) { |
| 33 |
$top_level_items = array_filter( $menu_items, fn($menu_item) => $menu_item->menu_item_parent == 0 ); |
| 34 |
$last_top_level_item = end( $top_level_items ); |
| 35 |
if ( $last_top_level_item && $last_top_level_item->ID === $item->ID ) $is_last_item = true; |
| 36 |
} |
| 37 |
} |
| 38 |
if ( $is_last_item && $menu_last_item_setting === 'cta' ) $classes[] = 'last-item'; |
| 39 |
|
| 40 |
$filtered_classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ); |
| 41 |
if ( ! is_array( $filtered_classes ) ) $filtered_classes = array_filter( $classes ); |
| 42 |
|
| 43 |
|
| 44 |
$class_names = ' class="' . esc_attr( join( ' ', $filtered_classes ) ) . $submenu . ' eel-creative-menu"'; |
| 45 |
$value = apply_filters( 'nav_menu_li_values', $value ); |
| 46 |
|
| 47 |
$output .= $indent . '<li id="menu-item-' . $item->ID . '"' . $value . $class_names . '>'; |
| 48 |
|
| 49 |
// anchor attributes |
| 50 |
if ( isset( $item->target ) && '_blank' === $item->target && isset( $item->xfn ) && false === strpos( $item->xfn, 'noopener' ) ) $rel_xfn = ' noopener'; |
| 51 |
if ( isset( $item->target ) && '_blank' === $item->target && isset( $item->xfn ) && empty( $item->xfn ) ) $rel_blank = 'rel="noopener"'; |
| 52 |
|
| 53 |
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : ''; |
| 54 |
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : ''; |
| 55 |
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . $rel_xfn . '"' : '' . $rel_blank; |
| 56 |
$attributes .= ! empty( $item->url ) ? ' href="' . esc_url( $item->url ) . '"' : ''; |
| 57 |
if ( $args->has_children ) $attributes .= ' aria-haspopup="true" aria-expanded="false"'; |
| 58 |
|
| 59 |
$atts = apply_filters( 'Easyel_Menu_Walker_nav_menu_attrs', $attributes ); |
| 60 |
|
| 61 |
$item_output = $args->before; |
| 62 |
$item_output .= '<a' . $atts; |
| 63 |
if ( 0 === $depth ) { |
| 64 |
$anchor_class = 'eel-menu-item'; |
| 65 |
if ( $is_last_item && $menu_last_item_setting === 'cta' ) $anchor_class .= ' elementor-button'; |
| 66 |
$item_output .= ' class="' . $anchor_class . '"'; |
| 67 |
} else { |
| 68 |
$item_output .= in_array( 'current-menu-item', $classes ) ? ' class="eel-sub-menu-item eel-sub-menu-item-active"' : ' class="eel-sub-menu-item"'; |
| 69 |
} |
| 70 |
$item_output .= '>'; |
| 71 |
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; |
| 72 |
$item_output .= '</a>'; |
| 73 |
$item_output .= $args->after; |
| 74 |
|
| 75 |
// --------------------------- |
| 76 |
// Elementor Mega Menu Injection |
| 77 |
// --------------------------- |
| 78 |
|
| 79 |
if ( function_exists( 'easy_element_is_enabled' ) && easy_element_is_enabled( 'enable_megamenu_builder' ) ) { |
| 80 |
if ( class_exists( '\Elementor\Plugin' ) && ! \Elementor\Plugin::$instance->preview->is_preview_mode() ) { |
| 81 |
$template_id = get_post_meta( $item->ID, 'easyel__menu_item_elementor_template', true ); |
| 82 |
if ( $template_id !== false ) { |
| 83 |
$template_content = \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id ); |
| 84 |
if ( $template_content ) { |
| 85 |
$item_output .= '<ul class="easyel--elementor-template-mega-menu easyel-mega--current sub-menu">' . $template_content . '</ul>'; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Start level (submenu) |
| 96 |
*/ |
| 97 |
public function start_lvl( &$output, $depth = 0, $args = [] ) { |
| 98 |
$indent = str_repeat( "\t", $depth ); |
| 99 |
$submenu_class = 'sub-menu'; |
| 100 |
if ( $depth > 0 ) $submenu_class .= ' sub-sub-menu'; |
| 101 |
$output .= "\n" . $indent . '<ul class="' . $submenu_class . '">' . "\n"; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* End level (submenu) |
| 106 |
*/ |
| 107 |
public function end_lvl( &$output, $depth = 0, $args = [] ) { |
| 108 |
$indent = str_repeat( "\t", $depth ); |
| 109 |
$output .= $indent . "</ul>\n"; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Display element (children check) |
| 114 |
*/ |
| 115 |
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
| 116 |
$id_field = $this->db_fields['id']; |
| 117 |
if ( is_object( $args[0] ) ) { |
| 118 |
$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] ); |
| 119 |
} |
| 120 |
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 121 |
} |
| 122 |
} |
| 123 |
|