From d680e222ce6395a6cc696f831ee19ef9d81170ea Mon Sep 17 00:00:00 2001 From: Robin Devitt Date: Mon, 15 Aug 2022 16:53:06 +0200 Subject: [PATCH 1/6] Show all users in post author column. --- inc/template.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/inc/template.php b/inc/template.php index a573de3..eaf3146 100644 --- a/inc/template.php +++ b/inc/template.php @@ -21,19 +21,21 @@ * @return int[] Array of user IDs. */ function get_author_ids( WP_Post $post ) : array { - if ( ! is_post_type_supported( $post->post_type ) ) { - if ( post_type_supports( $post->post_type, 'author' ) ) { - return [ intval( $post->post_author ) ]; + $authors = wp_get_post_terms( $post->ID, TAXONOMY ); + // If $authors is an error or empty. + if ( is_wp_error( $authors ) || empty( $authors ) ) { + // Check if the post_type supports Authorship. + if ( is_post_type_supported( $post->post_type ) ) { + // Check the post_type support author. + if ( post_type_supports( $post->post_type, 'author' ) ) { + return [ intval( $post->post_author ) ]; + } + return []; } return []; } - $authors = wp_get_post_terms( $post->ID, TAXONOMY ); - if ( is_wp_error( $authors ) ) { - return []; - } - return array_map( function ( WP_Term $term ) : int { return intval( $term->name ); }, $authors ); @@ -53,6 +55,7 @@ function get_authors( WP_Post $post ) : array { /** @var WP_User[] */ $users = get_users( [ + 'blog_id' => 0, 'include' => $author_ids, 'orderby' => 'include', ] ); From e681ffddc5baff0c41985c72ac55c91afcb0c4c8 Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Wed, 17 Aug 2022 11:04:18 +0100 Subject: [PATCH 2/6] Check users from the entire network in all cases --- inc/namespace.php | 1 + inc/template.php | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index 8fd2f16..967a20c 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -428,6 +428,7 @@ function validate_authors( $authors, WP_REST_Request $request, string $param, st /** @var WP_User[] */ $users = get_users( [ + 'blog_id' => 0, // Check all sites. 'include' => $authors, 'orderby' => 'include', ] ); diff --git a/inc/template.php b/inc/template.php index eaf3146..beac4c6 100644 --- a/inc/template.php +++ b/inc/template.php @@ -21,21 +21,19 @@ * @return int[] Array of user IDs. */ function get_author_ids( WP_Post $post ) : array { - $authors = wp_get_post_terms( $post->ID, TAXONOMY ); - // If $authors is an error or empty. - if ( is_wp_error( $authors ) || empty( $authors ) ) { - // Check if the post_type supports Authorship. - if ( is_post_type_supported( $post->post_type ) ) { - // Check the post_type support author. - if ( post_type_supports( $post->post_type, 'author' ) ) { - return [ intval( $post->post_author ) ]; - } - return []; + if ( ! is_post_type_supported( $post->post_type ) ) { + if ( post_type_supports( $post->post_type, 'author' ) ) { + return [ intval( $post->post_author ) ]; } return []; } + $authors = wp_get_post_terms( $post->ID, TAXONOMY ); + if ( is_wp_error( $authors ) ) { + return []; + } + return array_map( function ( WP_Term $term ) : int { return intval( $term->name ); }, $authors ); @@ -55,7 +53,7 @@ function get_authors( WP_Post $post ) : array { /** @var WP_User[] */ $users = get_users( [ - 'blog_id' => 0, + 'blog_id' => 0, // Check all sites. 'include' => $author_ids, 'orderby' => 'include', ] ); @@ -158,6 +156,7 @@ function set_authors( WP_Post $post, array $authors ) : array { /** @var WP_User[] */ $users = get_users( [ + 'blog_id' => 0, // Check all sites. 'include' => $authors, 'orderby' => 'include', ] ); From fd65bf3dfdb6c930678fb4815fd68f4912ddd509 Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Wed, 17 Aug 2022 11:08:01 +0100 Subject: [PATCH 3/6] Fix code style for comments --- inc/namespace.php | 3 ++- inc/template.php | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index 967a20c..79b3522 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -428,7 +428,8 @@ function validate_authors( $authors, WP_REST_Request $request, string $param, st /** @var WP_User[] */ $users = get_users( [ - 'blog_id' => 0, // Check all sites. + // Check all sites. + 'blog_id' => 0, 'include' => $authors, 'orderby' => 'include', ] ); diff --git a/inc/template.php b/inc/template.php index beac4c6..152d53b 100644 --- a/inc/template.php +++ b/inc/template.php @@ -53,7 +53,8 @@ function get_authors( WP_Post $post ) : array { /** @var WP_User[] */ $users = get_users( [ - 'blog_id' => 0, // Check all sites. + // Check all sites. + 'blog_id' => 0, 'include' => $author_ids, 'orderby' => 'include', ] ); @@ -156,7 +157,8 @@ function set_authors( WP_Post $post, array $authors ) : array { /** @var WP_User[] */ $users = get_users( [ - 'blog_id' => 0, // Check all sites. + // Check all sites. + 'blog_id' => 0, 'include' => $authors, 'orderby' => 'include', ] ); From 7fee0642ce66f2e70ea7f54bf90d81e143b63d98 Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Wed, 17 Aug 2022 11:18:20 +0100 Subject: [PATCH 4/6] Fix code style for comments --- inc/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/template.php b/inc/template.php index 152d53b..761288a 100644 --- a/inc/template.php +++ b/inc/template.php @@ -157,7 +157,7 @@ function set_authors( WP_Post $post, array $authors ) : array { /** @var WP_User[] */ $users = get_users( [ - // Check all sites. + // Check all sites. 'blog_id' => 0, 'include' => $authors, 'orderby' => 'include', From 49bccb2c92bdb07ae06785b75b8b28cecd40a41d Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Wed, 17 Aug 2022 13:56:59 +0100 Subject: [PATCH 5/6] Add multisite specific tests --- tests/phpunit/test-multisite.php | 95 ++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tests/phpunit/test-multisite.php diff --git a/tests/phpunit/test-multisite.php b/tests/phpunit/test-multisite.php new file mode 100644 index 0000000..ce6b675 --- /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(); + } + +} From c2712352b3658f01e8c35a481f8c811329f757c0 Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Wed, 17 Aug 2022 13:58:52 +0100 Subject: [PATCH 6/6] Fix code style issue, double quote -> single --- tests/phpunit/test-multisite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/test-multisite.php b/tests/phpunit/test-multisite.php index ce6b675..6ab46e2 100644 --- a/tests/phpunit/test-multisite.php +++ b/tests/phpunit/test-multisite.php @@ -41,7 +41,7 @@ public static function wpSetUpBeforeClass( \WP_UnitTest_Factory $factory ) { self::$super_admin = $factory->user->create_and_get( [ 'role' => 'administrator', 'display_name' => 'Super Admin', - 'user_email' => "super-admin.role@example.org", + 'user_email' => 'super-admin.role@example.org', ] ); grant_super_admin( self::$super_admin->ID );