diff --git a/src/wp-admin/nav-menus.php b/src/wp-admin/nav-menus.php index f70826f8ff..ed9d052333 100644 --- a/src/wp-admin/nav-menus.php +++ b/src/wp-admin/nav-menus.php @@ -354,7 +354,13 @@ switch ( $action ) { // Update menu items. if ( ! is_wp_error( $_menu_object ) ) { - $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) ); + $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) ); + + // If the menu ID changed, redirect to the new URL. + if ( $nav_menu_selected_id != $_nav_menu_selected_id ) { + wp_redirect( admin_url( 'nav-menus.php?menu=' . intval( $_nav_menu_selected_id ) ) ); + exit(); + } } } break; diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index a1c9c97335..46fafc18b3 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -330,6 +330,7 @@ add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 ); // Split term updates. add_action( 'split_shared_term', '_wp_check_split_default_terms', 10, 4 ); add_action( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 ); +add_action( 'split_shared_term', '_wp_check_split_nav_menu_terms', 10, 4 ); /** * Filters formerly mixed into wp-includes diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index 29666f079b..9eb101933c 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -315,6 +315,8 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { if ( is_wp_error( $update_response ) ) return $update_response; + $menu_id = (int) $update_response['term_id']; + /** * Fires after a navigation menu has been successfully updated. * diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index c2d0acc307..8e3e03d8d5 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -4395,6 +4395,34 @@ function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_ } } +/** + * If the term being split is a nav_menu, change associations. + * + * @ignore + * @since 4.3.0 + * + * @global wpdb $wpdb + * + * @param int $term_id ID of the formerly shared term. + * @param int $new_term_id ID of the new term created for the $term_taxonomy_id. + * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. + * @param string $taxonomy Taxonomy for the split term. + */ +function _wp_check_split_nav_menu_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { + if ( 'nav_menu' !== $taxonomy ) { + return; + } + + // Update menu locations. + $locations = get_nav_menu_locations(); + foreach ( $locations as $location => $menu_id ) { + if ( $term_id == $menu_id ) { + $locations[ $location ] = $new_term_id; + } + } + set_theme_mod( 'nav_menu_locations', $locations ); +} + /** * Get data about terms that previously shared a single term_id, but have since been split. * diff --git a/tests/phpunit/tests/term/splitSharedTerm.php b/tests/phpunit/tests/term/splitSharedTerm.php index 66fb8bc5b8..b5439ef099 100644 --- a/tests/phpunit/tests/term/splitSharedTerm.php +++ b/tests/phpunit/tests/term/splitSharedTerm.php @@ -195,6 +195,75 @@ class Tests_Term_SplitSharedTerm extends WP_UnitTestCase { $this->assertEquals( $new_term_id, get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) ); } + /** + * @ticket 33187 + * @group navmenus + */ + public function test_nav_menu_locations_should_be_updated_on_split() { + global $wpdb; + + $cat_term = wp_insert_term( 'Foo Menu', 'category' ); + $shared_term_id = $cat_term['term_id']; + + $nav_term_id = wp_create_nav_menu( 'Foo Menu' ); + $nav_term = get_term( $nav_term_id, 'nav_menu' ); + + // Manually modify because shared terms shouldn't naturally occur. + $wpdb->update( $wpdb->term_taxonomy, + array( 'term_id' => $shared_term_id ), + array( 'term_taxonomy_id' => $nav_term->term_taxonomy_id ) + ); + + set_theme_mod( 'nav_menu_locations', array( 'foo' => $shared_term_id ) ); + + // Splitsville. + $new_term_id = _split_shared_term( $shared_term_id, $nav_term->term_taxonomy_id ); + + $locations = get_nav_menu_locations(); + $this->assertEquals( $new_term_id, $locations['foo'] ); + } + + /** + * @ticket 33187 + * @group navmenus + */ + public function test_nav_menu_term_should_retain_menu_items_on_split() { + global $wpdb; + + $cat_term = wp_insert_term( 'Foo Menu', 'category' ); + $shared_term_id = $cat_term['term_id']; + + $nav_term_id = wp_create_nav_menu( 'Foo Menu' ); + $nav_term = get_term( $nav_term_id, 'nav_menu' ); + + // Manually modify because shared terms shouldn't naturally occur. + $wpdb->update( $wpdb->term_taxonomy, + array( 'term_id' => $shared_term_id ), + array( 'term_taxonomy_id' => $nav_term->term_taxonomy_id ) + ); + + $t1 = wp_insert_term( 'Random term', 'category' ); + $cat_menu_item = wp_update_nav_menu_item( $shared_term_id, 0, array( + 'menu-item-type' => 'taxonomy', + 'menu-item-object' => 'category', + 'menu-item-object-id' => $t1['term_id'], + 'menu-item-status' => 'publish' + ) ); + + // Updating the menu will split the shared term. + $new_nav_menu_id = wp_update_nav_menu_object( $shared_term_id, array( + 'description' => 'Updated Foo Menu', + 'menu-name' => 'Updated Foo Menu', + ) ); + + $menu = wp_get_nav_menu_object( $new_nav_menu_id ); + $this->assertSame( 'Updated Foo Menu', $menu->name ); + $this->assertSame( 'Updated Foo Menu', $menu->description ); + + $menu_items = wp_get_nav_menu_items( $new_nav_menu_id ); + $this->assertEquals( array( $cat_menu_item ), wp_list_pluck( $menu_items, 'ID' ) ); + } + public function test_wp_get_split_terms() { $found = wp_get_split_terms( $this->terms['t1']['term_id'] );