Open binaryfire opened 5 years ago
There is 2 things that will have to be overcome before being able to implement that in WP Rocket:
For now we can't implement the support for tokens, but I'll keep the issue open for the future.
Ok no problems, look forward to this being added. I might try disabling the cloudflare addon and writing something myself until then. Are there any hooks that rocket fires when it clears the cache for a page or post? Or is save_post the best option?
There is, for example: https://github.com/wp-media/wp-rocket/blob/b8a54656afd7acc86fec1b1ac4e85d7b72ba5317/inc/common/purge.php#L207
You also have others if needed, you can easily fine them in the source code.
Here's a very simple implementation.
<?php
// Set in WP Config
// define('CLOUDFLARE_API_TOKEN', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
// define('CLOUDFLARE_ZONE_ID', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
function bh_purge_cloudflare_cache(){
if(empty(CLOUDFLARE_API_TOKEN) || empty(CLOUDFLARE_ZONE_ID)){
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf("https://api.cloudflare.com/client/v4/zones/%s/purge_cache", CLOUDFLARE_ZONE_ID));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"purge_everything":true}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = sprintf('Authorization: Bearer %s', CLOUDFLARE_API_TOKEN);
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($error) {
//echo 'Error:' . $error;
return false;
}else{
//echo $response;
$resp = json_decode($response);
if($resp->success == 'true'){
return true;
}
return false;
}
}
add_action( 'after_rocket_clean_domain', 'bh_purge_cloudflare_cache' );
another request https://secure.helpscout.net/mailbox/5de4805abf96951b/2683093/
Another request: https://secure.helpscout.net/conversation/1309531926/202524/
Another request: https://secure.helpscout.net/conversation/1349011599/216427
Any thoughts or updates, given this was over a year ago?
For larger organizations, using the Global API is a huge risk and pain.
With the Cloudflare API token system that came out in mid-2019, we can mitigate this by creating tokens that are tied to specific zones and have limited or read-only access.
In many, specifically with enterprise clients with many zones/brands/products, the Global API key simply isn't an option.
Related ticket: https://secure.helpscout.net/conversation/1585840300/283756/
related ticket: https://secure.helpscout.net/conversation/1685827717/305649/
Kindly, I've just seen either W3 Total Cache has this issue and are working on it, please add this feature as soon as possible due to security and protection: https://github.com/W3EDGE/w3-total-cache/issues/450
Before grooming this, we need to determine how we're going to display the choice between the old and new authentification system in the UI
+1 on this
Hi guys
Cloudflare have just released a new API tokens feature. This allows the creation of API credentials with restricted permissions and is the new recommended way of granting access to apps like Wordpress. Additionally, tokens can be restricted to a specific domain which is a huge for security. They have a Wordpress template too. From https://api.cloudflare.com/#getting-started-requests:
Here's a screenshot of their Wordpress template:
The "cache purge" functionality wasn't set in the template however it's very easy to add this permission manually. I've posted on the cloudflare forum so hopefully it will be added to the wordpress template soon.
These new API requests use a single H "Authorization: Bearer xxxxxxxxx" header instead of the "X-Auth-Email" and "X-Auth-Key" headers. So using the token in WP Rocket's existing "Global API" field doesn't work.
Would it be possible to add support for tokens in the next update? All other API request headers are identical so it would be easy to add the option to use a token instead of the global key if desired. Please note that the account email is no longer required with tokens. So the email field shouldn't be a required field if "API Token" is selected instead of "Global access key".
We would also need to option to define it in wp-config.php i.e: define('WP_ROCKET_CF_API_TOKEN', 'put-your-API-token-here'); define( 'WP_ROCKET_CF_API_TOKEN_HIDDEN', true );
Cheers!