'any', 'post_type' => 'nav_menu_item', 'meta_key' => 'menu_type', 'meta_value' => 'custom' ) );
// Check which menu is selected and if menu is in edit already
if ( isset( $_GET['edit-menu'] ) ) {
$menu_selected_id = (int) $_GET['edit-menu'];
$updated = true;
} elseif ( isset( $_POST[ 'menu-id-in-edit' ] ) ) {
$menu_selected_id = (int) $_POST[ 'menu-id-in-edit' ];
} else {
$menu_selected_id = 0;
}
// Delete a menu
if ( isset($_GET['delete-menu']) && $_GET['delete-menu'] > 0 ) {
// if ( ! current_user_can('delete_menus') )
// wp_die( __( 'Cheatin’ uh?' ));
$menu_id = (int) $_GET['delete-menu'];
check_admin_referer( 'delete_menu-' . $menu_id );
wp_delete_nav_menu( $menu_id );
$messages_div = '
' . __('Menu successfully deleted.') . '
';
$menu_selected_id = 0;
$updated = true;
}
// Default Menu to show
$menus = wp_get_nav_menus();
if ( empty($menus) && empty($_POST) ) {
wp_create_default_nav_menu();
$menus = wp_get_nav_menus();
}
if ( ! $menu_selected_id && ! empty($menus) )
$menu_selected_id = $menus[0]->term_id;
// Get the name of the current Menu
$menu_title = '';
$valid_menu = false;
if ( $menu_selected_id > 0 ) {
foreach ( $menus as $menu ) {
if ( $menu->term_id == $menu_selected_id ) {
$menu_title = $menu->name;
$valid_menu = true;
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['create-menu'] ) && ! $updated ) {
$insert_menu_name = $_POST['create-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 {
$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['item-title'.$k] )? $_POST['item-title'.$k] : '';
$custom_linkurl = ( isset( $_POST['item-url'.$k] ) && 'custom' == $_POST['linktype'.$k] ) ? $_POST['item-url'.$k] : '';
$custom_description = isset( $_POST['item-description'.$k] )? $_POST['item-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['item-attr-title'.$k] )? $_POST['item-attr-title'.$k] : $custom_title;
$new_window = isset( $_POST['item-target'.$k] )? $_POST['item-target'.$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.') . '
';
}
?>