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],
'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 );
?>
<?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; ?>
</ul>
</div><!-- /.tabs-panel -->
@ -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 );
?>
<?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; ?>
</ul>
</div><!-- /.tabs-panel -->

View File

@ -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]);
}

File diff suppressed because one or more lines are too long

View File

@ -219,8 +219,14 @@ switch ( $action ) {
$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
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_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 = '<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;
}
}
// 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;
// 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],
);
$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] );
}
}
}

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' );
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']) )
$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 <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 );
if ( ! is_wp_error( $update_response ) )

View File

@ -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." ),
) );