Skip to content

Commit a52838d

Browse files
committed
Sitemaps: do not list users who only authored pages.
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
1 parent 47387cd commit a52838d

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/wp-includes/sitemaps/providers/class-wp-sitemaps-users.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ protected function get_users_query_args() {
136136
)
137137
);
138138

139-
// We're not supporting sitemaps for author pages for attachments.
139+
// We're not supporting sitemaps for author pages for attachments and pages.
140140
unset( $public_post_types['attachment'] );
141+
unset( $public_post_types['page'] );
141142

142143
/**
143144
* Filters the query arguments for authors with public posts.

tests/phpunit/tests/sitemaps/wpSitemapsUsers.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/**
44
* @group sitemaps
5+
*
6+
* @coversDefaultClass WP_Sitemaps_Users
57
*/
68
class Tests_Sitemaps_wpSitemapsUsers extends WP_UnitTestCase {
79

@@ -10,14 +12,14 @@ class Tests_Sitemaps_wpSitemapsUsers extends WP_UnitTestCase {
1012
*
1113
* @var array
1214
*/
13-
public static $users;
15+
private static $users;
1416

1517
/**
1618
* Editor ID for use in some tests.
1719
*
1820
* @var int
1921
*/
20-
public static $editor_id;
22+
private static $editor_id;
2123

2224
/**
2325
* Set up fixtures.
@@ -32,6 +34,8 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
3234
/**
3335
* Test getting a URL list for a users sitemap page via
3436
* WP_Sitemaps_Users::get_url_list().
37+
*
38+
* @covers ::get_url_list
3539
*/
3640
public function test_get_url_list_users() {
3741
// Set up the user to an editor to assign posts to other users.
@@ -40,7 +44,7 @@ public function test_get_url_list_users() {
4044
// Create a set of posts for each user and generate the expected URL list data.
4145
$expected = array_map(
4246
static function ( $user_id ) {
43-
$post = self::factory()->post->create_and_get( array( 'post_author' => $user_id ) );
47+
self::factory()->post->create( array( 'post_author' => $user_id ) );
4448

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

5660
$this->assertSameSets( $expected, $url_list );
5761
}
62+
63+
/**
64+
* @covers ::get_url_list
65+
* @covers ::get_users_query_args
66+
*/
67+
public function test_get_url_list_skips_users_with_only_attachments_and_pages() {
68+
// Set up the user to an editor to assign posts to other users.
69+
wp_set_current_user( self::$editor_id );
70+
71+
foreach ( self::$users as $user_id ) {
72+
self::factory()->post->create(
73+
array(
74+
'post_author' => $user_id,
75+
'post_type' => 'attachment',
76+
)
77+
);
78+
self::factory()->post->create(
79+
array(
80+
'post_author' => $user_id,
81+
'post_type' => 'page',
82+
)
83+
);
84+
}
85+
86+
$user_provider = new WP_Sitemaps_Users();
87+
88+
$url_list = $user_provider->get_url_list( 1 );
89+
90+
$this->assertEmpty( $url_list );
91+
}
5892
}

0 commit comments

Comments
 (0)