X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/58f607a1de715c9bca69340a4d6fb9e1b9c2bed2..e3ff8f35458a959c1879c0a4976701ed8dcfe651:/wp-admin/nav-menus.php diff --git a/wp-admin/nav-menus.php b/wp-admin/nav-menus.php index ba4d08d8..19348c23 100644 --- a/wp-admin/nav-menus.php +++ b/wp-admin/nav-menus.php @@ -10,7 +10,7 @@ */ /** Load WordPress Administration Bootstrap */ -require_once( 'admin.php' ); +require_once( dirname( __FILE__ ) . '/admin.php' ); // Load all the nav menu interface functions require_once( ABSPATH . 'wp-admin/includes/nav-menu.php' ); @@ -19,25 +19,18 @@ if ( ! current_theme_supports( 'menus' ) && ! current_theme_supports( 'widgets' wp_die( __( 'Your theme does not support navigation menus or widgets.' ) ); // Permissions Check -if ( ! current_user_can('edit_theme_options') ) - wp_die( __( 'Cheatin’ uh?' ) ); - -// Nav Menu CSS -wp_admin_css( 'nav-menu' ); - -// jQuery -wp_enqueue_script( 'jquery' ); -wp_enqueue_script( 'jquery-ui-draggable' ); -wp_enqueue_script( 'jquery-ui-droppable' ); -wp_enqueue_script( 'jquery-ui-sortable' ); +if ( ! current_user_can( 'edit_theme_options' ) ) { + wp_die( + '

' . __( 'Cheatin’ uh?' ) . '

' . + '

' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '

