From a4b48f6d6a86f79e7e22a014aae587948d599aab Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 23 Feb 2010 22:06:21 +0000 Subject: [PATCH] Some nav menu API. see #11817 git-svn-id: https://develop.svn.wordpress.org/trunk@13344 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/custom-navigation.php | 28 ++++------ wp-includes/custom-navigation.php | 92 +++++++++++++++++++------------ 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/wp-admin/custom-navigation.php b/wp-admin/custom-navigation.php index 341fef3596..6083884170 100644 --- a/wp-admin/custom-navigation.php +++ b/wp-admin/custom-navigation.php @@ -29,7 +29,7 @@ wp_enqueue_script( 'custom-navigation-php-functions' ); require_once('admin-header.php'); require_once (ABSPATH . WPINC . '/custom-navigation.php'); -function wp_custom_nav_reset() { +function wp_reset_nav_menu() { wp_custom_navigation_setup(true); return true; @@ -77,20 +77,15 @@ if ( isset( $_POST['add_menu'] ) ) { $insert_menu_name = $_POST['add_menu_name']; if ( $insert_menu_name != '' ) { - $existing_term = get_term_by( 'name', $insert_menu_name, 'nav_menu' ); - if ( $existing_term ) { - $messagesdiv = '

' . esc_html( sprintf( e__('A menu named "%s" already exists; please try another name.'), $existing_term->name ) ) . '

'; + $menu = wp_create_nav_menu($insert_menu_name); + if ( is_wp_error($menu) ) { + $messagesdiv = '

' . esc_html( $menu->get_error_message() ) . '

'; } else { - $term = wp_insert_term( $insert_menu_name, 'nav_menu' ); - if ( !is_wp_error($term) ) { - $term = get_term($term['term_id'], 'nav_menu'); - $custom_menus[$term->term_id] = $term; - $menu_selected_id = $term->term_id; - $menu_id_in_edit = $menu_selected_id; - $messagesdiv = '

' . esc_html( sprintf( __('"%s" menu has been created!'), $term->name ) ) . '

'; - - $postCounter = 0; - } + $custom_menus[$menu->term_id] = $menu; + $menu_selected_id = $menu->term_id; + $menu_id_in_edit = $menu_selected_id; + $messagesdiv = '

' . esc_html( sprintf( __('"%s" menu has been created!'), $menu->name ) ) . '

'; + $postCounter = 0; } } else { $messagesdiv = '

' . __('Please enter a valid menu name.') . '

'; @@ -98,7 +93,7 @@ if ( isset( $_POST['add_menu'] ) ) { } if ( isset($_POST['reset_wp_menu']) ) { - $success = wp_custom_nav_reset(); + $success = wp_reset_nav_menu(); if ( $success ) { // DISPLAY SUCCESS MESSAGE IF Menu Reset Correctly $messagesdiv = '

' . __('The menu has been reset.') . '

'; @@ -110,8 +105,7 @@ if ( isset($_POST['reset_wp_menu']) ) { $messagesdiv = '

' . __('The menu could not be reset. Please try again.') . '

'; } } elseif ( $postCounter > 0 && $menu_selected_id > 0 ) { - $menu_objects = get_objects_in_term( $menu_selected_id, 'nav_menu' ); - $menu_items = wp_custom_navigation_get_menu_items( $menu_objects ); + $menu_items = wp_get_nav_menu_items( $menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') ); // Loop through all POST variables for ( $k = 1; $k <= $postCounter; $k++ ) { diff --git a/wp-includes/custom-navigation.php b/wp-includes/custom-navigation.php index 2fa8db24a1..7cb7344c80 100644 --- a/wp-includes/custom-navigation.php +++ b/wp-includes/custom-navigation.php @@ -10,50 +10,71 @@ * @subpackage Administration */ -function wp_custom_navigation_get_menu_items( $menu_objects, $key = 'ID' ) { - $menu_items = array(); - if ( !empty( $menu_objects ) && !empty( $key ) ) { - $args = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish' ); - if ( count( $menu_objects ) > 1 ) - $args['include'] = implode( ',', $menu_objects ); - else - $args['include'] = $menu_objects[0]; - $posts = get_posts( $args ); - if ( ! empty( $posts ) ) { - foreach ( $posts as $post ) { - $menu_items[ $post->$key ] = $post; - } - } - unset( $posts ); - ksort( $menu_items ); - } - return $menu_items; -} - function wp_custom_navigation_setup($override = false) { // Custom Navigation Menu Setup // Override for menu descriptions update_option('wp_settings_custom_nav_advanced_options', 'yes'); - $custom_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) ); - if ( !empty( $custom_menus ) ) { - foreach ( $custom_menus as $menu ) { - wp_custom_navigation_delete_menu( $menu->term_id ); + $menus = wp_get_nav_menus(); + if ( !empty( $menus ) ) { + foreach ( $menus as $menu ) { + wp_delete_nav_menu( $menu->term_id ); } } + + wp_create_nav_menu( __('Main') ); } -function wp_custom_navigation_delete_menu( $menu_term_id ) { - $term_id = (int) $menu_term_id; - if ( $term_id > 0 ) { - $menu_objects = get_objects_in_term( $term_id, 'nav_menu' ); - if ( !empty( $menu_objects ) ) { - foreach ( $menu_objects as $item ) { - wp_delete_post( $item ); - } +function wp_delete_nav_menu( $menu_id ) { + $menu_id = (int) $menu_id; + if ( !$menu_id ) + return false; + + $menu_objects = get_objects_in_term( $menu_id, 'nav_menu' ); + if ( !empty( $menu_objects ) ) { + foreach ( $menu_objects as $item ) { + wp_delete_post( $item ); } - wp_delete_term( $term_id, 'nav_menu' ); + } + wp_delete_term( $menu_id, 'nav_menu' ); +} + +function wp_create_nav_menu( $menu_name ) { + $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' ); + + if ( $menu_exists ) + return WP_Error('menu_exists', sprintf( __('A menu named "%s" already exists; please try another name.'), $menu_exists->name )); + + return wp_insert_term( $menu_name, 'nav_menu' ); +} + +function wp_get_nav_menus() { + return get_terms( 'nav_menu', array( 'hide_empty' => false ) ); +} + +function wp_get_nav_menu_items( $menu, $args = array() ) { + $items = get_objects_in_term( (int) $menu, 'nav_menu' ); + + $defaults = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order' ); + $args = wp_parse_args($args, $defaults); + if ( count( $items ) > 1 ) + $args['include'] = implode( ',', $items ); + else + $args['include'] = $items[0]; + + $items = get_posts( $args ); + + if ( ARRAY_A == $args['output'] ) { + $output = array(); + foreach ( $items as $item ) { + $output[$item->$args['output_key']] = $item; + } + unset($items); + ksort($output); + return $output; + } else { + return $items; } } @@ -263,14 +284,13 @@ function wp_custom_navigation_output( $args = array() ) { $args = wp_parse_args($args, $defaults); extract($args, EXTR_SKIP); - $menu_objects = get_objects_in_term( $id, 'nav_menu' ); - $menu_items = wp_custom_navigation_get_menu_items( $menu_objects, 'menu_order' ); + $menu_items = wp_get_nav_menu_items( $id ); $parent_stack = array(); $current_parent = 0; $parent_menu_order = array(); // Setup parentage - foreach ( $menu_items as $key => $menu_item ) { + foreach ( $menu_items as $menu_item ) { $parent_menu_order[ $menu_item->ID ] = $menu_item->menu_order; }