Make WordPress Core


Ignore:
Timestamp:
05/27/2010 10:22:09 PM (14 years ago)
Author:
nacin
Message:

New pending menu item behavior. props filosofo, see #13579.

File:
1 edited

Legend:

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

    r14971 r15008  
    248248 * @since 3.0.0
    249249 *
    250  * @param int $menu_id The ID of the menu. Required.
     250 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
    251251 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
    252252 * @param array $menu_item_data The menu item's data.
     
    258258
    259259    // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
    260     if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) ) {
     260    if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
    261261        return new WP_Error('update_nav_menu_item_failed', __('The given object ID is not that of a menu item.'));
    262     }
    263262
    264263    $menu = wp_get_nav_menu_object( $menu_id );
    265264
    266     if ( ! $menu || is_wp_error( $menu ) ) {
     265    if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) )
    267266        return $menu;
    268     }
    269 
    270     $menu_items = (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
     267
     268    $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
    271269
    272270    $count = count( $menu_items );
     
    290288
    291289    $args = wp_parse_args( $menu_item_data, $defaults );
    292 
    293     if ( 0 == (int) $args['menu-item-position'] ) {
     290   
     291    if ( 0 == $menu_id ) {
     292        $args['menu-item-position'] = 1;
     293    } elseif ( 0 == (int) $args['menu-item-position'] ) {
    294294        $last_item = array_pop( $menu_items );
    295295        $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : $count;
     
    340340        'post_title' => $args['menu-item-title'],
    341341        'post_type' => 'nav_menu_item',
    342         'tax_input' => array( 'nav_menu' => array( intval( $menu->term_id ) ) ),
    343342    );
     343
     344    if ( 0 != $menu_id )
     345        $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) );
    344346
    345347    // New menu item. Default is draft status
     
    375377        update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
    376378        update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
    377 
    378         // @todo: only save custom link urls.
    379379        update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
     380       
     381        if ( 0 == $menu_id )
     382            update_post_meta( $menu_item_db_id, '_menu_item_orphaned', time() );
     383        else
     384            delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
    380385
    381386        do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
     
    741746}
    742747
     748/**
     749 * Automatically add newly published page objects to menus with that as an option.
     750 *
     751 * @since 3.0.0
     752 * @access private
     753 *
     754 * @param string $new_status The new status of the post object.
     755 * @param string $old_status The old status of the post object.
     756 * @param object $post The post object being transitioned from one status to another.
     757 * @return void
     758 */
    743759function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
    744760    if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
Note: See TracChangeset for help on using the changeset viewer.