' . $delete_nav_menu->get_error_message() . '
';
} else {
$messages_div = '' . __('The menu has been successfully deleted.') . '
';
$nav_menu_selected_id = 0; // Reset the selected menu
}
unset( $delete_nav_menu );
}
break;
case 'update':
check_admin_referer( 'update-nav_menu' );
// Add Menu
if ( isset($_POST['create-menu-button']) ) {
if ( current_theme_supports('nav-menus') ) {
$add_nav_menu = esc_html( $_POST['create-menu-name'] );
if ( $add_nav_menu ) {
$add_nav_menu = wp_create_nav_menu( $add_nav_menu );
if ( is_wp_error( $add_nav_menu ) ) {
$messages_div = '' . $add_nav_menu->get_error_message() . '
';
} else {
$nav_menu_selected_id = $add_nav_menu->term_id;
$nav_menu_selected_title = $add_nav_menu->name;
$messages_div = '' . sprintf( __('The %s menu has been successfully created.'), $add_nav_menu->name ) . '
';
}
} else {
$messages_div = '' . __('Please enter a valid menu name.') . '
';
}
unset( $add_nav_menu );
}
} else {
// @todo wrap this into wp_update_nav_menu_object();
if ( isset($_POST['menu-name']) ) {
$old_nav_menu = get_term( $nav_menu_selected_id, 'nav_menu', ARRAY_A );
$args = array( 'name' => $_POST['menu-name'], 'slug' => null, 'description' => $old_nav_menu['description'], 'parent' => $old_nav_menu['parent'], );
$new_nav_menu = wp_update_term( $nav_menu_selected_id, 'nav_menu', $args );
}
// Update menu items
// @todo: wrap update logic into wp_update_nav_menu();
$update_count = isset( $_POST['li-count'] ) ? (int) $_POST['li-count'] : 0;
$update_nav_menu = is_nav_menu( $nav_menu_selected_id );
if ( !is_wp_error($update_nav_menu) ) {
$menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') );
// Loop through all POST variables
for ( $k = 0; $k < $update_count; $k++ ) {
// Menu item title can't be blank
if ( '' == $_POST['menu-item-title'][$k] )
continue;
$menu_item_db_id = isset( $_POST['menu-item-db-id'][$k] ) ? $_POST['menu-item-db-id'][$k] : 0;
$menu_item_object_id = isset( $_POST['menu-item-object-id'][$k] ) ? $_POST['menu-item-object-id'][$k] : 0;
$menu_item_object = isset( $_POST['menu-item-object'][$k] ) ? $_POST['menu-item-object'][$k] : '';
$menu_item_parent_id = isset( $_POST['menu-item-parent-id'][$k] ) ? $_POST['menu-item-parent-id'][$k] : 0;
$menu_item_position = isset( $_POST['menu-item-position'][$k] ) ? $_POST['menu-item-position'][$k] : 0;
$menu_item_type = isset( $_POST['menu-item-type'][$k] ) ? $_POST['menu-item-type'][$k] : 'custom';
$menu_item_append = isset( $_POST['menu-item-append'][$k] ) ? $_POST['menu-item-append'][$k] : 'custom';
$menu_item_title = isset( $_POST['menu-item-title'][$k] ) ? $_POST['menu-item-title'][$k] : '';
$menu_item_url = isset( $_POST['menu-item-url'][$k] ) ? $_POST['menu-item-url'][$k] : '';
$menu_item_description = isset( $_POST['menu-item-description'][$k] ) ? $_POST['menu-item-description'][$k] : '';
$menu_item_attr_title = isset( $_POST['menu-item-attr-title'][$k] ) ? $_POST['menu-item-attr-title'][$k] : '';
$menu_item_target = isset( $_POST['menu-item-target'][$k] ) ? $_POST['menu-item-target'][$k] : '_self';
$menu_item_classes = isset( $_POST['menu-item-classes'][$k] ) ? $_POST['menu-item-classes'][$k] : '';
$menu_item_xfn = isset( $_POST['menu-item-xfn'][$k] ) ? $_POST['menu-item-xfn'][$k] : '';
// Populate the menu item object
$post = array(
'post_status' => 'publish', 'post_type' => 'nav_menu_item', 'ping_status' => 0,
'post_author' => $user_ID, 'tax_input' => array( 'nav_menu' => $update_nav_menu->name ),
'post_title' => $menu_item_title, 'post_excerpt' => $menu_item_attr_title,
'post_parent' => $menu_item_parent_id, 'menu_order' => $menu_item_position,
'post_content' => $menu_item_description,
);
// New menu item
if ( $menu_item_db_id == 0 ) {
$menu_item_db_id = wp_insert_post( $post );
// Update existing menu item
} elseif ( isset($menu_items[$menu_item_db_id]) || ( 'custom' == $menu_item_type && 0 != $menu_item_db_id ) ) {
$post['ID'] = $menu_item_db_id;
wp_update_post( $post );
unset( $menu_items[$menu_item_db_id] );
}
update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($menu_item_type) );
update_post_meta( $menu_item_db_id, '_menu_item_object_id', (int) $menu_item_object_id );
update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($menu_item_object) );
update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($menu_item_target) );
// @todo handle sanitizing multiple classes separated by whitespace.
update_post_meta( $menu_item_db_id, '_menu_item_classes', sanitize_html_class($menu_item_classes) );
update_post_meta( $menu_item_db_id, '_menu_item_xfn', sanitize_html_class($menu_item_xfn) );
// @todo: only save custom link urls.
update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($menu_item_url) );
}
// Remove menu items from the menu that weren't in $_POST
if ( !empty( $menu_items ) ) {
foreach ( array_keys( $menu_items ) as $menu_item_id ) {
wp_delete_post( $menu_item_id );
}
}
do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
$messages_div = '' . sprintf( __('The %s menu has been updated.'), $update_nav_menu->name ) . '
';
unset( $update_nav_menu, $update_count, $menu_items );
}
}
break;
}
// Get all nav menus
$nav_menus = wp_get_nav_menus();
// Get recently edited nav menu
$recently_edited = get_user_option( 'nav_menu_recently_edited' );
// If there was no recently edited menu, and $nav_menu_selected_id is a nav menu, update recently edited menu.
if ( !$recently_edited && is_nav_menu($nav_menu_selected_id) ) {
$recently_edited = $nav_menu_selected_id;
// Else if $nav_menu_selected_id is not a menu, but $recently_edited is, grab that one.
} elseif ( 0 == $nav_menu_selected_id && is_nav_menu($recently_edited) ) {
$nav_menu_selected_id = $recently_edited;
// Else try to grab the first menu from the menus list
} elseif ( 0 == $nav_menu_selected_id && ! empty($nav_menus) ) {
$nav_menu_selected_id = $nav_menus[0]->term_id;
}
// Update the user's setting
if ( $nav_menu_selected_id != $recently_edited && is_nav_menu($nav_menu_selected_id) )
update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
// If there's a menu, get it's name.
if ( !$nav_menu_selected_title && $nav_menu_selected_title = is_nav_menu( $nav_menu_selected_id ) ) {
$nav_menu_selected_title = $nav_menu_selected_title->name;
}
// Create Menu Metabox
add_meta_box( 'create-menu', __('Create Menu'), 'wp_nav_menu_create_metabox', 'nav-menus', 'side', 'core' );
// The user has no menus.
if ( !is_nav_menu( $nav_menu_selected_id ) ) {
$messages_div = '' . __('You do not have any menus. Create a new menu.') . '
';
// The theme supports menus
} elseif ( current_theme_supports('nav-menus') ) {
// Register nav menu metaboxes
add_meta_box( 'manage-menu', __( 'Menu Settings' ), 'wp_nav_menu_manage_menu_metabox', 'nav-menus', 'side', 'high', array( $nav_menu_selected_id, $nav_menu_selected_title ) );
wp_nav_menu_metaboxes_setup();
// The theme does not support menus
} else {
remove_meta_box( 'create-menu', 'nav-menus', 'side' );
$messages_div = '' . __('The current theme does not support menus.') . '
';
}
// Get the admin header
require_once( 'admin-header.php' );
?>
1 && current_theme_supports('nav-menus') ) : ?>