@@ -19,7 +19,8 @@ function sc_file_cache( $buffer, $flags ) {
1919
2020 $ cache_dir = sc_get_cache_dir ();
2121
22- if ( mb_strlen ( $ buffer ) < 255 ) {
22+ // Don't cache small requests unless it's a REST API request.
23+ if ( mb_strlen ( $ buffer ) < 255 && ( ! defined ( 'REST_REQUEST ' ) || ! mb_strlen ( $ buffer ) > 0 ) ) {
2324 return $ buffer ;
2425 }
2526
@@ -28,6 +29,11 @@ function sc_file_cache( $buffer, $flags ) {
2829 return $ buffer ;
2930 }
3031
32+ // Do not cache the REST API if the user has not opted-in or it's an authenticated REST API request.
33+ if ( defined ( 'REST_REQUEST ' ) && REST_REQUEST && ( empty ( $ GLOBALS ['sc_config ' ]['page_cache_enable_rest_api_cache ' ] ) || ! empty ( $ _SERVER ['HTTP_AUTHORIZATION ' ] ) ) ) {
34+ return $ buffer ;
35+ }
36+
3137 // Make sure we can read/write files to cache dir parent
3238 if ( ! file_exists ( dirname ( $ cache_dir ) ) ) {
3339 if ( ! @mkdir ( dirname ( $ cache_dir ) ) ) {
@@ -67,6 +73,13 @@ function sc_file_cache( $buffer, $flags ) {
6773
6874 $ modified_time = time (); // Make sure modified time is consistent.
6975
76+ $ file_extension = '.html ' ;
77+
78+ // Store JSON files for the REST API.
79+ if ( defined ( 'REST_REQUEST ' ) && REST_REQUEST ) {
80+ $ file_extension = '.json ' ;
81+ }
82+
7083 // Prevent mixed content when there's an http request but the site URL uses https.
7184 $ home_url = get_home_url ();
7285
@@ -82,16 +95,24 @@ function sc_file_cache( $buffer, $flags ) {
8295 $ buffer .= "\n<!-- Cache served by Simple Cache - Last modified: " . gmdate ( 'D, d M Y H:i:s ' , $ modified_time ) . " GMT --> \n" ;
8396 }
8497
98+ // Save the response body.
8599 if ( ! empty ( $ GLOBALS ['sc_config ' ]['enable_gzip_compression ' ] ) && function_exists ( 'gzencode ' ) ) {
86- file_put_contents ( $ path . '/index.gzip.html ' , gzencode ( $ buffer , 3 ) );
87- touch ( $ path . '/index.gzip.html ' , $ modified_time );
100+ file_put_contents ( $ path . '/index.gzip ' . $ file_extension , gzencode ( $ buffer , 3 ) );
101+ touch ( $ path . '/index.gzip ' . $ file_extension , $ modified_time );
88102 } else {
89- file_put_contents ( $ path . '/index.html ' , $ buffer );
90- touch ( $ path . '/index.html ' , $ modified_time );
103+ file_put_contents ( $ path . '/index ' . $ file_extension , $ buffer );
104+ touch ( $ path . '/index ' . $ file_extension , $ modified_time );
105+ }
106+
107+ // Save the resonse headers.
108+ if ( ! empty ( $ GLOBALS ['sc_config ' ]['page_cache_restore_headers ' ] ) ) {
109+ file_put_contents ( $ path . '/headers.json ' , wp_json_encode ( headers_list () ) );
91110 }
92111
93112 header ( 'Cache-Control: no-cache ' ); // Check back every time to see if re-download is necessary.
94113
114+ header ( 'X-Simple-Cache: MISS ' );
115+
95116 header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s ' , $ modified_time ) . ' GMT ' );
96117
97118 if ( function_exists ( 'ob_gzhandler ' ) && ! empty ( $ GLOBALS ['sc_config ' ]['enable_gzip_compression ' ] ) ) {
@@ -131,17 +152,24 @@ function sc_get_cache_path() {
131152function sc_serve_file_cache () {
132153 $ cache_dir = ( defined ( 'SC_CACHE_DIR ' ) ) ? rtrim ( SC_CACHE_DIR , '/ ' ) : rtrim ( WP_CONTENT_DIR , '/ ' ) . '/cache/simple-cache ' ;
133154
134- $ file_name = 'index.html ' ;
155+ $ file_name = 'index. ' ;
135156
136157 if ( function_exists ( 'gzencode ' ) && ! empty ( $ GLOBALS ['sc_config ' ]['enable_gzip_compression ' ] ) ) {
137- $ file_name = 'index.gzip.html ' ;
158+ $ file_name = 'index.gzip. ' ;
138159 }
139160
140- $ path = $ cache_dir . '/ ' . rtrim ( sc_get_url_path (), '/ ' ) . '/ ' . $ file_name ;
161+ $ html_path = $ cache_dir . '/ ' . rtrim ( sc_get_url_path (), '/ ' ) . '/ ' . $ file_name . 'html ' ;
162+ $ json_path = $ cache_dir . '/ ' . rtrim ( sc_get_url_path (), '/ ' ) . '/ ' . $ file_name . 'json ' ;
163+ $ header_path = $ cache_dir . '/ ' . rtrim ( sc_get_url_path (), '/ ' ) . '/headers.json ' ;
141164
142- $ modified_time = (int ) @filemtime ( $ path );
165+ if ( @file_exists ( $ html_path ) && @is_readable ( $ html_path ) ) {
166+ $ path = $ html_path ;
167+ } elseif ( @file_exists ( $ json_path ) && @is_readable ( $ json_path ) ) {
168+ $ path = $ json_path ;
169+ header ( 'Content-Type: application/json; charset=UTF-8 ' );
170+ }
143171
144- header ( ' Cache-Control: no-cache ' ); // Check back in an hour.
172+ $ modified_time = ( int ) @ filemtime ( $ path );
145173
146174 if ( ! empty ( $ modified_time ) && ! empty ( $ _SERVER ['HTTP_IF_MODIFIED_SINCE ' ] ) && strtotime ( $ _SERVER ['HTTP_IF_MODIFIED_SINCE ' ] ) === $ modified_time ) {
147175 if ( function_exists ( 'gzencode ' ) && ! empty ( $ GLOBALS ['sc_config ' ]['enable_gzip_compression ' ] ) ) {
@@ -152,11 +180,25 @@ function sc_serve_file_cache() {
152180 exit ;
153181 }
154182
155- if ( @file_exists ( $ path ) && @is_readable ( $ path ) ) {
183+ if ( isset ( $ path ) ) {
184+
185+ // Restore the headers if a `header.json` file is found.
186+ if ( @file_exists ( $ header_path ) && @is_readable ( $ header_path ) ) {
187+ $ headers = json_decode ( @file_get_contents ( $ header_path ) );
188+ foreach ( $ headers as $ header ) {
189+ header ( $ header );
190+ }
191+ } else {
192+ header ( 'Cache-Control: no-cache ' );
193+ }
194+
195+ // Set the GZIP header if we are serving gzipped content.
156196 if ( function_exists ( 'gzencode ' ) && ! empty ( $ GLOBALS ['sc_config ' ]['enable_gzip_compression ' ] ) ) {
157197 header ( 'Content-Encoding: gzip ' );
158198 }
159199
200+ header ( 'X-Simple-Cache: HIT ' );
201+
160202 @readfile ( $ path );
161203
162204 exit ;
0 commit comments