Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tlovett1 committed May 4, 2021
2 parents 80aa30d + c8ff439 commit cdd58a0
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 226 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
- WP_VERSION=3.9 WP_MULTISITE=1

before_script:
- composer self-update --1
- composer install

script:
Expand Down
8 changes: 8 additions & 0 deletions assets/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const pageCacheLengthSimple = document.getElementById('sc_page_cache_length_simp
const pageCacheLengthAdvanced = document.getElementById('sc_page_cache_length_advanced');
const pageCacheLengthUnitSimple = document.getElementById('sc_page_cache_length_unit_simple');
const pageCacheLengthUnitAdvanced = document.getElementById('sc_page_cache_length_unit_advanced');
const pageRestApiCache = document.getElementById('sc_page_cache_enable_rest_api_cache');
const pageRestoreHeaders = document.getElementById('sc_page_cache_restore_headers');
const gzipCompressionSimple = document.getElementById('sc_enable_gzip_compression_simple');
const gzipCompressionAdvanced = document.getElementById('sc_enable_gzip_compression_advanced');

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

pageRestApiCache.disabled = false;
pageRestoreHeaders.disabled = false;

if (gzipCompressionSimple) {
gzipCompressionSimple.disabled = true;
gzipCompressionAdvanced.disabled = false;
Expand All @@ -58,6 +63,9 @@ $advancedModeToggle.on('change', (event) => {
pageCacheLengthUnitSimple.disabled = false;
pageCacheLengthUnitAdvanced.disabled = true;

pageRestApiCache.disabled = true;
pageRestoreHeaders.disabled = true;

if (gzipCompressionSimple) {
gzipCompressionSimple.disabled = false;
gzipCompressionAdvanced.disabled = true;
Expand Down
2 changes: 1 addition & 1 deletion dist/js/settings.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions inc/class-sc-advanced-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,19 @@ public function purge_post_on_comment( $comment_id, $approved, $commentdata ) {
* @since 1.3
*/
public function purge_post_on_update( $post_id ) {
$post_type = get_post_type( $post_id );
$post = get_post( $post_id );

if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || 'revision' === $post_type ) {
// Do not purge the cache if it's an autosave or it is updating a revision.
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || 'revision' === $post->post_type ) {
return;

// Do not purge the cache if the user cannot edit the post.
} elseif ( ! current_user_can( 'edit_post', $post_id ) && ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) ) {
return;

// Do not purge the cache if the user is editing an unpublished post.
} elseif ( 'draft' === $post->post_status ) {
return;
}

$config = SC_Config::factory()->get();
Expand Down
26 changes: 17 additions & 9 deletions inc/class-sc-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,47 @@ class SC_Config {
public function __construct() {

$this->defaults = array(
'enable_page_caching' => array(
'enable_page_caching' => array(
'default' => false,
'sanitizer' => array( $this, 'boolval' ),
),
'advanced_mode' => array(
'advanced_mode' => array(
'default' => false,
'sanitizer' => array( $this, 'boolval' ),
),
'enable_in_memory_object_caching' => array(
'enable_in_memory_object_caching' => array(
'default' => false,
'sanitizer' => array( $this, 'boolval' ),
),
'enable_gzip_compression' => array(
'enable_gzip_compression' => array(
'default' => false,
'sanitizer' => array( $this, 'boolval' ),
),
'in_memory_cache' => array(
'in_memory_cache' => array(
'default' => 'memcached',
'sanitizer' => 'sanitize_text_field',
),
'page_cache_length' => array(
'page_cache_length' => array(
'default' => 24,
'sanitizer' => 'absint',
),
'page_cache_length_unit' => array(
'page_cache_length_unit' => array(
'default' => 'hours',
'sanitizer' => array( $this, 'sanitize_length_unit' ),
),
'cache_exception_urls' => array(
'cache_exception_urls' => array(
'default' => '',
'sanitizer' => 'wp_kses_post',
),
'enable_url_exemption_regex' => array(
'enable_url_exemption_regex' => array(
'default' => false,
'sanitizer' => array( $this, 'boolval' ),
),
'page_cache_enable_rest_api_cache' => array(
'default' => false,
'sanitizer' => array( $this, 'boolval' ),
),
'page_cache_restore_headers' => array(
'default' => false,
'sanitizer' => array( $this, 'boolval' ),
),
Expand Down
23 changes: 23 additions & 0 deletions inc/class-sc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ public function screen_options() {
</select>
</td>
</tr>

<tr>
<th scope="row"><label for="sc_page_cache_enable_rest_api_cache"><?php esc_html_e( 'Cache REST API', 'simple-cache' ); ?></label></th>
<td>
<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">
<option value="0"><?php esc_html_e( 'No', 'simple-cache' ); ?></option>
<option <?php selected( $config['page_cache_enable_rest_api_cache'], true ); ?> value="1"><?php esc_html_e( 'Yes', 'simple-cache' ); ?></option>
</select>
<p class="description"><?php esc_html_e( 'When enabled, the REST API requests will be cached.', 'simple-cache' ); ?></p>
</td>
</tr>

<tr>
<th scope="row"><label for="sc_page_cache_restore_headers"><?php esc_html_e( 'Restore Headers', 'simple-cache' ); ?></label></th>
<td>
<select <?php if ( empty( $config['advanced_mode'] ) ) : ?>disabled<?php endif; ?> name="sc_simple_cache[page_cache_restore_headers]" id="sc_page_cache_restore_headers">
<option value="0"><?php esc_html_e( 'No', 'simple-cache' ); ?></option>
<option <?php selected( $config['page_cache_restore_headers'], true ); ?> value="1"><?php esc_html_e( 'Yes', 'simple-cache' ); ?></option>
</select>
<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>
</td>
</tr>

<tr>
<th scope="row" colspan="2">
<h2 class="cache-title"><?php esc_html_e( 'Object Cache (Redis or Memcached)', 'simple-cache' ); ?></h2>
Expand Down
2 changes: 1 addition & 1 deletion inc/dropins/file-based-page-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

defined( 'ABSPATH' ) || exit;

if ( ! function_exists('sc_serve_file_cache') ) {
if ( ! function_exists( 'sc_serve_file_cache' ) ) {
return;
}

Expand Down
64 changes: 53 additions & 11 deletions inc/pre-wp-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function sc_file_cache( $buffer, $flags ) {

$cache_dir = sc_get_cache_dir();

if ( mb_strlen( $buffer ) < 255 ) {
// Don't cache small requests unless it's a REST API request.
if ( mb_strlen( $buffer ) < 255 && ( ! defined( 'REST_REQUEST' ) || ! mb_strlen( $buffer ) > 0 ) ) {
return $buffer;
}

Expand All @@ -28,6 +29,11 @@ function sc_file_cache( $buffer, $flags ) {
return $buffer;
}

// Do not cache the REST API if the user has not opted-in or it's an authenticated REST API request.
if ( defined( 'REST_REQUEST' ) && REST_REQUEST && ( empty( $GLOBALS['sc_config']['page_cache_enable_rest_api_cache'] ) || ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ) ) {
return $buffer;
}

// Make sure we can read/write files to cache dir parent
if ( ! file_exists( dirname( $cache_dir ) ) ) {
if ( ! @mkdir( dirname( $cache_dir ) ) ) {
Expand Down Expand Up @@ -67,6 +73,13 @@ function sc_file_cache( $buffer, $flags ) {

$modified_time = time(); // Make sure modified time is consistent.

$file_extension = '.html';

// Store JSON files for the REST API.
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
$file_extension = '.json';
}

// Prevent mixed content when there's an http request but the site URL uses https.
$home_url = get_home_url();

Expand All @@ -82,16 +95,24 @@ function sc_file_cache( $buffer, $flags ) {
$buffer .= "\n<!-- Cache served by Simple Cache - Last modified: " . gmdate( 'D, d M Y H:i:s', $modified_time ) . " GMT -->\n";
}

// Save the response body.
if ( ! empty( $GLOBALS['sc_config']['enable_gzip_compression'] ) && function_exists( 'gzencode' ) ) {
file_put_contents( $path . '/index.gzip.html', gzencode( $buffer, 3 ) );
touch( $path . '/index.gzip.html', $modified_time );
file_put_contents( $path . '/index.gzip' . $file_extension, gzencode( $buffer, 3 ) );
touch( $path . '/index.gzip' . $file_extension, $modified_time );
} else {
file_put_contents( $path . '/index.html', $buffer );
touch( $path . '/index.html', $modified_time );
file_put_contents( $path . '/index' . $file_extension, $buffer );
touch( $path . '/index' . $file_extension, $modified_time );
}

// Save the resonse headers.
if ( ! empty( $GLOBALS['sc_config']['page_cache_restore_headers'] ) ) {
file_put_contents( $path . '/headers.json', wp_json_encode( headers_list() ) );
}

header( 'Cache-Control: no-cache' ); // Check back every time to see if re-download is necessary.

header( 'X-Simple-Cache: MISS' );

header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $modified_time ) . ' GMT' );

if ( function_exists( 'ob_gzhandler' ) && ! empty( $GLOBALS['sc_config']['enable_gzip_compression'] ) ) {
Expand Down Expand Up @@ -131,17 +152,24 @@ function sc_get_cache_path() {
function sc_serve_file_cache() {
$cache_dir = ( defined( 'SC_CACHE_DIR' ) ) ? rtrim( SC_CACHE_DIR, '/' ) : rtrim( WP_CONTENT_DIR, '/' ) . '/cache/simple-cache';

$file_name = 'index.html';
$file_name = 'index.';

if ( function_exists( 'gzencode' ) && ! empty( $GLOBALS['sc_config']['enable_gzip_compression'] ) ) {
$file_name = 'index.gzip.html';
$file_name = 'index.gzip.';
}

$path = $cache_dir . '/' . rtrim( sc_get_url_path(), '/' ) . '/' . $file_name;
$html_path = $cache_dir . '/' . rtrim( sc_get_url_path(), '/' ) . '/' . $file_name . 'html';
$json_path = $cache_dir . '/' . rtrim( sc_get_url_path(), '/' ) . '/' . $file_name . 'json';
$header_path = $cache_dir . '/' . rtrim( sc_get_url_path(), '/' ) . '/headers.json';

$modified_time = (int) @filemtime( $path );
if ( @file_exists( $html_path ) && @is_readable( $html_path ) ) {
$path = $html_path;
} elseif ( @file_exists( $json_path ) && @is_readable( $json_path ) ) {
$path = $json_path;
header( 'Content-Type: application/json; charset=UTF-8' );
}

header( 'Cache-Control: no-cache' ); // Check back in an hour.
$modified_time = (int) @filemtime( $path );

if ( ! empty( $modified_time ) && ! empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) && strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) === $modified_time ) {
if ( function_exists( 'gzencode' ) && ! empty( $GLOBALS['sc_config']['enable_gzip_compression'] ) ) {
Expand All @@ -152,11 +180,25 @@ function sc_serve_file_cache() {
exit;
}

if ( @file_exists( $path ) && @is_readable( $path ) ) {
if ( isset( $path ) ) {

// Restore the headers if a `header.json` file is found.
if ( @file_exists( $header_path ) && @is_readable( $header_path ) ) {
$headers = json_decode( @file_get_contents( $header_path ) );
foreach ( $headers as $header ) {
header( $header );
}
} else {
header( 'Cache-Control: no-cache' );
}

// Set the GZIP header if we are serving gzipped content.
if ( function_exists( 'gzencode' ) && ! empty( $GLOBALS['sc_config']['enable_gzip_compression'] ) ) {
header( 'Content-Encoding: gzip' );
}

header( 'X-Simple-Cache: HIT' );

@readfile( $path );

exit;
Expand Down
Binary file modified languages/simple-cache-es_ES.mo
Binary file not shown.
Loading

0 comments on commit cdd58a0

Please sign in to comment.