-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-cloudflare-cache-clearer.php
87 lines (75 loc) · 2.76 KB
/
simple-cloudflare-cache-clearer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
Plugin Name: Simple Cloudflare Cache Clearer
Description: Clears Cloudflare cache when SpinupWP cache clear is triggered, also adds a one-click admin menu item to manually clear the CF cache
Version: 0.0.1
Author: NodleStudios
*/
// Make sure we don't expose any info if called directly
if (!function_exists('add_action')) {
exit;
}
// Function to clear Cloudflare cache
function clear_cloudflare_cache()
{
// Check if the Cloudflare plugin is active and the Hooks class exists
if (class_exists('CF\WordPress\Hooks')) {
$cloudflareHooks = new \CF\WordPress\Hooks();
// Log debug message
error_log('Purging Cloudflare cache');
// Clear the Cloudflare cache
$cloudflareHooks->purgeCacheEverything();
return true;
}
return false;
}
// Hook our function to the spinupwp_site_purged action
add_action('spinupwp_site_purged', 'clear_cloudflare_cache');
// Add admin bar menu item
function cloudflare_cache_clear_admin_bar_menu($wp_admin_bar)
{
$wp_admin_bar->add_menu(array(
'id' => 'clear-cloudflare-cache',
'title' => 'Clear CF Cache',
'href' => '#',
'meta' => array('onclick' => 'clearCloudflareCache(); return false;')
));
}
add_action('admin_bar_menu', 'cloudflare_cache_clear_admin_bar_menu', 100);
// Add JavaScript to admin footer
function cloudflare_cache_clear_admin_footer()
{
?>
<script type="text/javascript">
function clearCloudflareCache() {
var xhr = new XMLHttpRequest();
xhr.open('POST', ajaxurl, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
xhr.onload = function() {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
alert(response.data);
} else {
alert('An error occurred while clearing the cache.');
}
};
xhr.send('action=clear_cloudflare_cache&nonce=' + <?php echo json_encode(wp_create_nonce('clear_cloudflare_cache_nonce')); ?>);
}
</script>
<?php
}
add_action('admin_footer', 'cloudflare_cache_clear_admin_footer');
// AJAX handler for clearing cache
function ajax_clear_cloudflare_cache()
{
check_ajax_referer('clear_cloudflare_cache_nonce', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error('You do not have permission to perform this action.');
}
if (clear_cloudflare_cache()) {
wp_send_json_success('Cloudflare cache has been cleared successfully!');
} else {
wp_send_json_error('Failed to clear Cloudflare cache. Make sure the Cloudflare plugin is active.');
}
}
add_action('wp_ajax_clear_cloudflare_cache', 'ajax_clear_cloudflare_cache');