', + 403 + ); +} -// Nav Menu functions wp_enqueue_script( 'nav-menu' ); -// Metaboxes -wp_enqueue_script( 'common' ); -wp_enqueue_script( 'wp-lists' ); -wp_enqueue_script( 'postbox' ); +if ( wp_is_mobile() ) + wp_enqueue_script( 'jquery-touch-punch' ); // Container for any messages displayed to the user $messages = array(); @@ -48,9 +41,20 @@ $nav_menu_selected_title = ''; // The menu id of the current menu being edited $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0; +// Get existing menu locations assignments +$locations = get_registered_nav_menus(); +$menu_locations = get_nav_menu_locations(); +$num_locations = count( array_keys( $locations ) ); + // Allowed actions: add, update, delete $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit'; +/* + * If a JSON blob of navigation menu data is found, expand it and inject it + * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134. + */ +_wp_expand_nav_menu_post_data(); + switch ( $action ) { case 'add-menu-item': check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' ); @@ -60,7 +64,8 @@ switch ( $action ) { wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] ); break; case 'move-down-menu-item' : - // moving down a menu item is the same as moving up the next in order + + // Moving down a menu item is the same as moving up the next in order. check_admin_referer( 'move-menu_item' ); $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0; if ( is_nav_menu_item( $menu_item_id ) ) { @@ -70,10 +75,10 @@ switch ( $action ) { $ordered_menu_items = wp_get_nav_menu_items( $menu_id ); $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) ); - // setup the data we need in one pass through the array of menu items + // Set up the data we need in one pass through the array of menu items. $dbids_to_orders = array(); $orders_to_dbids = array(); - foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) { + foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { if ( isset( $ordered_menu_item_object->ID ) ) { if ( isset( $ordered_menu_item_object->menu_order ) ) { $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order; @@ -82,14 +87,14 @@ switch ( $action ) { } } - // get next in order + // Get next in order. if ( isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1] ) ) { $next_item_id = $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1]; $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) ); - // if not siblings of same parent, bubble menu item up but keep order + // If not siblings of same parent, bubble menu item up but keep order. if ( ! empty( $menu_item_data['menu_item_parent'] ) && ( @@ -109,7 +114,7 @@ switch ( $action ) { } - // make menu item a child of its next sibling + // Make menu item a child of its next sibling. } else { $next_item_data['menu_order'] = $next_item_data['menu_order'] - 1; $menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1; @@ -121,8 +126,7 @@ switch ( $action ) { wp_update_post($next_item_data); } - - // the item is last but still has a parent, so bubble up + // The item is last but still has a parent, so bubble up. } elseif ( ! empty( $menu_item_data['menu_item_parent'] ) && in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids ) @@ -144,10 +148,10 @@ switch ( $action ) { $ordered_menu_items = wp_get_nav_menu_items( $menu_id ); $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) ); - // setup the data we need in one pass through the array of menu items + // Set up the data we need in one pass through the array of menu items. $dbids_to_orders = array(); $orders_to_dbids = array(); - foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) { + foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { if ( isset( $ordered_menu_item_object->ID ) ) { if ( isset( $ordered_menu_item_object->menu_order ) ) { $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order; @@ -156,11 +160,10 @@ switch ( $action ) { } } - - // if this menu item is not first + // If this menu item is not first. if ( ! empty( $dbids_to_orders[$menu_item_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ) { - // if this menu item is a child of the previous + // If this menu item is a child of the previous. if ( ! empty( $menu_item_data['menu_item_parent'] ) && in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) && @@ -173,7 +176,10 @@ switch ( $action ) { if ( ! is_wp_error( $parent_object ) ) { $parent_data = (array) $parent_object; - // if there is something before the parent and parent a child of it, make menu item a child also of it + /* + * If there is something before the parent and parent a child of it, + * make menu item a child also of it. + */ if ( ! empty( $dbids_to_orders[$parent_db_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] ) && @@ -181,7 +187,10 @@ switch ( $action ) { ) { $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent']; - // else if there is something before parent and parent not a child of it, make menu item a child of that something's parent + /* + * Else if there is something before parent and parent not a child of it, + * make menu item a child of that something's parent + */ } elseif ( ! empty( $dbids_to_orders[$parent_db_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] ) @@ -192,24 +201,24 @@ switch ( $action ) { else $menu_item_data['menu_item_parent'] = 0; - // else there isn't something before the parent + // Else there isn't something before the parent. } else { $menu_item_data['menu_item_parent'] = 0; } - // set former parent's [menu_order] to that of menu-item's + // Set former parent's [menu_order] to that of menu-item's. $parent_data['menu_order'] = $parent_data['menu_order'] + 1; - // set menu-item's [menu_order] to that of former parent + // Set menu-item's [menu_order] to that of former parent. $menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1; - // save changes + // Save changes. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] ); wp_update_post($menu_item_data); wp_update_post($parent_data); } - // else this menu item is not a child of the previous + // Else this menu item is not a child of the previous. } elseif ( empty( $menu_item_data['menu_order'] ) || empty( $menu_item_data['menu_item_parent'] ) || @@ -217,7 +226,7 @@ switch ( $action ) { empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) || $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] != $menu_item_data['menu_item_parent'] ) { - // just make it a child of the previous; keep the order + // Just make it a child of the previous; keep the order. $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1]; update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] ); wp_update_post($menu_item_data); @@ -232,55 +241,65 @@ switch ( $action ) { check_admin_referer( 'delete-menu_item_' . $menu_item_id ); - if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) ) - $messages[] = '

' . __('The menu item has been successfully deleted.') . '

'; + $messages[] = '

' . __('The menu item has been successfully deleted.') . '

'; break; + case 'delete': check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id ); - if ( is_nav_menu( $nav_menu_selected_id ) ) { - $deleted_nav_menu = wp_get_nav_menu_object( $nav_menu_selected_id ); - $delete_nav_menu = wp_delete_nav_menu( $nav_menu_selected_id ); - - if ( is_wp_error($delete_nav_menu) ) { - $messages[] = '

' . $delete_nav_menu->get_error_message() . '

'; - } else { - // Remove this menu from any locations. - $locations = get_theme_mod( 'nav_menu_locations' ); - foreach ( (array) $locations as $location => $menu_id ) { - if ( $menu_id == $nav_menu_selected_id ) - $locations[ $location ] = 0; - } - set_theme_mod( 'nav_menu_locations', $locations ); - $messages[] = '

' . __('The menu has been successfully deleted.') . '

