rdusnr

Untitled

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