Skip to content

Commit

Permalink
WP Super Cache: don't serve a cached page when a POST request is made…
Browse files Browse the repository at this point in the history
… (#36211)

* Check for DONOTCACHEPAGE in postload function

This is the entry for creating cache pages, and serving cache files
in late init.

* Define DONOTCACHEPAGE when caching is disabled early on

* Separate out this check to make logging better.

* Define DONOTCACHEPAGE so postload function doesn't serve a cached
  page.

* changelog

---------

Co-authored-by: Peter Petrov <[email protected]>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8174387724
  • Loading branch information
donnchawp authored and matticbot committed Mar 6, 2024
1 parent 98e869e commit 03a2071
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This is an alpha version! The changes listed here are not final.
- WP Super Cache - fix the wp_super_cache_clear_post_cache so the homepage cache isn't deleted too.
- WP Super Cache: bail if the request uri isn't set. It means the plugin isn't configured yet.
- WP Super Cache: don't create an output buffer if there's already one active
- WP Super Cache: fixed serving a cached page on POST with late init enabled.
- WP Super Cache: fix the output buffer check, and make debug logs pre-formatted.

## [1.11.0] - 2023-11-08
Expand Down
16 changes: 15 additions & 1 deletion wp-cache-phase1.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@

// don't cache in wp-admin
if ( wpsc_is_backend() ) {
define( 'DONOTCACHEPAGE', 1 );
return true;
}

Expand All @@ -122,6 +123,7 @@

if ( wpsc_is_caching_user_disabled() ) {
wp_cache_debug( 'Caching disabled for logged in users on settings page.' );
define( 'DONOTCACHEPAGE', 1 );
return true;
}

Expand All @@ -133,8 +135,20 @@
// an init action wpsc plugins can hook on to.
do_cacheaction( 'cache_init' );

if ( ! $cache_enabled ) {
return true;
}

// don't cache or serve cached files for various URLs, including the Customizer.
if ( ! $cache_enabled || ( isset( $_SERVER['REQUEST_METHOD'] ) && in_array( $_SERVER['REQUEST_METHOD'], array( 'POST', 'PUT', 'DELETE' ) ) ) || isset( $_GET['customize_changeset_uuid'] ) ) {
if ( isset( $_SERVER['REQUEST_METHOD'] ) && in_array( $_SERVER['REQUEST_METHOD'], array( 'POST', 'PUT', 'DELETE' ), true ) ) {
wp_cache_debug( 'Caching disabled for non GET request.' );
define( 'DONOTCACHEPAGE', 1 );
return true;
}

if ( isset( $_GET['customize_changeset_uuid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
wp_cache_debug( 'Caching disabled for customizer.' );
define( 'DONOTCACHEPAGE', 1 );
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions wp-cache-phase2.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ function wp_cache_postload() {
global $cache_enabled, $wp_super_cache_late_init;
global $wp_cache_request_uri;

if ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) {
wp_cache_debug( 'wp_cache_postload: DONOTCACHEPAGE defined. Not running.' );
return false;
}

if ( empty( $wp_cache_request_uri ) ) {
wp_cache_debug( 'wp_cache_postload: no request uri configured. Not running.' );
return false;
Expand Down

0 comments on commit 03a2071

Please sign in to comment.