WordPress.org

Make WordPress Core

Changeset 27556


Ignore:
Timestamp:
03/15/2014 06:06:41 AM (6 years ago)
Author:
nacin
Message:

Explicitly assign menu term relationship rather than piggybacking on wp_insert_post() with the tax_input argument.

That argument currently depends on user context (see #19373).

Adds unit test for properly updating orphaned menu items.

props danielbachhuber.
fixes #27113.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu.php

    r27251 r27556  
    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 ) {
     
    377373        if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
    378374            return $menu_item_db_id;
     375    }
     376
     377    // Associate the menu item with the menu term
     378    // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
     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' );
    379381    }
    380382
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r27150 r27556  
    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}
Note: See TracChangeset for help on using the changeset viewer.