Make WordPress Core

Changeset 27150


Ignore:
Timestamp:
02/09/2014 09:36:15 PM (11 years ago)
Author:
ocean90
Message:

Nav Menu: Remove post/page items from the Nav Menu when the post/page is deleted.

This was broken through a change in [25163]. _menu_item_object in wp_get_associated_nav_menu_items() is not relevant for post types.
Adds unit tests.

props UmeshSingla for initial patch.
fixes #26795.

Location:
trunk
Files:
2 edited

Legend:

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

    r25289 r27150  
    703703    foreach( (array) $menu_items as $menu_item ) {
    704704        if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
    705             if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) !== $object_type ||
    706                 get_post_meta( $menu_item->ID, '_menu_item_object', true ) !== $taxonomy )
    707                 continue;
    708 
    709             $menu_item_ids[] = (int) $menu_item->ID;
     705            $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
     706            if (
     707                'post_type' == $object_type &&
     708                'post_type' == $menu_item_type
     709            ) {
     710                $menu_item_ids[] = (int) $menu_item->ID;
     711            } else if (
     712                'taxonomy' == $object_type &&
     713                'taxonomy' == $menu_item_type &&
     714                get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
     715            ) {
     716                $menu_item_ids[] = (int) $menu_item->ID;
     717            }
    710718        }
    711719    }
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r25163 r27150  
    1919        $tag_id = $this->factory->tag->create();
    2020        $cat_id = $this->factory->category->create();
     21        $post_id = $this->factory->post->create();
     22        $post_2_id = $this->factory->post->create();
     23        $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    2124
    2225        $tag_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     
    3437        ) );
    3538
     39        $post_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     40            'menu-item-type' => 'post_type',
     41            'menu-item-object' => 'post',
     42            'menu-item-object-id' => $post_id,
     43            'menu-item-status' => 'publish'
     44        ) );
     45
     46        // Item without menu-item-object arg
     47        $post_2_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     48            'menu-item-type' => 'post_type',
     49            'menu-item-object-id' => $post_2_id,
     50            'menu-item-status' => 'publish'
     51        ) );
     52
     53        $page_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     54            'menu-item-type' => 'post_type',
     55            'menu-item-object' => 'page',
     56            'menu-item-object-id' => $page_id,
     57            'menu-item-status' => 'publish'
     58        ) );
     59
    3660        $tag_items = wp_get_associated_nav_menu_items( $tag_id, 'taxonomy', 'post_tag' );
    3761        $this->assertEqualSets( array( $tag_insert ), $tag_items );
    3862        $cat_items = wp_get_associated_nav_menu_items( $cat_id, 'taxonomy', 'category' );
    3963        $this->assertEqualSets( array( $cat_insert ), $cat_items );
     64        $post_items = wp_get_associated_nav_menu_items( $post_id );
     65        $this->assertEqualSets( array( $post_insert ), $post_items );
     66        $post_2_items = wp_get_associated_nav_menu_items( $post_2_id );
     67        $this->assertEqualSets( array( $post_2_insert ), $post_2_items );
     68        $page_items = wp_get_associated_nav_menu_items( $page_id );
     69        $this->assertEqualSets( array( $page_insert ), $page_items );
     70
     71        wp_delete_term( $tag_id, 'post_tag' );
     72        $tag_items = wp_get_associated_nav_menu_items( $tag_id, 'taxonomy', 'post_tag' );
     73        $this->assertEqualSets( array(), $tag_items );
     74
     75        wp_delete_term( $cat_id, 'category' );
     76        $cat_items = wp_get_associated_nav_menu_items( $cat_id, 'taxonomy', 'category' );
     77        $this->assertEqualSets( array(), $cat_items );
     78
     79        wp_delete_post( $post_id, true );
     80        $post_items = wp_get_associated_nav_menu_items( $post_id );
     81        $this->assertEqualSets( array(), $post_items );
     82
     83        wp_delete_post( $post_2_id, true );
     84        $post_2_items = wp_get_associated_nav_menu_items( $post_2_id );
     85        $this->assertEqualSets( array(), $post_2_items );
     86
     87        wp_delete_post( $page_id, true );
     88        $page_items = wp_get_associated_nav_menu_items( $page_id );
     89        $this->assertEqualSets( array(), $page_items );
    4090    }
    4191}
Note: See TracChangeset for help on using the changeset viewer.