diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-provider.php b/src/wp-includes/sitemaps/class-wp-sitemaps-provider.php
index 3440f62b34..7a12e2530b 100644
a
|
b
|
abstract class WP_Sitemaps_Provider { |
111 | 111 | 'loc' => $this->get_sitemap_url( $type['name'], $page ), |
112 | 112 | ); |
113 | 113 | |
| 114 | // Get last modified date. |
| 115 | $args = array( |
| 116 | 'post_type' => 'any', |
| 117 | 'post_status' => 'publish', |
| 118 | 'posts_per_page' => 1, |
| 119 | 'no_found_rows' => true, |
| 120 | 'update_post_meta_cache' => false, |
| 121 | 'update_post_term_cache' => false, |
| 122 | 'orderby' => 'modified', |
| 123 | 'order' => 'DESC', |
| 124 | ); |
| 125 | |
| 126 | switch ( $this->object_type ) { |
| 127 | case 'post': |
| 128 | $args['post_type'] = $type['name']; |
| 129 | break; |
| 130 | case 'term': |
| 131 | $args['update_post_term_cache'] = true; |
| 132 | $args['tax_query'] = array( |
| 133 | array( |
| 134 | 'taxonomy' => $type['name'], |
| 135 | 'operator' => 'ANY', |
| 136 | ), |
| 137 | ); |
| 138 | break; |
| 139 | case 'user': |
| 140 | $args['post_type'] = 'post'; |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | $post = new WP_Query( $args ); |
| 145 | |
| 146 | if ( ! empty( $post->posts ) ) { |
| 147 | $sitemap_entry['lastmod'] = gmdate( 'c', strtotime( end( $post->posts )->post_modified_gmt ) ); |
| 148 | } |
| 149 | |
114 | 150 | /** |
115 | 151 | * Filters the sitemap entry for the sitemap index. |
116 | 152 | * |
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 56acfb7697..6e6a25ed4c 100644
a
|
b
|
class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { |
106 | 106 | 'loc' => home_url( '/' ), |
107 | 107 | ); |
108 | 108 | |
| 109 | // Get last modified date from the latest post. |
| 110 | $latest_post = new WP_Query( |
| 111 | array( |
| 112 | 'post_type' => 'post', |
| 113 | 'orderby' => 'modified', |
| 114 | 'order' => 'DESC', |
| 115 | 'posts_per_page' => 1, |
| 116 | 'no_found_rows' => true, |
| 117 | 'update_post_meta_cache' => false, |
| 118 | 'update_post_term_cache' => false, |
| 119 | ) |
| 120 | ); |
| 121 | |
| 122 | if ( ! empty( $latest_post->posts ) ) { |
| 123 | $sitemap_entry['lastmod'] = gmdate( 'c', strtotime( end( $latest_post->posts )->post_modified_gmt ) ); |
| 124 | } |
| 125 | |
109 | 126 | /** |
110 | 127 | * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'. |
111 | 128 | * |
… |
… |
class WP_Sitemaps_Posts extends WP_Sitemaps_Provider { |
119 | 136 | |
120 | 137 | foreach ( $query->posts as $post ) { |
121 | 138 | $sitemap_entry = array( |
122 | | 'loc' => get_permalink( $post ), |
| 139 | 'loc' => get_permalink( $post ), |
| 140 | 'lastmod' => gmdate( 'c', strtotime( $post->post_modified_gmt ) ), |
123 | 141 | ); |
124 | 142 | |
125 | 143 | /** |
diff --git a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php
index e8fcc6d5ab..2cc71c4e70 100644
a
|
b
|
class WP_Sitemaps_Taxonomies extends WP_Sitemaps_Provider { |
109 | 109 | 'loc' => $term_link, |
110 | 110 | ); |
111 | 111 | |
| 112 | // Get last modified date from the latest post in this taxonomy. |
| 113 | $post = new WP_Query( |
| 114 | array( |
| 115 | 'post_type' => 'any', |
| 116 | 'post_status' => 'publish', |
| 117 | 'posts_per_page' => 1, |
| 118 | 'no_found_rows' => true, |
| 119 | 'update_post_meta_cache' => false, |
| 120 | 'orderby' => 'modified', |
| 121 | 'order' => 'DESC', |
| 122 | 'tax_query' => array( |
| 123 | array( |
| 124 | 'taxonomy' => $taxonomy, |
| 125 | 'field' => 'term_id', |
| 126 | 'terms' => $term, |
| 127 | 'include_children' => false, |
| 128 | ), |
| 129 | ), |
| 130 | ) |
| 131 | ); |
| 132 | |
| 133 | if ( ! empty( $post->posts ) ) { |
| 134 | $sitemap_entry['lastmod'] = gmdate( 'c', strtotime( end( $post->posts )->post_modified_gmt ) ); |
| 135 | } |
| 136 | |
112 | 137 | /** |
113 | 138 | * Filters the sitemap entry for an individual term. |
114 | 139 | * |
diff --git a/src/wp-includes/sitemaps/providers/class-wp-sitemaps-users.php b/src/wp-includes/sitemaps/providers/class-wp-sitemaps-users.php
index 93e63a3b51..832e753968 100644
a
|
b
|
class WP_Sitemaps_Users extends WP_Sitemaps_Provider { |
70 | 70 | 'loc' => get_author_posts_url( $user->ID ), |
71 | 71 | ); |
72 | 72 | |
| 73 | // Get last modified date from the latest post by this user. |
| 74 | $post = new WP_Query( |
| 75 | array( |
| 76 | 'post_type' => 'post', |
| 77 | 'post_status' => 'publish', |
| 78 | 'posts_per_page' => 1, |
| 79 | 'no_found_rows' => true, |
| 80 | 'update_post_meta_cache' => false, |
| 81 | 'update_post_term_cache' => false, |
| 82 | 'orderby' => 'modified', |
| 83 | 'order' => 'DESC', |
| 84 | 'author' => $user->ID, |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | if ( ! empty( $post->posts ) ) { |
| 89 | $sitemap_entry['lastmod'] = gmdate( 'c', strtotime( end( $post->posts )->post_modified_gmt ) ); |
| 90 | } |
| 91 | |
73 | 92 | /** |
74 | 93 | * Filters the sitemap entry for an individual user. |
75 | 94 | * |
diff --git a/tests/phpunit/tests/sitemaps/sitemaps-taxonomies.php b/tests/phpunit/tests/sitemaps/sitemaps-taxonomies.php
index e98c6b71ee..90c80d628d 100644
a
|
b
|
class Test_WP_Sitemaps_Taxonomies extends WP_UnitTestCase { |
59 | 59 | $expected_cats = array_map( |
60 | 60 | static function ( $id ) use ( $post ) { |
61 | 61 | return array( |
62 | | 'loc' => get_term_link( $id, 'category' ), |
| 62 | 'loc' => get_term_link( $id, 'category' ), |
| 63 | 'lastmod' => gmdate( 'c', strtotime( $post->post_modified_gmt ) ), |
63 | 64 | ); |
64 | 65 | }, |
65 | 66 | $categories |
… |
… |
class Test_WP_Sitemaps_Taxonomies extends WP_UnitTestCase { |
72 | 73 | $expected_tags = array_map( |
73 | 74 | static function ( $id ) use ( $post ) { |
74 | 75 | return array( |
75 | | 'loc' => get_term_link( $id, 'post_tag' ), |
| 76 | 'loc' => get_term_link( $id, 'post_tag' ), |
| 77 | 'lastmod' => gmdate( 'c', strtotime( $post->post_modified_gmt ) ), |
76 | 78 | ); |
77 | 79 | }, |
78 | 80 | self::$post_tags |
… |
… |
class Test_WP_Sitemaps_Taxonomies extends WP_UnitTestCase { |
101 | 103 | $expected = array_map( |
102 | 104 | static function ( $id ) use ( $taxonomy, $post ) { |
103 | 105 | return array( |
104 | | 'loc' => get_term_link( $id, $taxonomy ), |
| 106 | 'loc' => get_term_link( $id, $taxonomy ), |
| 107 | 'lastmod' => gmdate( 'c', strtotime( $post->post_modified_gmt ) ), |
105 | 108 | ); |
106 | 109 | }, |
107 | 110 | $terms |
diff --git a/tests/phpunit/tests/sitemaps/sitemaps-users.php b/tests/phpunit/tests/sitemaps/sitemaps-users.php
index 3cd76d3376..917608c957 100644
a
|
b
|
class Test_WP_Sitemaps_Users extends WP_UnitTestCase { |
42 | 42 | $post = self::factory()->post->create_and_get( array( 'post_author' => $user_id ) ); |
43 | 43 | |
44 | 44 | return array( |
45 | | 'loc' => get_author_posts_url( $user_id ), |
| 45 | 'loc' => get_author_posts_url( $user_id ), |
| 46 | 'lastmod' => gmdate( 'c', strtotime( $post->post_modified_gmt ) ), |
46 | 47 | ); |
47 | 48 | }, |
48 | 49 | self::$users |
diff --git a/tests/phpunit/tests/sitemaps/sitemaps.php b/tests/phpunit/tests/sitemaps/sitemaps.php
index da47954e8e..3595e46255 100644
a
|
b
|
class Test_Sitemaps extends WP_UnitTestCase { |
116 | 116 | public function test_get_sitemap_entries() { |
117 | 117 | $entries = $this->_get_sitemap_entries(); |
118 | 118 | |
| 119 | $latest_post = $this->_get_latest_post(); |
| 120 | $last_modified_post = gmdate( 'c', strtotime( $latest_post->post_modified_gmt ) ); |
| 121 | |
| 122 | $latest_page = $this->_get_latest_post( 'page' ); |
| 123 | $last_modified_page = gmdate( 'c', strtotime( $latest_page->post_modified_gmt ) ); |
| 124 | |
119 | 125 | $expected = array( |
120 | 126 | array( |
121 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=post&paged=1', |
| 127 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=post&paged=1', |
| 128 | 'lastmod' => $last_modified_post, |
122 | 129 | ), |
123 | 130 | array( |
124 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=page&paged=1', |
| 131 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=page&paged=1', |
| 132 | 'lastmod' => $last_modified_page, |
125 | 133 | ), |
126 | 134 | array( |
127 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=category&paged=1', |
| 135 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=category&paged=1', |
| 136 | 'lastmod' => $last_modified_post, |
128 | 137 | ), |
129 | 138 | array( |
130 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1', |
| 139 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1', |
| 140 | 'lastmod' => $last_modified_post, |
131 | 141 | ), |
132 | 142 | array( |
133 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=users&paged=1', |
| 143 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=users&paged=1', |
| 144 | 'lastmod' => $last_modified_post, |
134 | 145 | ), |
135 | 146 | ); |
136 | 147 | |
… |
… |
class Test_Sitemaps extends WP_UnitTestCase { |
145 | 156 | |
146 | 157 | $entries = $this->_get_sitemap_entries(); |
147 | 158 | |
| 159 | $latest_post = $this->_get_latest_post(); |
| 160 | $last_modified = gmdate( 'c', strtotime( $latest_post->post_modified_gmt ) ); |
| 161 | |
| 162 | $latest_page = $this->_get_latest_post( 'page' ); |
| 163 | $last_modified_page = gmdate( 'c', strtotime( $latest_page->post_modified_gmt ) ); |
| 164 | |
148 | 165 | $expected = array( |
149 | 166 | array( |
150 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml', |
| 167 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml', |
| 168 | 'lastmod' => $last_modified, |
151 | 169 | ), |
152 | 170 | array( |
153 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml', |
| 171 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml', |
| 172 | 'lastmod' => $last_modified_page, |
154 | 173 | ), |
155 | 174 | array( |
156 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml', |
| 175 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml', |
| 176 | 'lastmod' => $last_modified, |
157 | 177 | ), |
158 | 178 | array( |
159 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml', |
| 179 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml', |
| 180 | 'lastmod' => $last_modified, |
160 | 181 | ), |
161 | 182 | array( |
162 | | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml', |
| 183 | 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml', |
| 184 | 'lastmod' => $last_modified, |
163 | 185 | ), |
164 | 186 | ); |
165 | 187 | |
… |
… |
class Test_Sitemaps extends WP_UnitTestCase { |
253 | 275 | |
254 | 276 | $expected = $this->_get_expected_url_list( 'page', self::$pages ); |
255 | 277 | |
| 278 | $latest_post = $this->_get_latest_post( 'page' ); |
| 279 | $last_modified = gmdate( 'c', strtotime( $latest_post->post_modified_gmt ) ); |
| 280 | |
256 | 281 | // Add the homepage to the front of the URL list. |
257 | 282 | array_unshift( |
258 | 283 | $expected, |
259 | 284 | array( |
260 | | 'loc' => home_url( '/' ), |
| 285 | 'loc' => home_url( '/' ), |
| 286 | 'lastmod' => $last_modified, |
261 | 287 | ) |
262 | 288 | ); |
263 | 289 | |
… |
… |
class Test_Sitemaps extends WP_UnitTestCase { |
378 | 404 | return array_map( |
379 | 405 | static function ( $post ) { |
380 | 406 | return array( |
381 | | 'loc' => get_permalink( $post ), |
| 407 | 'loc' => get_permalink( $post ), |
| 408 | 'lastmod' => gmdate( 'c', strtotime( $post->post_modified_gmt ) ), |
382 | 409 | ); |
383 | 410 | }, |
384 | 411 | $posts |
385 | 412 | ); |
386 | 413 | } |
387 | 414 | |
| 415 | /** |
| 416 | * Helper function to get the latest post. |
| 417 | * |
| 418 | * @param string $post_type Post type. |
| 419 | * |
| 420 | * @return WP_Post |
| 421 | */ |
| 422 | public function _get_latest_post( $post_type = 'post' ) { |
| 423 | $latest_post = new WP_Query( |
| 424 | array( |
| 425 | 'post_type' => $post_type, |
| 426 | 'orderby' => 'modified', |
| 427 | 'order' => 'DESC', |
| 428 | 'posts_per_page' => 1, |
| 429 | 'no_found_rows' => true, |
| 430 | 'update_post_meta_cache' => false, |
| 431 | 'update_post_term_cache' => false, |
| 432 | ) |
| 433 | ); |
| 434 | return end( $latest_post->posts ); |
| 435 | } |
| 436 | |
388 | 437 | /** |
389 | 438 | * Test functionality that adds a new sitemap provider to the registry. |
390 | 439 | */ |