diff --git a/CHANGELOG.md b/CHANGELOG.md index f168cb852..a116dc919 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 This is an alpha version! The changes listed here are not final. +### Added +- Jetpack Boost: ensure that "init" fires before allowing a page to be cached to avoid caching error pages + ### Fixed - Detect when WP_CACHE is defined with "const" in wp-config.php - General: Fix instance where Boost can break caching for other caching plugins when getting deactivated. diff --git a/app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php b/app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php index 92923a724..698826496 100644 --- a/app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php +++ b/app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php @@ -54,6 +54,11 @@ class Boost_Cache { */ private static $cache_engine_loaded = false; + /** + * @var bool - Indicates whether WordPress initialized correctly and we can cache the page. + */ + private $do_cache = false; + /** * @param ?Storage\Storage $storage - Optionally provide a Storage subclass to handle actually storing and retrieving cached content. Defaults to a new instance of File_Storage. */ @@ -75,6 +80,7 @@ public function init_actions() { add_action( 'switch_theme', array( $this, 'invalidate_cache' ) ); add_action( 'wp_trash_post', array( $this, 'delete_on_post_trash' ), 10, 2 ); add_filter( 'wp_php_error_message', array( $this, 'disable_caching_on_error' ) ); + add_filter( 'init', array( $this, 'init_do_cache' ) ); } /** @@ -176,6 +182,11 @@ public function ob_start() { public function ob_callback( $buffer ) { if ( strlen( $buffer ) > 0 && $this->request->is_cacheable() ) { + // Do not cache the page as WordPress did not initialize correctly. + if ( ! $this->do_cache ) { + return $buffer; + } + if ( false === stripos( $buffer, '' ) ) { Logger::debug( 'Closing HTML tag not found, not caching' ); return $buffer; @@ -468,4 +479,14 @@ public function disable_caching_on_error( $message ) { Logger::debug( 'Fatal error detected, caching disabled' ); return $message; } + + /** + * This function is called after WordPress is loaded, on "init". + * It is used to indicate that it is safe to cache and that no + * fatal errors occurred. + */ + public function init_do_cache() { + Logger::debug( 'postload: init succeeded' ); + $this->do_cache = true; + } }