From 0ad07266171153be3ed206fbf83f5ba862100446 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 23 Oct 2023 15:40:44 +0000 Subject: [PATCH] Sitemaps: add `lastmod` for individual posts and the homepage. When the XML sitemaps feature was originally introduced, the `lastmod` field was omitted because guidance at the time indicated it was less important for search engines, plus for some entities it was computationally expensive to add. Now that the guidance has slightly changed, we are revisiting this and adding `lastmod` where easily possible. - Adds `lastmod` to all individual post objects (of any post type) in the sitemap - Adds `lastmod` to the homepage sitemap entry if the homepage is set to display the latest posts. No `lastmod` is added for the individual sitemap pages in the sitemap index, nor for term archives or user archives. Those enhancements require additional changes, such as storing the modified date for a taxonomy term when something is added to that term. They can be revisited in separate follow-up tickets. Props swissspidy, joemcgill. Fixes #52099 git-svn-id: https://develop.svn.wordpress.org/trunk@56985 602fd350-edb4-49c9-b593-d223f7449a82 --- .../providers/class-wp-sitemaps-posts.php | 25 ++++++++++++++++++- tests/phpunit/tests/sitemaps/sitemaps.php | 8 ++++-- .../tests/sitemaps/wpSitemapsPosts.php | 15 ++++++++--- 3 files changed, 42 insertions(+), 6 deletions(-) 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 dff85a70e1..581b4ca2a3 100644 --- a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php +++ b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php @@ -112,6 +112,28 @@ class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { 'loc' => home_url( '/' ), ); + /* + * Get the most recent posts displayed on the homepage, + * and then sort them by their modified date to find + * the date the homepage was approximately last updated. + */ + $latest_posts = new WP_Query( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'orderby' => 'date', + 'order' => 'DESC', + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + ) + ); + + if ( ! empty( $latest_posts->posts ) ) { + $posts = wp_list_sort( $latest_posts->posts, 'post_modified_gmt', 'DESC' ); + $sitemap_entry['lastmod'] = wp_date( DATE_W3C, strtotime( $posts[0]->post_modified_gmt ) ); + } + /** * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'. * @@ -125,7 +147,8 @@ class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { foreach ( $query->posts as $post ) { $sitemap_entry = array( - 'loc' => get_permalink( $post ), + 'loc' => get_permalink( $post ), + 'lastmod' => wp_date( DATE_W3C, strtotime( $post->post_modified_gmt ) ), ); /** diff --git a/tests/phpunit/tests/sitemaps/sitemaps.php b/tests/phpunit/tests/sitemaps/sitemaps.php index a4fa6b14d7..349bdd961c 100644 --- a/tests/phpunit/tests/sitemaps/sitemaps.php +++ b/tests/phpunit/tests/sitemaps/sitemaps.php @@ -251,13 +251,16 @@ class Tests_Sitemaps_Sitemaps extends WP_UnitTestCase { $post_list = $providers['posts']->get_url_list( 1, 'page' ); + $post_list_sorted = wp_list_sort( $post_list, 'lastmod', 'DESC' ); + $expected = $this->_get_expected_url_list( 'page', self::$pages ); // Add the homepage to the front of the URL list. array_unshift( $expected, array( - 'loc' => home_url( '/' ), + 'loc' => home_url( '/' ), + 'lastmod' => $post_list_sorted[0]['lastmod'], ) ); @@ -378,7 +381,8 @@ class Tests_Sitemaps_Sitemaps extends WP_UnitTestCase { return array_map( static function ( $post ) { return array( - 'loc' => get_permalink( $post ), + 'loc' => get_permalink( $post ), + 'lastmod' => get_post_modified_time( DATE_W3C, true, $post ), ); }, $posts diff --git a/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php b/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php index 2bdeb92e8e..70b2fbc250 100644 --- a/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php +++ b/tests/phpunit/tests/sitemaps/wpSitemapsPosts.php @@ -59,14 +59,20 @@ class Tests_Sitemaps_wpSitemapsPosts extends WP_UnitTestCase { $url_list = $posts_provider->get_url_list( 1, 'page' ); $sitemap_entry = array_shift( $url_list ); - $this->assertArrayHasKey( 'lastmod', $sitemap_entry ); + $this->assertEqualSetsWithIndex( + array( + 'loc' => home_url( '/' ), + 'lastmod' => '2000-01-01', + ), + $sitemap_entry + ); } /** * Callback for 'wp_sitemaps_posts_show_on_front_entry' filter. */ public function _show_on_front_entry( $sitemap_entry ) { - $sitemap_entry['lastmod'] = wp_date( DATE_W3C, time() ); + $sitemap_entry['lastmod'] = '2000-01-01'; return $sitemap_entry; } @@ -93,7 +99,10 @@ class Tests_Sitemaps_wpSitemapsPosts extends WP_UnitTestCase { $expected = array(); foreach ( $post_ids as $post_id ) { - $expected[] = array( 'loc' => home_url( "?p={$post_id}" ) ); + $expected[] = array( + 'loc' => home_url( "?p={$post_id}" ), + 'lastmod' => get_post_modified_time( DATE_W3C, true, $post_id ), + ); } // Check that the URL list is still in the order of the post IDs (i.e., sticky post wasn't moved to the front).