From 2ce2ce88f75d848440da2395cd3bbe375bdac537 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 7 Mar 2014 16:15:59 -0800 Subject: [PATCH] Model core's behavior when determining whether the author archive should be displayed The author archive should be displayed when the user is a member of the blog, or the user has a previously published post on the blog. The author archive shouldn't ever be displayed when the user has no published posts on the blog. See https://core.trac.wordpress.org/changeset/27290 Fixes #133, #169 --- co-authors-plus.php | 38 ++++++++++++----- tests/test-author-queries.php | 79 +++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 tests/test-author-queries.php diff --git a/co-authors-plus.php b/co-authors-plus.php index 3d6c377b..a35468fb 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -242,7 +242,7 @@ function get_coauthor_by( $key, $value, $force = false ) { if ( 'login' == $key || 'slug' == $key ) $value = preg_replace( '#^cap\-#', '', $value ); $user = get_user_by( $key, $value ); - if ( !$user || !is_user_member_of_blog( $user->ID ) ) + if ( ! $user ) return false; $user->type = 'wpuser'; // However, if guest authors are enabled and there's a guest author linked to this @@ -912,8 +912,10 @@ function current_user_can_set_authors( $post = null ) { /** * Fix for author pages 404ing or not properly displaying on author pages * - * If an author has no posts, we need to still force the queried object to be - * set in case a site wants to still display the author's profile. + * If an author has no posts, we only want to force the queried object to be + * the author if they're a member of the blog. + * + * If the author does have posts, it doesn't matter that they're not an author. * * Alternatively, on an author archive, if the first story has coauthors and * the first author is NOT the same as the author for the archive, @@ -921,19 +923,33 @@ function current_user_can_set_authors( $post = null ) { * * Also, we have to do some hacky WP_Query modification for guest authors */ - function fix_author_page() { + public function fix_author_page() { - if ( !is_author() ) + if ( ! is_author() ) { return; + } + + $author_name = sanitize_title( get_query_var( 'author_name' ) ); + if ( ! $author_name ) { + return; + } + + $author = $this->get_coauthor_by( 'user_nicename', $author_name ); global $wp_query, $authordata; - if ( $author_name = sanitize_title( get_query_var( 'author_name' ) ) ) { - $authordata = $this->get_coauthor_by( 'user_nicename', $author_name ); - if ( is_object( $authordata ) ) { - $wp_query->queried_object = $authordata; - $wp_query->queried_object_id = $authordata->ID; - } + if ( is_object( $author ) ) { + $authordata = $author; + $term = $this->get_author_term( $authordata ); + } + if ( ( is_object( $authordata ) && is_user_member_of_blog( $authordata->ID ) ) + || ( ! empty( $term ) && $term->count ) ) { + $wp_query->queried_object = $authordata; + $wp_query->queried_object_id = $authordata->ID; + } else { + $wp_query->queried_object = $wp_query->queried_object_id = null; + $wp_query->is_author = $wp_query->is_archive = false; + $wp_query->is_404 = false; } } diff --git a/tests/test-author-queries.php b/tests/test-author-queries.php new file mode 100644 index 00000000..31c458cf --- /dev/null +++ b/tests/test-author-queries.php @@ -0,0 +1,79 @@ +factory->user->create( array( 'user_login' => 'msauthor1' ) ); + $author2 = $this->factory->user->create( array( 'user_login' => 'msauthor2' ) ); + $blog2 = $this->factory->blog->create( array( 'user_id' => $author1 ) ); + + switch_to_blog( $blog2 ); + $wp_rewrite->init(); + + $blog2_post1 = $this->factory->post->create( array( + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_author' => $author1, + ) ); + + /** + * Author 1 is an author on the blog + */ + $this->go_to( get_author_posts_url( $author1 ) ); + $this->assertQueryTrue( 'is_author', 'is_archive' ); + + /** + * Author 2 is not yet an author on the blog + */ + $this->go_to( get_author_posts_url( $author2 ) ); + $this->assertQueryTrue( 'is_404' ); + + // Add the user to the blog + add_user_to_blog( $blog2, $author2, 'author' ); + + /** + * Author 2 is now on the blog, but not yet published + */ + $this->go_to( get_author_posts_url( $author2 ) ); + $this->assertQueryTrue( 'is_author', 'is_archive' ); + + // Add the user as an author on the original post + $author2_obj = get_user_by( 'id', $author2 ); + $coauthors_plus->add_coauthors( $blog2_post1, array( $author2_obj->user_login ), true ); + + /** + * Author 2 is now on the blog, and published + */ + $this->go_to( get_author_posts_url( $author2 ) ); + $this->assertQueryTrue( 'is_author', 'is_archive' ); + + // Remove the user from the blog + remove_user_from_blog( $author2, $blog2 ); + + /** + * Author 2 was removed from the blog, but still a published author + */ + $this->go_to( get_author_posts_url( $author2 ) ); + $this->assertQueryTrue( 'is_author', 'is_archive' ); + + restore_current_blog(); + + } + +} \ No newline at end of file