From 1ae37c5b2e8aba6bdaa70b8a32ff9372e5824cd0 Mon Sep 17 00:00:00 2001 From: donnchawp Date: Mon, 2 Dec 2024 14:25:36 +0000 Subject: [PATCH] WPSC: Check that there is cached content to display in browser (#40342) Check that there is cached content to display in browser Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12121105781 Upstream-Ref: Automattic/jetpack@adabdc736a53bf85813dd9b579724bf060024953 --- CHANGELOG.md | 1 + wp-cache-phase2.php | 31 +++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9db53577..4cbae6e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ This is an alpha version! The changes listed here are not final. - General: Update minimum WordPress version to 6.6. ### Fixed +- Caching: make sure there is cache content to serve, even if the cache file was found - Lossless image optimization for images (should improve performance with no visible changes). - Move trailing space out of i18n message. - Revert recent SVG image optimizations. diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index 392d25bf..44287b46 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -227,18 +227,44 @@ function wp_cache_serve_cache_file() { if ( $wp_cache_gzip_encoding ) { if ( file_exists( $file . '.gz' ) ) { $cachefiledata = file_get_contents( $file . '.gz' ); + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached gzip file could not be read. Must generate a new one.' ); + return false; + } + wp_cache_debug( "Fetched gzip static page data from supercache file using PHP. File: $file.gz" ); } else { - $cachefiledata = gzencode( file_get_contents( $file ), 6, FORCE_GZIP ); + $cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); + return false; + } + + $cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP ); wp_cache_debug( "Fetched static page data from supercache file using PHP and gzipped it. File: $file" ); } } else { $cachefiledata = file_get_contents( $file ); + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); + return false; + } + wp_cache_debug( "Fetched static page data from supercache file using PHP. File: $file" ); } } else { // get dynamic data from filtered file - $cachefiledata = do_cacheaction( 'wpsc_cachedata', file_get_contents( $file ) ); + $cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + + if ( false === $cachefiledata ) { + wp_cache_debug( 'The cached file could not be read. Must generate a new one.' ); + return false; + } + + $cachefiledata = do_cacheaction( 'wpsc_cachedata', $cachefiledata ); if ( $wp_cache_gzip_encoding ) { $cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP ); wp_cache_debug( "Fetched dynamic page data from supercache file using PHP and gzipped it. File: $file" ); @@ -301,6 +327,7 @@ function wp_cache_serve_cache_file() { } header( 'Last-Modified: ' . $local_mod_time ); } + echo $cachefiledata; exit(); } else {