From f683e9aae1f606f9720537f71139276d6ceee52b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 7 Oct 2016 16:49:19 +0000 Subject: [PATCH] Menus: Do not show trashed posts in nav menus. Trashed posts cannot be accessed by site visitors and thus should not be visible on the front end. By marking menu items of trashed posts as invalid, they are excluded from the output. Props solarissmoke, swissspidy. Fixes #19038. git-svn-id: https://develop.svn.wordpress.org/trunk@38744 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/nav-menu.php | 4 ++++ tests/phpunit/tests/post/nav-menu.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index 58a5d1133e..16653cab1e 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -747,6 +747,10 @@ function wp_setup_nav_menu_item( $menu_item ) { $menu_item->_invalid = true; } + if ( 'trash' === get_post_status( $menu_item->object_id ) ) { + $menu_item->_invalid = true; + } + $menu_item->url = get_permalink( $menu_item->object_id ); $original_object = get_post( $menu_item->object_id ); diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index bbcbbabbdf..83edce53cb 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -276,6 +276,26 @@ class Test_Nav_Menus extends WP_UnitTestCase { $this->assertEmpty( $post_type_archive_item->description ); } + /** + * @ticket 19038 + */ + function test_wp_setup_nav_menu_item_for_trashed_post() { + $post_id = self::factory()->post->create( array( + 'post_status' => 'trash', + ) ); + + $menu_item_id = 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', + ) ); + + $menu_item = wp_setup_nav_menu_item( get_post( $menu_item_id ) ); + + $this->assertTrue( ! _is_valid_nav_menu_item( $menu_item ) ); + } + /** * @ticket 35206 */