0 ) {
wp_delete_nav_menu( $menu_selected_id );
$menu_selected_id = 0;
$updated = true;
}
// Default Menu to show
$custom_menus = wp_get_nav_menus();
if ( empty($custom_menus) && empty($_POST) ) {
wp_create_default_nav_menu();
$custom_menus = wp_get_nav_menus();
}
if ( ! $menu_selected_id && ! empty( $custom_menus ) )
$menu_selected_id = $custom_menus[0]->term_id;
$menu_title = '';
if ( $menu_selected_id > 0 ) {
foreach ( $custom_menus as $menu ) {
if ( $menu->term_id == $menu_selected_id ) {
$menu_title = $menu->name;
break;
}
}
}
if ( isset( $_POST['li-count'] ) )
$post_counter = $_POST['li-count'];
else
$post_counter = 0;
// Create a new menu. Menus are stored as terms in the 'menu' taxonomy.
if ( isset( $_POST['add-menu'] ) && ! $updated ) {
$insert_menu_name = $_POST['add-menu-name'];
if ( $insert_menu_name ) {
$menu = wp_create_nav_menu( $insert_menu_name );
if ( is_wp_error( $menu ) ) {
$messages_div = '
' . $menu->get_error_message() . '
';
} else {
$custom_menus[$menu->term_id] = $menu;
$menu_selected_id = $menu->term_id;
$menu_id_in_edit = $menu_selected_id;
$menu_title = $menu->name;
$messages_div = '' . sprintf( __('“%s” menu has been created.'), esc_html( $menu->name ) ) . '
';
$post_counter = 0;
}
} else {
$messages_div = '' . __('Please enter a valid menu name.') . '
';
}
$updated = true;
}
if ( $post_counter > 0 && $menu_selected_id > 0 && ! $updated ) {
$menu_items = wp_get_nav_menu_items( $menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') );
$parent_menu_ids = array();
// Loop through all POST variables
for ( $k = 1; $k <= $post_counter; $k++ ) {
$db_id = isset( $_POST['dbid'.$k] )? $_POST['dbid'.$k] : 0;
$object_id = isset( $_POST['postmenu'.$k] )? $_POST['postmenu'.$k] : 0;
$parent_id = isset( $_POST['parent'.$k] )? $_POST['parent'.$k] : 0;
$custom_title = isset( $_POST['title'.$k] )? $_POST['title'.$k] : '';
$custom_linkurl = ( isset( $_POST['linkurl'.$k] ) && 'custom' == $_POST['linktype'.$k] ) ? $_POST['linkurl'.$k] : '';
$custom_description = isset( $_POST['description'.$k] )? $_POST['description'.$k] : '';
// doesn't seem to be used by UI
$icon = isset( $_POST['icon'.$k] )? $_POST['icon'.$k] : 0;
$position = isset( $_POST['position'.$k] )? $_POST['position'.$k] : 0;
$linktype = isset( $_POST['linktype'.$k] )? $_POST['linktype'.$k] : 'custom';
$custom_anchor_title = isset( $_POST['anchortitle'.$k] )? $_POST['anchortitle'.$k] : $custom_title;
$new_window = isset( $_POST['newwindow'.$k] )? $_POST['newwindow'.$k] : 0;
$post = array( 'post_status' => 'publish', 'post_type' => 'nav_menu_item', 'post_author' => $user_ID,
'ping_status' => 0, 'post_parent' => 0, 'menu_order' => $position,
'post_excerpt' => $custom_anchor_title, 'tax_input' => array( 'nav_menu' => $menu_title ),
'post_content' => $custom_description, 'post_title' => $custom_title );
if ( $parent_id > 0 && isset( $parent_menu_ids[$parent_id] ) )
$post['post_parent'] = $parent_menu_ids[$parent_id];
// New menu item
if ( $db_id == 0 ) {
$db_id = wp_insert_post( $post );
} elseif ( isset( $menu_items[$db_id] ) ) {
$post['ID'] = $db_id;
wp_update_post( $post );
unset( $menu_items[$db_id] );
}
$parent_menu_ids[ $k ] = $db_id;
update_post_meta( $db_id, 'menu_type', $linktype );
update_post_meta( $db_id, 'object_id', $object_id );
if ( $new_window )
update_post_meta( $db_id, 'menu_new_window', 1 );
else
update_post_meta( $db_id, 'menu_new_window', 0 );
if ( $custom_linkurl )
update_post_meta( $db_id, 'menu_link', esc_url_raw( $custom_linkurl ) );
}
if ( !empty( $menu_items ) ) {
foreach ( array_keys( $menu_items ) as $menu_id ) {
wp_delete_post( $menu_id );
}
}
$messages_div = '' . __('The menu has been updated.') . '
';
}
?>
style="display:none;" />