Make WordPress Core

Changeset 33611


Ignore:
Timestamp:
08/12/2015 02:06:21 PM (10 years ago)
Author:
boonebgorges
Message:

When splitting a shared 'nav_menu' term, ensure that nav items and theme locations are retained.

Props boonebgorges, dd32.
Fixes #33187.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/nav-menus.php

    r33355 r33611  
    355355            // Update menu items.
    356356            if ( ! is_wp_error( $_menu_object ) ) {
    357                 $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) );
     357                $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) );
     358
     359                // If the menu ID changed, redirect to the new URL.
     360                if ( $nav_menu_selected_id != $_nav_menu_selected_id ) {
     361                    wp_redirect( admin_url( 'nav-menus.php?menu=' . intval( $_nav_menu_selected_id ) ) );
     362                    exit();
     363                }
    358364            }
    359365        }
  • trunk/src/wp-includes/default-filters.php

    r33590 r33611  
    331331add_action( 'split_shared_term', '_wp_check_split_default_terms',  10, 4 );
    332332add_action( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 );
     333add_action( 'split_shared_term', '_wp_check_split_nav_menu_terms', 10, 4 );
    333334
    334335/**
  • trunk/src/wp-includes/nav-menu.php

    r33488 r33611  
    315315    if ( is_wp_error( $update_response ) )
    316316        return $update_response;
     317
     318    $menu_id = (int) $update_response['term_id'];
    317319
    318320    /**
  • trunk/src/wp-includes/taxonomy.php

    r33238 r33611  
    43974397
    43984398/**
     4399 * If the term being split is a nav_menu, change associations.
     4400 *
     4401 * @ignore
     4402 * @since 4.3.0
     4403 *
     4404 * @global wpdb $wpdb
     4405 *
     4406 * @param int    $term_id          ID of the formerly shared term.
     4407 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
     4408 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
     4409 * @param string $taxonomy         Taxonomy for the split term.
     4410 */
     4411function _wp_check_split_nav_menu_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
     4412    if ( 'nav_menu' !== $taxonomy ) {
     4413        return;
     4414    }
     4415
     4416    // Update menu locations.
     4417    $locations = get_nav_menu_locations();
     4418    foreach ( $locations as $location => $menu_id ) {
     4419        if ( $term_id == $menu_id ) {
     4420            $locations[ $location ] = $new_term_id;
     4421        }
     4422    }
     4423    set_theme_mod( 'nav_menu_locations', $locations );
     4424}
     4425
     4426/**
    43994427 * Get data about terms that previously shared a single term_id, but have since been split.
    44004428 *
  • trunk/tests/phpunit/tests/term/splitSharedTerm.php

    r32208 r33611  
    196196    }
    197197
     198    /**
     199     * @ticket 33187
     200     * @group navmenus
     201     */
     202    public function test_nav_menu_locations_should_be_updated_on_split() {
     203        global $wpdb;
     204
     205        $cat_term = wp_insert_term( 'Foo Menu', 'category' );
     206        $shared_term_id = $cat_term['term_id'];
     207
     208        $nav_term_id = wp_create_nav_menu( 'Foo Menu' );
     209        $nav_term = get_term( $nav_term_id, 'nav_menu' );
     210
     211        // Manually modify because shared terms shouldn't naturally occur.
     212        $wpdb->update( $wpdb->term_taxonomy,
     213            array( 'term_id' => $shared_term_id ),
     214            array( 'term_taxonomy_id' => $nav_term->term_taxonomy_id )
     215        );
     216
     217        set_theme_mod( 'nav_menu_locations', array( 'foo' => $shared_term_id ) );
     218
     219        // Splitsville.
     220        $new_term_id = _split_shared_term( $shared_term_id, $nav_term->term_taxonomy_id );
     221
     222        $locations = get_nav_menu_locations();
     223        $this->assertEquals( $new_term_id, $locations['foo'] );
     224    }
     225
     226    /**
     227     * @ticket 33187
     228     * @group navmenus
     229     */
     230    public function test_nav_menu_term_should_retain_menu_items_on_split() {
     231        global $wpdb;
     232
     233        $cat_term = wp_insert_term( 'Foo Menu', 'category' );
     234        $shared_term_id = $cat_term['term_id'];
     235
     236        $nav_term_id = wp_create_nav_menu( 'Foo Menu' );
     237        $nav_term = get_term( $nav_term_id, 'nav_menu' );
     238
     239        // Manually modify because shared terms shouldn't naturally occur.
     240        $wpdb->update( $wpdb->term_taxonomy,
     241            array( 'term_id' => $shared_term_id ),
     242            array( 'term_taxonomy_id' => $nav_term->term_taxonomy_id )
     243        );
     244
     245        $t1 = wp_insert_term( 'Random term', 'category' );
     246        $cat_menu_item = wp_update_nav_menu_item( $shared_term_id, 0, array(
     247            'menu-item-type' => 'taxonomy',
     248            'menu-item-object' => 'category',
     249            'menu-item-object-id' => $t1['term_id'],
     250            'menu-item-status' => 'publish'
     251        ) );
     252
     253        // Updating the menu will split the shared term.
     254        $new_nav_menu_id = wp_update_nav_menu_object( $shared_term_id, array(
     255            'description' => 'Updated Foo Menu',
     256            'menu-name' => 'Updated Foo Menu',
     257        ) );
     258
     259        $menu = wp_get_nav_menu_object( $new_nav_menu_id );
     260        $this->assertSame( 'Updated Foo Menu', $menu->name );
     261        $this->assertSame( 'Updated Foo Menu', $menu->description );
     262
     263        $menu_items = wp_get_nav_menu_items( $new_nav_menu_id );
     264        $this->assertEquals( array( $cat_menu_item ), wp_list_pluck( $menu_items, 'ID' ) );
     265    }
     266
    198267    public function test_wp_get_split_terms() {
    199268        $found = wp_get_split_terms( $this->terms['t1']['term_id'] );
Note: See TracChangeset for help on using the changeset viewer.