From 90ea03383fff164a2f8c75a3636fceb7e668029b Mon Sep 17 00:00:00 2001 From: nacin Date: Wed, 28 Apr 2010 20:44:08 +0000 Subject: [PATCH] Fix handling of nav menu search, also menu naming conflicts. props filosofo, fixes #13138 git-svn-id: http://svn.automattic.com/wordpress/trunk@14285 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/nav-menu.php | 12 +++++++ wp-admin/js/nav-menu.dev.js | 12 +++++-- wp-admin/js/nav-menu.js | 2 +- wp-admin/nav-menus.php | 60 +++++++++++++++++++--------------- wp-includes/nav-menu.php | 13 +++++++- wp-includes/script-loader.php | 3 +- 6 files changed, 70 insertions(+), 32 deletions(-) diff --git a/wp-admin/includes/nav-menu.php b/wp-admin/includes/nav-menu.php index c8d9366d5c..0e6a68accf 100644 --- a/wp-admin/includes/nav-menu.php +++ b/wp-admin/includes/nav-menu.php @@ -216,6 +216,8 @@ function _wp_ajax_menu_quick_search( $request = array() ) { 'post_type' => $matches[2], 's' => $query, )); + if ( ! have_posts() ) + echo '-1'; while ( have_posts() ) { the_post(); if ( 'markup' == $response_format ) { @@ -236,6 +238,8 @@ function _wp_ajax_menu_quick_search( $request = array() ) { 'name__like' => $query, 'number' => 10, )); + if ( empty( $terms ) || is_wp_error( $terms ) ) + echo '-1'; foreach( (array) $terms as $term ) { if ( 'markup' == $response_format ) { echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args ); @@ -531,6 +535,10 @@ function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) { $args['walker'] = new Walker_Nav_Menu_Checklist; echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args ); ?> + +
  • get_error_message(); ?>
  • + +
  • @@ -698,6 +706,10 @@ function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) { $args['walker'] = $walker; echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args ); ?> + +
  • get_error_message(); ?>
  • + +
  • diff --git a/wp-admin/js/nav-menu.dev.js b/wp-admin/js/nav-menu.dev.js index df65371f76..cadbf3db0d 100644 --- a/wp-admin/js/nav-menu.dev.js +++ b/wp-admin/js/nav-menu.dev.js @@ -614,6 +614,7 @@ var WPNavMenuHandler = function () { i, items, matched, + message, newID, pattern = new RegExp('menu-item\\[(\[^\\]\]*)'), resultList; @@ -642,12 +643,17 @@ var WPNavMenuHandler = function () { resultList.appendChild(items[0]); } } else if ( req.type ) { - matched = /quick-search-posttype-([a-zA-Z_-]*)/.exec(req.type); - if ( matched && matched[1] ) { - resultList = document.getElementById(matched[1] + '-search-checklist'); + matched = /quick-search-(posttype|taxonomy)-([a-zA-Z_-]*)/.exec(req.type); + if ( matched && matched[2] ) { + resultList = document.getElementById(matched[2] + '-search-checklist'); if ( resultList ) { resultList.innerHTML = ''; i = items.length; + if ( ! i ) { + message = document.createElement('li'); + message.appendChild(document.createTextNode(navMenuL10n.noResultsFound)); + resultList.appendChild(message); + } while( i-- ) { resultList.appendChild(items[i]); } diff --git a/wp-admin/js/nav-menu.js b/wp-admin/js/nav-menu.js index 8c9c3380e6..27fc330181 100644 --- a/wp-admin/js/nav-menu.js +++ b/wp-admin/js/nav-menu.js @@ -1 +1 @@ -var WPNavMenuHandler=function(){var i=jQuery,a=false,k=null,h,d,n,j,m={},o=function(q,u,r,t){if(q&&q[0]){var s=i.parseJSON(q[0]);if(s.post_title){if(s.ID&&s.post_type){m[s.post_title]={ID:s.ID,object_type:s.post_type}}return s.post_title}}},p=function(q,u,r,t){if(q&&q[0]){var s=i.parseJSON(q[0]);if(s.post_title){return s.post_title}}},b=function(w,v){if(!w){return false}v=v||document;var r=["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"],q={},s=v.getElementsByTagName("input"),u=s.length,t,x=document.getElementById("nav-menu-meta-object-id").value;while(u--){t=r.length;while(t--){if(s[u]&&s[u].name&&"menu-item["+w+"]["+r[t]+"]"==s[u].name){q[r[t]]=s[u].value}}}return q},c=function(){var u=this.getElementsByTagName("input"),r=u.length,q,t,s;while(r--){if(-1!=u[r].name.indexOf("menu-item-parent-id["+parseInt(this.id.replace("menu-item-",""),10)+"]")){if(!this.parentNode.className||-1==this.parentNode.className.indexOf("sub-menu")){u[r].value=0}else{if("LI"==this.parentNode.parentNode.nodeName&&-1!=this.parentNode.parentNode.id.indexOf("menu-item-")){t=this.parentNode.parentNode;s=t.getElementsByTagName("input");q=s.length;while(q--){if(s[q].name&&-1!=s[q].name.indexOf("menu-item-object-id["+parseInt(t.id.replace("menu-item-",""),10)+"]")){u[r].value=parseInt(s[q].value,10);break}}}}break}}},e=function(r,t){var s=document.getElementById("nav-menu-meta"),q;while(r.parentNode&&(!r.className||-1==r.className.indexOf(t))&&r.parentNode!=s){r=r.parentNode}return r},f=function(q){var r=this;i(q).droppable({accept:".menu li",tolerance:"pointer",drop:function(t,s){r.eventOnDrop(s.draggable[0],this,s,t)},over:function(t,s){r.eventOnDragOver(s.draggable[0],this,s,t)},out:function(t,s){r.eventOnDragOut(s.draggable[0],this,s,t)}})},l,g=function(t){if(!t){return}var r=document.getElementById(t.id+"-dummy-list-item"),q=t.getElementsByTagName("li"),s=q.length;if(!r){r=document.createElement("li");r.id=t.id+"-dummy-list-item";t.appendChild(r);this.setupListItemDragAndDrop(r)}while(s--){this.setupListItemDragAndDrop(q[s])}};return{init:function(){l=document.getElementById("menu-to-edit");this.attachMenuEditListeners();this.attachMenuMetaListeners(document.getElementById("nav-menu-meta"));this.attachTabsPanelListeners();g.call(this,l);postboxes.add_postbox_toggles("nav-menus")},attachMenuEditListeners:function(){var q=this;i("#update-nav-menu").bind("click",function(r){if(r.target&&r.target.className){if(-1!=r.target.className.indexOf("item-edit")){return q.eventOnClickEditLink(r.target)}else{if(-1!=r.target.className.indexOf("menu-delete")){return q.eventOnClickMenuDelete(r.target)}else{if(-1!=r.target.className.indexOf("item-delete")){return q.eventOnClickMenuItemDelete(r.target)}}}}})},attachMenuMetaListeners:function(q){if(!q){return}var r=this;h=document.getElementById("custom-menu-item-name");d=document.getElementById("custom-menu-item-url");if(h){n="undefined"!=typeof h.defaultValue?h.defaultValue:h.getAttribute("value");j="undefined"!=typeof d.defaultValue?d.defaultValue:d.getAttribute("value");i(h).bind("focus",function(s){this.value=n==this.value?"":this.value});i(h).bind("blur",function(s){this.value=""==this.value?n:this.value})}i("input.quick-search").each(function(s,t){r.setupQuickSearchEventListeners(t)});i(q).bind("submit",function(s){return r.eventSubmitMetaForm.call(r,this,s)})},attachTabsPanelListeners:function(){i("#menu-settings-column").bind("click",function(v){if(v.target&&v.target.className&&-1!=v.target.className.indexOf("menu-tab-link")){var w,r=/#(.*)$/.exec(v.target.href),u,x=e(v.target,"inside"),q=x?x.getElementsByTagName("input"):[],s=q.length;while(s--){q[s].checked=false}i(".tabs-panel",x).each(function(){if(this.className){this.className=this.className.replace("tabs-panel-active","tabs-panel-inactive")}});i(".tabs",x).each(function(){this.className=this.className.replace("tabs","")});v.target.parentNode.className+=" tabs";if(r&&r[1]){w=document.getElementById(r[1]);if(w){w.className=w.className.replace("tabs-panel-inactive","tabs-panel-active")}}return false}else{if(v.target&&v.target.className&&-1!=v.target.className.indexOf("select-all")){var t=/#(.*)$/.exec(v.target.href);if(t&&t[1]){i("#"+t[1]+" .tabs-panel-active input[type=checkbox]").attr("checked","checked");return false}}}})},setupListItemDragAndDrop:function(s){var q=s.getElementsByTagName("dl"),t=this.makeListItemDropzone(s),r=q.length;f.call(this,t);this.makeListItemDraggable(s);while(r--){f.call(this,q[r])}},setupQuickSearchEventListeners:function(q){var r=this;i(q).autocomplete(ajaxurl+"?action=menu-quick-search&type="+q.name,{delay:500,formatItem:o,formatResult:p,minchars:2,multiple:false}).bind("blur",function(u){var s=m[this.value],t=this;if(s){i.post(ajaxurl+"?action=menu-quick-search&type=get-post-item&response-format=markup",s,function(v){r.processQuickSearchQueryResponse.call(r,v,s);m[t.value]=false})}})},eventOnClickEditLink:function(q){var s,r=/#(.*)$/.exec(q.href);if(r&&r[1]){s=document.getElementById(r[1]);if(s){if(-1!=s.className.indexOf("menu-item-edit-inactive")){s.className=s.className.replace("menu-item-edit-inactive","menu-item-edit-active")}else{s.className=s.className.replace("menu-item-edit-active","menu-item-edit-inactive")}return false}}},eventOnClickMenuDelete:function(q){if(confirm(navMenuL10n.warnDeleteMenu)){return true}else{return false}},eventOnClickMenuItemDelete:function(q){var t,s,r=this;if(confirm(navMenuL10n.warnDeleteMenuItem)){s=/_wpnonce=([a-zA-Z0-9]*)$/.exec(q.href);if(s&&s[1]){t=parseInt(q.id.replace("delete-",""),10);i.post(ajaxurl,{action:"delete-menu-item","menu-item":t,_wpnonce:s[1]},function(u){if("1"==u){r.removeMenuItem(document.getElementById("menu-item-"+t))}});return false}return true}else{return false}},eventOnDragOver:function(q,r){a=true;k=r;r.className+=" sortable-placeholder"},eventOnDragOut:function(q,r){a=false;(function(s){setTimeout(function(){if(s!=k||(!a&&s.className&&-1!=s.className.indexOf("sortable-placeholder"))){s.className=s.className.replace(/sortable-placeholder/g,"")}},800)})(r)},eventOnDrop:function(t,w){var u=!!(-1==w.className.indexOf("dropzone")),r=w.parentNode.getElementsByTagName("ul"),v=false,s=r.length,q;a=false;w.className=w.className.replace(/sortable-placeholder/g,"");if(u){while(s--){if(r[s]&&1!=r[s].className.indexOf("sub-menu")){v=true;q=r[s]}}if(!v){q=document.createElement("ul");q.className="sub-menu";w.parentNode.appendChild(q)}q.appendChild(t)}else{w.parentNode.parentNode.insertBefore(t,w.parentNode)}this.recalculateSortOrder(l);c.call(t)},eventSubmitMetaForm:function(r,y){var v,w=r.getElementsByTagName("input"),u=w.length,t,A,q,x,s={},z=function(){},B=new RegExp("menu-item\\[([^\\]]*)");that=this;s.action="";while(u--){if(w[u].name&&-1!=w[u].name.indexOf("menu-item-object-id")&&w[u].checked||("undefined"!=typeof w[u].id&&"custom-menu-item-url"==w[u].id&&""!=w[u].value&&"http://"!=w[u].value)){s.action="add-menu-item";z=that.processAddMenuItemResponse;x=B.exec(w[u].name);q="undefined"==typeof x[1]?0:parseInt(x[1],10);A=b(q);for(t in A){s["menu-item["+q+"]["+t+"]"]=A[t]}v=e(w[u],"inside");w[u].checked=false}else{if(""==s.action&&""!=w[u].value&&w[u].className&&-1!=w[u].className.search(/quick-search\b[^-]/)){v=e(w[u],"inside");s.action="menu-quick-search";s.q=w[u].value;s["response-format"]="markup";s.type=w[u].name;z=that.processQuickSearchQueryResponse}}}if(v){v.className=v.className+" processing",s.menu=r.elements.menu.value}s["menu-settings-column-nonce"]=r.elements["menu-settings-column-nonce"].value;i.post(ajaxurl,s,function(C){z.call(that,C,s);v.className=v.className.replace(/processing/g,"")});return false},makeListItemDraggable:function(q){i(q).draggable({handle:" > 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 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,A,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|taxonomy)-([a-zA-Z_-]*)/.exec(z.type);if(s&&s[2]){t=document.getElementById(s[2]+"-search-checklist");if(t){t.innerHTML="";v=y.length;if(!v){A=document.createElement("li");A.appendChild(document.createTextNode(navMenuL10n.noResultsFound));t.appendChild(A)}while(v--){t.appendChild(y[v])}}}}}},recalculateSortOrder:function(s){var t=s.getElementsByTagName("input"),r,q=0;for(r=0;r $_POST['menu-name'] ) ) ); - $nav_menu_selected_title = $_menu_object->name; + $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $_POST['menu-name'] ) ); + if ( is_wp_error( $_nav_menu_selected_id ) ) { + $_menu_object = $_nav_menu_selected_id; + $messages_div = '

    ' . $_nav_menu_selected_id->get_error_message() . '

    '; + } else { + $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id ); + $nav_menu_selected_title = $_menu_object->name; + } } // Update menu items @@ -229,33 +235,35 @@ switch ( $action ) { $menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') ); // Loop through all the menu items' POST variables - foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) { + if ( ! empty( $_POST['menu-item-db-id'] ) ) { + foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) { - // Menu item title can't be blank - if ( '' == $_POST['menu-item-title'][$_key] ) - continue; - - $args = array( - 'menu-item-db-id' => $_POST['menu-item-db-id'][$_key], - 'menu-item-object-id' => $_POST['menu-item-object-id'][$_key], - 'menu-item-object' => $_POST['menu-item-object'][$_key], - 'menu-item-parent-id' => $_POST['menu-item-parent-id'][$_key], - 'menu-item-position' => $_POST['menu-item-position'][$_key], - 'menu-item-type' => $_POST['menu-item-type'][$_key], - 'menu-item-append' => $_POST['menu-item-append'][$_key], - 'menu-item-title' => $_POST['menu-item-title'][$_key], - 'menu-item-url' => $_POST['menu-item-url'][$_key], - 'menu-item-description' => $_POST['menu-item-description'][$_key], - 'menu-item-attr-title' => $_POST['menu-item-attr-title'][$_key], - 'menu-item-target' => $_POST['menu-item-target'][$_key], - 'menu-item-classes' => $_POST['menu-item-classes'][$_key], - 'menu-item-xfn' => $_POST['menu-item-xfn'][$_key], - ); + // Menu item title can't be blank + if ( '' == $_POST['menu-item-title'][$_key] ) + continue; + + $args = array( + 'menu-item-db-id' => $_POST['menu-item-db-id'][$_key], + 'menu-item-object-id' => $_POST['menu-item-object-id'][$_key], + 'menu-item-object' => $_POST['menu-item-object'][$_key], + 'menu-item-parent-id' => $_POST['menu-item-parent-id'][$_key], + 'menu-item-position' => $_POST['menu-item-position'][$_key], + 'menu-item-type' => $_POST['menu-item-type'][$_key], + 'menu-item-append' => $_POST['menu-item-append'][$_key], + 'menu-item-title' => $_POST['menu-item-title'][$_key], + 'menu-item-url' => $_POST['menu-item-url'][$_key], + 'menu-item-description' => $_POST['menu-item-description'][$_key], + 'menu-item-attr-title' => $_POST['menu-item-attr-title'][$_key], + 'menu-item-target' => $_POST['menu-item-target'][$_key], + 'menu-item-classes' => $_POST['menu-item-classes'][$_key], + 'menu-item-xfn' => $_POST['menu-item-xfn'][$_key], + ); - $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args ); + $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args ); - if ( ! is_wp_error( $menu_item_db_id ) && isset( $menu_items[$menu_item_db_id] ) ) { - unset( $menu_items[$menu_item_db_id] ); + if ( ! is_wp_error( $menu_item_db_id ) && isset( $menu_items[$menu_item_db_id] ) ) { + unset( $menu_items[$menu_item_db_id] ); + } } } diff --git a/wp-includes/nav-menu.php b/wp-includes/nav-menu.php index 5bab5f428b..94548915be 100644 --- a/wp-includes/nav-menu.php +++ b/wp-includes/nav-menu.php @@ -89,7 +89,7 @@ function wp_create_nav_menu( $menu_name, $args = array() ) { $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' ); if ( $menu_exists ) - return new WP_Error( 'menu_exists', sprintf( __('A menu named %s already exists; please try another name.'), esc_html( $menu_exists->name ) ) ); + return new WP_Error( 'menu_exists', sprintf( __('The menu name %s conflicts with another menu name. Please try another.'), esc_html( $menu_name ) ) ); if ( isset($args['slug']) ) $slug = $args['slug']; @@ -172,6 +172,17 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { $menu_id = (int) $_menu->term_id; + // double-check that we're not changing a menu to the name of another + $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); + if ( + $_possible_existing && + ! is_wp_error( $_possible_existing ) && + isset( $_possible_existing->term_id ) && + $_possible_existing->term_id != $menu_id + ) { + return new WP_Error( 'menu_exists', sprintf( __('The menu name %s conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) ); + } + $update_response = wp_update_term( $menu_id, 'nav_menu', $args ); if ( ! is_wp_error( $update_response ) ) diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 05ac945fa6..b7cc1720c6 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -393,11 +393,12 @@ function wp_default_scripts( &$scripts ) { ) ); // Custom Navigation - $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100428' ); + $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100428b' ); $scripts->localize( 'nav-menu', 'navMenuL10n', array( 'custom' => _x('Custom', 'menu nav item type'), 'thickbox' => _x('Edit Menu Item', 'Thickbox Title'), 'edit' => _x('Edit', 'menu item edit text'), + 'noResultsFound' => _x('No results found.', 'search results'), 'warnDeleteMenu' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ), 'warnDeleteMenuItem' => __( "You are about to permanently delete this menu item. \n 'Cancel' to stop, 'OK' to delete." ), ) );