Skip to content

Commit

Permalink
Model core's behavior when determining whether the author archive sho…
Browse files Browse the repository at this point in the history
…uld 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
  • Loading branch information
danielbachhuber committed Mar 8, 2014
1 parent 5a8c9fa commit 2ce2ce8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
38 changes: 27 additions & 11 deletions co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -912,28 +912,44 @@ 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,
* the query_var is changed.
*
* 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;
}
}

Expand Down
79 changes: 79 additions & 0 deletions tests/test-author-queries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Test Co-Authors Plus' modifications of author queries
*/

class Test_Author_Queries extends CoAuthorsPlus_TestCase {

/**
* On author pages, the queried object should only be set
* to a user that's not a member of the blog if they
* have at least one published post. This matches core behavior.
*
* @see https://core.trac.wordpress.org/changeset/27290
*/
function test_author_queried_object_fix() {
global $wp_rewrite, $coauthors_plus;

/**
* Set up
*/
$author1 = $this->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();

}

}

0 comments on commit 2ce2ce8

Please sign in to comment.