'; - // Select the next available menu - $nav_menu_selected_id = 0; - $_nav_menus = wp_get_nav_menus( array('orderby' => 'name') ); - foreach( $_nav_menus as $index => $_nav_menu ) { - if ( strcmp( $_nav_menu->name, $deleted_nav_menu->name ) >= 0 - || $index == count( $_nav_menus ) - 1 ) { - $nav_menu_selected_id = $_nav_menu->term_id; - break; - } - } - } - unset( $delete_nav_menu, $deleted_nav_menu, $_nav_menus ); + $deletion = wp_delete_nav_menu( $nav_menu_selected_id ); } else { - // Reset the selected menu + // Reset the selected menu. $nav_menu_selected_id = 0; unset( $_REQUEST['menu'] ); } + + if ( ! isset( $deletion ) ) + break; + + if ( is_wp_error( $deletion ) ) + $messages[] = '

' . $deletion->get_error_message() . '

'; + else + $messages[] = '

' . __( 'The menu has been successfully deleted.' ) . '

'; + break; + + case 'delete_menus': + check_admin_referer( 'nav_menus_bulk_actions' ); + foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) { + if ( ! is_nav_menu( $menu_id_to_delete ) ) + continue; + + $deletion = wp_delete_nav_menu( $menu_id_to_delete ); + if ( is_wp_error( $deletion ) ) { + $messages[] = '

' . $deletion->get_error_message() . '

'; + $deletion_error = true; + } + } + + if ( empty( $deletion_error ) ) + $messages[] = '

' . __( 'Selected menus have been successfully deleted.' ) . '

'; break; case 'update': check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); - // Update menu theme locations - if ( isset( $_POST['menu-locations'] ) ) - set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) ); + // Remove menu locations that have been unchecked. + foreach ( $locations as $location => $description ) { + if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id ) + unset( $menu_locations[ $location ] ); + } + + // Merge new and existing menu locations if any new ones are set. + if ( isset( $_POST['menu-locations'] ) ) { + $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] ); + $menu_locations = array_merge( $menu_locations, $new_menu_locations ); + } + + // Set menu locations. + set_theme_mod( 'nav_menu_locations', $menu_locations ); - // Add Menu + // Add Menu. if ( 0 == $nav_menu_selected_id ) { $new_menu_title = trim( esc_html( $_POST['menu-name'] ) ); @@ -288,25 +307,48 @@ switch ( $action ) { $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) ); if ( is_wp_error( $_nav_menu_selected_id ) ) { - $messages[] = '

' . $_nav_menu_selected_id->get_error_message() . '

'; + $messages[] = '

' . $_nav_menu_selected_id->get_error_message() . '

'; } else { $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id ); $nav_menu_selected_id = $_nav_menu_selected_id; $nav_menu_selected_title = $_menu_object->name; - $messages[] = '

' . sprintf( __('The %s menu has been successfully created.'), $nav_menu_selected_title ) . '

'; + if ( isset( $_REQUEST['menu-item'] ) ) + wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) ); + if ( isset( $_REQUEST['zero-menu-state'] ) ) { + // If there are menu items, add them + wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ); + // Auto-save nav_menu_locations + $locations = get_nav_menu_locations(); + foreach ( $locations as $location => $menu_id ) { + $locations[ $location ] = $nav_menu_selected_id; + break; // There should only be 1 + } + set_theme_mod( 'nav_menu_locations', $locations ); + } + if ( isset( $_REQUEST['use-location'] ) ) { + $locations = get_registered_nav_menus(); + $menu_locations = get_nav_menu_locations(); + if ( isset( $locations[ $_REQUEST['use-location'] ] ) ) + $menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id; + set_theme_mod( 'nav_menu_locations', $menu_locations ); + } + + // $messages[] = '

' . sprintf( __( '%s has been created.' ), $nav_menu_selected_title ) . '

'; + wp_redirect( admin_url( 'nav-menus.php?menu=' . $_nav_menu_selected_id ) ); + exit(); } } else { - $messages[] = '

' . __('Please enter a valid menu name.') . '

'; + $messages[] = '

' . __( 'Please enter a valid menu name.' ) . '

