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
This commit is contained in:
nacin 2010-04-28 20:44:08 +00:00
parent e94bc6979e
commit 90ea03383f
6 changed files with 70 additions and 32 deletions

View File

@ -216,6 +216,8 @@ function _wp_ajax_menu_quick_search( $request = array() ) {
'post_type' => $matches[2], 'post_type' => $matches[2],
's' => $query, 's' => $query,
)); ));
if ( ! have_posts() )
echo '-1';
while ( have_posts() ) { while ( have_posts() ) {
the_post(); the_post();
if ( 'markup' == $response_format ) { if ( 'markup' == $response_format ) {
@ -236,6 +238,8 @@ function _wp_ajax_menu_quick_search( $request = array() ) {
'name__like' => $query, 'name__like' => $query,
'number' => 10, 'number' => 10,
)); ));
if ( empty( $terms ) || is_wp_error( $terms ) )
echo '-1';
foreach( (array) $terms as $term ) { foreach( (array) $terms as $term ) {
if ( 'markup' == $response_format ) { if ( 'markup' == $response_format ) {
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args ); 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; $args['walker'] = new Walker_Nav_Menu_Checklist;
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args ); echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
?> ?>
<?php elseif ( is_wp_error( $search_results ) ) : ?>
<li><?php echo $search_results->get_error_message(); ?></li>
<?php elseif ( ! empty( $searched ) ) : ?>
<li><?php _e('No results found.'); ?></li>
<?php endif; ?> <?php endif; ?>
</ul> </ul>
</div><!-- /.tabs-panel --> </div><!-- /.tabs-panel -->
@ -698,6 +706,10 @@ function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
$args['walker'] = $walker; $args['walker'] = $walker;
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args ); echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
?> ?>
<?php elseif ( is_wp_error( $search_results ) ) : ?>
<li><?php echo $search_results->get_error_message(); ?></li>
<?php elseif ( ! empty( $searched ) ) : ?>
<li><?php _e('No results found.'); ?></li>
<?php endif; ?> <?php endif; ?>
</ul> </ul>
</div><!-- /.tabs-panel --> </div><!-- /.tabs-panel -->

View File

@ -614,6 +614,7 @@ var WPNavMenuHandler = function () {
i, i,
items, items,
matched, matched,
message,
newID, newID,
pattern = new RegExp('menu-item\\[(\[^\\]\]*)'), pattern = new RegExp('menu-item\\[(\[^\\]\]*)'),
resultList; resultList;
@ -642,12 +643,17 @@ var WPNavMenuHandler = function () {
resultList.appendChild(items[0]); resultList.appendChild(items[0]);
} }
} else if ( req.type ) { } else if ( req.type ) {
matched = /quick-search-posttype-([a-zA-Z_-]*)/.exec(req.type); matched = /quick-search-(posttype|taxonomy)-([a-zA-Z_-]*)/.exec(req.type);
if ( matched && matched[1] ) { if ( matched && matched[2] ) {
resultList = document.getElementById(matched[1] + '-search-checklist'); resultList = document.getElementById(matched[2] + '-search-checklist');
if ( resultList ) { if ( resultList ) {
resultList.innerHTML = ''; resultList.innerHTML = '';
i = items.length; i = items.length;
if ( ! i ) {
message = document.createElement('li');
message.appendChild(document.createTextNode(navMenuL10n.noResultsFound));
resultList.appendChild(message);
}
while( i-- ) { while( i-- ) {
resultList.appendChild(items[i]); resultList.appendChild(items[i]);
} }

File diff suppressed because one or more lines are too long

View File

@ -219,9 +219,15 @@ switch ( $action ) {
$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id ); $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
if ( ! is_wp_error( $_menu_object ) ) { if ( ! is_wp_error( $_menu_object ) ) {
$_menu_object = wp_get_nav_menu_object( wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $_POST['menu-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 = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
} else {
$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
$nav_menu_selected_title = $_menu_object->name; $nav_menu_selected_title = $_menu_object->name;
} }
}
// Update menu items // Update menu items
@ -229,6 +235,7 @@ switch ( $action ) {
$menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') ); $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 // Loop through all the menu items' POST variables
if ( ! empty( $_POST['menu-item-db-id'] ) ) {
foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) { foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
// Menu item title can't be blank // Menu item title can't be blank
@ -258,6 +265,7 @@ switch ( $action ) {
unset( $menu_items[$menu_item_db_id] ); unset( $menu_items[$menu_item_db_id] );
} }
} }
}
// Remove menu items from the menu that weren't in $_POST // Remove menu items from the menu that weren't in $_POST
if ( ! empty( $menu_items ) ) { if ( ! empty( $menu_items ) ) {

View File

@ -89,7 +89,7 @@ function wp_create_nav_menu( $menu_name, $args = array() ) {
$menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' ); $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' );
if ( $menu_exists ) if ( $menu_exists )
return new WP_Error( 'menu_exists', sprintf( __('A menu named <strong>%s</strong> already exists; please try another name.'), esc_html( $menu_exists->name ) ) ); return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_name ) ) );
if ( isset($args['slug']) ) if ( isset($args['slug']) )
$slug = $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; $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 <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) );
}
$update_response = wp_update_term( $menu_id, 'nav_menu', $args ); $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
if ( ! is_wp_error( $update_response ) ) if ( ! is_wp_error( $update_response ) )

View File

@ -393,11 +393,12 @@ function wp_default_scripts( &$scripts ) {
) ); ) );
// Custom Navigation // 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( $scripts->localize( 'nav-menu', 'navMenuL10n', array(
'custom' => _x('Custom', 'menu nav item type'), 'custom' => _x('Custom', 'menu nav item type'),
'thickbox' => _x('Edit Menu Item', 'Thickbox Title'), 'thickbox' => _x('Edit Menu Item', 'Thickbox Title'),
'edit' => _x('Edit', 'menu item edit text'), '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." ), '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." ), 'warnDeleteMenuItem' => __( "You are about to permanently delete this menu item. \n 'Cancel' to stop, 'OK' to delete." ),
) ); ) );