/** * Displays a navigation menu. * * Optional $args contents: * * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank. * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'. * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented. * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'. * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'. * container_id - The ID that is applied to the container. Defaults to blank. * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'nxt_page_menu'. Set to false for no fallback. * before - Text before the link text. * after - Text after the link text. * link_before - Text before the link. * link_after - Text after the link. * echo - Whether to echo the menu or return it. Defaults to echo. * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0. * walker - allows a custom walker to be specified. * theme_location - the location in the theme to be used. Must be registered with register_nav_menu() in order to be selectable by the user. * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders. * * @since 3.0.0 * * @param array $args Arguments */ function nxt_nav_menu($args = array()) { static $menu_id_slugs = array(); $defaults = array('menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'nxt_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 0, 'walker' => '', 'theme_location' => ''); $args = nxt_parse_args($args, $defaults); $args = apply_filters('nxt_nav_menu_args', $args); $args = (object) $args; // Get the nav menu based on the requested menu $menu = nxt_get_nav_menu_object($args->menu); // Get the nav menu based on the theme_location if (!$menu && $args->theme_location && ($locations = get_nav_menu_locations()) && isset($locations[$args->theme_location])) { $menu = nxt_get_nav_menu_object($locations[$args->theme_location]); } // get the first menu that has items if we still can't find a menu if (!$menu && !$args->theme_location) { $menus = nxt_get_nav_menus(); foreach ($menus as $menu_maybe) { if ($menu_items = nxt_get_nav_menu_items($menu_maybe->term_id)) { $menu = $menu_maybe; break; } } } // If the menu exists, get its items. if ($menu && !is_nxt_error($menu) && !isset($menu_items)) { $menu_items = nxt_get_nav_menu_items($menu->term_id); } // If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists if ((!$menu || is_nxt_error($menu) || isset($menu_items) && empty($menu_items) && !$args->theme_location) && $args->fallback_cb && is_callable($args->fallback_cb)) { return call_user_func($args->fallback_cb, (array) $args); } // If no fallback function was specified and the menu doesn't exists, bail. if (!$menu || is_nxt_error($menu)) { return false; } $nav_menu = $items = ''; $show_container = false; if ($args->container) { $allowed_tags = apply_filters('nxt_nav_menu_container_allowedtags', array('div', 'nav')); if (in_array($args->container, $allowed_tags)) { $show_container = true; $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-' . $menu->slug . '-container"'; $id = $args->container_id ? ' id="' . esc_attr($args->container_id) . '"' : ''; $nav_menu .= '<' . $args->container . $id . $class . '>'; } } // Set up the $menu_item variables _nxt_menu_item_classes_by_context($menu_items); $sorted_menu_items = array(); foreach ((array) $menu_items as $key => $menu_item) { $sorted_menu_items[$menu_item->menu_order] = $menu_item; } unset($menu_items); $sorted_menu_items = apply_filters('nxt_nav_menu_objects', $sorted_menu_items, $args); $items .= walk_nav_menu_tree($sorted_menu_items, $args->depth, $args); unset($sorted_menu_items); // Attributes if (!empty($args->menu_id)) { $wrap_id = $args->menu_id; } else { $wrap_id = 'menu-' . $menu->slug; while (in_array($wrap_id, $menu_id_slugs)) { if (preg_match('#-(\\d+)$#', $wrap_id, $matches)) { $wrap_id = preg_replace('#-(\\d+)$#', '-' . ++$matches[1], $wrap_id); } else { $wrap_id = $wrap_id . '-1'; } } } $menu_id_slugs[] = $wrap_id; $wrap_class = $args->menu_class ? $args->menu_class : ''; // Allow plugins to hook into the menu to add their own <li>'s $items = apply_filters('nxt_nav_menu_items', $items, $args); $items = apply_filters("nxt_nav_menu_{$menu->slug}_items", $items, $args); $nav_menu .= sprintf($args->items_wrap, esc_attr($wrap_id), esc_attr($wrap_class), $items); unset($items); if ($show_container) { $nav_menu .= '</' . $args->container . '>'; } $nav_menu = apply_filters('nxt_nav_menu', $nav_menu, $args); if ($args->echo) { echo $nav_menu; } else { return $nav_menu; } }
$messages[] = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>'; $menu_title = $_menu_object->name; } if (!is_nxt_error($_menu_object)) { $_nav_menu_selected_id = nxt_update_nav_menu_object($nav_menu_selected_id, array('menu-name' => $menu_title)); if (is_nxt_error($_nav_menu_selected_id)) { $_menu_object = $_nav_menu_selected_id; $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>'; } else { $_menu_object = nxt_get_nav_menu_object($_nav_menu_selected_id); $nav_menu_selected_title = $_menu_object->name; } } // Update menu items if (!is_nxt_error($_menu_object)) { $unsorted_menu_items = nxt_get_nav_menu_items($nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish')); $menu_items = array(); // Index menu items by db ID foreach ($unsorted_menu_items as $_item) { $menu_items[$_item->db_id] = $_item; } $post_fields = array('menu-item-db-id', 'menu-item-object-id', 'menu-item-object', 'menu-item-parent-id', 'menu-item-position', 'menu-item-type', 'menu-item-title', 'menu-item-url', 'menu-item-description', 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn'); nxt_defer_term_counting(true); // Loop through all the menu items' POST variables if (!empty($_POST['menu-item-db-id'])) { foreach ((array) $_POST['menu-item-db-id'] as $_key => $k) { // Menu item title can't be blank if (empty($_POST['menu-item-title'][$_key])) { continue; } $args = array();
/** * Automatically add newly published page objects to menus with that as an option. * * @since 3.0.0 * @access private * * @param string $new_status The new status of the post object. * @param string $old_status The old status of the post object. * @param object $post The post object being transitioned from one status to another. * @return void */ function _nxt_auto_add_pages_to_menu($new_status, $old_status, $post) { if ('publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type) { return; } if (!empty($post->post_parent)) { return; } $auto_add = get_option('nav_menu_options'); if (empty($auto_add) || !is_array($auto_add) || !isset($auto_add['auto_add'])) { return; } $auto_add = $auto_add['auto_add']; if (empty($auto_add) || !is_array($auto_add)) { return; } $args = array('menu-item-object-id' => $post->ID, 'menu-item-object' => $post->post_type, 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish'); foreach ($auto_add as $menu_id) { $items = nxt_get_nav_menu_items($menu_id, array('post_status' => 'publish,draft')); if (!is_array($items)) { continue; } foreach ($items as $item) { if ($post->ID == $item->object_id) { continue 2; } } nxt_update_nav_menu_item($menu_id, 0, $args); } }
function admin_main($data) { if (!$data) { $data = array(); } ?> <div class='level-operation' id='main-menu'> <h2 class='sidebar-name'><?php _e('Menu', 'membership'); ?> <span><a href='#remove' id='remove-menu' class='removelink' title='<?php _e("Remove Menu from this rules area.", 'membership'); ?> '><?php _e('Remove', 'membership'); ?> </a></span></h2> <div class='inner-operation'> <p><?php _e('Select the Menu items to be covered by this rule by checking the box next to the relevant menu labels.', 'membership'); ?> </p> <?php $navs = nxt_get_nav_menus(array('orderby' => 'name')); if (!empty($navs)) { ?> <table cellspacing="0" class="widefat fixed"> <thead> <tr> <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Menu / Item title', 'membership'); ?> </th> </tr> </thead> <tfoot> <tr> <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Menu / Item title', 'membership'); ?> </th> </tr> </tfoot> <tbody> <?php foreach ($navs as $key => $nav) { ?> <tr valign="middle" class="alternate" id="menu-<?php echo $nav->term_id; ?> -0"> <td class="column-name" colspan='2'> <strong><?php echo __('MENU', 'membership') . " - " . esc_html($nav->name); ?> </strong> </td> </tr> <?php $items = nxt_get_nav_menu_items($nav->term_id); if (!empty($items)) { foreach ($items as $ikey => $item) { ?> <tr valign="middle" class="alternate" id="menu-<?php //echo $nav->term_id . '-'; echo $item->ID; ?> "> <th class="check-column" scope="row"> <input type="checkbox" value="<?php //echo $nav->term_id . '-'; echo $item->ID; ?> " name="menu[]" <?php if (in_array($item->ID, $data)) { echo 'checked="checked"'; } ?> > </th> <td class="column-name"> <strong> – <?php if ($item->menu_item_parent != 0) { echo "– "; } echo esc_html($item->title); ?> </strong> </td> </tr> <?php } } } ?> </tbody> </table> <?php } ?> </div> </div> <?php }
/** * Returns the menu formatted to edit. * * @since 3.0.0 * * @param string $menu_id The ID of the menu to format. * @return string|nxt_Error $output The menu formatted to edit or error object on failure. */ function nxt_get_nav_menu_to_edit($menu_id = 0) { $menu = nxt_get_nav_menu_object($menu_id); // If the menu exists, get its items. if (is_nav_menu($menu)) { $menu_items = nxt_get_nav_menu_items($menu->term_id, array('post_status' => 'any')); $result = '<div id="menu-instructions" class="post-body-plain'; $result .= !empty($menu_items) ? ' menu-instructions-inactive">' : '">'; $result .= '<p>' . __('Select menu items (pages, categories, links) from the boxes at left to begin building your custom menu.') . '</p>'; $result .= '</div>'; if (empty($menu_items)) { return $result . ' <ul class="menu" id="menu-to-edit"> </ul>'; } $walker_class_name = apply_filters('nxt_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id); if (class_exists($walker_class_name)) { $walker = new $walker_class_name(); } else { return new nxt_Error('menu_walker_not_exist', sprintf(__('The Walker class named <strong>%s</strong> does not exist.'), $walker_class_name)); } $some_pending_menu_items = $some_invalid_menu_items = false; foreach ((array) $menu_items as $menu_item) { if (isset($menu_item->post_status) && 'draft' == $menu_item->post_status) { $some_pending_menu_items = true; } if (!empty($menu_item->_invalid)) { $some_invalid_menu_items = true; } } if ($some_pending_menu_items) { $result .= '<div class="updated inline"><p>' . __('Click Save Menu to make pending menu items public.') . '</p></div>'; } if ($some_invalid_menu_items) { $result .= '<div class="error inline"><p>' . __('There are some invalid menu items. Please check or delete them.') . '</p></div>'; } $result .= '<ul class="menu" id="menu-to-edit"> '; $result .= walk_nav_menu_tree(array_map('nxt_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker)); $result .= ' </ul> '; return $result; } elseif (is_nxt_error($menu)) { return $menu; } }