diff --git a/src/features/class-full-page-cache-404.php b/src/features/class-full-page-cache-404.php index 93ee09c..3756b12 100644 --- a/src/features/class-full-page-cache-404.php +++ b/src/features/class-full-page-cache-404.php @@ -85,6 +85,15 @@ public static function get_stale_cache_time(): int { return apply_filters( 'wp_404_caching_stale_cache_time', self::DEFAULT_STALE_CACHE_TIME ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound } + /** + * Check if caching is enabled. + * + * @return bool + */ + public static function caching_enabled(): bool { + return apply_filters( 'wp_404_caching_enabled', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound + } + /** * Boot the feature. */ @@ -101,10 +110,6 @@ public function boot(): void { return; } - if ( ! apply_filters( 'wp_404_caching_enabled', true ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound - return; - } - // Return 404 page cache on template_redirect. add_action( 'template_redirect', [ self::class, 'action__template_redirect' ], 1 ); @@ -126,6 +131,10 @@ public function boot(): void { */ public static function action__template_redirect(): void { + if ( ! self::caching_enabled() ) { + return; + } + // Don't cache if not a 404. if ( ! is_404() ) { return; @@ -212,6 +221,10 @@ public static function send_header( string $type, bool $stale = false ): void { * @global WP_Query $wp_query WordPress database access object. */ public static function action__wp(): void { + if ( ! self::caching_enabled() ) { + return; + } + if ( isset( $_SERVER['REQUEST_URI'] ) && self::TEMPLATE_GENERATOR_URI === $_SERVER['REQUEST_URI'] ) { global $wp_query; @@ -319,6 +332,10 @@ public static function prepare_response( string $content ): string { * Spin up a request to the guaranteed 404 page to populate the cache. */ public static function trigger_404_page_cache(): void { + if ( ! self::caching_enabled() ) { + return; + } + $url = home_url( self::TEMPLATE_GENERATOR_URI, 'https' ); // Replace http with https to ensure the styles don't get blocked due to insecure content.