diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 009ed06cee..7b2d3a59fc 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -3472,13 +3472,13 @@ function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { $user_id = $user_id ? (int) $user_id : get_current_user_id(); $blogs = get_blogs_of_user( $user_id ); - if ( ! is_super_admin() && empty($blogs) ) { + if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty($blogs) ) { $url = user_admin_url( $path, $scheme ); } elseif ( ! is_multisite() ) { $url = admin_url( $path, $scheme ); } else { $current_blog = get_current_blog_id(); - if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { + if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { $url = admin_url( $path, $scheme ); } else { $active = get_active_blog_for_user( $user_id ); diff --git a/tests/phpunit/tests/link/getDashboardUrl.php b/tests/phpunit/tests/link/getDashboardUrl.php new file mode 100644 index 0000000000..b91edc7da8 --- /dev/null +++ b/tests/phpunit/tests/link/getDashboardUrl.php @@ -0,0 +1,77 @@ +user->create( array( 'role' => 'administrator' ) ); + } + + public static function wpTearDownAfterClass() { + wpmu_delete_user( self::$user_id ); + } + + /** + * @ticket 39065 + */ + public function test_get_dashboard_url_for_current_site_user() { + $this->assertEquals( admin_url(), get_dashboard_url( self::$user_id ) ); + } + + /** + * @ticket 39065 + */ + public function test_get_dashboard_url_for_user_with_no_sites() { + add_filter( 'get_blogs_of_user', '__return_empty_array' ); + + $expected = is_multisite() ? user_admin_url() : admin_url(); + + $this->assertEquals( $expected, get_dashboard_url( self::$user_id ) ); + } + + /** + * @ticket 39065 + */ + public function test_get_dashboard_url_for_network_administrator_with_no_sites() { + if ( ! is_multisite() ) { + $this->markTestSkipped( 'Test only runs in multisite.' ); + } + + grant_super_admin( self::$user_id ); + + add_filter( 'get_blogs_of_user', '__return_empty_array' ); + + $expected = admin_url(); + $result = get_dashboard_url( self::$user_id ); + + revoke_super_admin( self::$user_id ); + + $this->assertEquals( $expected, $result ); + } + + /** + * @ticket 39065 + */ + public function test_get_dashboard_url_for_administrator_of_different_site() { + if ( ! is_multisite() ) { + $this->markTestSkipped( 'Test only runs in multisite.' ); + } + + $site_id = self::factory()->blog->create( array( 'user_id' => self::$user_id ) ); + + remove_user_from_blog( self::$user_id, get_current_blog_id() ); + + $expected = get_admin_url( $site_id ); + $result = get_dashboard_url( self::$user_id ); + + remove_user_from_blog( self::$user_id, $site_id ); + add_user_to_blog( get_current_blog_id(), self::$user_id, 'administrator'); + + wpmu_delete_blog( $site_id, true ); + + $this->assertEquals( $expected, $result ); + } +}