Advertisement
rdusnr

Untitled

Jun 29th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.21 KB | None | 0 0
  1. /**
  2. * Modify some elements for the menu
  3. */
  4.  
  5. if ( ! class_exists( 'kleo_walker_nav_menu' ) ) {
  6.  
  7. class kleo_walker_nav_menu extends Walker_Nav_Menu {
  8.  
  9. /**
  10. * @see Walker::start_lvl()
  11. * @since 3.0.0
  12. *
  13. * @param string $output Passed by reference. Used to append additional content.
  14. * @param int $depth Depth of page. Used for padding.
  15. */
  16. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  17. $indent = str_repeat( "\t", $depth );
  18. $output .= "\n$indent<ul role=\"menu\" class=\"dropdown-menu sub-menu".($depth ===0?" pull-left":"")."\">\n";
  19. }
  20.  
  21. /**
  22. * @see Walker::start_el()
  23. * @since 3.0.0
  24. *
  25. * @param string $output Passed by reference. Used to append additional content.
  26. * @param object $item Menu item data object.
  27. * @param int $depth Depth of menu item. Used for padding.
  28. * @param array $args
  29. * @param int $id Menu item ID.
  30. */
  31. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  32. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  33.  
  34. /**
  35. * Dividers, Headers or Disabled
  36. * =============================
  37. * Determine whether the item is a Divider, Header, Disabled or regular
  38. * menu item. To prevent errors we use the strcasecmp() function to so a
  39. * comparison that is not case sensitive. The strcasecmp() function returns
  40. * a 0 if the strings are equal.
  41. */
  42. if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
  43. $output .= $indent . '<li role="presentation" class="divider">';
  44. } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
  45. $output .= $indent . '<li role="presentation" class="divider">';
  46. } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
  47. $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
  48. } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
  49. $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
  50. } else {
  51.  
  52. $class_names = $value = '';
  53.  
  54. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  55. $classes[] = 'menu-item-' . $item->ID;
  56.  
  57. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  58.  
  59. if ( $args->has_children && $depth === 1 ) {
  60. $class_names .= ' dropdown-submenu';
  61. }
  62. elseif($args->has_children) {
  63. $class_names .= ' dropdown';
  64. if( isset( $item->mega ) ){
  65. if ($item->mega == 'yes') {
  66. $class_names .= ' kleo-megamenu';
  67. }
  68. }
  69. $submenus = $depth == 0 ? get_posts( array( 'post_type' => 'nav_menu_item', 'numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array( array( 'key' => '_menu_item_menu_item_parent', 'value' => $item->ID ) ) ) ) : false;
  70. $count = $submenus ? count( $submenus ) : 'no';
  71.  
  72. $class_names .= ' mega-' . $count . '-cols';
  73. }
  74.  
  75. if ( in_array( 'current-menu-item', $classes ) ) {
  76. $class_names .= ' active';
  77. }
  78.  
  79. if ( $depth === 0 && isset( $item->istyle ) && $item->istyle == 'border' ) {
  80. $class_names .= ' has-btn-see-through';
  81. }
  82. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  83.  
  84. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  85. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  86.  
  87. $data_li = $indent . '<li' . $id . $value . $class_names .'>';
  88.  
  89. $atts = array();
  90. if (strpos($item->attr_title,'class=') !== false) {
  91. $atts['class'] = (isset($atts['class']) ? $atts['class']." " : '') . str_replace('class=', '', $item->attr_title);
  92. }else {
  93. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ( ! empty( $item->title ) ? esc_attr(wp_strip_all_tags($item->title)) : '' );
  94. }
  95. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  96. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  97.  
  98. // If item has_children add atts to a.
  99. if ( $args->has_children && $depth === 0 ) {
  100. //$atts['href'] = '#';
  101. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  102. //$atts['data-toggle'] = 'dropdown';
  103. $atts['class'] = 'js-activated';
  104. } else {
  105. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  106. }
  107.  
  108. if ( $depth === 0 && isset( $item->istyle ) ) {
  109. if ( $item->istyle == 'buy' ) {
  110. $atts['class'] = (isset($atts['class']) ? $atts['class'] : '' ) . ' btn-buy';
  111. } elseif( $item->istyle == 'border' ) {
  112. $atts['class'] = (isset($atts['class']) ? $atts['class'] : '' ) . ' btn btn-see-through';
  113. } elseif( $item->istyle == 'highlight' ) {
  114. $atts['class'] = (isset($atts['class']) ? $atts['class'] : '' ) . ' btn btn-highlight';
  115. }
  116. }
  117.  
  118.  
  119.  
  120. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  121.  
  122. $attributes = '';
  123. foreach ( $atts as $attr => $value ) {
  124. if ( ! empty( $value ) ) {
  125. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  126. $attributes .= ' ' . $attr . '="' . $value . '"';
  127. }
  128. }
  129.  
  130. $item_output = $args->before;
  131. $item_output .= '<a'. $attributes .'>';
  132.  
  133. /* allow shortcodes in item title */
  134. $item->title = do_shortcode( $item->title );
  135.  
  136. /* Menu icons */
  137. if (isset( $item->icon ) && $item->icon != '') {
  138. $title_icon = '<i class="icon-' . $item->icon . '"></i>';
  139.  
  140. if ( $item->iconpos == 'after' ) {
  141. $title = $item->title . ' ' . $title_icon;
  142. }
  143. elseif ( $item->iconpos == 'icon' ) {
  144. $title = $title_icon;
  145. }
  146. else {
  147. $title = $title_icon . ' ' . $item->title;
  148. }
  149. }
  150. else {
  151. $title = $item->title;
  152. }
  153.  
  154. $item_output .= $args->link_before . apply_filters( 'the_title', $title, $item->ID ) . $args->link_after;
  155.  
  156. $item_output .= ( $args->has_children ) ? ' <span class="caret"></span></a>' : '</a>';
  157. $item_output .= $args->after;
  158.  
  159. //custom filters
  160. $css_target = preg_match( '/\skleo-(.*)-nav/', implode( ' ', $item->classes), $matches );
  161. // If this isn't a KLEO menu item, we can stop here
  162. if ( ! empty( $matches[1] ) ) {
  163. $item_output = apply_filters( 'walker_nav_menu_start_el_' . $matches[1], $item_output, $item, $depth, $args );
  164. $data_li = apply_filters( 'walker_nav_menu_start_el_li_' . $matches[1], $data_li, $item, $depth, $args);
  165. }
  166.  
  167. $output .= $data_li;
  168.  
  169. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  170. }
  171. }
  172.  
  173. /**
  174. * Traverse elements to create list from elements.
  175. *
  176. * Display one element if the element doesn't have any children otherwise,
  177. * display the element and its children. Will only traverse up to the max
  178. * depth and no ignore elements under that depth.
  179. *
  180. * This method shouldn't be called directly, use the walk() method instead.
  181. *
  182. * @see Walker::start_el()
  183. * @since 2.5.0
  184. *
  185. * @param object $element Data object
  186. * @param array $children_elements List of elements to continue traversing.
  187. * @param int $max_depth Max depth to traverse.
  188. * @param int $depth Depth of current element.
  189. * @param array $args
  190. * @param string $output Passed by reference. Used to append additional content.
  191. * @return null Null on failure with no changes to parameters.
  192. */
  193. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  194. if ( ! $element )
  195. return;
  196.  
  197. $id_field = $this->db_fields['id'];
  198.  
  199. // Display this element.
  200. if ( is_object( $args[0] ) )
  201. $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
  202.  
  203. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  204. }
  205.  
  206. /**
  207. * Menu Fallback
  208. * =============
  209. * If this function is assigned to the wp_nav_menu's fallback_cb variable
  210. * and a menu has not been assigned to the theme location in the WordPress
  211. * menu manager the function with display nothing to a non-logged in user,
  212. * and will add a link to the WordPress menu manager if logged in as an admin.
  213. *
  214. * @param array $args passed from the wp_nav_menu function.
  215. *
  216. */
  217. public static function fallback( $args ) {
  218. if ( current_user_can( 'manage_options' ) ) {
  219.  
  220. extract( $args );
  221.  
  222. $fb_output = null;
  223.  
  224. if ( $container ) {
  225. $fb_output = '<' . $container;
  226.  
  227. if ( $container_id )
  228. $fb_output .= ' id="' . $container_id . '"';
  229.  
  230. if ( $container_class )
  231. $fb_output .= ' class="' . $container_class . '"';
  232.  
  233. $fb_output .= '>';
  234. }
  235.  
  236. $fb_output .= '<ul';
  237.  
  238. if ( $menu_id )
  239. $fb_output .= ' id="' . $menu_id . '"';
  240.  
  241. if ( $menu_class )
  242. $fb_output .= ' class="' . $menu_class . '"';
  243.  
  244. $fb_output .= '>';
  245. $fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
  246. $fb_output .= '</ul>';
  247.  
  248. if ( $container )
  249. $fb_output .= '</' . $container . '>';
  250.  
  251. echo $fb_output;
  252. }
  253. }
  254. }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement