Make WordPress Core

Ticket #33187: 33187.2.diff

File 33187.2.diff, 10.1 KB (added by dd32, 10 years ago)
  • src/wp-admin/nav-menus.php

    switch ( $action ) { 
    342342                        }
    343343
    344344                        if ( ! is_wp_error( $_menu_object ) ) {
    345345                                $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
    346346                                if ( is_wp_error( $_nav_menu_selected_id ) ) {
    347347                                        $_menu_object = $_nav_menu_selected_id;
    348348                                        $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
    349349                                } else {
    350350                                        $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
    351351                                        $nav_menu_selected_title = $_menu_object->name;
    352352                                }
    353353                        }
    354354
    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, this will loose all $messages
     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                }
    360366                break;
    361367        case 'locations':
    362368                if ( ! $num_locations ) {
    363369                        wp_redirect( admin_url( 'nav-menus.php' ) );
    364370                        exit();
    365371                }
    366372
    367373                add_filter( 'screen_options_show_screen', '__return_false' );
    368374
    369375                if ( isset( $_POST['menu-locations'] ) ) {
    370376                        check_admin_referer( 'save-menu-locations' );
    371377
    372378                        $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
  • src/wp-includes/default-filters.php

    add_filter( 'heartbeat_settings', 'wp_he 
    318318
    319319// Check if the user is logged out
    320320add_action( 'admin_enqueue_scripts', 'wp_auth_check_load' );
    321321add_filter( 'heartbeat_send',        'wp_auth_check' );
    322322add_filter( 'heartbeat_nopriv_send', 'wp_auth_check' );
    323323
    324324// Default authentication filters
    325325add_filter( 'authenticate', 'wp_authenticate_username_password',  20, 3 );
    326326add_filter( 'authenticate', 'wp_authenticate_spam_check',         99    );
    327327add_filter( 'determine_current_user', 'wp_validate_auth_cookie'          );
    328328add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 );
    329329
    330330// Split term updates.
    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/**
    335336 * Filters formerly mixed into wp-includes
    336337 */
    337338// Theme
    338339add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
    339340add_action( 'plugins_loaded', '_wp_customize_include' );
    340341add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
    341342add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
    342343
    343344// Calendar widget cache
    344345add_action( 'save_post', 'delete_get_calendar_cache' );
    345346add_action( 'delete_post', 'delete_get_calendar_cache' );
    346347add_action( 'update_option_start_of_week', 'delete_get_calendar_cache' );
    347348add_action( 'update_option_gmt_offset', 'delete_get_calendar_cache' );
  • src/wp-includes/nav-menu.php

    function wp_update_nav_menu_object( $men 
    299299                 *
    300300                 * @param int   $term_id   ID of the new menu.
    301301                 * @param array $menu_data An array of menu data.
    302302                 */
    303303                do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
    304304
    305305                return (int) $_menu['term_id'];
    306306        }
    307307
    308308        if ( ! $_menu || ! isset( $_menu->term_id ) )
    309309                return 0;
    310310
    311311        $menu_id = (int) $_menu->term_id;
    312312
    313313        $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
     314        $menu_id = (int) $update_response['term_id'];
    314315
    315316        if ( is_wp_error( $update_response ) )
    316317                return $update_response;
    317318
    318319        /**
    319320         * Fires after a navigation menu has been successfully updated.
    320321         *
    321322         * @since 3.0.0
    322323         *
    323324         * @param int   $menu_id   ID of the updated menu.
    324325         * @param array $menu_data An array of menu data.
    325326         */
    326327        do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
    327328        return $menu_id;
    328329}
  • src/wp-includes/taxonomy.php

    function _wp_check_split_terms_in_menus( 
    43844384                WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
    43854385                        AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )
    43864386                        AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
    43874387                $taxonomy,
    43884388                $term_id
    43894389        ) );
    43904390
    43914391        if ( $post_ids ) {
    43924392                foreach ( $post_ids as $post_id ) {
    43934393                        update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
    43944394                }
    43954395        }
    43964396}
    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 *
    44014429 * @since 4.2.0
    44024430 *
    44034431 * @param int $old_term_id Term ID. This is the old, pre-split term ID.
    44044432 * @return array Array of new term IDs, keyed by taxonomy.
    44054433 */
    44064434function wp_get_split_terms( $old_term_id ) {
    44074435        $split_terms = get_option( '_split_terms', array() );
    44084436
    44094437        $terms = array();
    44104438        if ( isset( $split_terms[ $old_term_id ] ) ) {
    44114439                $terms = $split_terms[ $old_term_id ];
    44124440        }
    44134441
  • tests/phpunit/tests/term/splitSharedTerm.php

    class Tests_Term_SplitSharedTerm extends 
    183183
    184184                $menu_id = wp_create_nav_menu( rand_str() );
    185185                $cat_menu_item = wp_update_nav_menu_item( $menu_id, 0, array(
    186186                        'menu-item-type' => 'taxonomy',
    187187                        'menu-item-object' => 'category',
    188188                        'menu-item-object-id' => $t1['term_id'],
    189189                        'menu-item-status' => 'publish'
    190190                ) );
    191191                $this->assertEquals( $t1['term_id'], get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
    192192
    193193                $new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
    194194                $this->assertNotEquals( $new_term_id, $t1['term_id'] );
    195195                $this->assertEquals( $new_term_id, get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
    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'] );
    200269
    201270                $expected = array(
    202271                        'wptests_tax_2' => $this->terms['t2']['term_id'],
    203272                        'wptests_tax_3' => $this->terms['t3']['term_id'],
    204273                );
    205274
    206275                $this->assertEqualSets( $expected, $found );
    207276        }
    208277
    209278        public function test_wp_get_split_term() {
    210279                $found = wp_get_split_term( $this->terms['t1']['term_id'], 'wptests_tax_3' );
    211280                $this->assertEquals( $this->terms['t3']['term_id'], $found );
    212281        }