Skip to content

Commit cdd58a0

Browse files
committed
Merge branch 'develop'
2 parents 80aa30d + c8ff439 commit cdd58a0

14 files changed

Lines changed: 454 additions & 226 deletions

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ env:
1010
- WP_VERSION=3.9 WP_MULTISITE=1
1111

1212
before_script:
13+
- composer self-update --1
1314
- composer install
1415

1516
script:

assets/js/settings.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const pageCacheLengthSimple = document.getElementById('sc_page_cache_length_simp
2424
const pageCacheLengthAdvanced = document.getElementById('sc_page_cache_length_advanced');
2525
const pageCacheLengthUnitSimple = document.getElementById('sc_page_cache_length_unit_simple');
2626
const pageCacheLengthUnitAdvanced = document.getElementById('sc_page_cache_length_unit_advanced');
27+
const pageRestApiCache = document.getElementById('sc_page_cache_enable_rest_api_cache');
28+
const pageRestoreHeaders = document.getElementById('sc_page_cache_restore_headers');
2729
const gzipCompressionSimple = document.getElementById('sc_enable_gzip_compression_simple');
2830
const gzipCompressionAdvanced = document.getElementById('sc_enable_gzip_compression_advanced');
2931

@@ -41,6 +43,9 @@ $advancedModeToggle.on('change', (event) => {
4143
pageCacheLengthUnitSimple.disabled = true;
4244
pageCacheLengthUnitAdvanced.disabled = false;
4345

46+
pageRestApiCache.disabled = false;
47+
pageRestoreHeaders.disabled = false;
48+
4449
if (gzipCompressionSimple) {
4550
gzipCompressionSimple.disabled = true;
4651
gzipCompressionAdvanced.disabled = false;
@@ -58,6 +63,9 @@ $advancedModeToggle.on('change', (event) => {
5863
pageCacheLengthUnitSimple.disabled = false;
5964
pageCacheLengthUnitAdvanced.disabled = true;
6065

66+
pageRestApiCache.disabled = true;
67+
pageRestoreHeaders.disabled = true;
68+
6169
if (gzipCompressionSimple) {
6270
gzipCompressionSimple.disabled = false;
6371
gzipCompressionAdvanced.disabled = true;

dist/js/settings.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/class-sc-advanced-cache.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,19 @@ public function purge_post_on_comment( $comment_id, $approved, $commentdata ) {
9696
* @since 1.3
9797
*/
9898
public function purge_post_on_update( $post_id ) {
99-
$post_type = get_post_type( $post_id );
99+
$post = get_post( $post_id );
100100

101-
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || 'revision' === $post_type ) {
101+
// Do not purge the cache if it's an autosave or it is updating a revision.
102+
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || 'revision' === $post->post_type ) {
102103
return;
104+
105+
// Do not purge the cache if the user cannot edit the post.
103106
} elseif ( ! current_user_can( 'edit_post', $post_id ) && ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) ) {
104107
return;
108+
109+
// Do not purge the cache if the user is editing an unpublished post.
110+
} elseif ( 'draft' === $post->post_status ) {
111+
return;
105112
}
106113

107114
$config = SC_Config::factory()->get();

inc/class-sc-config.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,47 @@ class SC_Config {
2929
public function __construct() {
3030

3131
$this->defaults = array(
32-
'enable_page_caching' => array(
32+
'enable_page_caching' => array(
3333
'default' => false,
3434
'sanitizer' => array( $this, 'boolval' ),
3535
),
36-
'advanced_mode' => array(
36+
'advanced_mode' => array(
3737
'default' => false,
3838
'sanitizer' => array( $this, 'boolval' ),
3939
),
40-
'enable_in_memory_object_caching' => array(
40+
'enable_in_memory_object_caching' => array(
4141
'default' => false,
4242
'sanitizer' => array( $this, 'boolval' ),
4343
),
44-
'enable_gzip_compression' => array(
44+
'enable_gzip_compression' => array(
4545
'default' => false,
4646
'sanitizer' => array( $this, 'boolval' ),
4747
),
48-
'in_memory_cache' => array(
48+
'in_memory_cache' => array(
4949
'default' => 'memcached',
5050
'sanitizer' => 'sanitize_text_field',
5151
),
52-
'page_cache_length' => array(
52+
'page_cache_length' => array(
5353
'default' => 24,
5454
'sanitizer' => 'absint',
5555
),
56-
'page_cache_length_unit' => array(
56+
'page_cache_length_unit' => array(
5757
'default' => 'hours',
5858
'sanitizer' => array( $this, 'sanitize_length_unit' ),
5959
),
60-
'cache_exception_urls' => array(
60+
'cache_exception_urls' => array(
6161
'default' => '',
6262
'sanitizer' => 'wp_kses_post',
6363
),
64-
'enable_url_exemption_regex' => array(
64+
'enable_url_exemption_regex' => array(
65+
'default' => false,
66+
'sanitizer' => array( $this, 'boolval' ),
67+
),
68+
'page_cache_enable_rest_api_cache' => array(
69+
'default' => false,
70+
'sanitizer' => array( $this, 'boolval' ),
71+
),
72+
'page_cache_restore_headers' => array(
6573
'default' => false,
6674
'sanitizer' => array( $this, 'boolval' ),
6775
),

inc/class-sc-settings.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,29 @@ public function screen_options() {
306306
</select>
307307
</td>
308308
</tr>
309+
310+
<tr>
311+
<th scope="row"><label for="sc_page_cache_enable_rest_api_cache"><?php esc_html_e( 'Cache REST API', 'simple-cache' ); ?></label></th>
312+
<td>
313+
<select <?php if ( empty( $config['advanced_mode'] ) ) : ?>disabled<?php endif; ?> name="sc_simple_cache[page_cache_enable_rest_api_cache]" id="sc_page_cache_enable_rest_api_cache">
314+
<option value="0"><?php esc_html_e( 'No', 'simple-cache' ); ?></option>
315+
<option <?php selected( $config['page_cache_enable_rest_api_cache'], true ); ?> value="1"><?php esc_html_e( 'Yes', 'simple-cache' ); ?></option>
316+
</select>
317+
<p class="description"><?php esc_html_e( 'When enabled, the REST API requests will be cached.', 'simple-cache' ); ?></p>
318+
</td>
319+
</tr>
320+
321+
<tr>
322+
<th scope="row"><label for="sc_page_cache_restore_headers"><?php esc_html_e( 'Restore Headers', 'simple-cache' ); ?></label></th>
323+
<td>
324+
<select <?php if ( empty( $config['advanced_mode'] ) ) : ?>disabled<?php endif; ?> name="sc_simple_cache[page_cache_restore_headers]" id="sc_page_cache_restore_headers">
325+
<option value="0"><?php esc_html_e( 'No', 'simple-cache' ); ?></option>
326+
<option <?php selected( $config['page_cache_restore_headers'], true ); ?> value="1"><?php esc_html_e( 'Yes', 'simple-cache' ); ?></option>
327+
</select>
328+
<p class="description"><?php esc_html_e( 'When enabled, the plugin will save the response headers present when the page is cached and it will send send them again when it serves the cached page. This is recommended when caching the REST API.', 'simple-cache' ); ?></p>
329+
</td>
330+
</tr>
331+
309332
<tr>
310333
<th scope="row" colspan="2">
311334
<h2 class="cache-title"><?php esc_html_e( 'Object Cache (Redis or Memcached)', 'simple-cache' ); ?></h2>

inc/dropins/file-based-page-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
defined( 'ABSPATH' ) || exit;
99

10-
if ( ! function_exists('sc_serve_file_cache') ) {
10+
if ( ! function_exists( 'sc_serve_file_cache' ) ) {
1111
return;
1212
}
1313

inc/pre-wp-functions.php

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {
131152
function 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;

languages/simple-cache-es_ES.mo

1.48 KB
Binary file not shown.

0 commit comments

Comments
 (0)