Last active
February 22, 2023 15:21
-
-
Save robertuniqid/dc5e2e17a277268b4bfe26b49950735f to your computer and use it in GitHub Desktop.
Clear WP Rocket Cache & WP Rocket Cloudflare automatically if the Frontpage doesn't display all the recent posts. Requires adjustments on per site basis, but, the idea is here, ensure the Cloudflare cache is reset properly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Load WordPress. | |
require( 'wp-load.php' ); | |
$site_url = get_site_url(); | |
$latest_5_post_ids = get_posts([ | |
'posts_per_page' => 5, | |
'paged' => 1, | |
'post_type' => [ 'post' ], | |
'fields' => 'ids' | |
] ); | |
$missing_post_ids = 0; | |
$site_content = wp_remote_get( $site_url, [ | |
'user-agent' => 'Internal Cache Monitoring Tool;', | |
] ); | |
if( empty( $site_content ) ) | |
exit( '<p style="color:#e74c3c;">Cannot retrieve site content</p>' ); | |
if( is_wp_error( $site_content ) ) | |
exit( '<p style="color:#e74c3c;">' . esc_html( $site_content->get_error_message() ) . '</p>' ); | |
$site_content = wp_remote_retrieve_body( $site_content ); | |
if( empty( $site_content ) ) | |
exit( '<p style="color:#e74c3c;">Cannot retrieve site content</p>' ); | |
foreach( $latest_5_post_ids as $post_id ) | |
$missing_post_ids += ( strpos( $site_content, 'id="post-' . $post_id . '"' ) === false ? 1 : 0 ); | |
if( $missing_post_ids === 0 ) | |
exit( '<p style="color:#3498db;">No reset required</p>' ); | |
// Clear cache. | |
if ( function_exists( 'rocket_clean_domain' ) ) { | |
rocket_clean_domain(); | |
echo '<p style="color:#3498db;">rocket_clean_domain - success</p>'; | |
} else { | |
echo '<p style="color:#e67e22;">rocket_clean_domain - failed</p>'; | |
} | |
$options = get_option( 'wp_rocket_settings' ); | |
// Would be great if WP Rocket realized the convenient function they had for this, is not working. | |
// And there's really no explanation on how to use their function, getting this standalone was more convenient. | |
if( $options[ 'do_cloudflare' ] === 1 ) { | |
//Purge the entire cache via API | |
$ch_purge = curl_init(); | |
curl_setopt($ch_purge, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/". $options[ 'cloudflare_zone_id' ] ."/purge_cache"); | |
curl_setopt($ch_purge, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
curl_setopt($ch_purge, CURLOPT_RETURNTRANSFER, 1); | |
$headers = [ | |
'X-Auth-Email: '. $options[ 'cloudflare_email' ], | |
'X-Auth-Key: '. $options[ 'cloudflare_api_key' ], | |
'Content-Type: application/json' | |
]; | |
$data = json_encode([ "purge_everything" => true ] ); | |
curl_setopt($ch_purge, CURLOPT_POST, true ); | |
curl_setopt($ch_purge, CURLOPT_POSTFIELDS, $data ); | |
curl_setopt($ch_purge, CURLOPT_HTTPHEADER, $headers ); | |
$result = json_decode(curl_exec($ch_purge),true); | |
curl_close($ch_purge); | |
if( intval( $result['success'] ) == 1 ) | |
echo '<p style="color:#3498db;">purge_cloudflare - success</p>'; | |
else | |
echo '<p style="color:#e67e22;">purge_cloudflare - error</p>'; | |
} else { | |
echo '<p style="color:#3498db;">purge_cloudflare - skipped</p>'; | |
} | |
$missing_post_ids = 0; | |
$site_content = wp_remote_get( $site_url, [ | |
'user-agent' => 'Internal Cache Monitoring Tool;', | |
] ); | |
if( empty( $site_content ) ) | |
exit( '<p style="color:#e74c3c;">Reset might have failed, check manually - check 1</p>' ); | |
if( is_wp_error( $site_content ) ) | |
exit( '<p style="color:#e74c3c;">Reset might have failed, check manually - check 2</p>' ); | |
$site_content = wp_remote_retrieve_body( $site_content ); | |
foreach( $latest_5_post_ids as $post_id ) | |
$missing_post_ids += ( strpos( $site_content, 'id="post-' . $post_id . '"' ) === false ? 1 : 0 ); | |
if( $missing_post_ids === 0 ) | |
exit( '<p style="color:#2ecc71;">Reset Success</p>' ); | |
else | |
exit( '<p style="color:#e74c3c;">Reset might have failed, check manually - check 3</p>' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment