Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if caching is enabled after boot #8

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Changes from all 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
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