Make WordPress Core


Ignore:
Timestamp:
02/16/2013 04:53:59 AM (11 years ago)
Author:
markjaquith
Message:

Improve the UX of the Nav Menus screen. Kill the tabs, and change to a
dropdown, unless you have zero or one menus (which is the most common),
in which case you jump right into editing your sole menu.

Do assignment to location using checkboxes in the main menu editing
section instead of the backwards menu => location assignment in a
random meta box.

More to come, but this gets us started.

props lessbloat, DrewAPicture, jkudish. see #23119

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/nav-menu.php

    r22396 r23441  
    8282        $title = empty( $item->label ) ? $title : $item->label;
    8383
     84        $submenu_text = '';
     85        if ( 0 == $depth )
     86            $submenu_text = 'style="display: none;"';
     87
    8488        ?>
    8589        <li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
    8690            <dl class="menu-item-bar">
    8791                <dt class="menu-item-handle">
    88                     <span class="item-title"><?php echo esc_html( $title ); ?></span>
     92                    <span class="item-title"><?php echo esc_html( $title ); ?> <span class="is-submenu" <?php echo $submenu_text; ?>><?php _e( 'sub item' ); ?></span></span>
    8993                    <span class="item-controls">
    9094                        <span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
     
    183187                                'menu-item' => $item_id,
    184188                            ),
    185                             remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
     189                            admin_url( 'nav-menus.php' )
    186190                        ),
    187191                        'delete-menu_item_' . $item_id
    188                     ); ?>"><?php _e('Remove'); ?></a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array('edit-menu-item' => $item_id, 'cancel' => time()), remove_query_arg( $removed_args, admin_url( 'nav-menus.php' ) ) ) );
     192                    ); ?>"><?php _e( 'Remove' ); ?></a> <span class="meta-sep hide-if-no-js"> | </span> <a class="item-cancel submitcancel hide-if-no-js" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array( 'edit-menu-item' => $item_id, 'cancel' => time() ), admin_url( 'nav-menus.php' ) ) );
    189193                        ?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
    190194                </div>
     
    383387function wp_nav_menu_setup() {
    384388    // Register meta boxes
    385     if ( wp_get_nav_menus() )
    386         add_meta_box( 'nav-menu-theme-locations', __( 'Theme Locations' ), 'wp_nav_menu_locations_meta_box' , 'nav-menus', 'side', 'default' );
    387     add_meta_box( 'add-custom-links', __('Custom Links'), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
    388389    wp_nav_menu_post_type_meta_boxes();
     390    add_meta_box( 'add-custom-links', __( 'Add Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
    389391    wp_nav_menu_taxonomy_meta_boxes();
    390392
     
    412414        return;
    413415
    414     $initial_meta_boxes = array( 'nav-menu-theme-locations', 'add-custom-links', 'add-page', 'add-category' );
     416    $initial_meta_boxes = array( 'nav-menu-theme-locations', 'add-page', 'add-custom-links', 'add-category' );
    415417    $hidden_meta_boxes = array();
    416418
     
    490492    $num_locations = count( array_keys($locations) );
    491493
    492     echo '<p class="howto">' . 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>';
     494    echo '<p class="howto">' . _n( 'Select a menu to use within your theme.', 'Select the menus you will use in your theme.', $num_locations ) . '</p>';
    493495
    494496    foreach ( $locations as $location => $description ) {
     
    513515    ?>
    514516    <p class="button-controls">
    515         <?php submit_button( __( 'Save' ), 'primary right', 'nav-menu-locations', false, disabled( $nav_menu_selected_id, 0, false ) ); ?>
     517        <?php submit_button( __( 'Save' ), 'primary right', 'nav-menu-locations', false, wp_nav_menu_disabled_check( $nav_menu_selected_id ) ); ?>
    516518        <span class="spinner"></span>
    517519    </p>
    518520    <?php
     521}
     522
     523/**
     524 * Check whether to disable the Menu Locations meta box submit button
     525 *
     526 * @since 3.6.0
     527 *
     528 * @uses global $one_theme_location_no_menus to determine if no menus exist
     529 * @uses disabled() to output the disabled attribute in $other_attributes param in submit_button()
     530 *
     531 * @param int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
     532 * @return string Disabled attribute if at least one menu exists, false if not
     533*/
     534function wp_nav_menu_disabled_check( $nav_menu_selected_id ) {
     535    global $one_theme_location_no_menus;
     536
     537    if ( $one_theme_location_no_menus )
     538        return false;
     539
     540    return disabled( $nav_menu_selected_id, 0 );
    519541}
    520542
     
    555577            <p id="menu-item-name-wrap">
    556578                <label class="howto" for="custom-menu-item-name">
    557                     <span><?php _e('Label'); ?></span>
     579                    <span><?php _e( 'Link Text' ); ?></span>
    558580                    <input id="custom-menu-item-name" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text" class="regular-text menu-item-textbox input-with-default-title" title="<?php esc_attr_e('Menu Item'); ?>" />
    559581                </label>
     
    562584        <p class="button-controls">
    563585            <span class="add-to-menu">
    564                 <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
     586                <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
    565587                <span class="spinner"></span>
    566588            </span>
     
    769791            <span class="list-controls">
    770792                <a href="<?php
    771                     echo esc_url(add_query_arg(
     793                    echo esc_url( add_query_arg(
    772794                        array(
    773795                            $post_type_name . '-tab' => 'all',
    774796                            'selectall' => 1,
    775797                        ),
    776                         remove_query_arg($removed_args)
     798                        remove_query_arg( $removed_args )
    777799                    ));
    778800                ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
     
    780802
    781803            <span class="add-to-menu">
    782                 <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-post-type-menu-item" id="submit-posttype-<?php echo $post_type_name; ?>" />
     804                <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( __( 'Add to Menu' ) ); ?>" name="add-post-type-menu-item" id="<?php esc_attr_e( 'submit-posttype-' . $post_type_name ); ?>" />
    783805                <span class="spinner"></span>
    784806            </span>
     
    956978
    957979            <span class="add-to-menu">
    958                 <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-taxonomy-menu-item" id="submit-taxonomy-<?php echo $taxonomy_name; ?>" />
     980                <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( __( 'Add to Menu' ) ); ?>" name="add-taxonomy-menu-item" id="<?php esc_attr_e( 'submit-taxonomy-' . $taxonomy_name ); ?>" />
    959981                <span class="spinner"></span>
    960982            </span>
     
    10871109        $result = '<div id="menu-instructions" class="post-body-plain';
    10881110        $result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">';
    1089         $result .= '<p>' . __('Select menu items (pages, categories, links) from the boxes at left to begin building your custom menu.') . '</p>';
     1111        $result .= '<p>' . __( 'Next, add menu items (i.e. pages, links, categories) from the column on the left.' ) . '</p>';
    10901112        $result .= '</div>';
    10911113
     
    11591181        wp_delete_post( $menu_item_id, true );
    11601182}
    1161 
    11621183add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items');
     1184
     1185/**
     1186 * Delete nav menus from the nav menu management screen
     1187 *
     1188 * @since 3.6.0
     1189 * @access private
     1190 *
     1191 * @param int|string $nav_menu_id The menu to delete (id, slug, or name)
     1192 * @return false if Error, otherwise true
     1193 */
     1194function _wp_delete_nav_menu( $nav_menu_id ) {
     1195
     1196    if ( ! is_nav_menu( $nav_menu_id ) )
     1197        return;
     1198
     1199    $deleted_nav_menu = wp_get_nav_menu_object( $nav_menu_id );
     1200    $delete_nav_menu = wp_delete_nav_menu( $nav_menu_id );
     1201
     1202    if ( is_wp_error( $delete_nav_menu ) )
     1203        return $delete_nav_menu;
     1204
     1205    // Remove this menu from any locations.
     1206    $locations = get_theme_mod( 'nav_menu_locations' );
     1207    foreach ( (array) $locations as $location => $menu_id ) {
     1208        if ( $menu_id == $nav_menu_id )
     1209            $locations[ $location ] = 0;
     1210    }
     1211    set_theme_mod( 'nav_menu_locations', $locations );
     1212    return true;
     1213}
     1214
     1215/**
     1216 * Saves nav menu items
     1217 *
     1218 * @since 3.6.0
     1219 *
     1220 * @uses wp_get_nav_menu_items() to retrieve the nav menu's menu items
     1221 * @uses wp_defer_term_counter() to enable then disable term counting
     1222 *
     1223 * @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu
     1224 * @param string $nav_menu_selected_title Title of the currently-selected menu
     1225 * @return array $messages The menu updated message
     1226 */
     1227function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) {
     1228    $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' ) );
     1229
     1230    $menu_items = array();
     1231    // Index menu items by db ID
     1232    foreach ( $unsorted_menu_items as $_item )
     1233        $menu_items[$_item->db_id] = $_item;
     1234
     1235    $post_fields = array(
     1236        'menu-item-db-id', 'menu-item-object-id', 'menu-item-object',
     1237        'menu-item-parent-id', 'menu-item-position', 'menu-item-type',
     1238        'menu-item-title', 'menu-item-url', 'menu-item-description',
     1239        'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn'
     1240    );
     1241
     1242    wp_defer_term_counting( true );
     1243    // Loop through all the menu items' POST variables
     1244    if ( ! empty( $_POST['menu-item-db-id'] ) ) {
     1245        foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
     1246
     1247            // Menu item title can't be blank
     1248            if ( empty( $_POST['menu-item-title'][$_key] ) )
     1249                continue;
     1250
     1251            $args = array();
     1252            foreach ( $post_fields as $field )
     1253                $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : '';
     1254
     1255            $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
     1256
     1257            if ( is_wp_error( $menu_item_db_id ) )
     1258                $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
     1259            elseif ( isset( $menu_items[$menu_item_db_id] ) )
     1260                unset( $menu_items[$menu_item_db_id] );
     1261        }
     1262    }
     1263
     1264    // Remove menu items from the menu that weren't in $_POST
     1265    if ( ! empty( $menu_items ) ) {
     1266        foreach ( array_keys( $menu_items ) as $menu_item_id ) {
     1267            if ( is_nav_menu_item( $menu_item_id ) ) {
     1268                wp_delete_post( $menu_item_id );
     1269            }
     1270        }
     1271    }
     1272
     1273    // Store 'auto-add' pages.
     1274    $auto_add = ! empty( $_POST['auto-add-pages'] );
     1275    $nav_menu_option = (array) get_option( 'nav_menu_options' );
     1276    if ( ! isset( $nav_menu_option['auto_add'] ) )
     1277        $nav_menu_option['auto_add'] = array();
     1278    if ( $auto_add ) {
     1279        if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) )
     1280            $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
     1281    } else {
     1282        if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) )
     1283            unset( $nav_menu_option['auto_add'][$key] );
     1284    }
     1285    // Remove nonexistent/deleted menus
     1286    $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
     1287    update_option( 'nav_menu_options', $nav_menu_option );
     1288
     1289    wp_defer_term_counting( false );
     1290
     1291    do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
     1292
     1293    $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%1$s</strong> has been updated.' ), $nav_menu_selected_title ) . '</p></div>';
     1294    unset( $menu_items, $unsorted_menu_items );
     1295
     1296    return $messages;
     1297}
Note: See TracChangeset for help on using the changeset viewer.