WordPress.org

Make WordPress Core

Changeset 15104


Ignore:
Timestamp:
06/02/2010 01:43:46 PM (8 years ago)
Author:
ryan
Message:

Nav menu API cleanups. Props filosofo. fixes #13696

File:
1 edited

Legend:

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

    r15091 r15104  
    147147 */
    148148function wp_create_nav_menu( $menu_name ) {
    149     $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' );
    150 
    151     if ( $menu_exists )
    152         return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_name ) ) );
    153 
    154     $menu = wp_insert_term( $menu_name, 'nav_menu' );
    155 
    156     if ( is_wp_error($menu) )
    157         return $menu;
    158 
    159     $result = get_term( $menu['term_id'], 'nav_menu' );
    160 
    161     if ( $result && !is_wp_error($result) )
    162         do_action( 'wp_create_nav_menu', $menu['term_id'] );
    163 
    164     return $result;
     149    return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
    165150}
    166151
     
    198183 * @since 3.0.0
    199184 *
    200  * @param int $menu_id The ID of the menu
     185 * @param int $menu_id The ID of the menu or "0" to create a new menu.
    201186 * @param array $menu_data The array of menu data.
    202187 * @return int|error object The menu's ID or WP_Error object.
     
    206191
    207192    $_menu = wp_get_nav_menu_object( $menu_id );
    208 
    209     // menu doesn't already exist
    210     if ( ! $_menu || is_wp_error( $_menu ) )
    211         $_menu = wp_create_nav_menu( $menu_data['menu-name'] );
    212 
    213     if ( is_wp_error( $_menu ) )
    214         return $_menu;
    215 
    216     if ( ! $_menu || ! isset( $_menu->term_id ) )
    217         return 0;
    218193
    219194    $args = array(
     
    224199    );
    225200
    226     $menu_id = (int) $_menu->term_id;
    227 
    228     // double-check that we're not changing a menu to the name of another
     201    // double-check that we're not going to have one menu take the name of another
    229202    $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
    230203    if (
     
    236209        return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) );
    237210
     211    // menu doesn't already exist, so create a new menu
     212    if ( ! $_menu || is_wp_error( $_menu ) ) {
     213        $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
     214
     215        if ( $menu_exists )
     216            return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) );
     217
     218        $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
     219
     220        if ( is_wp_error( $_menu ) )
     221            return $_menu;
     222
     223        do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
     224
     225        return (int) $_menu['term_id'];
     226    }
     227
     228    if ( ! $_menu || ! isset( $_menu->term_id ) )
     229        return 0;
     230
     231    $menu_id = (int) $_menu->term_id;
     232
    238233    $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
    239234
    240     if ( ! is_wp_error( $update_response ) )
    241         return $menu_id;
    242     else
     235    if ( is_wp_error( $update_response ) )
    243236        return $update_response;
     237
     238    do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
     239    return $menu_id;
    244240}
    245241
Note: See TracChangeset for help on using the changeset viewer.