Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions inc/sitemaps/class-post-type-sitemap-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ protected function get_posts( $post_type, $count, $offset ) {
$post_ids[] = $sanitized_post->ID;
}

/**
* Filter to disable priming the post and term caches for the sitemap.
*
* @since 28.0
*
* @param bool $disable_priming_post_caches Whether to disable priming the post and term caches. Defaults to false.
*/
if ( ! apply_filters( 'wpseo_disable_priming_post_caches_sitemap', false ) ) {
// Warm the post and term caches in bulk, so permalink and image building doesn't query per post.
_prime_post_caches( $post_ids, true, false );
}
update_meta_cache( 'post', $post_ids );

return $posts;
Expand Down
77 changes: 77 additions & 0 deletions tests/WP/Sitemaps/Post_Type_Sitemap_Provider_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,83 @@ public function test_password_protected_post_parent_attachment() {
$this->assertCount( 0, self::$class_instance->get_sitemap_links( 'attachment', 100, 0 ) );
}

/**
* Tests that generating sitemap links runs a constant number of queries, regardless of the post count.
*
* @covers ::get_sitemap_links
*
* @return void
*/
public function test_get_sitemap_links_query_count_does_not_grow_with_post_count() {
// A %category% permalink structure makes get_permalink() look up each post's terms.
$this->set_permalink_structure( '/%category%/%postname%/' );

$this->create_posts_with_own_category( 3 );
$queries_for_three_posts = $this->count_sitemap_links_queries();

$this->create_posts_with_own_category( 7 );
$queries_for_ten_posts = $this->count_sitemap_links_queries();

$this->assertSame(
$queries_for_three_posts,
$queries_for_ten_posts,
'Generating sitemap links should not run more queries when there are more posts',
);
}
Comment thread
Copilot marked this conversation as resolved.

/**
* Tests that the cache priming can be disabled through the wpseo_disable_priming_post_caches_sitemap filter.
*
* @covers ::get_sitemap_links
*
* @return void
*/
public function test_priming_post_caches_can_be_disabled_via_filter() {
// A %category% permalink structure makes get_permalink() look up each post's terms.
$this->set_permalink_structure( '/%category%/%postname%/' );

$this->create_posts_with_own_category( 3 );
$queries_with_priming = $this->count_sitemap_links_queries();

\add_filter( 'wpseo_disable_priming_post_caches_sitemap', '__return_true' );
$queries_without_priming = $this->count_sitemap_links_queries();
\remove_filter( 'wpseo_disable_priming_post_caches_sitemap', '__return_true' );

$this->assertGreaterThan(
$queries_with_priming,
$queries_without_priming,
'Disabling the cache priming through the filter should make per-post queries return',
);
}
Comment thread
Copilot marked this conversation as resolved.
Outdated

/**
* Creates posts that each have their own category.
*
* @param int $count The number of posts to create.
*
* @return void
*/
private function create_posts_with_own_category( $count ) {
for ( $i = 0; $i < $count; $i++ ) {
$category_id = $this->factory->category->create();
$this->factory->post->create( [ 'post_category' => [ $category_id ] ] );
}
}

/**
* Counts the number of database queries run to generate the post sitemap links, starting from a cold cache.
*
* @return int The number of queries.
*/
private function count_sitemap_links_queries() {
\wp_cache_flush();

$queries_before = \get_num_queries();
self::$class_instance->get_sitemap_links( 'post', 100, 1 );

return ( \get_num_queries() - $queries_before );
}

/**
* Filter to exclude desired posts from the sitemap.
*
Expand Down
Loading