From bd889f2c034c9624afd4c769d841d69e040fdf26 Mon Sep 17 00:00:00 2001 From: Damian Taggart <4309872+attackant@users.noreply.github.com> Date: Tue, 12 Mar 2024 09:15:15 -0600 Subject: [PATCH] Add default cache time constants and getter methods in WP 404 cache class Added DEFAULT_CACHE_TIME and DEFAULT_STALE_CACHE_TIME constants to class-full-page-cache-404.php. Also introduced respective getter methods for each one of them: get_cache_time() and get_stale_cache_time(). Consequently updated the calls in set_cache() method to use these getters. --- src/features/class-full-page-cache-404.php | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/features/class-full-page-cache-404.php b/src/features/class-full-page-cache-404.php index 53b191c7..4d07cbd4 100644 --- a/src/features/class-full-page-cache-404.php +++ b/src/features/class-full-page-cache-404.php @@ -59,6 +59,20 @@ final class Full_Page_Cache_404 implements Feature { */ public const STALE_CACHE_TIME = DAY_IN_SECONDS; + /** + * Cache time. + * + * @var int + */ + public const DEFAULT_CACHE_TIME = HOUR_IN_SECONDS; + + /** + * Stale cache time. + * + * @var int + */ + public const DEFAULT_STALE_CACHE_TIME = DAY_IN_SECONDS; + /** * Guaranteed 404 URI. * Used for populating the cache. @@ -67,6 +81,24 @@ final class Full_Page_Cache_404 implements Feature { */ public const TEMPLATE_GENERATOR_URI = '/wp-404-caching/404-template-generator/?generate=1&uri=1'; + /** + * Get cache time. + * + * @return int + */ + public static function get_cache_time(): int { + return apply_filters( 'wp_404_caching_cache_time', self::DEFAULT_CACHE_TIME ); + } + + /** + * Get stale cache time. + * + * @return int + */ + public static function get_stale_cache_time(): int { + return apply_filters( 'wp_404_caching_stale_cache_time', self::DEFAULT_STALE_CACHE_TIME ); + } + /** * Boot the feature. */ @@ -254,8 +286,8 @@ public static function get_stale_cache(): mixed { * @param string $buffer The Output Buffer. */ public static function set_cache( string $buffer ): void { - wp_cache_set( self::CACHE_KEY, $buffer, self::CACHE_GROUP, self::CACHE_TIME ); // phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined - wp_cache_set( self::STALE_CACHE_KEY, $buffer, self::CACHE_GROUP, self::STALE_CACHE_TIME ); // phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined + wp_cache_set( self::CACHE_KEY, $buffer, self::CACHE_GROUP, self::get_cache_time() ); // phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined + wp_cache_set( self::STALE_CACHE_KEY, $buffer, self::CACHE_GROUP, self::get_stale_cache_time() ); // phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined } /**