assets
1 week ago
views
7 months ago
api.php
1 week ago
init.php
1 week ago
options.php
4 years ago
walker-nav-menu.php
1 week ago
walker-nav-menu.php
331 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite; |
| 3 | |
| 4 | use \ElementsKit_Lite\Modules\Megamenu\Init; |
| 5 | use \ElementsKit_Lite\Libs\Framework\Attr; |
| 6 | |
| 7 | class ElementsKit_Menu_Walker extends \Walker_Nav_Menu { |
| 8 | |
| 9 | public $menu_Settings; |
| 10 | |
| 11 | // custom methods |
| 12 | public function get_item_meta( $menu_item_id ) { |
| 13 | $meta_key = Init::$menuitem_settings_key; |
| 14 | $data = get_post_meta( $menu_item_id, $meta_key, true ); |
| 15 | $data = (array) json_decode( $data ); |
| 16 | |
| 17 | $default = array( |
| 18 | 'menu_id' => null, |
| 19 | 'menu_has_child' => '', |
| 20 | 'menu_enable' => 0, |
| 21 | 'menu_icon' => '', |
| 22 | 'menu_icon_color' => '', |
| 23 | 'menu_badge_text' => '', |
| 24 | 'menu_badge_color' => '', |
| 25 | 'menu_badge_background' => '', |
| 26 | 'mobile_submenu_content_type' => 'builder_content', |
| 27 | 'vertical_megamenu_position_type' => 'relative_position', |
| 28 | 'vertical_menu_width' => '', |
| 29 | 'megamenu_width_type' => 'default_width', |
| 30 | 'megamenu_ajax_load' => 'no', |
| 31 | ); |
| 32 | return array_merge( $default, $data ); |
| 33 | } |
| 34 | |
| 35 | public function is_megamenu( $menu_slug ) { |
| 36 | $menu_obj = wp_get_nav_menu_object($menu_slug); |
| 37 | $menu_slug = ( ( ( gettype( $menu_obj ) == 'object' ) && ( isset( $menu_obj->slug ) ) ) ? $menu_obj->slug : $menu_slug ); |
| 38 | |
| 39 | $cache_key = 'elementskit_megamenu_data_' . $menu_slug; |
| 40 | $cached = wp_cache_get( $cache_key ); |
| 41 | if ( false !== $cached ) { |
| 42 | return $cached; |
| 43 | } |
| 44 | |
| 45 | $return = 0; |
| 46 | |
| 47 | $modules_all = \ElementsKit_Lite\Config\Module_List::instance()->get_list(); |
| 48 | $modules_active = \ElementsKit_Lite\Libs\Framework\Classes\Utils::instance()->get_option( 'module_list', $modules_all ); |
| 49 | $modules_active = ( ! isset( $modules_active[0] ) ? array_keys( $modules_active ) : $modules_active ); |
| 50 | |
| 51 | $settings = Attr::instance()->utils->get_option( Init::$megamenu_settings_key, array() ); |
| 52 | $term = get_term_by( 'slug', $menu_slug, 'nav_menu' ); |
| 53 | |
| 54 | if ( in_array( 'megamenu', $modules_active ) |
| 55 | && isset( $term->term_id ) |
| 56 | && isset( $settings[ 'menu_location_' . $term->term_id ] ) |
| 57 | && $settings[ 'menu_location_' . $term->term_id ]['is_enabled'] == '1' ) { |
| 58 | |
| 59 | $return = 1; |
| 60 | } |
| 61 | |
| 62 | wp_cache_set( $cache_key, $return ); |
| 63 | return $return; |
| 64 | } |
| 65 | |
| 66 | public function is_megamenu_item( $item_meta, $menu ) { |
| 67 | if ( $this->is_megamenu( $menu ) == 1 && $item_meta['menu_enable'] == 1 && class_exists( 'Elementor\Plugin' ) ) { |
| 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Starts the list before the elements are added. |
| 75 | * |
| 76 | * @see Walker::start_lvl() |
| 77 | * |
| 78 | * @since 3.0.0 |
| 79 | * |
| 80 | * @param string $output Passed by reference. Used to append additional content. |
| 81 | * @param int $depth Depth of menu item. Used for padding. |
| 82 | * @param array $args An array of arguments. @see wp_nav_menu() |
| 83 | */ |
| 84 | public function start_lvl( &$output, $depth = 0, $args = array() ) { |
| 85 | $indent = str_repeat( "\t", $depth ); |
| 86 | $output .= "\n$indent<ul class=\"elementskit-dropdown elementskit-submenu-panel\">\n"; |
| 87 | } |
| 88 | /** |
| 89 | * Ends the list of after the elements are added. |
| 90 | * |
| 91 | * @see Walker::end_lvl() |
| 92 | * |
| 93 | * @since 3.0.0 |
| 94 | * |
| 95 | * @param string $output Passed by reference. Used to append additional content. |
| 96 | * @param int $depth Depth of menu item. Used for padding. |
| 97 | * @param array $args An array of arguments. @see wp_nav_menu() |
| 98 | */ |
| 99 | public function end_lvl( &$output, $depth = 0, $args = array() ) { |
| 100 | $indent = str_repeat( "\t", $depth ); |
| 101 | $output .= "$indent</ul>\n"; |
| 102 | } |
| 103 | /** |
| 104 | * Start the element output. |
| 105 | * |
| 106 | * @see Walker::start_el() |
| 107 | * |
| 108 | * @since 3.0.0 |
| 109 | * |
| 110 | * @param string $output Passed by reference. Used to append additional content. |
| 111 | * @param object $item Menu item data object. |
| 112 | * @param int $depth Depth of menu item. Used for padding. |
| 113 | * @param array $args An array of arguments. @see wp_nav_menu() |
| 114 | * @param int $id Current item ID. |
| 115 | */ |
| 116 | public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
| 117 | $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
| 118 | $classes = empty( $item->classes ) ? array() : (array) $item->classes; |
| 119 | $classes[] = 'menu-item-' . $item->ID; |
| 120 | |
| 121 | /** |
| 122 | * Filter the CSS class(es) applied to a menu item's list item element. |
| 123 | * |
| 124 | * @since 3.0.0 |
| 125 | * @since 4.1.0 The `$depth` parameter was added. |
| 126 | * |
| 127 | * @param array $classes The CSS classes that are applied to the menu item's `<li>` element. |
| 128 | * @param object $item The current menu item. |
| 129 | * @param array $args An array of {@see wp_nav_menu()} arguments. |
| 130 | * @param int $depth Depth of menu item. Used for padding. |
| 131 | */ |
| 132 | $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) ); |
| 133 | // New |
| 134 | $class_names .= ' nav-item'; |
| 135 | $item_meta = $this->get_item_meta( $item->ID ); |
| 136 | $is_megamenu_item = $this->is_megamenu_item( $item_meta, $args->menu ); |
| 137 | |
| 138 | if ( in_array( 'menu-item-has-children', $classes ) || $is_megamenu_item == true ) { |
| 139 | $class_names .= ' elementskit-dropdown-has ' . $item_meta['vertical_megamenu_position_type'] . ' elementskit-dropdown-menu-' . $item_meta['megamenu_width_type'] . ''; |
| 140 | } |
| 141 | |
| 142 | if ( $is_megamenu_item == true ) { |
| 143 | $class_names .= ' elementskit-megamenu-has'; |
| 144 | } |
| 145 | |
| 146 | if ( $item_meta['mobile_submenu_content_type'] == 'builder_content' ) { |
| 147 | $class_names .= ' elementskit-mobile-builder-content'; |
| 148 | } |
| 149 | |
| 150 | if ( in_array( 'current-menu-item', $classes ) ) { |
| 151 | $class_names .= ' active'; |
| 152 | } |
| 153 | |
| 154 | $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
| 155 | |
| 156 | /** |
| 157 | * Filter the ID applied to a menu item's list item element. |
| 158 | * |
| 159 | * @since 3.0.1 |
| 160 | * @since 4.1.0 The `$depth` parameter was added. |
| 161 | * |
| 162 | * @param string $menu_id The ID that is applied to the menu item's `<li>` element. |
| 163 | * @param object $item The current menu item. |
| 164 | * @param array $args An array of {@see wp_nav_menu()} arguments. |
| 165 | * @param int $depth Depth of menu item. Used for padding. |
| 166 | */ |
| 167 | $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth ); |
| 168 | $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
| 169 | // New |
| 170 | $data_attr = ''; |
| 171 | switch ( $item_meta['megamenu_width_type'] ) { |
| 172 | case 'default_width': |
| 173 | $data_attr = esc_attr( ' data-vertical-menu=750px' ); |
| 174 | break; |
| 175 | |
| 176 | case 'full_width': |
| 177 | $data_attr = ' data-vertical-menu=""'; |
| 178 | break; |
| 179 | |
| 180 | case 'custom_width': |
| 181 | $data_attr = $item_meta['vertical_menu_width'] === '' ? esc_attr( ' data-vertical-menu=750px' ) : esc_attr( ' data-vertical-menu=' . $item_meta['vertical_menu_width'] . '' ); |
| 182 | break; |
| 183 | |
| 184 | default: |
| 185 | $data_attr = esc_attr( ' data-vertical-menu=750px' ); |
| 186 | break; |
| 187 | } |
| 188 | // |
| 189 | $output .= $indent . '<li' . $id . $class_names . $data_attr . '>'; |
| 190 | $atts = array(); |
| 191 | $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
| 192 | $atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
| 193 | $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
| 194 | $atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
| 195 | |
| 196 | $submenu_indicator = ''; |
| 197 | |
| 198 | // New |
| 199 | if ( $depth === 0 ) { |
| 200 | $atts['class'] = 'ekit-menu-nav-link'; |
| 201 | } |
| 202 | if ( $depth === 0 && in_array( 'menu-item-has-children', $classes ) ) { |
| 203 | $atts['class'] .= ' ekit-menu-dropdown-toggle'; |
| 204 | } |
| 205 | if ( in_array( 'menu-item-has-children', $classes ) || $is_megamenu_item == true ) { |
| 206 | // Use an if statement to conditionally display the submenu indicator icon |
| 207 | if(!empty($args->submenu_indicator_icon)) { |
| 208 | $submenu_indicator .= $args->submenu_indicator_icon; |
| 209 | } else { |
| 210 | $submenu_indicator .= '<i class="icon icon-down-arrow1 elementskit-submenu-indicator"></i>'; |
| 211 | } |
| 212 | } |
| 213 | if ( $depth > 0 ) { |
| 214 | $manual_class = array_values( $classes )[0] . ' ' . 'dropdown-item'; |
| 215 | $atts ['class'] = $manual_class; |
| 216 | } |
| 217 | if ( in_array( 'current-menu-item', $item->classes ) ) { |
| 218 | $atts['class'] .= ' active'; |
| 219 | } |
| 220 | |
| 221 | // |
| 222 | /** |
| 223 | * Filter the HTML attributes applied to a menu item's anchor element. |
| 224 | * |
| 225 | * @since 3.6.0 |
| 226 | * @since 4.1.0 The `$depth` parameter was added. |
| 227 | * |
| 228 | * @param array $atts { |
| 229 | * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. |
| 230 | * |
| 231 | * @type string $title Title attribute. |
| 232 | * @type string $target Target attribute. |
| 233 | * @type string $rel The rel attribute. |
| 234 | * @type string $href The href attribute. |
| 235 | * } |
| 236 | * @param object $item The current menu item. |
| 237 | * @param array $args An array of {@see wp_nav_menu()} arguments. |
| 238 | * @param int $depth Depth of menu item. Used for padding. |
| 239 | */ |
| 240 | $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); |
| 241 | $attributes = ''; |
| 242 | foreach ( $atts as $attr => $value ) { |
| 243 | if ( ! empty( $value ) ) { |
| 244 | $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
| 245 | $attributes .= ' ' . $attr . '="' . $value . '"'; |
| 246 | } |
| 247 | } |
| 248 | $item_output = $args->before; |
| 249 | // New |
| 250 | |
| 251 | // |
| 252 | $item_output .= '<a' . $attributes . '>'; |
| 253 | |
| 254 | if ( $this->is_megamenu( $args->menu ) == 1 ) { |
| 255 | // add badge text |
| 256 | if ( $item_meta['menu_badge_text'] != '' ) { |
| 257 | $badge_background = sanitize_hex_color( $item_meta['menu_badge_background'] ); |
| 258 | $badge_color = sanitize_hex_color( $item_meta['menu_badge_color'] ); |
| 259 | $badge_style = 'background:' . $badge_background . '; color:' . $badge_color; |
| 260 | $badge_carret_style = 'border-top-color:' . $badge_background; |
| 261 | $item_output .= '<span style="' . esc_attr( $badge_style ) . '" class="ekit-menu-badge">' . esc_html( $item_meta['menu_badge_text'] ) . '<i style="' . esc_attr( $badge_carret_style ) . '" class="ekit-menu-badge-arrow"></i></span>'; |
| 262 | } |
| 263 | |
| 264 | // add menu icon & style |
| 265 | if ( $item_meta['menu_icon'] != '' ) { |
| 266 | $icon_style = 'color:' . sanitize_hex_color( $item_meta['menu_icon_color'] ); |
| 267 | $item_output .= '<i class="ekit-menu-icon ' . esc_attr( $item_meta['menu_icon'] ) . '" style="' . esc_attr( $icon_style ) . '" ></i>'; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** This filter is documented in wp-includes/post-template.php */ |
| 272 | $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; |
| 273 | $item_output .= $submenu_indicator . '</a>'; |
| 274 | $item_output .= $args->after; |
| 275 | /** |
| 276 | * Filter a menu item's starting output. |
| 277 | * |
| 278 | * The menu item's starting output only includes `$args->before`, the opening `<a>`, |
| 279 | * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is |
| 280 | * no filter for modifying the opening and closing `<li>` for a menu item. |
| 281 | * |
| 282 | * @since 3.0.0 |
| 283 | * |
| 284 | * @param string $item_output The menu item's starting HTML output. |
| 285 | * @param object $item Menu item data object. |
| 286 | * @param int $depth Depth of menu item. Used for padding. |
| 287 | * @param array $args An array of {@see wp_nav_menu()} arguments. |
| 288 | */ |
| 289 | $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
| 290 | } |
| 291 | /** |
| 292 | * Ends the element output, if needed. |
| 293 | * |
| 294 | * @see Walker::end_el() |
| 295 | * |
| 296 | * @since 3.0.0 |
| 297 | * |
| 298 | * @param string $output Passed by reference. Used to append additional content. |
| 299 | * @param object $item Page data object. Not used. |
| 300 | * @param int $depth Depth of page. Not Used. |
| 301 | * @param array $args An array of arguments. @see wp_nav_menu() |
| 302 | */ |
| 303 | public function end_el( &$output, $item, $depth = 0, $args = array() ) { |
| 304 | if ( $depth === 0 ) { |
| 305 | if ( $this->is_megamenu( $args->menu ) == 1 ) { |
| 306 | $item_meta = $this->get_item_meta( $item->ID ); |
| 307 | if ( $item_meta['menu_enable'] == 1 && class_exists( 'Elementor\Plugin' ) ) { |
| 308 | $builder_post_title = 'dynamic-content-megamenu-menuitem' . $item->ID; |
| 309 | $builder_post = Utils::get_page_by_title( $builder_post_title, 'elementskit_content' ); |
| 310 | $output .= '<div class="elementskit-megamenu-panel">'; |
| 311 | if ( $builder_post != null ) { |
| 312 | $mega_menu_output = \ElementsKit_Lite\Utils::render_elementor_content( $builder_post->ID ); |
| 313 | |
| 314 | // if ajax load is enable and not elementor editor mode |
| 315 | if(!empty($item_meta['megamenu_ajax_load']) && $item_meta['megamenu_ajax_load'] == 'yes') { |
| 316 | $mega_menu_output = sprintf('<div class="megamenu-ajax-load" data-id="%1$s"></div>', $builder_post->ID); |
| 317 | } |
| 318 | |
| 319 | $output .= $mega_menu_output; |
| 320 | } else { |
| 321 | $output .= esc_html__( 'No content found', 'elementskit-lite' ); |
| 322 | } |
| 323 | |
| 324 | $output .= '</div>'; |
| 325 | } |
| 326 | } |
| 327 | $output .= "</li>\n"; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 |