'; } - // update existing menu + // Update existing menu. } else { $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id ); $menu_title = trim( esc_html( $_POST['menu-name'] ) ); if ( ! $menu_title ) { - $messages[] = '

' . __('Please enter a valid menu name.') . '

'; + $messages[] = '

' . __( 'Please enter a valid menu name.' ) . '

'; $menu_title = $_menu_object->name; } @@ -314,101 +356,111 @@ switch ( $action ) { $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) ); if ( is_wp_error( $_nav_menu_selected_id ) ) { $_menu_object = $_nav_menu_selected_id; - $messages[] = '

' . $_nav_menu_selected_id->get_error_message() . '

'; + $messages[] = '

' . $_nav_menu_selected_id->get_error_message() . '

'; } else { $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id ); $nav_menu_selected_title = $_menu_object->name; } } - // Update menu items - + // Update menu items. if ( ! is_wp_error( $_menu_object ) ) { - $unsorted_menu_items = wp_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' ); - wp_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(); - foreach ( $post_fields as $field ) - $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : ''; - - $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args ); - - if ( is_wp_error( $menu_item_db_id ) ) - $messages[] = '

' . $menu_item_db_id->get_error_message() . '

'; - elseif ( isset( $menu_items[$menu_item_db_id] ) ) - unset( $menu_items[$menu_item_db_id] ); - } - } + $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) ); - // Remove menu items from the menu that weren't in $_POST - if ( ! empty( $menu_items ) ) { - foreach ( array_keys( $menu_items ) as $menu_item_id ) { - if ( is_nav_menu_item( $menu_item_id ) ) { - wp_delete_post( $menu_item_id ); - } - } + // If the menu ID changed, redirect to the new URL. + if ( $nav_menu_selected_id != $_nav_menu_selected_id ) { + wp_redirect( admin_url( 'nav-menus.php?menu=' . intval( $_nav_menu_selected_id ) ) ); + exit(); } + } + } + break; + case 'locations': + if ( ! $num_locations ) { + wp_redirect( admin_url( 'nav-menus.php' ) ); + exit(); + } - // Store 'auto-add' pages. - $auto_add = ! empty( $_POST['auto-add-pages'] ); - $nav_menu_option = (array) get_option( 'nav_menu_options' ); - if ( ! isset( $nav_menu_option['auto_add'] ) ) - $nav_menu_option['auto_add'] = array(); - if ( $auto_add ) { - if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) - $nav_menu_option['auto_add'][] = $nav_menu_selected_id; - } else { - if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) ) - unset( $nav_menu_option['auto_add'][$key] ); - } - // Remove nonexistent/deleted menus - $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) ); - update_option( 'nav_menu_options', $nav_menu_option ); + add_filter( 'screen_options_show_screen', '__return_false' ); - wp_defer_term_counting(false); + if ( isset( $_POST['menu-locations'] ) ) { + check_admin_referer( 'save-menu-locations' ); - do_action( 'wp_update_nav_menu', $nav_menu_selected_id ); + $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] ); + $menu_locations = array_merge( $menu_locations, $new_menu_locations ); + // Set menu locations + set_theme_mod( 'nav_menu_locations', $menu_locations ); - $messages[] = '

' . sprintf( __('The %s menu has been updated.'), $nav_menu_selected_title ) . '

'; - unset( $menu_items, $unsorted_menu_items ); - } + $messages[] = '

' . __( 'Menu locations updated.' ) . '

'; } break; } -// Get all nav menus -$nav_menus = wp_get_nav_menus( array('orderby' => 'name') ); +// Get all nav menus. +$nav_menus = wp_get_nav_menus(); +$menu_count = count( $nav_menus ); -// Get recently edited nav menu -$recently_edited = (int) get_user_option( 'nav_menu_recently_edited' ); +// Are we on the add new screen? +$add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false; -// 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 ) ) { +$locations_screen = ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) ? true : false; + +/* + * If we have one theme location, and zero menus, we take them right + * into editing their first menu. + */ +$page_count = wp_count_posts( 'page' ); +$one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false; + +$nav_menus_l10n = array( + 'oneThemeLocationNoMenus' => $one_theme_location_no_menus, + 'moveUp' => __( 'Move up one' ), + 'moveDown' => __( 'Move down one' ), + 'moveToTop' => __( 'Move to the top' ), + /* translators: %s: previous item name */ + 'moveUnder' => __( 'Move under %s' ), + /* translators: %s: previous item name */ + 'moveOutFrom' => __( 'Move out from under %s' ), + /* translators: %s: previous item name */ + 'under' => __( 'Under %s' ), + /* translators: %s: previous item name */ + 'outFrom' => __( 'Out from under %s' ), + /* translators: 1: item name, 2: item position, 3: total number of items */ + 'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ), + /* translators: 1: item name, 2: item position, 3: parent item name */ + 'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ), +); +wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n ); + +/* + * Redirect to add screen if there are no menus and this users has either zero, + * or more than 1 theme locations. + */ +if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) + wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) ); + +// Get recently edited nav menu. +$recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) ); +if ( empty( $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 and not requesting that we create a new menu, but $recently_edited is a menu, grab that one. -} elseif ( 0 == $nav_menu_selected_id && ! isset( $_REQUEST['menu'] ) && is_nav_menu( $recently_edited ) ) { +// Use $recently_edited if none are selected. +if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && 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 && ! isset( $_REQUEST['menu'] ) && ! empty($nav_menus) ) { +// On deletion of menu, if another menu exists, show it. +if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] ) + $nav_menu_selected_id = $nav_menus[0]->term_id; + +// Set $nav_menu_selected_id to 0 if no menus. +if ( $one_theme_location_no_menus ) { + $nav_menu_selected_id = 0; +} elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) { + // if we have no selection yet, and we have menus, set to the first one in the list. $nav_menu_selected_id = $nav_menus[0]->term_id; } -// Update the user's setting +// 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 ); @@ -418,184 +470,384 @@ if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) { $nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : ''; } -// Generate truncated menu names -foreach( (array) $nav_menus as $key => $_nav_menu ) { - $_nav_menu->truncated_name = trim( wp_html_excerpt( $_nav_menu->name, 40 ) ); - if ( $_nav_menu->truncated_name != $_nav_menu->name ) - $_nav_menu->truncated_name .= '…'; +// Generate truncated menu names. +foreach ( (array) $nav_menus as $key => $_nav_menu ) { + $nav_menus[$key]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '…' ); +} - $nav_menus[$key]->truncated_name = $_nav_menu->truncated_name; +// Retrieve menu locations. +if ( current_theme_supports( 'menus' ) ) { + $locations = get_registered_nav_menus(); + $menu_locations = get_nav_menu_locations(); } -// Ensure the user will be able to scroll horizontally -// by adding a class for the max menu depth. +/* + * Ensure the user will be able to scroll horizontally + * by adding a class for the max menu depth. + * + * @global int $_wp_nav_menu_max_depth + */ global $_wp_nav_menu_max_depth; $_wp_nav_menu_max_depth = 0; -// Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth -if ( is_nav_menu( $nav_menu_selected_id ) ) - $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id ); +// Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth. +if ( is_nav_menu( $nav_menu_selected_id ) ) { + $menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) ); + $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id ); +} -function wp_nav_menu_max_depth() { +/** + * + * @global int $_wp_nav_menu_max_depth + * + * @param string $classes + * @return string + */ +function wp_nav_menu_max_depth( $classes ) { global $_wp_nav_menu_max_depth; - return "menu-max-depth-$_wp_nav_menu_max_depth"; + return "$classes menu-max-depth-$_wp_nav_menu_max_depth"; } -add_action('admin_body_class','wp_nav_menu_max_depth'); +add_filter('admin_body_class', 'wp_nav_menu_max_depth'); wp_nav_menu_setup(); wp_initial_nav_menu_meta_boxes(); -if ( ! current_theme_supports( 'menus' ) && ! wp_get_nav_menus() ) - $messages[] = '

