Skip to content

Commit 7d867da

Browse files
thingalonMark George
and
Mark George
authored
[Boost] minor cache tweaks (#35616)
* Add .html extension to cache files * Do not cache empty buffer * changelog * Check if is_404 and is_feed can be called before calling them --------- Co-authored-by: Mark George <[email protected]>
1 parent 54e578b commit 7d867da

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

projects/plugins/boost/app/modules/cache/Boost_Cache.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ public function is_cacheable() {
110110
return false;
111111
}
112112

113-
if ( function_exists( 'is_404' ) && is_404() ) {
113+
if ( Boost_Cache_Utils::is_404() ) {
114114
return false;
115115
}
116116

117-
if ( function_exists( 'is_feed' ) && is_feed() ) {
117+
if ( Boost_Cache_Utils::is_feed() ) {
118118
return false;
119119
}
120120

@@ -206,7 +206,7 @@ public function ob_start() {
206206
* @return string - The output buffer.
207207
*/
208208
public function ob_callback( $buffer ) {
209-
if ( $this->is_cacheable() ) {
209+
if ( strlen( $buffer ) > 0 && $this->is_cacheable() ) {
210210
$result = $this->storage->write( $this->request_uri, $this->request_parameters, $buffer );
211211

212212
if ( is_wp_error( $result ) ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf

projects/plugins/boost/app/modules/cache/Boost_Cache_Utils.php

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33
namespace Automattic\Jetpack_Boost\Modules\Page_Cache;
44

55
class Boost_Cache_Utils {
6+
7+
/**
8+
* "Safe" version of WordPress' is_404 method. When called before WordPress' query is run, returns
9+
* `null` (a falsey value) instead of outputting a _doing_it_wrong warning.
10+
*/
11+
public static function is_404() {
12+
global $wp_query;
13+
14+
if ( ! isset( $wp_query ) || ! function_exists( '\is_404' ) ) {
15+
return null;
16+
}
17+
18+
return \is_404();
19+
}
20+
21+
/**
22+
* "Safe" version of WordPress' is_feed method. When called before WordPress' query is run, returns
23+
* `null` (a falsey value) instead of outputting a _doing_it_wrong warning.
24+
*/
25+
public static function is_feed() {
26+
global $wp_query;
27+
28+
if ( ! isset( $wp_query ) || ! function_exists( '\is_feed' ) ) {
29+
return null;
30+
}
31+
32+
return \is_feed();
33+
}
34+
635
/*
736
* Recursively delete a directory.
837
* @param string $dir - The directory to delete.

projects/plugins/boost/app/modules/cache/storage/File_Storage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function get_request_filename( $request_uri, $parameters ) {
8383

8484
$key_components = apply_filters( 'boost_cache_key_components', $key_components );
8585

86-
return md5( json_encode( $key_components ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
86+
return md5( json_encode( $key_components ) ) . '.html'; // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
8787
}
8888

8989
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Significance: patch
2+
Type: fixed
3+
Comment: Fixed minor behavior in pre-release feature
4+
5+

0 commit comments

Comments
 (0)