mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-22 17:18:32 +01:00
Some nav menu API. see #11817
git-svn-id: http://svn.automattic.com/wordpress/trunk@13344 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
dd8dbfb6fe
commit
5fa6e0a242
@ -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 = '<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>';
|
||||
$menu = wp_create_nav_menu($insert_menu_name);
|
||||
if ( is_wp_error($menu) ) {
|
||||
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . esc_html( $menu->get_error_message() ) . '</p></div>';
|
||||
} 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 = '<div id="message" class="updated fade below-h2"><p>' . esc_html( sprintf( __('"%s" menu has been created!'), $term->name ) ) . '</p></div>';
|
||||
|
||||
$postCounter = 0;
|
||||
}
|
||||
$custom_menus[$menu->term_id] = $menu;
|
||||
$menu_selected_id = $menu->term_id;
|
||||
$menu_id_in_edit = $menu_selected_id;
|
||||
$messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . esc_html( sprintf( __('"%s" menu has been created!'), $menu->name ) ) . '</p></div>';
|
||||
$postCounter = 0;
|
||||
}
|
||||
} else {
|
||||
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . __('Please enter a valid menu name.') . '</p></div>';
|
||||
@ -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 = '<div id="message" class="updated fade below-h2"><p>' . __('The menu has been reset.') . '</p></div>';
|
||||
@ -110,8 +105,7 @@ if ( isset($_POST['reset_wp_menu']) ) {
|
||||
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . __('The menu could not be reset. Please try again.') . '</p></div>';
|
||||
}
|
||||
} 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++ ) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user