-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-et-cache.php
More file actions
92 lines (81 loc) · 3.31 KB
/
delete-et-cache.php
File metadata and controls
92 lines (81 loc) · 3.31 KB
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
88
89
90
91
92
<?php
/*
Plugin Name: Delete ET Cache on Cron
Plugin URI:
Description: Deletes /et-cache folder to work around issues in Pantheon's caching system and Divi creating thousands of files.
Version: 1.0
Author: Aidan Foster - Foster Interactive
Author URI: http://fosterinteractive.com
*/
// Activation hook to set an option that the plugin has been activated.
register_activation_hook(__FILE__, function() {
add_option('delete_et_cache_plugin_activated', true);
});
function delete_et_cache_admin_notice() {
if (get_option('delete_et_cache_plugin_activated')) {
echo '<div class="notice notice-warning"><p><strong>Delete ET Cache Plugin:</strong> Please define ENABLE_ET_CACHE_DELETION and ET_CACHE_DELETION_FREQUENCY in your wp-config.php to run the plugin.</p></div>';
delete_option('delete_et_cache_plugin_activated'); // Delete the option so the notice won't show again
}
}
add_action('admin_notices', 'delete_et_cache_admin_notice');
function check_required_constants() {
if (!defined('ENABLE_ET_CACHE_DELETION') || !defined('ET_CACHE_DELETION_FREQUENCY')) {
return false;
}
return true;
}
function schedule_deletion() {
if (check_required_constants()) {
$frequency = ET_CACHE_DELETION_FREQUENCY;
switch ($frequency) {
case 'hourly':
$interval = 'hourly';
$target_time = time();
break;
case 'daily':
$interval = 'daily';
$target_time = strtotime('tomorrow 3:00 am');
break;
case 'weekly':
$interval = 'weekly';
$target_time = strtotime('next Sunday 3:00 am');
break;
default:
$interval = 'weekly';
$target_time = strtotime('next Sunday 3:00 am');
break;
}
wp_clear_scheduled_hook('delete_et_cache_hook');
wp_schedule_event($target_time, $interval, 'delete_et_cache_hook');
} else {
wp_clear_scheduled_hook('delete_et_cache_hook');
}
}
add_action('init', 'schedule_deletion');
function delete_et_cache() {
if (check_required_constants() && ENABLE_ET_CACHE_DELETION) {
$cache_directory = WP_CONTENT_DIR . '/et-cache';
if (file_exists($cache_directory) && is_dir($cache_directory)) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cache_directory, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}
// Do not attempt to remove the /et-cache directory itself
// rmdir($cache_directory); <-- This line is removed to avoid permission
} else {
if (!file_exists($cache_directory)) {
error_log("Cache directory does not exist: " . $cache_directory);
} else if (!is_dir($cache_directory)) {
error_log("Specified path is not a directory: " . $cache_directory);
}
}
} else {
error_log("Required constants not set or deletion is disabled.");
}
}
add_action('delete_et_cache_hook', 'delete_et_cache');
?>