WordPress.org

Make WordPress Core

Changeset 13344


Ignore:
Timestamp:
02/23/2010 10:06:21 PM (8 years ago)
Author:
ryan
Message:

Some nav menu API. see #11817

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/custom-navigation.php

    r13342 r13344  
    3030require_once (ABSPATH . WPINC . '/custom-navigation.php');
    3131
    32 function wp_custom_nav_reset() {
     32function wp_reset_nav_menu() {
    3333    wp_custom_navigation_setup(true);
    3434
     
    7878
    7979    if ( $insert_menu_name != '' ) {
    80         $existing_term = get_term_by( 'name', $insert_menu_name, 'nav_menu' );
    81         if ( $existing_term ) {
    82             $messagesdiv = '<div id="message" class="error fade below-h2"><p>' . esc_html( sprintf( e__('A menu named "%s" already exists; please try another name.'), $existing_term->name ) ) . '</p></div>';
     80        $menu = wp_create_nav_menu($insert_menu_name);
     81        if ( is_wp_error($menu) ) {
     82            $messagesdiv = '<div id="message" class="error fade below-h2"><p>' . esc_html( $menu->get_error_message() ) . '</p></div>';
    8383        } else {
    84             $term = wp_insert_term( $insert_menu_name, 'nav_menu' );
    85             if ( !is_wp_error($term) ) {
    86                 $term = get_term($term['term_id'], 'nav_menu');
    87                 $custom_menus[$term->term_id] = $term;
    88                 $menu_selected_id = $term->term_id;
    89                 $menu_id_in_edit = $menu_selected_id;
    90                 $messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . esc_html( sprintf( __('"%s" menu has been created!'), $term->name ) ) . '</p></div>';
    91 
    92                 $postCounter = 0;
    93             }
     84            $custom_menus[$menu->term_id] = $menu;
     85            $menu_selected_id = $menu->term_id;
     86            $menu_id_in_edit = $menu_selected_id;
     87            $messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . esc_html( sprintf( __('"%s" menu has been created!'), $menu->name ) ) . '</p></div>';
     88            $postCounter = 0;
    9489        }
    9590    } else {
     
    9994
    10095if ( isset($_POST['reset_wp_menu']) ) {
    101     $success = wp_custom_nav_reset();
     96    $success = wp_reset_nav_menu();
    10297    if ( $success ) {
    10398        // DISPLAY SUCCESS MESSAGE IF Menu Reset Correctly
     
    111106    }
    112107} elseif ( $postCounter > 0 && $menu_selected_id > 0 ) {
    113     $menu_objects = get_objects_in_term( $menu_selected_id, 'nav_menu' );
    114     $menu_items = wp_custom_navigation_get_menu_items( $menu_objects );
     108    $menu_items = wp_get_nav_menu_items( $menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') );
    115109
    116110    // Loop through all POST variables
  • trunk/wp-includes/custom-navigation.php

    r13338 r13344  
    1111 */
    1212
    13 function wp_custom_navigation_get_menu_items( $menu_objects, $key = 'ID' ) {
    14     $menu_items = array();
    15     if ( !empty( $menu_objects ) && !empty( $key ) ) {
    16         $args = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish' );
    17         if ( count( $menu_objects ) > 1 )
    18             $args['include'] = implode( ',', $menu_objects );
    19         else
    20             $args['include'] = $menu_objects[0];
    21         $posts = get_posts( $args );
    22         if ( ! empty( $posts ) ) {
    23             foreach ( $posts as $post ) {
    24                 $menu_items[ $post->$key ] = $post;
    25             }
    26         }
    27         unset( $posts );
    28         ksort( $menu_items );
    29     }
    30     return $menu_items;
    31 }
    32 
    3313function wp_custom_navigation_setup($override = false) {
    3414    // Custom Navigation Menu Setup
     
    3717    update_option('wp_settings_custom_nav_advanced_options', 'yes');
    3818
    39     $custom_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
    40     if ( !empty( $custom_menus ) ) {
    41         foreach ( $custom_menus as $menu ) {
    42             wp_custom_navigation_delete_menu( $menu->term_id );
    43         }
    44     }
    45 }
    46 
    47 function wp_custom_navigation_delete_menu( $menu_term_id ) {
    48     $term_id = (int) $menu_term_id;
    49     if ( $term_id > 0 ) {
    50         $menu_objects = get_objects_in_term( $term_id, 'nav_menu' );
    51         if ( !empty( $menu_objects ) ) {
    52             foreach ( $menu_objects as $item ) {
    53                 wp_delete_post( $item );
    54             }
    55         }
    56         wp_delete_term( $term_id, 'nav_menu' );
     19    $menus = wp_get_nav_menus();
     20    if ( !empty( $menus ) ) {
     21        foreach ( $menus as $menu ) {
     22            wp_delete_nav_menu( $menu->term_id );
     23        }
     24    }
     25
     26    wp_create_nav_menu( __('Main') );
     27}
     28
     29function wp_delete_nav_menu( $menu_id ) {
     30    $menu_id = (int) $menu_id;
     31    if ( !$menu_id  )
     32        return false;
     33
     34    $menu_objects = get_objects_in_term( $menu_id, 'nav_menu' );
     35    if ( !empty( $menu_objects ) ) {
     36        foreach ( $menu_objects as $item ) {
     37            wp_delete_post( $item );
     38        }
     39    }
     40    wp_delete_term( $menu_id, 'nav_menu' );
     41}
     42
     43function wp_create_nav_menu( $menu_name ) {
     44    $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' );
     45
     46    if ( $menu_exists )
     47        return WP_Error('menu_exists', sprintf( __('A menu named "%s" already exists; please try another name.'), $menu_exists->name ));
     48
     49    return wp_insert_term( $menu_name, 'nav_menu' );
     50}
     51
     52function wp_get_nav_menus() {
     53    return get_terms( 'nav_menu', array( 'hide_empty' => false ) );
     54}
     55
     56function wp_get_nav_menu_items( $menu, $args = array() ) {
     57    $items = get_objects_in_term( (int) $menu, 'nav_menu' );
     58
     59    $defaults = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order' );
     60    $args = wp_parse_args($args, $defaults);
     61    if ( count( $items ) > 1 ) 
     62        $args['include'] = implode( ',', $items );
     63    else
     64        $args['include'] = $items[0];
     65
     66    $items = get_posts( $args );
     67
     68    if ( ARRAY_A == $args['output'] ) {
     69        $output = array();
     70        foreach ( $items as $item ) {
     71            $output[$item->$args['output_key']] = $item;
     72        }
     73        unset($items);
     74        ksort($output);
     75        return $output;
     76    } else {
     77        return $items;
    5778    }
    5879}
     
    264285        extract($args, EXTR_SKIP);
    265286
    266         $menu_objects = get_objects_in_term( $id, 'nav_menu' );
    267         $menu_items = wp_custom_navigation_get_menu_items( $menu_objects, 'menu_order' );
     287        $menu_items = wp_get_nav_menu_items( $id );
    268288
    269289        $parent_stack = array();
     
    271291        $parent_menu_order = array();
    272292        // Setup parentage
    273         foreach ( $menu_items as $key => $menu_item ) {
     293        foreach ( $menu_items as $menu_item ) {
    274294            $parent_menu_order[ $menu_item->ID ] = $menu_item->menu_order;
    275295        }
Note: See TracChangeset for help on using the changeset viewer.