From 0ee9100e6d4b85cfccd39c9e6847a19da25211ee Mon Sep 17 00:00:00 2001 From: swissspidy Date: Tue, 14 Jul 2020 12:24:02 +0000 Subject: [PATCH] =?UTF-8?q?Sitemaps:=20Ensure=20entry=20for=20=E2=80=98pag?= =?UTF-8?q?e=E2=80=99=20post=20type=20sitemap=20in=20index.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If there are no pages and no static homepage, there will still be one sitemap including the homepage URL. This change ensures that this sitemap is correctly listed in the sitemap index. Props Chouby, pacifika, elrae. Fixes #50571. git-svn-id: https://develop.svn.wordpress.org/trunk@48476 602fd350-edb4-49c9-b593-d223f7449a82 --- .../providers/class-wp-sitemaps-posts.php | 3 ++- .../phpunit/tests/sitemaps/sitemaps-posts.php | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php index e03af6e357..56acfb7697 100644 --- a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php +++ b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php @@ -174,7 +174,8 @@ class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { $query = new WP_Query( $args ); - return isset( $query->max_num_pages ) ? $query->max_num_pages : 1; + $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0; + return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1; } /** diff --git a/tests/phpunit/tests/sitemaps/sitemaps-posts.php b/tests/phpunit/tests/sitemaps/sitemaps-posts.php index 8d173942f4..d102518587 100644 --- a/tests/phpunit/tests/sitemaps/sitemaps-posts.php +++ b/tests/phpunit/tests/sitemaps/sitemaps-posts.php @@ -4,6 +4,29 @@ * @group sitemaps */ class Test_WP_Sitemaps_Posts extends WP_UnitTestCase { + /** + * Tests getting sitemap entries for post type page with 'posts' homepage. + * + * Ensures that an entry is added even if there are no pages. + * + * @ticket 50571 + */ + public function test_get_sitemap_entries_homepage() { + update_option( 'show_on_front', 'posts' ); + + $posts_provider = new WP_Sitemaps_Posts(); + + $post_list = $posts_provider->get_sitemap_entries(); + + $expected = array( + array( + 'loc' => home_url( '/?sitemap=posts&sitemap-subtype=page&paged=1' ), + ), + ); + + $this->assertEquals( $expected, $post_list ); + } + /** * Test ability to filter object subtypes. */