From 586cb2b140068ed090048c2098e6aa5849ef1ab9 Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Sun, 9 Feb 2014 21:36:15 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@27150 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/nav-menu.php | 18 +++++++--- tests/phpunit/tests/post/nav-menu.php | 52 ++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index cf9ac1427e..af69dfc897 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -702,11 +702,19 @@ function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_ ); foreach( (array) $menu_items as $menu_item ) { if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) { - if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) !== $object_type || - get_post_meta( $menu_item->ID, '_menu_item_object', true ) !== $taxonomy ) - continue; - - $menu_item_ids[] = (int) $menu_item->ID; + $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true ); + if ( + 'post_type' == $object_type && + 'post_type' == $menu_item_type + ) { + $menu_item_ids[] = (int) $menu_item->ID; + } else if ( + 'taxonomy' == $object_type && + 'taxonomy' == $menu_item_type && + get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy + ) { + $menu_item_ids[] = (int) $menu_item->ID; + } } } diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index 00912879f6..954e5385ab 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -18,6 +18,9 @@ class Test_Nav_Menus extends WP_UnitTestCase { function test_wp_get_associated_nav_menu_items() { $tag_id = $this->factory->tag->create(); $cat_id = $this->factory->category->create(); + $post_id = $this->factory->post->create(); + $post_2_id = $this->factory->post->create(); + $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); $tag_insert = wp_update_nav_menu_item( $this->menu_id, 0, array( 'menu-item-type' => 'taxonomy', @@ -33,9 +36,56 @@ class Test_Nav_Menus extends WP_UnitTestCase { 'menu-item-status' => 'publish' ) ); + $post_insert = wp_update_nav_menu_item( $this->menu_id, 0, array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish' + ) ); + + // Item without menu-item-object arg + $post_2_insert = wp_update_nav_menu_item( $this->menu_id, 0, array( + 'menu-item-type' => 'post_type', + 'menu-item-object-id' => $post_2_id, + 'menu-item-status' => 'publish' + ) ); + + $page_insert = wp_update_nav_menu_item( $this->menu_id, 0, array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'page', + 'menu-item-object-id' => $page_id, + 'menu-item-status' => 'publish' + ) ); + $tag_items = wp_get_associated_nav_menu_items( $tag_id, 'taxonomy', 'post_tag' ); $this->assertEqualSets( array( $tag_insert ), $tag_items ); $cat_items = wp_get_associated_nav_menu_items( $cat_id, 'taxonomy', 'category' ); $this->assertEqualSets( array( $cat_insert ), $cat_items ); + $post_items = wp_get_associated_nav_menu_items( $post_id ); + $this->assertEqualSets( array( $post_insert ), $post_items ); + $post_2_items = wp_get_associated_nav_menu_items( $post_2_id ); + $this->assertEqualSets( array( $post_2_insert ), $post_2_items ); + $page_items = wp_get_associated_nav_menu_items( $page_id ); + $this->assertEqualSets( array( $page_insert ), $page_items ); + + wp_delete_term( $tag_id, 'post_tag' ); + $tag_items = wp_get_associated_nav_menu_items( $tag_id, 'taxonomy', 'post_tag' ); + $this->assertEqualSets( array(), $tag_items ); + + wp_delete_term( $cat_id, 'category' ); + $cat_items = wp_get_associated_nav_menu_items( $cat_id, 'taxonomy', 'category' ); + $this->assertEqualSets( array(), $cat_items ); + + wp_delete_post( $post_id, true ); + $post_items = wp_get_associated_nav_menu_items( $post_id ); + $this->assertEqualSets( array(), $post_items ); + + wp_delete_post( $post_2_id, true ); + $post_2_items = wp_get_associated_nav_menu_items( $post_2_id ); + $this->assertEqualSets( array(), $post_2_items ); + + wp_delete_post( $page_id, true ); + $page_items = wp_get_associated_nav_menu_items( $page_id ); + $this->assertEqualSets( array(), $page_items ); } -} \ No newline at end of file +}