/** * Custom walker. Add the widgets into the menu. * * @see Walker::start_el() * * @since 1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu() * @param int $id Current item ID. */ function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { $indent = $depth ? str_repeat("\t", $depth) : ''; if (property_exists($item, 'megamenu_settings')) { $settings = $item->megamenu_settings; } else { $settings = Mega_Menu_Nav_Menus::get_menu_item_defaults(); } // Item Class $classes = empty($item->classes) ? array() : (array) $item->classes; $classes[] = 'menu-item-' . $item->ID; $class = join(' ', apply_filters('megamenu_nav_menu_css_class', array_filter($classes), $item, $args)); // strip widget classes back to how they're intended to be output $class = str_replace("mega-menu-widget-class-", "", $class); // Item ID $id = esc_attr(apply_filters('megamenu_nav_menu_item_id', "mega-menu-item-{$item->ID}", $item, $args)); $output .= "<li class='{$class}' id='{$id}'>"; // output the widgets if ($item->type == 'widget' && $item->content) { $item_output = $item->content; } else { $atts = array(); $atts['title'] = !empty($item->attr_title) ? $item->attr_title : ''; $atts['target'] = !empty($item->target) ? $item->target : ''; $atts['class'] = ''; $atts['rel'] = !empty($item->xfn) ? $item->xfn : ''; if ($settings['disable_link'] != 'true') { $atts['href'] = !empty($item->url) ? $item->url : ''; } if (isset($settings['icon']) && $settings['icon'] != 'disabled') { $atts['class'] = $settings['icon']; } $atts = apply_filters('megamenu_nav_menu_link_attributes', $atts, $item, $args); if (strlen($atts['class'])) { $atts['class'] = $atts['class'] . ' mega-menu-link'; } else { $atts['class'] = 'mega-menu-link'; } $attributes = ''; foreach ($atts as $attr => $value) { if (!empty($value)) { $value = 'href' === $attr ? esc_url($value) : esc_attr($value); $attributes .= ' ' . $attr . '="' . $value . '"'; } } $item_output = $args->before; $item_output .= '<a' . $attributes . '>'; if ($settings['hide_text'] == 'true' && $depth == 0) { /** This filter is documented in wp-includes/post-template.php */ } else { $item_output .= $args->link_before . apply_filters('megamenu_the_title', $item->title, $item->ID) . $args->link_after; } $item_output .= '</a>'; $item_output .= $args->after; } $output .= apply_filters('megamenu_walker_nav_menu_start_el', $item_output, $item, $depth, $args); }
/** * Set up the class * * @since 1.4 */ private function init() { if (isset($_POST['menu_item_id'])) { $this->menu_item_id = absint($_POST['menu_item_id']); $saved_settings = array_filter((array) get_post_meta($this->menu_item_id, '_megamenu', true)); $this->menu_item_meta = array_merge(Mega_Menu_Nav_Menus::get_menu_item_defaults(), $saved_settings); } if (isset($_POST['menu_item_depth'])) { $this->menu_item_depth = absint($_POST['menu_item_depth']); } if (isset($_POST['menu_id'])) { $this->menu_id = absint($_POST['menu_id']); } }
/** * Returns an array of immediate child menu items for the current item * * @since 1.5 * @return array */ private function get_second_level_menu_items() { $items = array(); // check we're using a valid menu ID if (!is_nav_menu($this->menu_id)) { return $items; } $menu = wp_get_nav_menu_items($this->menu_id); if (count($menu)) { foreach ($menu as $item) { // find the child menu items if ($item->menu_item_parent == $this->menu_item_id) { $saved_settings = array_filter((array) get_post_meta($item->ID, '_megamenu', true)); $settings = array_merge(Mega_Menu_Nav_Menus::get_menu_item_defaults(), $saved_settings); $items[] = array('id' => $item->ID, 'title' => $item->title, 'mega_columns' => $settings['mega_menu_columns']); } } } return $items; }
/** * Apply classes to nav menu items * * @since 1.8.2 * @param array $items - All menu item objects * @param object $args * @return array - Menu objects including widgets */ public function apply_megamenu_classes_to_menu_items($items, $args) { $parents = array(); foreach ($items as $item) { if ($item->depth == 0) { $item->classes[] = 'align-' . $item->megamenu_settings['align']; $item->classes[] = 'menu-' . $item->megamenu_settings['type']; } if ($item->megamenu_settings['hide_arrow'] == 'true') { $item->classes[] = 'hide-arrow'; } if ($item->megamenu_settings['hide_text'] == 'true' && $item->depth == 0) { $item->classes[] = 'hide-text'; } if ($item->megamenu_settings['item_align'] != 'left' && $item->depth == 0) { $item->classes[] = 'item-align-' . $item->megamenu_settings['item_align']; } if ($item->megamenu_settings['disable_link'] == 'true') { $item->classes[] = 'disable-link'; } // add column classes for second level menu items displayed in mega menus if ($item->depth == 1) { $parent_settings = array_filter((array) get_post_meta($item->menu_item_parent, '_megamenu', true)); if (isset($parent_settings['type']) && $parent_settings['type'] == 'megamenu') { $parent_settings = array_merge(Mega_Menu_Nav_Menus::get_menu_item_defaults(), $parent_settings); $span = $item->megamenu_settings['mega_menu_columns']; $total_columns = $parent_settings['panel_columns']; if ($total_columns >= $span) { $item->classes[] = "menu-columns-{$span}-of-{$total_columns}"; $column_count = $span; } else { $item->classes[] = "menu-columns-{$total_columns}-of-{$total_columns}"; $column_count = $total_columns; } if (!isset($parents[$item->menu_item_parent])) { $parents[$item->menu_item_parent] = $column_count; } else { $parents[$item->menu_item_parent] = $parents[$item->menu_item_parent] + $column_count; if ($parents[$item->menu_item_parent] > $total_columns) { $parents[$item->menu_item_parent] = $column_count; $item->classes[] = 'menu-clear'; } } } } } return $items; }
/** * Append the widget objects to the menu array before the * menu is processed by the walker. * * @since 1.0 * @param array $items - All menu item objects * @param object $args * @return array - Menu objects including widgets */ public function add_widgets_to_menu($items, $args) { // make sure we're working with a Mega Menu if (!is_a($args->walker, 'Mega_Menu_Walker')) { return $items; } $widget_manager = new Mega_Menu_Widget_Manager(); $default_columns = apply_filters("megamenu_default_columns", 1); // apply saved metadata to each menu item foreach ($items as $item) { $saved_settings = array_filter((array) get_post_meta($item->ID, '_megamenu', true)); $item->megamenu_settings = array_merge(Mega_Menu_Nav_Menus::get_menu_item_defaults(), $saved_settings); } $items = apply_filters("megamenu_nav_menu_objects_before", $items, $args); foreach ($items as $item) { // only look for widgets on top level items if ($item->menu_item_parent == 0 && $item->megamenu_settings['type'] == 'megamenu') { $panel_widgets = $widget_manager->get_widgets_for_menu_id($item->ID); if (count($panel_widgets)) { if (!in_array('menu-item-has-children', $item->classes)) { $item->classes[] = 'menu-item-has-children'; } $cols = 0; foreach ($panel_widgets as $widget) { $menu_item = array('type' => 'widget', 'title' => '', 'content' => $widget_manager->show_widget($widget['widget_id']), 'menu_item_parent' => $item->ID, 'db_id' => 0, 'ID' => $widget['widget_id'], 'classes' => array("menu-item", "menu-item-type-widget", "menu-columns-{$widget['mega_columns']}-of-{$item->megamenu_settings['panel_columns']}", "menu-widget-class-" . $widget_manager->get_widget_class($widget['widget_id']))); if ($cols == 0) { $menu_item['classes'][] = "menu-clear"; } $cols = $cols + $widget['mega_columns']; if ($cols > $item->megamenu_settings['panel_columns']) { $menu_item['classes'][] = "menu-clear"; $cols = $widget['mega_columns']; } $items[] = (object) $menu_item; } } } } $items = apply_filters("megamenu_nav_menu_objects_after", $items, $args); return $items; }