Skip to content

Commit

Permalink
Merge pull request #8 from alleyinteractive/feature/caching-enabled-u…
Browse files Browse the repository at this point in the history
…pdate

Check if caching is enabled after boot
  • Loading branch information
jacobschweitzer committed Mar 22, 2024
2 parents 6957628 + 644e124 commit 79dfb65
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/features/class-full-page-cache-404.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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 );

Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 79dfb65

Please sign in to comment.