Use nav_menu as the taxonomy name and nav_menu_item as the post type. Set object_id in postmeta. see #11817

git-svn-id: http://svn.automattic.com/wordpress/trunk@13284 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-02-21 18:24:41 +00:00
parent 8e74ffb282
commit 41bea20d51
4 changed files with 61 additions and 68 deletions

View File

@ -64,7 +64,7 @@ function wp_custom_navigation() {
$menu_selected_id = 0;
// Default Menu to show
$custom_menus = get_terms( 'menu', array( 'hide_empty' => false ) );
$custom_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
if ( !empty( $custom_menus ) )
$menu_selected_id = $custom_menus[0]->term_id;
@ -94,13 +94,13 @@ function wp_custom_navigation() {
$insert_menu_name = $_POST['add_menu_name'];
if ( $insert_menu_name != '' ) {
$existing_term = get_term_by( 'name', $insert_menu_name, 'menu' );
$existing_term = get_term_by( 'name', $insert_menu_name, 'nav_menu' );
if ( $existing_term ) {
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . $insert_menu_name . ' Menu has already created - please try another name</p></div>';
$messagesdiv = '<div id="message" class="error fade below-h2"><p>' . $existing_term->name . ' Menu has already created - please try another name</p></div>';
} else {
$term = wp_insert_term( $insert_menu_name, 'menu' );
$term = wp_insert_term( $insert_menu_name, 'nav_menu' );
if ( !is_wp_error($term) ) {
$term = get_term($term['term_id'], 'menu');
$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;
@ -127,13 +127,13 @@ function wp_custom_navigation() {
$messagesdiv = '<div id="message" class="error fade below-h2"><p>'.$themename.'s Custom 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, 'menu' );
$menu_objects = get_objects_in_term( $menu_selected_id, 'nav_menu' );
$menu_items = wp_custom_navigation_get_menu_items( $menu_objects );
// Loop through all POST variables
for ( $k = 1; $k <= $postCounter; $k++ ) {
if (isset($_POST['dbid'.$k])) { $db_id = $_POST['dbid'.$k]; } else { $db_id = 0; }
if (isset($_POST['postmenu'.$k])) { $post_id = $_POST['postmenu'.$k]; } else { $post_id = 0; }
if (isset($_POST['postmenu'.$k])) { $object_id = $_POST['postmenu'.$k]; } else { $object_id = 0; }
//@todo implement heirarchy
if (isset($_POST['parent'.$k])) { $parent_id = $_POST['parent'.$k]; } else { $parent_id = 0; }
if (isset($_POST['title'.$k])) { $custom_title = $_POST['title'.$k]; } else { $custom_title = ''; }
@ -146,9 +146,9 @@ function wp_custom_navigation() {
if (isset($_POST['anchortitle'.$k])) { $custom_anchor_title = $_POST['anchortitle'.$k]; } else { $custom_anchor_title = $custom_title; }
if (isset($_POST['newwindow'.$k])) { $new_window = $_POST['newwindow'.$k]; } else { $new_window = 0; }
$post = array( 'post_status' => 'publish', 'post_type' => 'menu_item', 'post_author' => $user_ID,
'ping_status' => 0, 'post_parent' => $post_id, 'menu_order' => $position,
'guid' => $custom_linkurl, 'post_excerpt' => $custom_anchor_title, 'tax_input' => array( 'menu' => $menu_title ),
$post = array( 'post_status' => 'publish', 'post_type' => 'nav_menu_item', 'post_author' => $user_ID,
'ping_status' => 0, 'post_parent' => 0, 'menu_order' => $position,
'guid' => $custom_linkurl, 'post_excerpt' => $custom_anchor_title, 'tax_input' => array( 'nav_menu' => $menu_title ),
'post_content' => $custom_description, 'post_title' => $custom_title );
if ( $new_window )
$post['post_content_filtered'] = '_blank';
@ -157,13 +157,14 @@ function wp_custom_navigation() {
// New menu item
if ( $db_id == 0 ) {
$db_id = $post_id = wp_insert_post( $post );
$db_id = wp_insert_post( $post );
} elseif ( isset( $menu_items[$db_id] ) ) {
$post['ID'] = $db_id;
wp_update_post( $post );
unset( $menu_items[$db_id] );
}
update_post_meta($db_id, 'menu_type', $linktype);
update_post_meta($db_id, 'object_id', $object_id);
}
if ( !empty( $menu_items ) ) {
foreach ( array_keys( $menu_items ) as $menu_id ) {
@ -280,8 +281,8 @@ function wp_custom_navigation() {
<?php
//DISPLAY SELECT OPTIONS
foreach( $custom_menus as $menu ) {
$menu_term = get_term( $menu, 'menu' );
foreach ( $custom_menus as $menu ) {
$menu_term = get_term( $menu, 'nav_menu' );
if ( ( $menu_id_in_edit == $menu->term_id ) || ( $menu_selected_id == $menu->term_id ) )
$selected_option = 'selected="selected"';
else

View File

@ -13,7 +13,7 @@
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' => 'menu_item', 'post_status' => 'publish' );
$args = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish' );
if ( count( $menu_objects ) > 1 )
$args['include'] = implode( ',', $menu_objects );
else
@ -49,15 +49,15 @@ function wp_custom_navigation_setup($override = false) {
if(($nav_version_in_db <> $nav_version) || ($override))
update_option('wp_settings_custom_nav_version',$nav_version);
$custom_menus = get_terms( 'menu', array( 'hide_empty' => false ) );
$custom_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
if ( !empty( $custom_menus ) ) {
foreach( $custom_menus as $menu ) {
$menu_objects = get_objects_in_term( $menu->term_id, 'menu' );
$menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
if ( !empty( $menu_objects ) ) {
foreach( $menu_objects as $item )
wp_delete_post( $item );
}
wp_delete_term( $menu->term_id, 'menu' );
wp_delete_term( $menu->term_id, 'nav_menu' );
}
}
@ -101,35 +101,34 @@ function wp_custom_navigation_output($args = array()) {
extract($args);
}
$menu_objects = get_objects_in_term( $id, 'menu' );
$menu_objects = get_objects_in_term( $id, 'nav_menu' );
$menu_items = wp_custom_navigation_get_menu_items( $menu_objects, 'menu_order' );
//Override for menu descriptions
$advanced_option_descriptions = get_option('wp_settings_custom_nav_advanced_options');
if ($advanced_option_descriptions == 'no')
{
if ( $advanced_option_descriptions == 'no' )
$desc = 2;
}
$queried_id = 0;
global $wp_query;
if ( is_page() )
$queried_id = $wp_query->post->ID;
$queried_id = $wp_query->get_queried_object_id();
elseif ( is_category() )
$queried_id = $wp_query->query_vars['cat'];
//DISPLAY Loop
foreach ($menu_items as $menu_item) {
//PREPARE Menu Data
//Page Menu Item
switch ( $menu_item->post_status ) {
case 'menu-page':
if ($menu_item->guid == '')
$link = get_permalink( $menu_item->post_parent );
$queried_id = $wp_query->get_queried_object_id();
// Display Loop
foreach ( $menu_items as $menu_item ) {
$menu_type = get_post_meta($menu_item->ID, 'menu_type', true);
$object_id = get_post_meta($menu_item->ID, 'object_id', true);
switch ( $menu_type ) {
// Page Menu Item
case 'page':
if ( $menu_item->guid == '' )
$link = get_permalink( $object_id );
else
$link = $menu_item->guid;
if ( $menu_item->post_title == '' )
$title = htmlentities( get_the_title( $menu_item->post_parent ) );
$title = htmlentities( get_the_title( $object_id ) );
else
$title = htmlentities( $menu_item->post_title );
@ -139,28 +138,28 @@ function wp_custom_navigation_output($args = array()) {
$description = htmlentities( $menu_item->post_content );
$target = '';
break;
//Category Menu Item
case 'menu-category':
if ($menu_item->guid == '')
$link = get_category_link( $menu_item->post_parent );
// Category Menu Item
case 'category':
if ( $menu_item->guid == '' )
$link = get_category_link( $object_id );
else
$link = $menu_item->guid;
if ( $menu_item->post_title == '' ) {
$title_raw = get_categories( 'include='.$menu_item->post_parent );
$title_raw = get_categories( array('include' => $object_id) );
$title = htmlentities($title_raw[0]->cat_name);
} else {
$title = htmlentities( $menu_item->post_title );
}
if ( $menu_item->post_content == '' )
$description = htmlentities( strip_tags( category_description( $menu_item->post_parent ) ) );
$description = htmlentities( strip_tags( category_description( $object_id ) ) );
else
$description = htmlentities( $menu_item->post_content );
$target = '';
break;
default:
//Custom Menu Item
// Custom Menu Item
$link = $menu_item->guid;
$title = htmlentities( $menu_item->post_title );
$description = htmlentities( $menu_item->post_content );
@ -192,16 +191,14 @@ function wp_custom_navigation_output($args = array()) {
}
}
*/
//List Items
// List Items
?><li id="menu-<?php echo $menu_item->ID; ?>" value="<?php echo $menu_item->ID; ?>" <?php echo $li_class; ?>><?php
//@todo: update front end to use post data
//FRONTEND Link
if ($type == "frontend")
{
if ( $type == 'frontend' ) {
?><a title="<?php echo $anchor_title; ?>" href="<?php echo $link; ?>" <?php echo $target; ?>><?php echo $before_title.$title.$after_title; ?><?php
if ( $advanced_option_descriptions == 'no' )
{
if ( $advanced_option_descriptions == 'no' ) {
// 2 widget override do NOT display descriptions
// 1 widget override display descriptions
// 0 widget override not set
@ -213,14 +210,11 @@ function wp_custom_navigation_output($args = array()) {
{ }
else
{ }
}
else
{
} else {
// 2 widget override do NOT display descriptions
// 1 widget override display descriptions
// 0 widget override not set
if ($desc == 1)
{
if ( $desc == 1 ) {
?><span class="nav-description"><?php echo $description; ?></span><?php
}
elseif (($desc == 2) || ($desc == 0))
@ -230,11 +224,9 @@ function wp_custom_navigation_output($args = array()) {
}
?></a><?php
}
//BACKEND draggable and droppable elements
elseif ($type == "backend")
$link_type = substr( $menu_item->post_status, 5 );
{
} elseif ( $type == 'backend' ) {
//BACKEND draggable and droppable elements
$link_type = $menu_type;
?>
<dl>

View File

@ -63,14 +63,14 @@ function create_initial_post_types() {
'query_var' => false,
) );
register_post_type( 'menu_item', array( 'public' => false,
'show_ui' => false,
'_builtin' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
) );
register_post_type( 'nav_menu_item', array( 'public' => false,
'show_ui' => false,
'_builtin' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
) );
register_post_status( 'publish', array( 'label' => _x('Published', 'post'),
'public' => true,

View File

@ -38,11 +38,11 @@ function create_initial_taxonomies() {
'_builtin' => true
) ) ;
register_taxonomy( 'menu', 'menu_item', array( 'hierarchical' => false,
'query_var' => false,
'rewrite' => false,
'show_ui' => false,
) ) ;
register_taxonomy( 'nav_menu', 'nav_menu_item', array( 'hierarchical' => false,
'query_var' => false,
'rewrite' => false,
'show_ui' => false,
) ) ;
register_taxonomy( 'link_category', 'link', array( 'hierarchical' => false,
'label' => __('Categories'),