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('admin-header.php');
|
||||||
require_once (ABSPATH . WPINC . '/custom-navigation.php');
|
require_once (ABSPATH . WPINC . '/custom-navigation.php');
|
||||||
|
|
||||||
function wp_custom_nav_reset() {
|
function wp_reset_nav_menu() {
|
||||||
wp_custom_navigation_setup(true);
|
wp_custom_navigation_setup(true);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -77,28 +77,23 @@ if ( isset( $_POST['add_menu'] ) ) {
|
||||||
$insert_menu_name = $_POST['add_menu_name'];
|
$insert_menu_name = $_POST['add_menu_name'];
|
||||||
|
|
||||||
if ( $insert_menu_name != '' ) {
|
if ( $insert_menu_name != '' ) {
|
||||||
$existing_term = get_term_by( 'name', $insert_menu_name, 'nav_menu' );
|
$menu = wp_create_nav_menu($insert_menu_name);
|
||||||
if ( $existing_term ) {
|
if ( is_wp_error($menu) ) {
|
||||||
$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>';
|
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . esc_html( $menu->get_error_message() ) . '</p></div>';
|
||||||
} else {
|
} else {
|
||||||
$term = wp_insert_term( $insert_menu_name, 'nav_menu' );
|
$custom_menus[$menu->term_id] = $menu;
|
||||||
if ( !is_wp_error($term) ) {
|
$menu_selected_id = $menu->term_id;
|
||||||
$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;
|
$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>';
|
$messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . esc_html( sprintf( __('"%s" menu has been created!'), $menu->name ) ) . '</p></div>';
|
||||||
|
|
||||||
$postCounter = 0;
|
$postCounter = 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . __('Please enter a valid menu name.') . '</p></div>';
|
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . __('Please enter a valid menu name.') . '</p></div>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( isset($_POST['reset_wp_menu']) ) {
|
if ( isset($_POST['reset_wp_menu']) ) {
|
||||||
$success = wp_custom_nav_reset();
|
$success = wp_reset_nav_menu();
|
||||||
if ( $success ) {
|
if ( $success ) {
|
||||||
// DISPLAY SUCCESS MESSAGE IF Menu Reset Correctly
|
// DISPLAY SUCCESS MESSAGE IF Menu Reset Correctly
|
||||||
$messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . __('The menu has been reset.') . '</p></div>';
|
$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>';
|
$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 ) {
|
} elseif ( $postCounter > 0 && $menu_selected_id > 0 ) {
|
||||||
$menu_objects = get_objects_in_term( $menu_selected_id, 'nav_menu' );
|
$menu_items = wp_get_nav_menu_items( $menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') );
|
||||||
$menu_items = wp_custom_navigation_get_menu_items( $menu_objects );
|
|
||||||
|
|
||||||
// Loop through all POST variables
|
// Loop through all POST variables
|
||||||
for ( $k = 1; $k <= $postCounter; $k++ ) {
|
for ( $k = 1; $k <= $postCounter; $k++ ) {
|
||||||
|
|
|
@ -10,50 +10,71 @@
|
||||||
* @subpackage Administration
|
* @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) {
|
function wp_custom_navigation_setup($override = false) {
|
||||||
// Custom Navigation Menu Setup
|
// Custom Navigation Menu Setup
|
||||||
|
|
||||||
// Override for menu descriptions
|
// Override for menu descriptions
|
||||||
update_option('wp_settings_custom_nav_advanced_options', 'yes');
|
update_option('wp_settings_custom_nav_advanced_options', 'yes');
|
||||||
|
|
||||||
$custom_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
|
$menus = wp_get_nav_menus();
|
||||||
if ( !empty( $custom_menus ) ) {
|
if ( !empty( $menus ) ) {
|
||||||
foreach ( $custom_menus as $menu ) {
|
foreach ( $menus as $menu ) {
|
||||||
wp_custom_navigation_delete_menu( $menu->term_id );
|
wp_delete_nav_menu( $menu->term_id );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function wp_custom_navigation_delete_menu( $menu_term_id ) {
|
wp_create_nav_menu( __('Main') );
|
||||||
$term_id = (int) $menu_term_id;
|
}
|
||||||
if ( $term_id > 0 ) {
|
|
||||||
$menu_objects = get_objects_in_term( $term_id, 'nav_menu' );
|
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 ) ) {
|
if ( !empty( $menu_objects ) ) {
|
||||||
foreach ( $menu_objects as $item ) {
|
foreach ( $menu_objects as $item ) {
|
||||||
wp_delete_post( $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);
|
$args = wp_parse_args($args, $defaults);
|
||||||
extract($args, EXTR_SKIP);
|
extract($args, EXTR_SKIP);
|
||||||
|
|
||||||
$menu_objects = get_objects_in_term( $id, 'nav_menu' );
|
$menu_items = wp_get_nav_menu_items( $id );
|
||||||
$menu_items = wp_custom_navigation_get_menu_items( $menu_objects, 'menu_order' );
|
|
||||||
|
|
||||||
$parent_stack = array();
|
$parent_stack = array();
|
||||||
$current_parent = 0;
|
$current_parent = 0;
|
||||||
$parent_menu_order = array();
|
$parent_menu_order = array();
|
||||||
// Setup parentage
|
// 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;
|
$parent_menu_order[ $menu_item->ID ] = $menu_item->menu_order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue