walker.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // disable direct access
  4. }
  5. if( ! class_exists( 'Mega_Menu_Walker' ) ) :
  6. /**
  7. * @package WordPress
  8. * @since 1.0.0
  9. * @uses Walker
  10. */
  11. class Mega_Menu_Walker extends Walker_Nav_Menu {
  12. /**
  13. * Starts the list before the elements are added.
  14. *
  15. * @see Walker::start_lvl()
  16. *
  17. * @since 1.0
  18. *
  19. * @param string $output Passed by reference. Used to append additional content.
  20. * @param int $depth Depth of menu item. Used for padding.
  21. * @param array $args An array of arguments. @see wp_nav_menu()
  22. */
  23. function start_lvl( &$output, $depth = 0, $args = array() ) {
  24. $indent = str_repeat("\t", $depth);
  25. $output .= "\n$indent<ul class=\"mega-sub-menu\">\n";
  26. }
  27. /**
  28. * Ends the list of after the elements are added.
  29. *
  30. * @see Walker::end_lvl()
  31. *
  32. * @since 1.0
  33. *
  34. * @param string $output Passed by reference. Used to append additional content.
  35. * @param int $depth Depth of menu item. Used for padding.
  36. * @param array $args An array of arguments. @see wp_nav_menu()
  37. */
  38. function end_lvl( &$output, $depth = 0, $args = array() ) {
  39. $indent = str_repeat("\t", $depth);
  40. $output .= "$indent</ul>\n";
  41. }
  42. /**
  43. * Custom walker. Add the widgets into the menu.
  44. *
  45. * @see Walker::start_el()
  46. *
  47. * @since 1.0
  48. *
  49. * @param string $output Passed by reference. Used to append additional content.
  50. * @param object $item Menu item data object.
  51. * @param int $depth Depth of menu item. Used for padding.
  52. * @param array $args An array of arguments. @see wp_nav_menu()
  53. * @param int $id Current item ID.
  54. */
  55. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  56. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  57. if ( property_exists( $item, 'megamenu_settings' ) ) {
  58. $settings = $item->megamenu_settings;
  59. } else {
  60. $settings = Mega_Menu_Nav_Menus::get_menu_item_defaults();
  61. }
  62. // Item Class
  63. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  64. if ( is_array( $item->classes ) && ! in_array( "menu-column", $item->classes ) && ! in_array( "menu-row", $item->classes ) ) {
  65. $classes[] = 'menu-item-' . $item->ID;
  66. }
  67. $class = join( ' ', apply_filters( 'megamenu_nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  68. // strip widget classes back to how they're intended to be output
  69. $class = str_replace( "mega-menu-widget-class-", "", $class );
  70. // Item ID
  71. if ( is_array( $item->classes ) && ! in_array( "menu-column", $item->classes ) && ! in_array( "menu-row", $item->classes ) ) {
  72. $id = "mega-menu-item-{$item->ID}";
  73. } else {
  74. $id = "mega-menu-{$item->ID}";
  75. }
  76. $id = esc_attr( apply_filters( 'megamenu_nav_menu_item_id', $id, $item, $args ) );
  77. $output .= "<li class='{$class}' id='{$id}'>";
  78. // output the widgets
  79. if ( $item->type == 'widget' && $item->content ) {
  80. $item_output = $item->content;
  81. } else {
  82. $atts = array();
  83. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  84. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  85. $atts['class'] = '';
  86. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  87. if ( $settings['disable_link'] != 'true') {
  88. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  89. } else {
  90. $atts['tabindex'] = 0;
  91. }
  92. if ( isset( $settings['icon']) && $settings['icon'] != 'disabled' && $settings['icon'] != 'custom' ) {
  93. $atts['class'] = $settings['icon'];
  94. }
  95. if ( isset( $settings['icon']) && $settings['icon'] == 'custom' ) {
  96. $atts['class'] = 'mega-custom-icon';
  97. }
  98. $atts = apply_filters( 'megamenu_nav_menu_link_attributes', $atts, $item, $args );
  99. if ( strlen( $atts['class'] ) ) {
  100. $atts['class'] = $atts['class'] . ' mega-menu-link';
  101. } else {
  102. $atts['class'] = 'mega-menu-link';
  103. }
  104. // required for Surface/Win10/Edge
  105. if ( in_array('menu-item-has-children', $classes ) ) {
  106. $atts['aria-haspopup'] = "true";
  107. }
  108. if ( $depth == 0 ) {
  109. $atts['tabindex'] = "0";
  110. }
  111. if ( $settings['hide_text'] == 'true' ) {
  112. $atts['aria-label'] = $item->title;
  113. }
  114. $attributes = '';
  115. foreach ( $atts as $attr => $value ) {
  116. if ( strlen( $value ) ) {
  117. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  118. $attributes .= ' ' . $attr . '="' . $value . '"';
  119. }
  120. }
  121. $item_output = $args->before;
  122. $item_output .= '<a'. $attributes .'>';
  123. if ( in_array('icon-top', $classes ) ) {
  124. $item_output .= "<span class='mega-title-below'>";
  125. }
  126. if ( $settings['hide_text'] == 'true' ) {
  127. /** This filter is documented in wp-includes/post-template.php */
  128. } else if ( property_exists( $item, 'mega_description' ) && strlen( $item->mega_description ) ) {
  129. $item_output .= '<span class="mega-description-group"><span class="mega-menu-title">' . $args->link_before . apply_filters( 'megamenu_the_title', $item->title, $item->ID ) . $args->link_after . '</span><span class="mega-menu-description">' . $item->description . '</span></span>';
  130. } else {
  131. $item_output .= $args->link_before . apply_filters( 'megamenu_the_title', $item->title, $item->ID ) . $args->link_after;
  132. }
  133. if ( is_array( $classes ) && in_array('icon-top', $classes ) ) {
  134. $item_output .= "</span>";
  135. }
  136. if ( in_array('menu-item-has-children', $classes ) ) {
  137. $item_output .= '<span class="mega-indicator"></span>';
  138. }
  139. $item_output .= '</a>';
  140. $item_output .= $args->after;
  141. if ( is_array( $item->classes ) && in_array( "menu-column", $item->classes ) || in_array( "menu-row", $item->classes ) ) {
  142. $item_output = "";
  143. }
  144. }
  145. $output .= apply_filters( 'megamenu_walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  146. }
  147. /**
  148. * Ends the element output, if needed.
  149. *
  150. * @see Walker::end_el()
  151. *
  152. * @since 1.7
  153. *
  154. * @param string $output Passed by reference. Used to append additional content.
  155. * @param object $item Page data object. Not used.
  156. * @param int $depth Depth of page. Not Used.
  157. * @param array $args An array of arguments. @see wp_nav_menu()
  158. */
  159. public function end_el( &$output, $item, $depth = 0, $args = array() ) {
  160. $output .= "</li>"; // remove new line to remove the 4px gap between menu items
  161. }
  162. }
  163. endif;