' . __('The current theme does not natively support menus, but you can use the “Custom Menu” widget to add any menus you create here to the theme’s sidebar.') . '

'; - -$help = '

' . __('This feature is new in version 3.0; to use a custom menu in place of your theme’s default menus, support for this feature must be registered in the theme’s functions.php file. If your theme does not support the custom menus feature yet (the new default theme, Twenty Ten, does), you can learn about adding support yourself by following the below link.') . '

'; -$help .= '

' . __('You can create custom menus for your site. These menus may contain links to pages, categories, custom links or other content types (use the Screen Options tab to decide which ones to show on the screen). You can specify a different navigation label for a menu item as well as other attributes. You can create multiple menus. If your theme includes more than one menu, you can choose which custom menu to associate with each. You can also use custom menus in conjunction with the Custom Menus widget.') . '

'; -$help .= '

' . __('To create a new custom menu, click on the + tab, give the menu a name, and click Create Menu. Next, add menu items from the appropriate boxes. You’ll be able to edit the information for each menu item, and can drag and drop to put them in order. You can also drag a menu item a little to the right to make it a submenu, to create menus with hierarchy. You’ll see when the position of the drop target shifts over to create the nested placement. Don’t forget to click Save when you’re finished.') . '

'; -$help .= '

' . __('For more information:') . '

