From bcf50fedcdcef633824600c85fbcd756a2812c84 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 3 Oct 2016 08:37:27 +0000 Subject: [PATCH] Toolbar: Be more strict about adding a 'View Posts' link to the toolbar. After [38634], this adjusts the behaviour to remove redundancy by not displaying the link if the latest posts are shown on the front page. In that scenario, the 'Visit Site' link already points to the latest posts. Fixes #34113. git-svn-id: https://develop.svn.wordpress.org/trunk@38708 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/admin-bar.php | 3 +- tests/phpunit/tests/adminbar.php | 82 +++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 1468e85153..e87ac141f2 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -598,7 +598,8 @@ function wp_admin_bar_edit_menu( $wp_admin_bar ) { && ( $post_type_object = get_post_type_object( $current_screen->post_type ) ) && ( $post_type_object->public ) && ( $post_type_object->show_in_admin_bar ) - && ( get_post_type_archive_link( $post_type_object->name ) ) ) + && ( get_post_type_archive_link( $post_type_object->name ) ) + && ! ( 'post' === $post_type_object->name && 'posts' === get_option( 'show_on_front' ) ) ) { $wp_admin_bar->add_node( array( 'id' => 'archive', diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 9039134d1f..176b83c00a 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -383,7 +383,22 @@ class Tests_AdminBar extends WP_UnitTestCase { /** * @ticket 34113 */ - public function test_admin_bar_contains_view_archive_link() { + public function test_admin_bar_has_no_archives_link_if_no_static_front_page() { + set_current_screen( 'edit-post' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node = $wp_admin_bar->get_node( 'archive' ); + + set_current_screen( 'front' ); + + $this->assertNull( $node ); + } + + /** + * @ticket 34113 + */ + public function test_admin_bar_contains_view_archive_link_if_static_front_page() { + update_option( 'show_on_front', 'page' ); set_current_screen( 'edit-post' ); $wp_admin_bar = $this->get_standard_admin_bar(); @@ -397,7 +412,7 @@ class Tests_AdminBar extends WP_UnitTestCase { /** * @ticket 34113 */ - public function test_admin_bar_has_no_archives_link_for_post_types_without_archive() { + public function test_admin_bar_has_no_archives_link_for_pages() { set_current_screen( 'edit-page' ); $wp_admin_bar = $this->get_standard_admin_bar(); @@ -472,4 +487,67 @@ class Tests_AdminBar extends WP_UnitTestCase { $this->assertArrayNotHasKey( 'tabindex', $wp_logo_node->meta ); $this->assertNotNull( $about_node ); } + + /** + * @ticket 34113 + */ + public function test_admin_bar_has_no_archives_link_for_non_public_cpt() { + register_post_type( 'foo-non-public', array( + 'public' => false, + 'has_archive' => true, + 'show_in_admin_bar' => true, + ) ); + + set_current_screen( 'edit-foo-non-public' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node = $wp_admin_bar->get_node( 'archive' ); + + set_current_screen( 'front' ); + unregister_post_type( 'foo-non-public' ); + + $this->assertNull( $node ); + } + + /** + * @ticket 34113 + */ + public function test_admin_bar_has_no_archives_link_for_cpt_without_archive() { + register_post_type( 'foo-non-public', array( + 'public' => true, + 'has_archive' => false, + 'show_in_admin_bar' => true, + ) ); + + set_current_screen( 'edit-foo-non-public' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node = $wp_admin_bar->get_node( 'archive' ); + + set_current_screen( 'front' ); + unregister_post_type( 'foo-non-public' ); + + $this->assertNull( $node ); + } + + /** + * @ticket 34113 + */ + public function test_admin_bar_has_no_archives_link_for_cpt_not_shown_in_admin_bar() { + register_post_type( 'foo-non-public', array( + 'public' => true, + 'has_archive' => true, + 'show_in_admin_bar' => false, + ) ); + + set_current_screen( 'edit-foo-non-public' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node = $wp_admin_bar->get_node( 'archive' ); + + set_current_screen( 'front' ); + unregister_post_type( 'foo-non-public' ); + + $this->assertNull( $node ); + } }