From 8c9f71bb57f41afc92e7169b1ac5807fe0870097 Mon Sep 17 00:00:00 2001 From: nacin Date: Wed, 28 Apr 2010 18:30:32 +0000 Subject: [PATCH] More nav menu fixes. props filosofo. see #13148. fixes #13155, fixes #13157, fixes #13138, see #13134. git-svn-id: http://svn.automattic.com/wordpress/trunk@14283 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/nav-menu.php | 85 ++++++++++++++++++++++--------- wp-admin/js/nav-menu.dev.js | 40 +++++++++++---- wp-admin/js/nav-menu.js | 2 +- wp-includes/default-filters.php | 1 + wp-includes/nav-menu-template.php | 10 ++-- wp-includes/nav-menu.php | 63 ++++++++++++++++++++--- wp-includes/script-loader.php | 2 +- 7 files changed, 154 insertions(+), 49 deletions(-) diff --git a/wp-admin/includes/nav-menu.php b/wp-admin/includes/nav-menu.php index a0da91b7e1..c8d9366d5c 100644 --- a/wp-admin/includes/nav-menu.php +++ b/wp-admin/includes/nav-menu.php @@ -305,12 +305,15 @@ function wp_initial_nav_menu_meta_boxes() { function wp_nav_menu_post_type_meta_boxes() { $post_types = get_post_types( array( 'public' => true ), 'object' ); - if ( !$post_types ) + if ( ! $post_types ) return; foreach ( $post_types as $post_type ) { - $id = $post_type->name; - add_meta_box( "add-{$id}", $post_type->label, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', 'default', $post_type ); + $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type ); + if ( $post_type ) { + $id = $post_type->name; + add_meta_box( "add-{$id}", $post_type->label, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', 'default', $post_type ); + } } } @@ -326,8 +329,11 @@ function wp_nav_menu_taxonomy_meta_boxes() { return; foreach ( $taxonomies as $tax ) { - $id = $tax->name; - add_meta_box( "add-{$id}", $tax->label, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax ); + $tax = apply_filters( 'nav_menu_meta_box_object', $tax ); + if ( $tax ) { + $id = $tax->name; + add_meta_box( "add-{$id}", $tax->label, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax ); + } } } @@ -337,8 +343,8 @@ function wp_nav_menu_taxonomy_meta_boxes() { * @since 3.0.0 */ function wp_nav_menu_item_link_meta_box() { - static $_placeholder; - $_placeholder = 0 > $_placeholder ? $_placeholder - 1 : -1; + global $_nav_menu_placeholder; + $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; // @note: hacky query, see #12660 $args = array( 'post_type' => 'nav_menu_item', 'post_status' => 'any', 'meta_key' => '_menu_item_type', 'numberposts' => -1, 'orderby' => 'title', ); @@ -364,39 +370,27 @@ function wp_nav_menu_item_link_meta_box() {
- +
-
- -
-