'; -$help .= '

' . __('Menus Documentation') . '

'; -$help .= '

' . __('Support Forums') . '

'; - -add_contextual_help($current_screen, $help); - -// Get the admin header -require_once( 'admin-header.php' ); +if ( ! current_theme_supports( 'menus' ) && ! $num_locations ) + $messages[] = '

' . sprintf( __( 'Your theme does not natively support menus, but you can use them in sidebars by adding a “Custom Menu” widget on the Widgets screen.' ), admin_url( 'widgets.php' ) ) . '

'; + +if ( ! $locations_screen ) : // Main tab + $overview = '

' . __( 'This screen is used for managing your custom navigation menus.' ) . '

'; + /* translators: 1: Widgets admin screen URL, 2 and 3: The name of the default themes */ + $overview .= '

' . sprintf( __( 'Menus can be displayed in locations defined by your theme, even used in sidebars by adding a “Custom Menu” widget on the Widgets 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 Sixteen', 'Twenty Seventeen' ) . '

'; + $overview .= '

' . __( 'From this screen you can:' ) . '

'; + $overview .= ''; + + get_current_screen()->add_help_tab( array( + 'id' => 'overview', + 'title' => __( 'Overview' ), + 'content' => $overview + ) ); + + $menu_management = '

' . __( 'The menu management box at the top of the screen is used to control which menu is opened in the editor below.' ) . '

'; + $menu_management .= ''; + $menu_management .= '

' . __( 'You can assign theme locations to individual menus by selecting the desired settings at the bottom of the menu editor. To assign menus to all theme locations at once, visit the Manage Locations tab at the top of the screen.' ) . '

'; + + get_current_screen()->add_help_tab( array( + 'id' => 'menu-management', + 'title' => __( 'Menu Management' ), + 'content' => $menu_management + ) ); + + $editing_menus = '

' . __( '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.' ) . '

'; + $editing_menus .= '

' . __( 'Clicking the arrow to the right of any menu item 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.' ) . '

'; + $editing_menus .= ''; + + get_current_screen()->add_help_tab( array( + 'id' => 'editing-menus', + 'title' => __( 'Editing Menus' ), + 'content' => $editing_menus + ) ); +else : // Locations Tab. + $locations_overview = '

' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '

'; + $locations_overview .= ''; + + get_current_screen()->add_help_tab( array( + 'id' => 'locations-overview', + 'title' => __( 'Overview' ), + 'content' => $locations_overview + ) ); +endif; + +get_current_screen()->set_help_sidebar( + '

' . __('For more information:') . '

' . + '

' . __('Documentation on Menus') . '

' . + '

' . __('Support Forums') . '

' +); + +// Get the admin header. +require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
- -

+

+ 'menu_locations' ) : array( 'panel' => 'nav_menus' ); + printf( + ' %2$s', + esc_url( add_query_arg( array( + array( 'autofocus' => $focus ), + 'return' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), + ), admin_url( 'customize.php' ) ) ), + __( 'Manage with Live Preview' ) + ); + endif; + ?> +

+ - - - - +