Skip to content

Commit

Permalink
Clear the month dropdown cache when a post is saved and the posts mon…
Browse files Browse the repository at this point in the history
…th is not included
  • Loading branch information
srtfisher committed Mar 6, 2024
1 parent ceb12df commit 0e0d1f4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
69 changes: 61 additions & 8 deletions src/alley/wp/alleyvate/features/class-cache-slow-queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,92 @@
* Cache slow queries that do not perform well for large sites.
*/
final class Cache_Slow_Queries implements Feature {
/**
* Whether to cache the months dropdown.
*
* @var bool
*/
public bool $cache_months_dropdown = false;

/**
* Boot the feature.
*/
public function boot(): void {
/**
* Filter if the months dropdown query should be cached.
*
* @param bool $cache_months_dropdown Whether to cache the months dropdown. Default true.
*/
if ( apply_filters( 'alleyvate_cache_months_dropdown', true ) ) {
add_filter( 'pre_months_dropdown_query', [ $this, 'filter__pre_months_dropdown_query' ], 10, 2 );
add_filter( 'months_dropdown_results', [ $this, 'filter__months_dropdown_results' ], 10, 2 );
add_action( 'save_post', [ $this, 'action__save_post' ], 10, 2 );
}
}

/**
* Filter the pre months dropdown query to return the cached result.
*
* @param object|false $months 'Months' drop-down results. Default false.
* @param string $post_type The post type.
* @return object|false
* @param object[]|false $months 'Months' drop-down results. Default false.
* @param string $post_type The post type.
* @return object[]|false
*/
public function filter__pre_months_dropdown_query( $months, $post_type ) {
$cache = wp_cache_get( $post_type, 'alleyvate_months_dropdown' );

return false !== $cache && \is_object( $cache ) ? $cache : $months;
if ( is_array( $cache ) ) {
return $cache;
}

// Set the flag to cache the months dropdown.
$this->cache_months_dropdown = true;

return $months;
}

/**
* Filter the months dropdown results.
*
* @param object $months Array of the months drop-down query results.
* @param string $post_type The post type.
* @return object|false
* @param object[] $months Array of the months drop-down query results.
* @param string $post_type The post type.
* @return object[]|false
*/
public function filter__months_dropdown_results( $months, $post_type ) {
wp_cache_set( $post_type, $months, 'alleyvate_months_dropdown', DAY_IN_SECONDS );
if ( $this->cache_months_dropdown ) {
wp_cache_set( $post_type, $months, 'alleyvate_months_dropdown', DAY_IN_SECONDS );

$this->cache_months_dropdown = false;
}

return $months;
}

/**
* Action to delete the cache when a post is published.
*
* @param int $post_id The post ID.
* @param \WP_Post $post The post object.
*/
public function action__save_post( $post_id, $post ): void {
if ( 'publish' !== $post->post_status ) {
return;
}

$cache = wp_cache_get( $post->post_type, 'alleyvate_months_dropdown' );

if ( ! is_array( $cache ) ) {
return;
}

// Check if the post's month is in the cache.
$cache = array_map(
fn ( $month ) => "{$month->year}-{$month->month}",
$cache,
);

// Clear the month dropdown cache if the post's month is not in the cache.
if ( ! in_array( get_the_date( 'Y-n', $post ), $cache, true ) ) {
wp_cache_delete( $post->post_type, 'alleyvate_months_dropdown' );
}
}
}
27 changes: 27 additions & 0 deletions tests/alley/wp/alleyvate/features/test-cache-slow-queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,31 @@ public function test_optimize_months_dropdown(): void {

$this->assertNotEmpty( wp_cache_get( 'post', 'alleyvate_months_dropdown' ) );
}

/**
* Test that the cache is cleared for the months dropdown query when the post is published.
*/
public function test_optimize_months_dropdown_cache_cleared_on_publish() {
self::factory()->post->create( [ 'post_date' => '2020-01-05 00:00:00' ] );

// Prime the cache with some old months.
wp_cache_set(
'post',
[
(object) [
'year' => 2020,
'month' => 1,
],
],
'alleyvate_months_dropdown'
);

// Boot feature.
$this->feature->boot();

// Publish a post which should clear the cache.
self::factory()->post->create( [ 'post_date' => '2022-02-05 00:00:00' ] );

$this->assertEmpty( wp_cache_get( 'post', 'alleyvate_months_dropdown' ) );
}
}

0 comments on commit 0e0d1f4

Please sign in to comment.