wp/wp-admin/nav-menus.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    18 if ( ! current_theme_supports( 'menus' ) && ! current_theme_supports( 'widgets' ) )
    18 if ( ! current_theme_supports( 'menus' ) && ! current_theme_supports( 'widgets' ) )
    19 	wp_die( __( 'Your theme does not support navigation menus or widgets.' ) );
    19 	wp_die( __( 'Your theme does not support navigation menus or widgets.' ) );
    20 
    20 
    21 // Permissions Check
    21 // Permissions Check
    22 if ( ! current_user_can('edit_theme_options') )
    22 if ( ! current_user_can('edit_theme_options') )
    23 	wp_die( __( 'Cheatin’ uh?' ) );
    23 	wp_die( __( 'Cheatin’ uh?' ), 403 );
    24 
    24 
    25 wp_enqueue_script( 'nav-menu' );
    25 wp_enqueue_script( 'nav-menu' );
    26 
    26 
    27 if ( wp_is_mobile() )
    27 if ( wp_is_mobile() )
    28 	wp_enqueue_script( 'jquery-touch-punch' );
    28 	wp_enqueue_script( 'jquery-touch-punch' );
    51 			set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
    51 			set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
    52 		elseif ( isset( $_REQUEST['menu-item'] ) )
    52 		elseif ( isset( $_REQUEST['menu-item'] ) )
    53 			wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
    53 			wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
    54 		break;
    54 		break;
    55 	case 'move-down-menu-item' :
    55 	case 'move-down-menu-item' :
    56 		// moving down a menu item is the same as moving up the next in order
    56 
       
    57 		// Moving down a menu item is the same as moving up the next in order.
    57 		check_admin_referer( 'move-menu_item' );
    58 		check_admin_referer( 'move-menu_item' );
    58 		$menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
    59 		$menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
    59 		if ( is_nav_menu_item( $menu_item_id ) ) {
    60 		if ( is_nav_menu_item( $menu_item_id ) ) {
    60 			$menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
    61 			$menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
    61 			if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
    62 			if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
    62 				$menu_id = (int) $menus[0];
    63 				$menu_id = (int) $menus[0];
    63 				$ordered_menu_items = wp_get_nav_menu_items( $menu_id );
    64 				$ordered_menu_items = wp_get_nav_menu_items( $menu_id );
    64 				$menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
    65 				$menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
    65 
    66 
    66 				// set up the data we need in one pass through the array of menu items
    67 				// Set up the data we need in one pass through the array of menu items.
    67 				$dbids_to_orders = array();
    68 				$dbids_to_orders = array();
    68 				$orders_to_dbids = array();
    69 				$orders_to_dbids = array();
    69 				foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
    70 				foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
    70 					if ( isset( $ordered_menu_item_object->ID ) ) {
    71 					if ( isset( $ordered_menu_item_object->ID ) ) {
    71 						if ( isset( $ordered_menu_item_object->menu_order ) ) {
    72 						if ( isset( $ordered_menu_item_object->menu_order ) ) {
    73 							$orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
    74 							$orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
    74 						}
    75 						}
    75 					}
    76 					}
    76 				}
    77 				}
    77 
    78 
    78 				// get next in order
    79 				// Get next in order.
    79 				if (
    80 				if (
    80 					isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1] )
    81 					isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1] )
    81 				) {
    82 				) {
    82 					$next_item_id = $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1];
    83 					$next_item_id = $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1];
    83 					$next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
    84 					$next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
    84 
    85 
    85 					// if not siblings of same parent, bubble menu item up but keep order
    86 					// If not siblings of same parent, bubble menu item up but keep order.
    86 					if (
    87 					if (
    87 						! empty( $menu_item_data['menu_item_parent'] ) &&
    88 						! empty( $menu_item_data['menu_item_parent'] ) &&
    88 						(
    89 						(
    89 							empty( $next_item_data['menu_item_parent'] ) ||
    90 							empty( $next_item_data['menu_item_parent'] ) ||
    90 							$next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent']
    91 							$next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent']
   100 							$menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
   101 							$menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
   101 							update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
   102 							update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
   102 
   103 
   103 						}
   104 						}
   104 
   105 
   105 					// make menu item a child of its next sibling
   106 					// Make menu item a child of its next sibling.
   106 					} else {
   107 					} else {
   107 						$next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
   108 						$next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
   108 						$menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
   109 						$menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
   109 
   110 
   110 						$menu_item_data['menu_item_parent'] = $next_item_data['ID'];
   111 						$menu_item_data['menu_item_parent'] = $next_item_data['ID'];
   112 
   113 
   113 						wp_update_post($menu_item_data);
   114 						wp_update_post($menu_item_data);
   114 						wp_update_post($next_item_data);
   115 						wp_update_post($next_item_data);
   115 					}
   116 					}
   116 
   117 
   117 				// the item is last but still has a parent, so bubble up
   118 				// The item is last but still has a parent, so bubble up.
   118 				} elseif (
   119 				} elseif (
   119 					! empty( $menu_item_data['menu_item_parent'] ) &&
   120 					! empty( $menu_item_data['menu_item_parent'] ) &&
   120 					in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids )
   121 					in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids )
   121 				) {
   122 				) {
   122 					$menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true);
   123 					$menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true);
   134 			if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
   135 			if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
   135 				$menu_id = (int) $menus[0];
   136 				$menu_id = (int) $menus[0];
   136 				$ordered_menu_items = wp_get_nav_menu_items( $menu_id );
   137 				$ordered_menu_items = wp_get_nav_menu_items( $menu_id );
   137 				$menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
   138 				$menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
   138 
   139 
   139 				// set up the data we need in one pass through the array of menu items
   140 				// Set up the data we need in one pass through the array of menu items.
   140 				$dbids_to_orders = array();
   141 				$dbids_to_orders = array();
   141 				$orders_to_dbids = array();
   142 				$orders_to_dbids = array();
   142 				foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
   143 				foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
   143 					if ( isset( $ordered_menu_item_object->ID ) ) {
   144 					if ( isset( $ordered_menu_item_object->ID ) ) {
   144 						if ( isset( $ordered_menu_item_object->menu_order ) ) {
   145 						if ( isset( $ordered_menu_item_object->menu_order ) ) {
   146 							$orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
   147 							$orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
   147 						}
   148 						}
   148 					}
   149 					}
   149 				}
   150 				}
   150 
   151 
   151 				// if this menu item is not first
   152 				// If this menu item is not first.
   152 				if ( ! empty( $dbids_to_orders[$menu_item_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ) {
   153 				if ( ! empty( $dbids_to_orders[$menu_item_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ) {
   153 
   154 
   154 					// if this menu item is a child of the previous
   155 					// If this menu item is a child of the previous.
   155 					if (
   156 					if (
   156 						! empty( $menu_item_data['menu_item_parent'] ) &&
   157 						! empty( $menu_item_data['menu_item_parent'] ) &&
   157 						in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) &&
   158 						in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) &&
   158 						isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) &&
   159 						isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) &&
   159 						( $menu_item_data['menu_item_parent'] == $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] )
   160 						( $menu_item_data['menu_item_parent'] == $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] )
   162 						$parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
   163 						$parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
   163 
   164 
   164 						if ( ! is_wp_error( $parent_object ) ) {
   165 						if ( ! is_wp_error( $parent_object ) ) {
   165 							$parent_data = (array) $parent_object;
   166 							$parent_data = (array) $parent_object;
   166 
   167 
   167 							// if there is something before the parent and parent a child of it, make menu item a child also of it
   168 							/*
       
   169 							 * If there is something before the parent and parent a child of it,
       
   170 							 * make menu item a child also of it.
       
   171 							 */
   168 							if (
   172 							if (
   169 								! empty( $dbids_to_orders[$parent_db_id] ) &&
   173 								! empty( $dbids_to_orders[$parent_db_id] ) &&
   170 								! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] ) &&
   174 								! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] ) &&
   171 								! empty( $parent_data['menu_item_parent'] )
   175 								! empty( $parent_data['menu_item_parent'] )
   172 							) {
   176 							) {
   173 								$menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
   177 								$menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
   174 
   178 
   175 							// else if there is something before parent and parent not a child of it, make menu item a child of that something's parent
   179 							/*
       
   180 							 * Else if there is something before parent and parent not a child of it,
       
   181 							 * make menu item a child of that something's parent
       
   182 							 */
   176 							} elseif (
   183 							} elseif (
   177 								! empty( $dbids_to_orders[$parent_db_id] ) &&
   184 								! empty( $dbids_to_orders[$parent_db_id] ) &&
   178 								! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] )
   185 								! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] )
   179 							) {
   186 							) {
   180 								$_possible_parent_id = (int) get_post_meta( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1], '_menu_item_menu_item_parent', true);
   187 								$_possible_parent_id = (int) get_post_meta( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1], '_menu_item_menu_item_parent', true);
   181 								if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ) ) )
   188 								if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ) ) )
   182 									$menu_item_data['menu_item_parent'] = $_possible_parent_id;
   189 									$menu_item_data['menu_item_parent'] = $_possible_parent_id;
   183 								else
   190 								else
   184 									$menu_item_data['menu_item_parent'] = 0;
   191 									$menu_item_data['menu_item_parent'] = 0;
   185 
   192 
   186 							// else there isn't something before the parent
   193 							// Else there isn't something before the parent.
   187 							} else {
   194 							} else {
   188 								$menu_item_data['menu_item_parent'] = 0;
   195 								$menu_item_data['menu_item_parent'] = 0;
   189 							}
   196 							}
   190 
   197 
   191 							// set former parent's [menu_order] to that of menu-item's
   198 							// Set former parent's [menu_order] to that of menu-item's.
   192 							$parent_data['menu_order'] = $parent_data['menu_order'] + 1;
   199 							$parent_data['menu_order'] = $parent_data['menu_order'] + 1;
   193 
   200 
   194 							// set menu-item's [menu_order] to that of former parent
   201 							// Set menu-item's [menu_order] to that of former parent.
   195 							$menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1;
   202 							$menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1;
   196 
   203 
   197 							// save changes
   204 							// Save changes.
   198 							update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
   205 							update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
   199 							wp_update_post($menu_item_data);
   206 							wp_update_post($menu_item_data);
   200 							wp_update_post($parent_data);
   207 							wp_update_post($parent_data);
   201 						}
   208 						}
   202 
   209 
   203 					// else this menu item is not a child of the previous
   210 					// Else this menu item is not a child of the previous.
   204 					} elseif (
   211 					} elseif (
   205 						empty( $menu_item_data['menu_order'] ) ||
   212 						empty( $menu_item_data['menu_order'] ) ||
   206 						empty( $menu_item_data['menu_item_parent'] ) ||
   213 						empty( $menu_item_data['menu_item_parent'] ) ||
   207 						! in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) ||
   214 						! in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) ||
   208 						empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ||
   215 						empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ||
   209 						$orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] != $menu_item_data['menu_item_parent']
   216 						$orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] != $menu_item_data['menu_item_parent']
   210 					) {
   217 					) {
   211 						// just make it a child of the previous; keep the order
   218 						// Just make it a child of the previous; keep the order.
   212 						$menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1];
   219 						$menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1];
   213 						update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
   220 						update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
   214 						wp_update_post($menu_item_data);
   221 						wp_update_post($menu_item_data);
   215 					}
   222 					}
   216 				}
   223 				}
   222 		$menu_item_id = (int) $_REQUEST['menu-item'];
   229 		$menu_item_id = (int) $_REQUEST['menu-item'];
   223 
   230 
   224 		check_admin_referer( 'delete-menu_item_' . $menu_item_id );
   231 		check_admin_referer( 'delete-menu_item_' . $menu_item_id );
   225 
   232 
   226 		if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) )
   233 		if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) )
   227 			$messages[] = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
   234 			$messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
   228 		break;
   235 		break;
   229 
   236 
   230 	case 'delete':
   237 	case 'delete':
   231 		check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
   238 		check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
   232 		if ( is_nav_menu( $nav_menu_selected_id ) ) {
   239 		if ( is_nav_menu( $nav_menu_selected_id ) ) {
   233 			$deletion = wp_delete_nav_menu( $nav_menu_selected_id );
   240 			$deletion = wp_delete_nav_menu( $nav_menu_selected_id );
   234 		} else {
   241 		} else {
   235 			// Reset the selected menu
   242 			// Reset the selected menu.
   236 			$nav_menu_selected_id = 0;
   243 			$nav_menu_selected_id = 0;
   237 			unset( $_REQUEST['menu'] );
   244 			unset( $_REQUEST['menu'] );
   238 		}
   245 		}
   239 
   246 
   240 		if ( ! isset( $deletion ) )
   247 		if ( ! isset( $deletion ) )
   241 			break;
   248 			break;
   242 
   249 
   243 		if ( is_wp_error( $deletion ) )
   250 		if ( is_wp_error( $deletion ) )
   244 			$messages[] = '<div id="message" class="error"><p>' . $deletion->get_error_message() . '</p></div>';
   251 			$messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>';
   245 		else
   252 		else
   246 			$messages[] = '<div id="message" class="updated"><p>' . __( 'The menu has been successfully deleted.' ) . '</p></div>';
   253 			$messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu has been successfully deleted.' ) . '</p></div>';
   247 		break;
   254 		break;
   248 
   255 
   249 	case 'delete_menus':
   256 	case 'delete_menus':
   250 		check_admin_referer( 'nav_menus_bulk_actions' );
   257 		check_admin_referer( 'nav_menus_bulk_actions' );
   251 		foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) {
   258 		foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) {
   252 			if ( ! is_nav_menu( $menu_id_to_delete ) )
   259 			if ( ! is_nav_menu( $menu_id_to_delete ) )
   253 				continue;
   260 				continue;
   254 
   261 
   255 			$deletion = wp_delete_nav_menu( $menu_id_to_delete );
   262 			$deletion = wp_delete_nav_menu( $menu_id_to_delete );
   256 			if ( is_wp_error( $deletion ) ) {
   263 			if ( is_wp_error( $deletion ) ) {
   257 				$messages[] = '<div id="message" class="error"><p>' . $deletion->get_error_message() . '</p></div>';
   264 				$messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>';
   258 				$deletion_error = true;
   265 				$deletion_error = true;
   259 			}
   266 			}
   260 		}
   267 		}
   261 
   268 
   262 		if ( empty( $deletion_error ) )
   269 		if ( empty( $deletion_error ) )
   263 			$messages[] = '<div id="message" class="updated"><p>' . __( 'Selected menus have been successfully deleted.' ) . '</p></div>';
   270 			$messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Selected menus have been successfully deleted.' ) . '</p></div>';
   264 		break;
   271 		break;
   265 
   272 
   266 	case 'update':
   273 	case 'update':
   267 		check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
   274 		check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
   268 
   275 
   269 		// Remove menu locations that have been unchecked
   276 		// Remove menu locations that have been unchecked.
   270 		foreach ( $locations as $location => $description ) {
   277 		foreach ( $locations as $location => $description ) {
   271 			if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id )
   278 			if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id )
   272 				unset( $menu_locations[ $location ] );
   279 				unset( $menu_locations[ $location ] );
   273 		}
   280 		}
   274 
   281 
   275 		// Merge new and existing menu locations if any new ones are set
   282 		// Merge new and existing menu locations if any new ones are set.
   276 		if ( isset( $_POST['menu-locations'] ) ) {
   283 		if ( isset( $_POST['menu-locations'] ) ) {
   277 			$new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
   284 			$new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
   278 			$menu_locations = array_merge( $menu_locations, $new_menu_locations );
   285 			$menu_locations = array_merge( $menu_locations, $new_menu_locations );
   279 		}
   286 		}
   280 
   287 
   281 		// Set menu locations
   288 		// Set menu locations.
   282 		set_theme_mod( 'nav_menu_locations', $menu_locations );
   289 		set_theme_mod( 'nav_menu_locations', $menu_locations );
   283 
   290 
   284 		// Add Menu
   291 		// Add Menu.
   285 		if ( 0 == $nav_menu_selected_id ) {
   292 		if ( 0 == $nav_menu_selected_id ) {
   286 			$new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
   293 			$new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
   287 
   294 
   288 			if ( $new_menu_title ) {
   295 			if ( $new_menu_title ) {
   289 				$_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
   296 				$_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
   290 
   297 
   291 				if ( is_wp_error( $_nav_menu_selected_id ) ) {
   298 				if ( is_wp_error( $_nav_menu_selected_id ) ) {
   292 					$messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
   299 					$messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
   293 				} else {
   300 				} else {
   294 					$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
   301 					$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
   295 					$nav_menu_selected_id = $_nav_menu_selected_id;
   302 					$nav_menu_selected_id = $_nav_menu_selected_id;
   296 					$nav_menu_selected_title = $_menu_object->name;
   303 					$nav_menu_selected_title = $_menu_object->name;
   297 					if ( isset( $_REQUEST['menu-item'] ) )
   304 					if ( isset( $_REQUEST['menu-item'] ) )
   312 						$menu_locations = get_nav_menu_locations();
   319 						$menu_locations = get_nav_menu_locations();
   313 						if ( isset( $locations[ $_REQUEST['use-location'] ] ) )
   320 						if ( isset( $locations[ $_REQUEST['use-location'] ] ) )
   314 							$menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id;
   321 							$menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id;
   315 						set_theme_mod( 'nav_menu_locations', $menu_locations );
   322 						set_theme_mod( 'nav_menu_locations', $menu_locations );
   316 					}
   323 					}
       
   324 
   317 					// $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%s</strong> has been created.' ), $nav_menu_selected_title ) . '</p></div>';
   325 					// $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%s</strong> has been created.' ), $nav_menu_selected_title ) . '</p></div>';
   318 					wp_redirect( admin_url( 'nav-menus.php?menu=' . $_nav_menu_selected_id ) );
   326 					wp_redirect( admin_url( 'nav-menus.php?menu=' . $_nav_menu_selected_id ) );
   319 					exit();
   327 					exit();
   320 				}
   328 				}
   321 			} else {
   329 			} else {
   322 				$messages[] = '<div id="message" class="error"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
   330 				$messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
   323 			}
   331 			}
   324 
   332 
   325 		// Update existing menu
   333 		// Update existing menu.
   326 		} else {
   334 		} else {
   327 
   335 
   328 			$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
   336 			$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
   329 
   337 
   330 			$menu_title = trim( esc_html( $_POST['menu-name'] ) );
   338 			$menu_title = trim( esc_html( $_POST['menu-name'] ) );
   331 			if ( ! $menu_title ) {
   339 			if ( ! $menu_title ) {
   332 				$messages[] = '<div id="message" class="error"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
   340 				$messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
   333 				$menu_title = $_menu_object->name;
   341 				$menu_title = $_menu_object->name;
   334 			}
   342 			}
   335 
   343 
   336 			if ( ! is_wp_error( $_menu_object ) ) {
   344 			if ( ! is_wp_error( $_menu_object ) ) {
   337 				$_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
   345 				$_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
   338 				if ( is_wp_error( $_nav_menu_selected_id ) ) {
   346 				if ( is_wp_error( $_nav_menu_selected_id ) ) {
   339 					$_menu_object = $_nav_menu_selected_id;
   347 					$_menu_object = $_nav_menu_selected_id;
   340 					$messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
   348 					$messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
   341 				} else {
   349 				} else {
   342 					$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
   350 					$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
   343 					$nav_menu_selected_title = $_menu_object->name;
   351 					$nav_menu_selected_title = $_menu_object->name;
   344 				}
   352 				}
   345 			}
   353 			}
   346 
   354 
   347 			// Update menu items
   355 			// Update menu items.
   348 			if ( ! is_wp_error( $_menu_object ) ) {
   356 			if ( ! is_wp_error( $_menu_object ) ) {
   349 				$messages = array_merge( $messages, wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) );
   357 				$messages = array_merge( $messages, wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) );
   350 			}
   358 			}
   351 		}
   359 		}
   352 		break;
   360 		break;
   364 			$new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
   372 			$new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
   365 			$menu_locations = array_merge( $menu_locations, $new_menu_locations );
   373 			$menu_locations = array_merge( $menu_locations, $new_menu_locations );
   366 			// Set menu locations
   374 			// Set menu locations
   367 			set_theme_mod( 'nav_menu_locations', $menu_locations );
   375 			set_theme_mod( 'nav_menu_locations', $menu_locations );
   368 
   376 
   369 			$messages[] = '<div id="message" class="updated"><p>' . __( 'Menu locations updated.' ) . '</p></div>';
   377 			$messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Menu locations updated.' ) . '</p></div>';
   370 		}
   378 		}
   371 		break;
   379 		break;
   372 }
   380 }
   373 
   381 
   374 // Get all nav menus
   382 // Get all nav menus.
   375 $nav_menus = wp_get_nav_menus( array('orderby' => 'name') );
   383 $nav_menus = wp_get_nav_menus();
   376 $menu_count = count( $nav_menus );
   384 $menu_count = count( $nav_menus );
   377 
   385 
   378 // Are we on the add new screen?
   386 // Are we on the add new screen?
   379 $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false;
   387 $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false;
   380 
   388 
   381 $locations_screen = ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) ? true : false;
   389 $locations_screen = ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) ? true : false;
   382 
   390 
   383 // If we have one theme location, and zero menus, we take them right into editing their first menu
   391 /*
       
   392  * If we have one theme location, and zero menus, we take them right
       
   393  * into editing their first menu.
       
   394  */
   384 $page_count = wp_count_posts( 'page' );
   395 $page_count = wp_count_posts( 'page' );
   385 $one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false;
   396 $one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false;
   386 
   397 
   387 $nav_menus_l10n = array(
   398 $nav_menus_l10n = array(
   388 	'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
   399 	'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
   402 	/* translators: 1: item name, 2: item position, 3: parent item name */
   413 	/* translators: 1: item name, 2: item position, 3: parent item name */
   403 	'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
   414 	'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
   404 );
   415 );
   405 wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
   416 wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
   406 
   417 
   407 // Redirect to add screen if there are no menus and this users has either zero, or more than 1 theme locations
   418 /*
       
   419  * Redirect to add screen if there are no menus and this users has either zero,
       
   420  * or more than 1 theme locations.
       
   421  */
   408 if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus )
   422 if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus )
   409 	wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
   423 	wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
   410 
   424 
   411 // Get recently edited nav menu
   425 // Get recently edited nav menu.
   412 $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
   426 $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
   413 if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) )
   427 if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) )
   414 	$recently_edited = $nav_menu_selected_id;
   428 	$recently_edited = $nav_menu_selected_id;
   415 
   429 
   416 // Use $recently_edited if none are selected
   430 // Use $recently_edited if none are selected.
   417 if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) )
   431 if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) )
   418 	$nav_menu_selected_id = $recently_edited;
   432 	$nav_menu_selected_id = $recently_edited;
   419 
   433 
   420 // On deletion of menu, if another menu exists, show it
   434 // On deletion of menu, if another menu exists, show it.
   421 if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] )
   435 if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] )
   422 	$nav_menu_selected_id = $nav_menus[0]->term_id;
   436 	$nav_menu_selected_id = $nav_menus[0]->term_id;
   423 
   437 
   424 // Set $nav_menu_selected_id to 0 if no menus
   438 // Set $nav_menu_selected_id to 0 if no menus.
   425 if ( $one_theme_location_no_menus ) {
   439 if ( $one_theme_location_no_menus ) {
   426 	$nav_menu_selected_id = 0;
   440 	$nav_menu_selected_id = 0;
   427 } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
   441 } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
   428 	// if we have no selection yet, and we have menus, set to the first one in the list
   442 	// if we have no selection yet, and we have menus, set to the first one in the list.
   429 	$nav_menu_selected_id = $nav_menus[0]->term_id;
   443 	$nav_menu_selected_id = $nav_menus[0]->term_id;
   430 }
   444 }
   431 
   445 
   432 // Update the user's setting
   446 // Update the user's setting.
   433 if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) )
   447 if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) )
   434 	update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
   448 	update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
   435 
   449 
   436 // If there's a menu, get its name.
   450 // If there's a menu, get its name.
   437 if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
   451 if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
   438 	$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
   452 	$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
   439 	$nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
   453 	$nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
   440 }
   454 }
   441 
   455 
   442 // Generate truncated menu names
   456 // Generate truncated menu names.
   443 foreach( (array) $nav_menus as $key => $_nav_menu ) {
   457 foreach( (array) $nav_menus as $key => $_nav_menu ) {
   444 	$nav_menus[$key]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '&hellip;' );
   458 	$nav_menus[$key]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '&hellip;' );
   445 }
   459 }
   446 
   460 
   447 // Retrieve menu locations
   461 // Retrieve menu locations.
   448 if ( current_theme_supports( 'menus' ) ) {
   462 if ( current_theme_supports( 'menus' ) ) {
   449 	$locations = get_registered_nav_menus();
   463 	$locations = get_registered_nav_menus();
   450 	$menu_locations = get_nav_menu_locations();
   464 	$menu_locations = get_nav_menu_locations();
   451 }
   465 }
   452 
   466 
   453 // Ensure the user will be able to scroll horizontally
   467 /*
   454 // by adding a class for the max menu depth.
   468  * Ensure the user will be able to scroll horizontally
       
   469  * by adding a class for the max menu depth.
       
   470  */
   455 global $_wp_nav_menu_max_depth;
   471 global $_wp_nav_menu_max_depth;
   456 $_wp_nav_menu_max_depth = 0;
   472 $_wp_nav_menu_max_depth = 0;
   457 
   473 
   458 // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth
   474 // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth.
   459 if ( is_nav_menu( $nav_menu_selected_id ) ) {
   475 if ( is_nav_menu( $nav_menu_selected_id ) ) {
   460 	$menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
   476 	$menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
   461 	$edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
   477 	$edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
   462 }
   478 }
   463 
   479 
   474 if ( ! current_theme_supports( 'menus' ) && ! $num_locations )
   490 if ( ! current_theme_supports( 'menus' ) && ! $num_locations )
   475 	$messages[] = '<div id="message" class="updated"><p>' . sprintf( __( 'Your theme does not natively support menus, but you can use them in sidebars by adding a &#8220;Custom Menu&#8221; widget on the <a href="%s">Widgets</a> screen.' ), admin_url( 'widgets.php' ) ) . '</p></div>';
   491 	$messages[] = '<div id="message" class="updated"><p>' . sprintf( __( 'Your theme does not natively support menus, but you can use them in sidebars by adding a &#8220;Custom Menu&#8221; widget on the <a href="%s">Widgets</a> screen.' ), admin_url( 'widgets.php' ) ) . '</p></div>';
   476 
   492 
   477 if ( ! $locations_screen ) : // Main tab
   493 if ( ! $locations_screen ) : // Main tab
   478 	$overview  = '<p>' . __( 'This screen is used for managing your custom navigation menus.' ) . '</p>';
   494 	$overview  = '<p>' . __( 'This screen is used for managing your custom navigation menus.' ) . '</p>';
   479 	$overview .= '<p>' . sprintf( __( 'Menus can be displayed in locations defined by your theme, even used in sidebars by adding a &#8220;Custom Menu&#8221; widget on the <a href="%1$s">Widgets</a> screen. If your theme does not support the custom menus feature (the default themes, %2$s and %3$s, do), you can learn about adding this support by following the Documentation link to the side.' ), admin_url( 'widgets.php' ), 'Twenty Thirteen', 'Twenty Twelve' ) . '</p>';
   495 	$overview .= '<p>' . sprintf( __( 'Menus can be displayed in locations defined by your theme, even used in sidebars by adding a &#8220;Custom Menu&#8221; widget on the <a href="%1$s">Widgets</a> screen. If your theme does not support the custom menus feature (the default themes, %2$s and %3$s, do), you can learn about adding this support by following the Documentation link to the side.' ), admin_url( 'widgets.php' ), 'Twenty Fifteen', 'Twenty Fourteen' ) . '</p>';
   480 	$overview .= '<p>' . __( 'From this screen you can:' ) . '</p>';
   496 	$overview .= '<p>' . __( 'From this screen you can:' ) . '</p>';
   481 	$overview .= '<ul><li>' . __( 'Create, edit, and delete menus' ) . '</li>';
   497 	$overview .= '<ul><li>' . __( 'Create, edit, and delete menus' ) . '</li>';
   482 	$overview .= '<li>' . __( 'Add, organize, and modify individual menu items' ) . '</li></ul>';
   498 	$overview .= '<li>' . __( 'Add, organize, and modify individual menu items' ) . '</li></ul>';
   483 
   499 
   484 	get_current_screen()->add_help_tab( array(
   500 	get_current_screen()->add_help_tab( array(
   499 	) );
   515 	) );
   500 
   516 
   501 	$editing_menus  = '<p>' . __( 'Each custom menu may contain a mix of links to pages, categories, custom URLs or other content types. Menu links are added by selecting items from the expanding boxes in the left-hand column below.' ) . '</p>';
   517 	$editing_menus  = '<p>' . __( 'Each custom menu may contain a mix of links to pages, categories, custom URLs or other content types. Menu links are added by selecting items from the expanding boxes in the left-hand column below.' ) . '</p>';
   502 	$editing_menus .= '<p>' . __( '<strong>Clicking the arrow to the right of any menu item</strong> in the editor will reveal a standard group of settings. Additional settings such as link target, CSS classes, link relationships, and link descriptions can be enabled and disabled via the Screen Options tab.' ) . '</p>';
   518 	$editing_menus .= '<p>' . __( '<strong>Clicking the arrow to the right of any menu item</strong> in the editor will reveal a standard group of settings. Additional settings such as link target, CSS classes, link relationships, and link descriptions can be enabled and disabled via the Screen Options tab.' ) . '</p>';
   503 	$editing_menus .= '<ul><li>' . __( 'Add one or several items at once by <strong>selecting the checkbox next to each item and clicking Add to Menu</strong>' ) . '</li>';
   519 	$editing_menus .= '<ul><li>' . __( 'Add one or several items at once by <strong>selecting the checkbox next to each item and clicking Add to Menu</strong>' ) . '</li>';
   504 	$editing_menus .= '<li>' . __( 'To add a custom link, <strong>expand the Links section, enter a URL and link text, and click Add to Menu</strong>' ) .'</li>';
   520 	$editing_menus .= '<li>' . __( 'To add a custom link, <strong>expand the Custom Links section, enter a URL and link text, and click Add to Menu</strong>' ) .'</li>';
   505 	$editing_menus .= '<li>' . __( 'To reorganize menu items, <strong>drag and drop items with your mouse or use your keyboard</strong>. Drag or move a menu item a little to the right to make it a submenu' ) . '</li>';
   521 	$editing_menus .= '<li>' . __( 'To reorganize menu items, <strong>drag and drop items with your mouse or use your keyboard</strong>. Drag or move a menu item a little to the right to make it a submenu' ) . '</li>';
   506 	$editing_menus .= '<li>' . __( 'Delete a menu item by <strong>expanding it and clicking the Remove link</strong>' ) . '</li></ul>';
   522 	$editing_menus .= '<li>' . __( 'Delete a menu item by <strong>expanding it and clicking the Remove link</strong>' ) . '</li></ul>';
   507 
   523 
   508 	get_current_screen()->add_help_tab( array(
   524 	get_current_screen()->add_help_tab( array(
   509 		'id'      => 'editing-menus',
   525 		'id'      => 'editing-menus',
   510 		'title'   => __( 'Editing Menus' ),
   526 		'title'   => __( 'Editing Menus' ),
   511 		'content' => $editing_menus
   527 		'content' => $editing_menus
   512 	) );
   528 	) );
   513 else : // Locations Tab
   529 else : // Locations Tab.
   514 	$locations_overview  = '<p>' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '</p>';
   530 	$locations_overview  = '<p>' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '</p>';
   515 	$locations_overview .= '<ul><li>' . __( 'To assign menus to one or more theme locations, <strong>select a menu from each location&#8217;s drop down.</strong> When you&#8217;re finished, <strong>click Save Changes</strong>' ) . '</li>';
   531 	$locations_overview .= '<ul><li>' . __( 'To assign menus to one or more theme locations, <strong>select a menu from each location&#8217;s drop down.</strong> When you&#8217;re finished, <strong>click Save Changes</strong>' ) . '</li>';
   516 	$locations_overview .= '<li>' . __( 'To edit a menu currently assigned to a theme location, <strong>click the adjacent &#8217;Edit&#8217; link</strong>' ) . '</li>';
   532 	$locations_overview .= '<li>' . __( 'To edit a menu currently assigned to a theme location, <strong>click the adjacent &#8217;Edit&#8217; link</strong>' ) . '</li>';
   517 	$locations_overview .= '<li>' . __( 'To add a new menu instead of assigning an existing one, <strong>click the &#8217;Use new menu&#8217; link</strong>. Your new menu will be automatically assigned to that theme location' ) . '</li></ul>';
   533 	$locations_overview .= '<li>' . __( 'To add a new menu instead of assigning an existing one, <strong>click the &#8217;Use new menu&#8217; link</strong>. Your new menu will be automatically assigned to that theme location' ) . '</li></ul>';
   518 
   534 
   523 	) );
   539 	) );
   524 endif;
   540 endif;
   525 
   541 
   526 get_current_screen()->set_help_sidebar(
   542 get_current_screen()->set_help_sidebar(
   527 	'<p><strong>' . __('For more information:') . '</strong></p>' .
   543 	'<p><strong>' . __('For more information:') . '</strong></p>' .
   528 	'<p>' . __('<a href="http://codex.wordpress.org/Appearance_Menus_Screen" target="_blank">Documentation on Menus</a>') . '</p>' .
   544 	'<p>' . __('<a href="https://codex.wordpress.org/Appearance_Menus_Screen" target="_blank">Documentation on Menus</a>') . '</p>' .
   529 	'<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
   545 	'<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
   530 );
   546 );
   531 
   547 
   532 // Get the admin header
   548 // Get the admin header.
   533 require_once( ABSPATH . 'wp-admin/admin-header.php' );
   549 require_once( ABSPATH . 'wp-admin/admin-header.php' );
   534 ?>
   550 ?>
   535 <div class="wrap">
   551 <div class="wrap">
   536 	<?php screen_icon(); ?>
       
   537 	<h2 class="nav-tab-wrapper">
   552 	<h2 class="nav-tab-wrapper">
   538 		<a href="<?php echo admin_url( 'nav-menus.php' ); ?>" class="nav-tab<?php if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' != $_GET['action'] ) echo ' nav-tab-active'; ?>"><?php esc_html_e( 'Edit Menus' ); ?></a>
   553 		<a href="<?php echo admin_url( 'nav-menus.php' ); ?>" class="nav-tab<?php if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' != $_GET['action'] ) echo ' nav-tab-active'; ?>"><?php esc_html_e( 'Edit Menus' ); ?></a>
   539 		<?php if ( $num_locations && $menu_count ) : ?>
   554 		<?php if ( $num_locations && $menu_count ) : ?>
   540 			<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>" class="nav-tab<?php if ( $locations_screen ) echo ' nav-tab-active'; ?>"><?php esc_html_e( 'Manage Locations' ); ?></a>
   555 			<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>" class="nav-tab<?php if ( $locations_screen ) echo ' nav-tab-active'; ?>"><?php esc_html_e( 'Manage Locations' ); ?></a>
   541 		<?php endif; ?>
   556 		<?php endif; ?>
   545 		echo $message . "\n";
   560 		echo $message . "\n";
   546 	endforeach;
   561 	endforeach;
   547 	?>
   562 	?>
   548 	<?php
   563 	<?php
   549 	if ( $locations_screen ) :
   564 	if ( $locations_screen ) :
   550 		echo '<p>' . sprintf( _n( 'Your theme supports %s menu. Select which menu you would like to use.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . '</p>';
   565 		if ( 1 == $num_locations ) {
       
   566 			echo '<p>' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '</p>';
       
   567 		} else {
       
   568 			echo '<p>' .  sprintf( _n( 'Your theme supports %s menu. Select which menu appears in each location.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . '</p>';
       
   569 		}
   551 	?>
   570 	?>
   552 	<div id="menu-locations-wrap">
   571 	<div id="menu-locations-wrap">
   553 		<form method="post" action="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>">
   572 		<form method="post" action="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>">
   554 			<table class="widefat fixed" cellspacing="0" id="menu-locations-table">
   573 			<table class="widefat fixed" id="menu-locations-table">
   555 				<thead>
   574 				<thead>
   556 				<tr>
   575 				<tr>
   557 					<th scope="col" class="manage-column column-locations"><?php _e( 'Theme Location' ); ?></th>
   576 					<th scope="col" class="manage-column column-locations"><?php _e( 'Theme Location' ); ?></th>
   558 					<th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
   577 					<th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
   559 				</tr>
   578 				</tr>
   560 				</thead>
   579 				</thead>
   561 				<!--<tfoot>
       
   562 				<tr>
       
   563 					<th scope="col" class="manage-column column-locations"><?php _e( 'Theme Location' ); ?></th>
       
   564 					<th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
       
   565 				</tr>
       
   566 				</tfoot>-->
       
   567 				<tbody class="menu-locations">
   580 				<tbody class="menu-locations">
   568 				<?php foreach ( $locations as $_location => $_name ) { ?>
   581 				<?php foreach ( $locations as $_location => $_name ) { ?>
   569 					<tr id="menu-locations-row">
   582 					<tr class="menu-locations-row">
   570 						<td class="menu-location-title"><strong><?php echo $_name; ?></strong></td>
   583 						<td class="menu-location-title"><label for="locations-<?php echo $_location; ?>"><?php echo $_name; ?></label></td>
   571 						<td class="menu-location-menus">
   584 						<td class="menu-location-menus">
   572 							<select name="menu-locations[<?php echo $_location; ?>]" id="locations-<?php echo $_location; ?>">
   585 							<select name="menu-locations[<?php echo $_location; ?>]" id="locations-<?php echo $_location; ?>">
   573 								<option value="0"><?php printf( '&mdash; %s &mdash;', esc_html__( 'Select a Menu' ) ); ?></option>
   586 								<option value="0"><?php printf( '&mdash; %s &mdash;', esc_html__( 'Select a Menu' ) ); ?></option>
   574 								<?php foreach ( $nav_menus as $menu ) : ?>
   587 								<?php foreach ( $nav_menus as $menu ) : ?>
   575 									<?php $selected = isset( $menu_locations[$_location] ) && $menu_locations[$_location] == $menu->term_id; ?>
   588 									<?php $selected = isset( $menu_locations[$_location] ) && $menu_locations[$_location] == $menu->term_id; ?>
   580 							</select>
   593 							</select>
   581 							<div class="locations-row-links">
   594 							<div class="locations-row-links">
   582 								<?php if ( isset( $menu_locations[ $_location ] ) && 0 != $menu_locations[ $_location ] ) : ?>
   595 								<?php if ( isset( $menu_locations[ $_location ] ) && 0 != $menu_locations[ $_location ] ) : ?>
   583 								<span class="locations-edit-menu-link">
   596 								<span class="locations-edit-menu-link">
   584 									<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => $menu_locations[$_location] ), admin_url( 'nav-menus.php' ) ) ); ?>">
   597 									<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => $menu_locations[$_location] ), admin_url( 'nav-menus.php' ) ) ); ?>">
   585 										<?php _ex( 'Edit', 'menu' ); ?>
   598 										<span aria-hidden="true"><?php _ex( 'Edit', 'menu' ); ?></span><span class="screen-reader-text"><?php _e( 'Edit selected menu' ); ?></span>
   586 									</a>
   599 									</a>
   587 								</span>
   600 								</span>
   588 								<?php endif; ?>
   601 								<?php endif; ?>
   589 								<span class="locations-add-menu-link">
   602 								<span class="locations-add-menu-link">
   590 									<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0, 'use-location' => $_location ), admin_url( 'nav-menus.php' ) ) ); ?>">
   603 									<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0, 'use-location' => $_location ), admin_url( 'nav-menus.php' ) ) ); ?>">
   591 										<?php _ex( 'Use new menu', 'menu' ); ?>
   604 										<?php _ex( 'Use new menu', 'menu' ); ?>
   592 									</a>
   605 									</a>
   593 								</span>
   606 								</span>
   594 							</div><!-- #locations-row-links -->
   607 							</div><!-- .locations-row-links -->
   595 						</td><!-- .menu-location-menus -->
   608 						</td><!-- .menu-location-menus -->
   596 					</tr><!-- #menu-locations-row -->
   609 					</tr><!-- .menu-locations-row -->
   597 				<?php } // foreach ?>
   610 				<?php } // foreach ?>
   598 				</tbody>
   611 				</tbody>
   599 			</table>
   612 			</table>
   600 			<p class="button-controls"><?php submit_button( __( 'Save Changes' ), 'primary left', 'nav-menu-locations', false ); ?></p>
   613 			<p class="button-controls"><?php submit_button( __( 'Save Changes' ), 'primary left', 'nav-menu-locations', false ); ?></p>
   601 			<?php wp_nonce_field( 'save-menu-locations' ); ?>
   614 			<?php wp_nonce_field( 'save-menu-locations' ); ?>
   602 			<input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
   615 			<input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
   603 		</form>
   616 		</form>
   604 	</div><!-- #menu-locations-wrap -->
   617 	</div><!-- #menu-locations-wrap -->
   605 	<?php do_action( 'after_menu_locations_table' ); ?>
   618 	<?php
       
   619 	/**
       
   620 	 * Fires after the menu locations table is displayed.
       
   621 	 *
       
   622 	 * @since 3.6.0
       
   623 	 */
       
   624 	do_action( 'after_menu_locations_table' ); ?>
   606 	<?php else : ?>
   625 	<?php else : ?>
   607 	<div class="manage-menus">
   626 	<div class="manage-menus">
   608  		<?php if ( $menu_count < 2 ) : ?>
   627  		<?php if ( $menu_count < 2 ) : ?>
   609 		<span class="add-edit-menu-action">
   628 		<span class="add-edit-menu-action">
   610 			<?php printf( __( 'Edit your menu below, or <a href="%s">create a new menu</a>.' ), esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0 ), admin_url( 'nav-menus.php' ) ) ) ); ?>
   629 			<?php printf( __( 'Edit your menu below, or <a href="%s">create a new menu</a>.' ), esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0 ), admin_url( 'nav-menus.php' ) ) ) ); ?>
   613 			<form method="get" action="<?php echo admin_url( 'nav-menus.php' ); ?>">
   632 			<form method="get" action="<?php echo admin_url( 'nav-menus.php' ); ?>">
   614 			<input type="hidden" name="action" value="edit" />
   633 			<input type="hidden" name="action" value="edit" />
   615 			<label for="menu" class="selected-menu"><?php _e( 'Select a menu to edit:' ); ?></label>
   634 			<label for="menu" class="selected-menu"><?php _e( 'Select a menu to edit:' ); ?></label>
   616 			<select name="menu" id="menu">
   635 			<select name="menu" id="menu">
   617 				<?php if ( $add_new_screen ) : ?>
   636 				<?php if ( $add_new_screen ) : ?>
   618 					<option value="0" selected="selected"><?php _e( '-- Select --' ); ?></option>
   637 					<option value="0" selected="selected"><?php _e( '&mdash; Select &mdash;' ); ?></option>
   619 				<?php endif; ?>
   638 				<?php endif; ?>
   620 				<?php foreach( (array) $nav_menus as $_nav_menu ) : ?>
   639 				<?php foreach( (array) $nav_menus as $_nav_menu ) : ?>
   621 					<option value="<?php echo esc_attr( $_nav_menu->term_id ); ?>" <?php selected( $_nav_menu->term_id, $nav_menu_selected_id ); ?>>
   640 					<option value="<?php echo esc_attr( $_nav_menu->term_id ); ?>" <?php selected( $_nav_menu->term_id, $nav_menu_selected_id ); ?>>
   622 						<?php
   641 						<?php
   623 						echo esc_html( $_nav_menu->truncated_name ) ;
   642 						echo esc_html( $_nav_menu->truncated_name ) ;
   624 
   643 
   625 						if ( ! empty( $menu_locations ) && in_array( $_nav_menu->term_id, $menu_locations ) ) {
   644 						if ( ! empty( $menu_locations ) && in_array( $_nav_menu->term_id, $menu_locations ) ) {
   626 							$locations_assigned_to_this_menu = array();
   645 							$locations_assigned_to_this_menu = array();
   627 							foreach ( array_keys( $menu_locations, $_nav_menu->term_id ) as $menu_location_key ) {
   646 							foreach ( array_keys( $menu_locations, $_nav_menu->term_id ) as $menu_location_key ) {
   628 								 $locations_assigned_to_this_menu[] = $locations[ $menu_location_key ];
   647 								if ( isset( $locations[ $menu_location_key ] ) ) {
       
   648 									$locations_assigned_to_this_menu[] = $locations[ $menu_location_key ];
       
   649 								}
   629 							}
   650 							}
       
   651 
       
   652 							/**
       
   653 							 * Filter the number of locations listed per menu in the drop-down select.
       
   654 							 *
       
   655 							 * @since 3.6.0
       
   656 							 *
       
   657 							 * @param int $locations Number of menu locations to list. Default 3.
       
   658 							 */
   630 							$assigned_locations = array_slice( $locations_assigned_to_this_menu, 0, absint( apply_filters( 'wp_nav_locations_listed_per_menu', 3 ) ) );
   659 							$assigned_locations = array_slice( $locations_assigned_to_this_menu, 0, absint( apply_filters( 'wp_nav_locations_listed_per_menu', 3 ) ) );
   631 
   660 
   632 							// Adds ellipses following the number of locations defined in $assigned_locations
   661 							// Adds ellipses following the number of locations defined in $assigned_locations.
   633 							printf( ' (%1$s%2$s)',
   662 							if ( ! empty( $assigned_locations ) ) {
   634 								implode( ', ', $assigned_locations ),
   663 								printf( ' (%1$s%2$s)',
   635 								count( $locations_assigned_to_this_menu ) > count( $assigned_locations ) ? ' &hellip;' : ''
   664 									implode( ', ', $assigned_locations ),
   636 							);
   665 									count( $locations_assigned_to_this_menu ) > count( $assigned_locations ) ? ' &hellip;' : ''
       
   666 								);
       
   667 							}
   637 						}
   668 						}
   638 						?>
   669 						?>
   639 					</option>
   670 					</option>
   640 				<?php endforeach; ?>
   671 				<?php endforeach; ?>
   641 			</select>
   672 			</select>
   642 			<span class="submit-btn"><input type="submit" class="button-secondary" value="<?php _e( 'Select' ); ?>"></span>
   673 			<span class="submit-btn"><input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Select' ); ?>"></span>
   643 			<span class="add-new-menu-action">
   674 			<span class="add-new-menu-action">
   644 				<?php printf( __( 'or <a href="%s">create a new menu</a>.' ), esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0 ), admin_url( 'nav-menus.php' ) ) ) ); ?>
   675 				<?php printf( __( 'or <a href="%s">create a new menu</a>.' ), esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0 ), admin_url( 'nav-menus.php' ) ) ) ); ?>
   645 			</span><!-- /add-new-menu-action -->
   676 			</span><!-- /add-new-menu-action -->
   646 		</form>
   677 		</form>
   647 	<?php endif; ?>
   678 	<?php endif; ?>
   649 	<div id="nav-menus-frame">
   680 	<div id="nav-menus-frame">
   650 	<div id="menu-settings-column" class="metabox-holder<?php if ( isset( $_GET['menu'] ) && '0' == $_GET['menu'] ) { echo ' metabox-holder-disabled'; } ?>">
   681 	<div id="menu-settings-column" class="metabox-holder<?php if ( isset( $_GET['menu'] ) && '0' == $_GET['menu'] ) { echo ' metabox-holder-disabled'; } ?>">
   651 
   682 
   652 		<div class="clear"></div>
   683 		<div class="clear"></div>
   653 
   684 
   654 		<form id="nav-menu-meta" action="" class="nav-menu-meta" method="post" enctype="multipart/form-data">
   685 		<form id="nav-menu-meta" class="nav-menu-meta" method="post" enctype="multipart/form-data">
   655 			<input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
   686 			<input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
   656 			<input type="hidden" name="action" value="add-menu-item" />
   687 			<input type="hidden" name="action" value="add-menu-item" />
   657 			<?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
   688 			<?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
   658 			<?php do_accordion_sections( 'nav-menus', 'side', null ); ?>
   689 			<?php do_accordion_sections( 'nav-menus', 'side', null ); ?>
   659 		</form>
   690 		</form>
   660 
   691 
   661 	</div><!-- /#menu-settings-column -->
   692 	</div><!-- /#menu-settings-column -->
   662 	<div id="menu-management-liquid">
   693 	<div id="menu-management-liquid">
   663 		<div id="menu-management">
   694 		<div id="menu-management">
   664 			<form id="update-nav-menu" action="" method="post" enctype="multipart/form-data">
   695 			<form id="update-nav-menu" method="post" enctype="multipart/form-data">
   665 				<div class="menu-edit <?php if ( $add_new_screen ) echo 'blank-slate'; ?>">
   696 				<div class="menu-edit <?php if ( $add_new_screen ) echo 'blank-slate'; ?>">
   666 					<?php
   697 					<?php
   667 					wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
   698 					wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
   668 					wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
   699 					wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
   669 					wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
   700 					wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
   749 							<span class="delete-action">
   780 							<span class="delete-action">
   750 								<a class="submitdelete deletion menu-delete" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'delete', 'menu' => $nav_menu_selected_id, admin_url() ) ), 'delete-nav_menu-' . $nav_menu_selected_id) ); ?>"><?php _e('Delete Menu'); ?></a>
   781 								<a class="submitdelete deletion menu-delete" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'delete', 'menu' => $nav_menu_selected_id, admin_url() ) ), 'delete-nav_menu-' . $nav_menu_selected_id) ); ?>"><?php _e('Delete Menu'); ?></a>
   751 							</span><!-- END .delete-action -->
   782 							</span><!-- END .delete-action -->
   752 							<?php endif; ?>
   783 							<?php endif; ?>
   753 							<div class="publishing-action">
   784 							<div class="publishing-action">
   754 								<?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
   785 								<?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_footer' ) ); ?>
   755 							</div><!-- END .publishing-action -->
   786 							</div><!-- END .publishing-action -->
   756 						</div><!-- END .major-publishing-actions -->
   787 						</div><!-- END .major-publishing-actions -->
   757 					</div><!-- /#nav-menu-footer -->
   788 					</div><!-- /#nav-menu-footer -->
   758 				</div><!-- /.menu-edit -->
   789 				</div><!-- /.menu-edit -->
   759 			</form><!-- /#update-nav-menu -->
   790 			</form><!-- /#update-nav-menu -->