Make WordPress Core


Ignore:
Timestamp:
03/22/2010 07:56:16 PM (15 years ago)
Author:
nacin
Message:

Various bug fixes and improvements to menu management. props ptahdunbar, see #11817.

File:
1 edited

Legend:

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

    r13739 r13802  
    11<?php
    2 
    3 /* Register Metaboxes */
    4 add_meta_box( 'create-menu', __('Create Menu'), 'wp_nav_menu_create_metabox', 'menus', 'side', 'core' );
    5 add_meta_box( 'add-custom-links', __('Add Custom Links'), 'wp_nav_menu_item_link_metabox', 'menus', 'side', 'default' );
    6 wp_nav_menu_post_type_metaboxes();
    7 wp_nav_menu_taxonomy_metaboxes();
     2/**
     3 * Register nav menu metaboxes
     4 *
     5 * @since 3.0.0
     6 **/
     7function wp_nav_menu_metaboxes_setup() {
     8    add_meta_box( 'add-custom-links', __('Add Custom Links'), 'wp_nav_menu_item_link_metabox', 'nav-menus', 'side', 'default' );
     9    wp_nav_menu_post_type_metaboxes();
     10    wp_nav_menu_taxonomy_metaboxes();
     11}
     12
     13/**
     14 * Limit the amount of meta boxes to just links, pages and cats for first time users.
     15 *
     16 * @since 3.0.0
     17 **/
     18function wp_initial_nav_menu_meta_boxes() {
     19    global $wp_meta_boxes;
     20   
     21    if ( !get_user_option( 'meta-box-hidden_nav-menus' ) && is_array($wp_meta_boxes) ) {
     22       
     23        $initial_meta_boxes = array( 'manage-menu', 'create-menu', 'add-custom-links', 'add-page', 'add-category' );
     24        $hidden_meta_boxes = array();
     25       
     26        foreach ( array_keys($wp_meta_boxes['nav-menus']) as $context ) {
     27            foreach ( array_keys($wp_meta_boxes['nav-menus'][$context]) as $priority ) {
     28                foreach ( $wp_meta_boxes['nav-menus'][$context][$priority] as $box ) {
     29                    if ( in_array( $box['id'], $initial_meta_boxes ) ) {
     30                        unset( $box['id'] );
     31                    } else {
     32                        $hidden_meta_boxes[] = $box['id'];
     33                    }
     34                }
     35            }
     36        }
     37        $user = wp_get_current_user();
     38        update_user_meta( $user->ID, 'meta-box-hidden_nav-menus', $hidden_meta_boxes );
     39       
     40        // returns all the hidden metaboxes to the js function: wpNavMenu.initial_meta_boxes()
     41        return join( ',', $hidden_meta_boxes );
     42    }
     43}
    844
    945/**
     
    1652
    1753    if ( !$post_types )
    18         return false;
    19 
    20     $allowed_types = apply_filters('post_types_allowed_in_menus', array('page'));
     54        return;
     55
    2156    foreach ( $post_types as $post_type ) {
    22         if ( !in_array($post_type->name, $allowed_types) )
    23             continue;
    2457        $id = $post_type->name;
    25 
    26         add_meta_box( "add-{$id}", sprintf( __('Add an Existing %s'), $post_type->singular_label ), 'wp_nav_menu_item_post_type_metabox', 'menus', 'side', 'default', $post_type );
     58        add_meta_box( "add-{$id}", sprintf( __('Add an Existing %s'), $post_type->singular_label ), 'wp_nav_menu_item_post_type_metabox', 'nav-menus', 'side', 'default', $post_type );
    2759    }
    2860}
     
    3769
    3870    if ( !$taxonomies )
    39         return false;
    40 
    41     $allowed_types = apply_filters('taxonomies_allowed_in_menus', array('category'));
     71        return;
     72
    4273    foreach ( $taxonomies as $tax ) {
    43         if ( !in_array($tax->name, $allowed_types) )
    44             continue;
    4574        $id = $tax->name;
    4675
    47         add_meta_box( "add-{$id}", sprintf( __('Add an Existing %s'), $tax->singular_label ), 'wp_nav_menu_item_taxonomy_metabox', 'menus', 'side', 'default', $tax );
     76        add_meta_box( "add-{$id}", sprintf( __('Add an Existing %s'), $tax->singular_label ), 'wp_nav_menu_item_taxonomy_metabox', 'nav-menus', 'side', 'default', $tax );
    4877    }
    4978}
     
    99128 */
    100129function wp_nav_menu_item_link_metabox() {
    101     $args = array( 'post_status' => 'any', 'post_type' => 'nav_menu_item', 'meta_value' => 'custom', 'showposts' => -1 );
     130    // @note: hacky query, see #12660
     131    $args = array( 'post_type' => 'nav_menu_item', 'post_status' => 'any', 'meta_key' => '_menu_item_type', 'numberposts' => -1, 'orderby' => 'title', );
    102132
    103133    // @todo transient caching of these results with proper invalidation on updating links
    104     $query = new WP_Query( $args );
    105 
     134    $links = get_posts( $args );
    106135    ?>
    107136    <p id="menu-item-url-wrap">
     
    111140        </label>
    112141    </p>
    113     <br class="clear" />
    114142    <p id="menu-item-name-wrap">
    115143        <label class="howto" for="custom-menu-item-name">
     
    120148
    121149    <p class="button-controls">
    122         <a class="show-all"><?php _e('View All'); ?></a>
    123         <a class="hide-all"><?php _e('Hide All'); ?></a>
     150        <span class="lists-controls">
     151            <a class="show-all"><?php _e('View All'); ?></a>
     152            <a class="hide-all"><?php _e('Hide All'); ?></a>
     153        </span>
     154       
     155        <span class="add-to-menu">
     156            <a class="button"><?php _e('Add to Menu'); ?></a>
     157        </span>
    124158    </p>
    125159    <div id="available-links" class="list-wrap">
    126160        <div class="list-container">
    127161            <ul class="list">
    128                 <?php echo wp_nav_menu_get_items( $query->posts, 'custom' ); ?>
     162                <?php echo wp_nav_menu_get_items( $links, 'custom', 'custom' ); ?>
    129163            </ul>
    130164        </div><!-- /.list-container-->
    131165    </div><!-- /#available-links-->
    132     <p class="add-to-menu">
    133         <a class="button"><?php _e('Add to Menu'); ?></a>
    134     </p>
    135166    <div class="clear"></div>
    136167    <?php
     
    146177 */
    147178function wp_nav_menu_item_post_type_metabox( $object, $post_type ) {
    148     $args = array( 'post_type' => $post_type['args']->name, 'post_status' => 'publish', 'showposts' => -1 );
    149 
    150     if ( 'attachment' == $post_type['args']->name )
    151         $args['post_status'] = 'any';
     179    $args = array( 'post_type' => $post_type['args']->name, 'numberposts' => -1, 'orderby' => 'title', );
    152180
    153181    // @todo transient caching of these results with proper invalidation on updating of a post of this type
    154     $query = new WP_Query( $args );
    155 
    156     if ( !$query->posts )
     182    $posts = get_posts( $args );
     183
     184    if ( !$posts )
    157185        $error = '<li id="error">'. sprintf( __( 'No %s exists' ), $post_type['args']->label ) .'</li>';
    158186
    159187    $pt_names = '';
    160     if ( is_array($query->posts) ) {
    161         foreach ( $query->posts as $post ) {
     188    if ( is_array($posts) ) {
     189        foreach ( $posts as $post ) {
    162190            if ( $post->post_title ) {
    163191                $pt_names .= htmlentities( $post->post_title ) .'|';
    164             } else {
    165                 $pt_names = sprintf( __('No %s exists'), $post_type['args']->label );
    166192            }
    167193        }
     
    176202
    177203    <p class="button-controls">
    178         <a class="show-all"><?php _e('View All'); ?></a>
    179         <a class="hide-all"><?php _e('Hide All'); ?></a>
     204        <span class="lists-controls">
     205            <a class="show-all"><?php _e('View All'); ?></a>
     206            <a class="hide-all"><?php _e('Hide All'); ?></a>
     207        </span>
     208       
     209        <span class="add-to-menu">
     210            <a class="button"><?php _e('Add to Menu'); ?></a>
     211        </span>
    180212    </p>
    181213
     
    183215        <div class="list-container">
    184216            <ul class="list">
    185                 <?php echo isset( $error ) ? $error : wp_nav_menu_get_items( $query->posts, 'post_type', $id ); ?>
     217                <?php echo isset( $error ) ? $error : wp_nav_menu_get_items( $posts, 'post_type', $id ); ?>
    186218            </ul>
    187219        </div><!-- /.list-container-->
    188220    </div><!-- /#existing-categories-->
    189     <p class="add-to-menu">
    190         <a class="button-secondary"><?php _e('Add to Menu'); ?></a>
    191     </p>
    192221    <input type="hidden" class="autocomplete" name="autocomplete-<?php echo esc_attr( $id ); ?>-names" value="<?php echo esc_js( $pt_names ); ?>" />
    193222    <br class="clear" />
     
    195224        // <![CDATA[
    196225        jQuery(document).ready(function(){
    197             wp_nav_menu_autocomplete('<?php echo esc_attr($id); ?>');
     226            wpNavMenu.autocomplete('<?php echo esc_attr($id); ?>');
    198227        });
    199228        // ]]>
     
    228257            if ( $term->name ) {
    229258                $term_names .= htmlentities( $term->name ) .'|';
    230             } else {
    231                 $term_names = sprintf( __('No %s exists'), $taxonomy['args']->label );
    232259            }
    233260        }
     
    242269
    243270    <p class="button-controls">
    244         <a class="show-all"><?php _e('View All'); ?></a>
    245         <a class="hide-all"><?php _e('Hide All'); ?></a>
     271        <span class="lists-controls">
     272            <a class="show-all"><?php _e('View All'); ?></a>
     273            <a class="hide-all"><?php _e('Hide All'); ?></a>
     274        </span>
     275       
     276        <span class="add-to-menu">
     277            <a class="button"><?php _e('Add to Menu'); ?></a>
     278        </span>
    246279    </p>
    247280
     
    253286        </div><!-- /.list-container-->
    254287    </div><!-- /#existing-categories-->
    255     <p class="add-to-menu">
    256         <a class="button-secondary"><?php _e('Add to Menu'); ?></a>
    257     </p>
    258288    <input type="hidden" class="autocomplete" name="autocomplete-<?php echo esc_attr($id); ?>-names" value="<?php echo esc_js( $term_names ); ?>" />
    259289    <br class="clear" />
     
    261291        // <![CDATA[
    262292        jQuery(document).ready(function(){
    263             wp_nav_menu_autocomplete('<?php echo esc_attr($id); ?>');
     293            wpNavMenu.autocomplete('<?php echo esc_attr($id); ?>');
    264294        });
    265295        // ]]>
     
    291321            $menu_item->post_parent = $menu_item->parent;
    292322
    293         // Cleanest way to get all attachements
    294         if ( 'attachment' == $object )
     323        // Get all attachements and links
     324        if ( in_array($object, array( 'attachment', 'custom' )) )
    295325            $menu_item->post_parent = 0;
    296326
     
    298328            // Set up the menu item
    299329            $menu_item = wp_setup_nav_menu_item( $menu_item, $object_type, $object );
     330           
     331            // No blank titles
     332            if ( empty($menu_item->title) )
     333                continue;
     334           
    300335            $attributes = ( 'backend' == $context ) ? ' id="menu-item-'. $i .'" value="'. $i .'"' : '';
    301 
     336           
    302337            $output .= '<li'. $attributes .'>';
    303338            $output .= wp_get_nav_menu_item( $menu_item, $object_type, $object );
Note: See TracChangeset for help on using the changeset viewer.