@@ -433,6 +427,9 @@ function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) { 'suppress_filters' => true, ); + if ( isset( $post_type['args']->_default_query ) ) + $args = array_merge($args, (array) $post_type['args']->_default_query ); + // @todo transient caching of these results with proper invalidation on updating of a post of this type $get_posts = new WP_Query; $posts = $get_posts->query( $args ); @@ -809,6 +806,48 @@ function wp_save_nav_menu_item( $menu_id = 0, $menu_data = array() ) { return $items_saved; } +/** + * Adds custom arguments to some of the meta box object types. + * + * @since 3.0.0 + * + * @access private + * + * @param object $object The post type or taxonomy meta-object. + * @return object The post type of taxonomy object. + */ +function _wp_nav_menu_meta_box_object( $object = null ) { + if ( isset( $object->name ) ) { + // don't show media meta box + if ( 'attachment' == $object->name ) + return false; + + // pages should show most recent + if ( 'page' == $object->name ) { + $object->_default_query = array( + 'orderby' => 'post_date', + 'order' => 'DESC', + 'post_status' => 'publish', + ); + + // posts should show only published items + } elseif ( 'post' == $object->name ) { + $object->_default_query = array( + 'post_status' => 'publish', + ); + + // cats should be in reverse chronological order + } elseif ( 'category' == $object->name ) { + $object->_default_query = array( + 'orderby' => 'id', + 'order' => 'DESC', + ); + } + } + + return $object; +} + /** * Returns the menu item formatted to edit. * @@ -818,8 +857,6 @@ function wp_save_nav_menu_item( $menu_id = 0, $menu_data = array() ) { * @return string|WP_Error $output The menu formatted to edit or error object on failure. */ function wp_get_nav_menu_to_edit( $menu_item_id = 0 ) { - static $_placeholder; - $menu = wp_get_nav_menu_object( $menu_item_id ); // If the menu exists, get its items. diff --git a/wp-admin/js/nav-menu.dev.js b/wp-admin/js/nav-menu.dev.js index 179250ad6d..df65371f76 100644 --- a/wp-admin/js/nav-menu.dev.js +++ b/wp-admin/js/nav-menu.dev.js @@ -159,8 +159,16 @@ var WPNavMenuHandler = function () { if ( ! list ) return; - var menuListItems = list.getElementsByTagName('li'), + var dummyListItem = document.getElementById(list.id + '-dummy-list-item'), + menuListItems = list.getElementsByTagName('li'), i = menuListItems.length; + + if ( ! dummyListItem ) { + dummyListItem = document.createElement('li'); + dummyListItem.id = list.id + '-dummy-list-item'; + list.appendChild(dummyListItem); + this.setupListItemDragAndDrop(dummyListItem); + } while ( i-- ) this.setupListItemDragAndDrop(menuListItems[i]); @@ -234,19 +242,23 @@ var WPNavMenuHandler = function () { attachTabsPanelListeners : function() { $('#menu-settings-column').bind('click', function(e) { if ( e.target && e.target.className && -1 != e.target.className.indexOf('menu-tab-link') ) { - var i = e.target.parentNode, - activePanel, + var activePanel, panelIdMatch = /#(.*)$/.exec(e.target.href), - tabPanels; - while ( ! i.className || -1 == i.className.indexOf('inside') ) { - i = i.parentNode; - } - $('.tabs-panel', i).each(function() { + tabPanels, + wrapper = getParentWrapper(e.target, 'inside'), + inputs = wrapper ? wrapper.getElementsByTagName('input') : [], + i = inputs.length; + + // upon changing tabs, we want to uncheck all checkboxes + while( i-- ) + inputs[i].checked = false; + + $('.tabs-panel', wrapper).each(function() { if ( this.className ) this.className = this.className.replace('tabs-panel-active', 'tabs-panel-inactive'); }); - $('.tabs', i).each(function() { + $('.tabs', wrapper).each(function() { this.className = this.className.replace('tabs', ''); }); @@ -396,7 +408,7 @@ var WPNavMenuHandler = function () { if ( that != currentDropzone || ( ! activeHovering && that.className && -1 != that.className.indexOf('sortable-placeholder') ) ) { that.className = that.className.replace(/sortable-placeholder/g, ''); } - }, 500); + }, 800); })(dropEl); }, @@ -563,6 +575,7 @@ var WPNavMenuHandler = function () { if ( ! req ) req = {}; var dropZone, + dummyListItem = document.getElementById(menuList.id + '-dummy-list-item'), i, listElements, wrap = document.createElement('ul'); @@ -572,9 +585,14 @@ var WPNavMenuHandler = function () { i = listElements.length; while ( i-- ) { this.setupListItemDragAndDrop(listElements[i]); - menuList.appendChild(listElements[i]); + if ( dummyListItem ) + menuList.insertBefore(listElements[i], dummyListItem); + else + menuList.appendChild(listElements[i]); } + this.recalculateSortOrder(menuList); + /* set custom link form back to defaults */ if ( customLinkNameInput && customLinkURLInput ) { customLinkNameInput.value = customLinkNameDefault; diff --git a/wp-admin/js/nav-menu.js b/wp-admin/js/nav-menu.js index aac395830a..8c9c3380e6 100644 --- a/wp-admin/js/nav-menu.js +++ b/wp-admin/js/nav-menu.js @@ -1 +1 @@ -var WPNavMenuHandler=function(){var h=jQuery,a=false,j=null,g,d,m,i,l={},n=function(p,t,q,s){if(p&&p[0]){var r=h.parseJSON(p[0]);if(r.post_title){if(r.ID&&r.post_type){l[r.post_title]={ID:r.ID,object_type:r.post_type}}return r.post_title}}},o=function(p,t,q,s){if(p&&p[0]){var r=h.parseJSON(p[0]);if(r.post_title){return r.post_title}}},b=function(v,u){if(!v){return false}u=u||document;var q=["menu-item-db-id","menu-item-object-id","menu-item-object","menu-item-parent-id","menu-item-position","menu-item-type","menu-item-append","menu-item-title","menu-item-url","menu-item-description","menu-item-attr-title","menu-item-target","menu-item-classes","menu-item-xfn"],p={},r=u.getElementsByTagName("input"),t=r.length,s,w=document.getElementById("nav-menu-meta-object-id").value;while(t--){s=q.length;while(s--){if(r[t]&&r[t].name&&"menu-item["+v+"]["+q[s]+"]"==r[t].name){p[q[s]]=r[t].value}}}return p},c=function(){var t=this.getElementsByTagName("input"),q=t.length,p,s,r;while(q--){if(-1!=t[q].name.indexOf("menu-item-parent-id["+parseInt(this.id.replace("menu-item-",""),10)+"]")){if(!this.parentNode.className||-1==this.parentNode.className.indexOf("sub-menu")){t[q].value=0}else{if("LI"==this.parentNode.parentNode.nodeName&&-1!=this.parentNode.parentNode.id.indexOf("menu-item-")){s=this.parentNode.parentNode;r=s.getElementsByTagName("input");p=r.length;while(p--){if(r[p].name&&-1!=r[p].name.indexOf("menu-item-object-id["+parseInt(s.id.replace("menu-item-",""),10)+"]")){t[q].value=parseInt(r[p].value,10);break}}}}break}}},e=function(p){var q=this;h(p).droppable({accept:".menu li",tolerance:"pointer",drop:function(s,r){q.eventOnDrop(r.draggable[0],this,r,s)},over:function(s,r){q.eventOnDragOver(r.draggable[0],this,r,s)},out:function(s,r){q.eventOnDragOut(r.draggable[0],this,r,s)}})},k,f=function(r){if(!r){return}var p=r.getElementsByTagName("li"),q=p.length;while(q--){this.setupListItemDragAndDrop(p[q])}};return{init:function(){k=document.getElementById("menu-to-edit");this.attachMenuEditListeners();this.attachMenuMetaListeners(document.getElementById("nav-menu-meta"));this.attachTabsPanelListeners();f.call(this,k);postboxes.add_postbox_toggles("nav-menus")},attachMenuEditListeners:function(){var p=this;h("#update-nav-menu").bind("click",function(q){if(q.target&&q.target.className){if(-1!=q.target.className.indexOf("item-edit")){return p.eventOnClickEditLink(q.target)}else{if(-1!=q.target.className.indexOf("menu-delete")){return p.eventOnClickMenuDelete(q.target)}else{if(-1!=q.target.className.indexOf("item-delete")){return p.eventOnClickMenuItemDelete(q.target)}}}}})},attachMenuMetaListeners:function(p){if(!p){return}var q=this;g=document.getElementById("custom-menu-item-name");d=document.getElementById("custom-menu-item-url");if(g){m="undefined"!=typeof g.defaultValue?g.defaultValue:g.getAttribute("value");i="undefined"!=typeof d.defaultValue?d.defaultValue:d.getAttribute("value");h(g).bind("focus",function(r){this.value=m==this.value?"":this.value});h(g).bind("blur",function(r){this.value=""==this.value?m:this.value})}h("input.quick-search").each(function(r,s){q.setupQuickSearchEventListeners(s)});h(p).bind("submit",function(r){return q.eventSubmitMetaForm.call(q,this,r)})},attachTabsPanelListeners:function(){h("#menu-settings-column").bind("click",function(t){if(t.target&&t.target.className&&-1!=t.target.className.indexOf("menu-tab-link")){var q=t.target.parentNode,u,p=/#(.*)$/.exec(t.target.href),s;while(!q.className||-1==q.className.indexOf("inside")){q=q.parentNode}h(".tabs-panel",q).each(function(){if(this.className){this.className=this.className.replace("tabs-panel-active","tabs-panel-inactive")}});h(".tabs",q).each(function(){this.className=this.className.replace("tabs","")});t.target.parentNode.className+=" tabs";if(p&&p[1]){u=document.getElementById(p[1]);if(u){u.className=u.className.replace("tabs-panel-inactive","tabs-panel-active")}}return false}else{if(t.target&&t.target.className&&-1!=t.target.className.indexOf("select-all")){var r=/#(.*)$/.exec(t.target.href);if(r&&r[1]){h("#"+r[1]+" .tabs-panel-active input[type=checkbox]").attr("checked","checked");return false}}}})},setupListItemDragAndDrop:function(r){var p=r.getElementsByTagName("dl"),s=this.makeListItemDropzone(r),q=p.length;e.call(this,s);this.makeListItemDraggable(r);while(q--){e.call(this,p[q])}},setupQuickSearchEventListeners:function(p){var q=this;h(p).autocomplete(ajaxurl+"?action=menu-quick-search&type="+p.name,{delay:500,formatItem:n,formatResult:o,minchars:2,multiple:false}).bind("blur",function(t){var r=l[this.value],s=this;if(r){h.post(ajaxurl+"?action=menu-quick-search&type=get-post-item&response-format=markup",r,function(u){q.processQuickSearchQueryResponse.call(q,u,r);l[s.value]=false})}})},eventOnClickEditLink:function(p){var r,q=/#(.*)$/.exec(p.href);if(q&&q[1]){r=document.getElementById(q[1]);if(r){if(-1!=r.className.indexOf("menu-item-edit-inactive")){r.className=r.className.replace("menu-item-edit-inactive","menu-item-edit-active")}else{r.className=r.className.replace("menu-item-edit-active","menu-item-edit-inactive")}return false}}},eventOnClickMenuDelete:function(p){if(confirm(navMenuL10n.warnDeleteMenu)){return true}else{return false}},eventOnClickMenuItemDelete:function(p){var s,r,q=this;if(confirm(navMenuL10n.warnDeleteMenuItem)){r=/_wpnonce=([a-zA-Z0-9]*)$/.exec(p.href);if(r&&r[1]){s=parseInt(p.id.replace("delete-",""),10);h.post(ajaxurl,{action:"delete-menu-item","menu-item":s,_wpnonce:r[1]},function(t){if("1"==t){q.removeMenuItem(document.getElementById("menu-item-"+s))}});return false}return true}else{return false}},eventOnDragOver:function(p,q){a=true;j=q;q.className+=" sortable-placeholder"},eventOnDragOut:function(p,q){a=false;(function(r){setTimeout(function(){if(r!=j||(!a&&r.className&&-1!=r.className.indexOf("sortable-placeholder"))){r.className=r.className.replace(/sortable-placeholder/g,"")}},500)})(q)},eventOnDrop:function(s,v){var t=!!(-1==v.className.indexOf("dropzone")),q=v.parentNode.getElementsByTagName("ul"),u=false,r=q.length,p;a=false;v.className=v.className.replace(/sortable-placeholder/g,"");if(t){while(r--){if(q[r]&&1!=q[r].className.indexOf("sub-menu")){u=true;p=q[r]}}if(!u){p=document.createElement("ul");p.className="sub-menu";v.parentNode.appendChild(p)}p.appendChild(s)}else{v.parentNode.parentNode.insertBefore(s,v.parentNode)}this.recalculateSortOrder(k);c.call(s)},eventSubmitMetaForm:function(q,w){var u=q.getElementsByTagName("input"),t=u.length,s,y,p,v,r={},x=function(){},z=new RegExp("menu-item\\[([^\\]]*)");q.className=q.className+" processing",that=this;r.action="";while(t--){if(u[t].name&&-1!=u[t].name.indexOf("menu-item-object-id")&&u[t].checked||("undefined"!=typeof u[t].id&&"custom-menu-item-url"==u[t].id&&""!=u[t].value&&"http://"!=u[t].value)){r.action="add-menu-item";x=that.processAddMenuItemResponse;v=z.exec(u[t].name);p="undefined"==typeof v[1]?0:parseInt(v[1],10);y=b(p);for(s in y){r["menu-item["+p+"]["+s+"]"]=y[s]}u[t].checked=false}else{if(""==r.action&&""!=u[t].value&&u[t].className&&-1!=u[t].className.search(/quick-search\b[^-]/)){r.action="menu-quick-search";r.q=u[t].value;r["response-format"]="markup";r.type=u[t].name;x=that.processQuickSearchQueryResponse}}}r.menu=q.elements.menu.value;r["menu-settings-column-nonce"]=q.elements["menu-settings-column-nonce"].value;h.post(ajaxurl,r,function(A){x.call(that,A,r);q.className=q.className.replace(/processing/g,"")});return false},makeListItemDraggable:function(p){h(p).draggable({handle:" > dl",opacity:0.8,addClasses:false,helper:"clone",zIndex:100})},makeListItemDropzone:function(r){if(!r){return false}var q=r.getElementsByTagName("div"),p=q.length,s=document.createElement("div");while(p--){if(q[p].className&&-1!=q[p].className.indexOf("dropzone")&&(r==q[p].parentNode)){return q[p]}}s.className="dropzone";r.insertBefore(s,r.firstChild);return s},processAddMenuItemResponse:function(p,t){if(!t){t={}}var u,r,q,s=document.createElement("ul");s.innerHTML=p;q=s.getElementsByTagName("li");r=q.length;while(r--){this.setupListItemDragAndDrop(q[r]);k.appendChild(q[r])}if(g&&d){g.value=m;d.value=i}},processQuickSearchQueryResponse:function(t,y){if(!y){y={}}var q=document.createElement("ul"),p=document.getElementById("nav-menu-meta"),u,x,r,v,w=new RegExp("menu-item\\[([^\\]]*)"),s;r=w.exec(t);if(r&&r[1]){v=r[1];while(p.elements["menu-item["+v+"][menu-item-type]"]){v--}if(v!=r[1]){t=t.replace(new RegExp("menu-item\\["+r[1]+"\\]","g"),"menu-item["+v+"]")}}q.innerHTML=t;x=q.getElementsByTagName("li");if(x[0]&&y.object_type){s=document.getElementById(y.object_type+"-search-checklist");if(s){s.appendChild(x[0])}}else{if(y.type){r=/quick-search-posttype-([a-zA-Z_-]*)/.exec(y.type);if(r&&r[1]){s=document.getElementById(r[1]+"-search-checklist");if(s){u=x.length;while(u--){s.appendChild(x[u])}}}}}},recalculateSortOrder:function(r){var s=r.getElementsByTagName("input"),q,p=0;for(q=0;q dl",opacity:0.8,addClasses:false,helper:"clone",zIndex:100})},makeListItemDropzone:function(s){if(!s){return false}var r=s.getElementsByTagName("div"),q=r.length,t=document.createElement("div");while(q--){if(r[q].className&&-1!=r[q].className.indexOf("dropzone")&&(s==r[q].parentNode)){return r[q]}}t.className="dropzone";s.insertBefore(t,s.firstChild);return t},processAddMenuItemResponse:function(q,v){if(!v){v={}}var w,r=document.getElementById(l.id+"-dummy-list-item"),t,s,u=document.createElement("ul");u.innerHTML=q;s=u.getElementsByTagName("li");t=s.length;while(t--){this.setupListItemDragAndDrop(s[t]);if(r){l.insertBefore(s[t],r)}else{l.appendChild(s[t])}}this.recalculateSortOrder(l);if(h&&d){h.value=n;d.value=j}},processQuickSearchQueryResponse:function(u,z){if(!z){z={}}var r=document.createElement("ul"),q=document.getElementById("nav-menu-meta"),v,y,s,w,x=new RegExp("menu-item\\[([^\\]]*)"),t;s=x.exec(u);if(s&&s[1]){w=s[1];while(q.elements["menu-item["+w+"][menu-item-type]"]){w--}if(w!=s[1]){u=u.replace(new RegExp("menu-item\\["+s[1]+"\\]","g"),"menu-item["+w+"]")}}r.innerHTML=u;y=r.getElementsByTagName("li");if(y[0]&&z.object_type){t=document.getElementById(z.object_type+"-search-checklist");if(t){t.innerHTML="";t.appendChild(y[0])}}else{if(z.type){s=/quick-search-posttype-([a-zA-Z_-]*)/.exec(z.type);if(s&&s[1]){t=document.getElementById(s[1]+"-search-checklist");if(t){t.innerHTML="";v=y.length;while(v--){t.appendChild(y[v])}}}}}},recalculateSortOrder:function(s){var t=s.getElementsByTagName("input"),r,q=0;for(r=0;r $_placeholder ? $_placeholder - 1 : -1; - $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_placeholder; + global $_nav_menu_placeholder; + + $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1; + $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder; $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0; + $possible_parent_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->post_parent : 0; $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; @@ -147,7 +149,7 @@ class Walker_Nav_Menu_Checklist extends Walker_Nav_Menu { // Menu item hidden fields $output .= ''; $output .= ''; - $output .= ''; + $output .= ''; $output .= ''; $output .= ''; $output .= ''; diff --git a/wp-includes/nav-menu.php b/wp-includes/nav-menu.php index 1ba421a12a..5bab5f428b 100644 --- a/wp-includes/nav-menu.php +++ b/wp-includes/nav-menu.php @@ -50,7 +50,12 @@ function is_nav_menu( $menu ) { $menu_obj = wp_get_nav_menu_object( $menu ); - if ( $menu_obj && ! is_wp_error( $menu_obj ) && ! empty( $menu_obj->term_id ) ) + if ( + $menu_obj && + ! is_wp_error( $menu_obj ) && + ! empty( $menu_obj->taxonomy ) && + 'nav_menu' == $menu_obj->taxonomy + ) return true; return false; @@ -143,7 +148,7 @@ function wp_delete_nav_menu( $menu ) { * * @param int $menu_id The ID of the menu * @param array $menu_data The array of menu data. - * @return int The menu's ID. + * @return int|error object The menu's ID or WP_Error object. */ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { $menu_id = (int) $menu_id; @@ -151,11 +156,13 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { $_menu = wp_get_nav_menu_object( $menu_id ); // menu doesn't already exist - if ( ! $_menu || is_wp_error( $_menu ) ) { + if ( ! $_menu || is_wp_error( $_menu ) ) $_menu = wp_create_nav_menu( $menu_data['menu-name'] ); - } - if ( $_menu && isset( $_menu->term_id ) && ! is_wp_error( $_menu ) ) { + if ( is_wp_error( $_menu ) ) + return $_menu; + + if ( $_menu && isset( $_menu->term_id ) ) { $args = array( 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ), 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ), @@ -167,9 +174,10 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { $update_response = wp_update_term( $menu_id, 'nav_menu', $args ); - if ( ! is_wp_error( $update_response ) ) { + if ( ! is_wp_error( $update_response ) ) return $menu_id; - } + else + return $update_response; } else { return 0; } @@ -237,6 +245,32 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item } } + if ( 'custom' != $args['menu-item-type'] ) { + /* if non-custom menu item, then: + * use original object's URL + * blank default title to sync with original object's + */ + + $args['menu-item-url'] = ''; + + $original_title = ''; + if ( 'taxonomy' == $args['menu-item-type'] ) { + $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); + } elseif ( 'post_type' == $args['menu-item-type'] ) { + $original_object = get_post( $args['menu-item-object-id'] ); + $original_title = $original_object->post_title; + } + + if ( empty( $args['menu-item-title'] ) || $args['menu-item-title'] == $original_title ) { + $args['menu-item-title'] = ''; + + // hack to get wp to create a post object when too many properties are empty + if ( empty( $args['menu-item-description'] ) ) { + $args['menu-item-description'] = ' '; + } + } + } + // Populate the menu item object $post = array( 'menu_order' => $args['menu-item-position'], @@ -363,6 +397,12 @@ function wp_get_nav_menu_items( $menu, $args = array() ) { $items = get_posts( $args ); + if ( is_wp_error( $items ) || ! is_array( $items ) ) { + return false; + } + + $items = array_map( 'wp_setup_nav_menu_item', $items ); + if ( ARRAY_A == $args['output'] ) { $GLOBALS['_menu_item_sort_prop'] = $args['output_key']; usort($items, '_sort_nav_menu_items'); @@ -410,18 +450,25 @@ function wp_setup_nav_menu_item( $menu_item ) { $object = get_post_type_object( $menu_item->object ); $menu_item->append = $object->singular_label; $menu_item->url = get_permalink( $menu_item->object_id ); + + $original_object = get_post( $menu_item->object_id ); + $original_title = $original_object->post_title; + $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; } elseif ( 'taxonomy' == $menu_item->type ) { $object = get_taxonomy( $menu_item->object ); $menu_item->append = $object->singular_label; $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); + $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' ); + $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; + } else { $menu_item->append = __('Custom'); + $menu_item->title = $menu_item->post_title; $menu_item->url = get_post_meta( $menu_item->ID, '_menu_item_url', true ); } - $menu_item->title = $menu_item->post_title; $menu_item->target = get_post_meta( $menu_item->ID, '_menu_item_target', true ); $menu_item->attr_title = strip_tags( $menu_item->post_excerpt ); diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 2e86b775b1..05ac945fa6 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -393,7 +393,7 @@ function wp_default_scripts( &$scripts ) { ) ); // Custom Navigation - $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100426' ); + $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100428' ); $scripts->localize( 'nav-menu', 'navMenuL10n', array( 'custom' => _x('Custom', 'menu nav item type'), 'thickbox' => _x('Edit Menu Item', 'Thickbox Title'),