diff --git a/inc/namespace.php b/inc/namespace.php index 8fd2f16..79b3522 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -428,6 +428,8 @@ function validate_authors( $authors, WP_REST_Request $request, string $param, st /** @var WP_User[] */ $users = get_users( [ + // Check all sites. + 'blog_id' => 0, 'include' => $authors, 'orderby' => 'include', ] ); diff --git a/inc/template.php b/inc/template.php index a573de3..761288a 100644 --- a/inc/template.php +++ b/inc/template.php @@ -53,6 +53,8 @@ function get_authors( WP_Post $post ) : array { /** @var WP_User[] */ $users = get_users( [ + // Check all sites. + 'blog_id' => 0, 'include' => $author_ids, 'orderby' => 'include', ] ); @@ -155,6 +157,8 @@ function set_authors( WP_Post $post, array $authors ) : array { /** @var WP_User[] */ $users = get_users( [ + // Check all sites. + 'blog_id' => 0, 'include' => $authors, 'orderby' => 'include', ] ); diff --git a/tests/phpunit/test-multisite.php b/tests/phpunit/test-multisite.php new file mode 100644 index 0000000..6ab46e2 --- /dev/null +++ b/tests/phpunit/test-multisite.php @@ -0,0 +1,95 @@ +user->create_and_get( [ + 'role' => 'administrator', + 'display_name' => 'Super Admin', + 'user_email' => 'super-admin.role@example.org', + ] ); + + grant_super_admin( self::$super_admin->ID ); + + // Create a subsite. + self::$sub_site = $factory->blog->create_and_get( [ + 'domain' => 'example.org', + 'path' => '/subsite', + 'title' => 'Authorship Sub Site', + 'user_id' => self::$users['admin']->ID, + ] ); + } + + public function testSuperAdminWithNoRoleOnSite() { + // Change site. + switch_to_blog( self::$sub_site->blog_id ); + + // Confirm no role on current site. + $super_admin = get_user_by( 'ID', self::$super_admin->ID ); + $this->assertEmpty( $super_admin->roles ); + + $factory = self::factory()->post; + + // Attributed to Super Admin, owned by Admin. + $post = $factory->create_and_get( [ + 'post_author' => self::$users['admin']->ID, + POSTS_PARAM => [ + self::$super_admin->ID, + ], + ] ); + + // Check super admin ID is stored. + $author_ids = \Authorship\get_author_ids( $post ); + $this->assertSame( [ self::$super_admin->ID ], $author_ids ); + + $author_url = get_author_posts_url( self::$super_admin->ID ); + $this->assertTrue( strpos( $author_url, '/subsite/' ) !== false ); + $this->go_to( $author_url ); + + /** @var \WP_Query */ + global $wp_query, $authordata; + + $this->assertQueryTrue( 'is_author', 'is_archive' ); + $this->assertTrue( is_author( self::$super_admin->ID ) ); + $this->assertSame( [ $post->ID ], wp_list_pluck( $wp_query->posts, 'ID' ) ); + $this->assertSame( self::$super_admin->ID, $authordata->ID ); + + restore_current_blog(); + } + +}