Skip to content

Commit

Permalink
Sitemaps: do not list users who only authored pages.
Browse files Browse the repository at this point in the history
Author archives are only generated for users who created at least one post.
Prevent adding author archives to the XML sitemap for users who only authored pages
as the links would otherwise result in a 404.

Props zodiac1978, huzaifaalmesbah.
Fixes #57816.

git-svn-id: https://develop.svn.wordpress.org/trunk@56708 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
swissspidy authored and Anton Vlasenko committed Sep 29, 2023
1 parent 82016e4 commit 6d35429
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ protected function get_users_query_args() {
)
);

// We're not supporting sitemaps for author pages for attachments.
// We're not supporting sitemaps for author pages for attachments and pages.
unset( $public_post_types['attachment'] );
unset( $public_post_types['page'] );

/**
* Filters the query arguments for authors with public posts.
Expand Down
40 changes: 37 additions & 3 deletions tests/phpunit/tests/sitemaps/wpSitemapsUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/**
* @group sitemaps
*
* @coversDefaultClass WP_Sitemaps_Users
*/
class Tests_Sitemaps_wpSitemapsUsers extends WP_UnitTestCase {

Expand All @@ -10,14 +12,14 @@ class Tests_Sitemaps_wpSitemapsUsers extends WP_UnitTestCase {
*
* @var array
*/
public static $users;
private static $users;

/**
* Editor ID for use in some tests.
*
* @var int
*/
public static $editor_id;
private static $editor_id;

/**
* Set up fixtures.
Expand All @@ -32,6 +34,8 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
/**
* Test getting a URL list for a users sitemap page via
* WP_Sitemaps_Users::get_url_list().
*
* @covers ::get_url_list
*/
public function test_get_url_list_users() {
// Set up the user to an editor to assign posts to other users.
Expand All @@ -40,7 +44,7 @@ public function test_get_url_list_users() {
// Create a set of posts for each user and generate the expected URL list data.
$expected = array_map(
static function ( $user_id ) {
$post = self::factory()->post->create_and_get( array( 'post_author' => $user_id ) );
self::factory()->post->create( array( 'post_author' => $user_id ) );

return array(
'loc' => get_author_posts_url( $user_id ),
Expand All @@ -55,4 +59,34 @@ static function ( $user_id ) {

$this->assertSameSets( $expected, $url_list );
}

/**
* @covers ::get_url_list
* @covers ::get_users_query_args
*/
public function test_get_url_list_skips_users_with_only_attachments_and_pages() {
// Set up the user to an editor to assign posts to other users.
wp_set_current_user( self::$editor_id );

foreach ( self::$users as $user_id ) {
self::factory()->post->create(
array(
'post_author' => $user_id,
'post_type' => 'attachment',
)
);
self::factory()->post->create(
array(
'post_author' => $user_id,
'post_type' => 'page',
)
);
}

$user_provider = new WP_Sitemaps_Users();

$url_list = $user_provider->get_url_list( 1 );

$this->assertEmpty( $url_list );
}
}

0 comments on commit 6d35429

Please sign in to comment.