Make WordPress Core

Ticket #27113: 27113.3.diff

File 27113.3.diff, 2.5 KB (added by danielbachhuber, 12 years ago)

Patch with tests

  • src/wp-includes/nav-menu.php

     
    365365
    366366        $update = 0 != $menu_item_db_id;
    367367
    368         // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
    369         if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) )
    370                 $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) );
    371 
    372368        // New menu item. Default is draft status
    373369        if ( ! $update ) {
    374370                $post['ID'] = 0;
     
    376372                $menu_item_db_id = wp_insert_post( $post );
    377373                if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
    378374                        return $menu_item_db_id;
     375
    379376        }
    380377
     378        // Associate the menu item with the menu term
     379         if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
     380                wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
     381        }
     382
    381383        if ( 'custom' == $args['menu-item-type'] ) {
    382384                $args['menu-item-object-id'] = $menu_item_db_id;
    383385                $args['menu-item-object'] = 'custom';
  • tests/phpunit/tests/post/nav-menu.php

     
    8888                $page_items = wp_get_associated_nav_menu_items( $page_id );
    8989                $this->assertEqualSets( array(), $page_items );
    9090        }
     91
     92        /**
     93         * @ticket 27113
     94         */
     95        function test_orphan_nav_menu_item() {
     96
     97                // Create an orphan nav menu item
     98                $custom_item_id = wp_update_nav_menu_item( 0, 0, array(
     99                        'menu-item-type'      => 'custom',
     100                        'menu-item-title'     => 'Wordpress.org',
     101                        'menu-item-link'      => 'http://wordpress.org',
     102                        'menu-item-status'    => 'publish'
     103                ) );
     104
     105                // Confirm it saved properly
     106                $custom_item = wp_setup_nav_menu_item( get_post( $custom_item_id ) );
     107                $this->assertEquals( 'Wordpress.org', $custom_item->title );
     108
     109                // Update the orphan with an associated nav menu
     110                wp_update_nav_menu_item( $this->menu_id, $custom_item_id, array(
     111                        'menu-item-title'     => 'WordPress.org',
     112                        ) );
     113                $menu_items = wp_get_nav_menu_items( $this->menu_id );
     114                $custom_item = wp_filter_object_list( $menu_items, array( 'db_id' => $custom_item_id ) );
     115                $custom_item = array_pop( $custom_item );
     116                $this->assertEquals( 'WordPress.org', $custom_item->title );
     117
     118        }
    91119}