wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
701 stars 218 forks source link

Make Cache Lifespan also purge Cloudflare APO Cache #6170

Open joejoe04 opened 1 year ago

joejoe04 commented 1 year ago

Is your feature request related to a problem? Please describe. Per the discussion here: https://wp-media.slack.com/archives/C056ZJMHG0P/p1694706892420009 https://wp-media.slack.com/archives/C056ZJMHG0P/p1694706892657559

Currently, the Cache Lifespan option is not purging the Cloudflare APO cache, as we don't have the Cloudflare APO purge hooked on it.

Describe the solution you'd like When Cache Lifespan clears cache, it should also clear the Cloudflare APO cache for the related pages.

This was brought to attention by a user with Cloudflare APO who is having errors caused due to expired nonces that are still present in the APO cache because it's not being cleared by the Cache Lifespan.

Additional context https://secure.helpscout.net/conversation/2344380208/439669/ https://www.siaonlinestore.com/

joejoe04 commented 1 year ago

In doing some additional testing to create a helper plugin to add this functionality until the issue is resolved, I noticed that WP Rocket functions like rocket_clean_home() are also not clearing the APO cache, so I think it would be good to apply the fix for cases when those functions are used as well.

Discussion thread for more info about this: https://wp-media.slack.com/archives/C056ZJMHG0P/p1695747439445229

webtrainingwheels commented 3 months ago

https://secure.helpscout.net/conversation/2659734118/504084?folderId=377611

alessandro-newzoo commented 4 weeks ago

Sorry to barge in but why is this not standard behavior? I've wasted so many hours trying to figure out why the two caches didn't match, until I dove deep into the docs and found this bit:

The automatic cache purging for Cloudflare APO Cache won't be triggered when Cache Lifespan runs the cache clearing for expired cache files.

Which leads me to some questions:

// Hook into WP Rocket's full cache clearing action
add_action( 'after_rocket_clean_domain', 'purge_cloudflare_cache' );

function purge_cloudflare_cache() {

        error_log( 'Cloudflare cache purge function triggered on live site.' );

        $apiKey = '<REDACTED>';
        $zoneId = '<REDACTED>';
        $url = "https://api.cloudflare.com/client/v4/zones/{$zoneId}/purge_cache";
        $data = json_encode(
            array(
                'purge_everything' => true,
            )
        );
        $headers = array(
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json',
        );
        $ch = curl_init( $url );

        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

        $response = curl_exec( $ch );

        curl_close( $ch );

        error_log( 'Cloudflare API Response: ' . $response );

        $responseData = json_decode( $response, true );
        if ( $responseData['success'] ) {
            error_log( 'Cloudflare cache purged successfully' );
        } else {
            error_log( 'Cloudflare cache purging failed: ' . $responseData['errors'][0]['message'] );
        }

}
joejoe04 commented 4 weeks ago

@alessandro-newzoo Sorry this has caused you a lot of trouble!

We may be able to provide a helper plugin for you that will trigger the APO cache to clear for pages cleared by the Cache Lifespan.

Please open a support ticket here: https://wp-rocket.me/support/

Ask that the ticket be transferred to Joseph and we can work on it together (normally, this can't be done, but since I've already been working on the helper plugin to do this, it makes sense in this case).

If we confirm the helper works for your case, then I'll post it here as well for anyone else with similar issues.

joejoe04 commented 3 weeks ago

Related: https://secure.helpscout.net/conversation/2734724693/517877