PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/elementskit-lite/modules/megamenu/walker-nav-menu.php

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