-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachemaster.php
More file actions
132 lines (115 loc) · 4.69 KB
/
cachemaster.php
File metadata and controls
132 lines (115 loc) · 4.69 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
Plugin Name: CacheMaster – Full Cache Control
Description: Limpia completamente la caché de WordPress, incluyendo transients, archivos temporales y caché de plugins compatibles. Desarrollado por Ezequiel Vidal.
Version: 1.0
Author: Ezequiel Vidal
Author URI: https://linkedin.com/in/ezeevidal
License: GPL2
*/
if (!defined('ABSPATH')) exit;
// Agregar menú al admin
add_action('admin_menu', function () {
add_menu_page(
'CacheMaster',
'CacheMaster',
'manage_options',
'cachemaster',
'cachemaster_render_admin_page',
'dashicons-update',
80
);
});
// Renderizar página admin
function cachemaster_render_admin_page() {
?>
<div class="wrap">
<h1>🧹 CacheMaster – Full Cache Control</h1>
<p>Seleccioná qué tipos de caché querés borrar:</p>
<form method="post">
<?php wp_nonce_field('cachemaster_action', 'cachemaster_nonce'); ?>
<label><input type="checkbox" name="clear_transients" checked> Borrar transients</label><br>
<label><input type="checkbox" name="clear_object_cache"> Borrar object cache</label><br>
<label><input type="checkbox" name="clear_cache_files"> Borrar archivos en /wp-content/cache/</label><br><br>
<input type="submit" name="cachemaster_clear" class="button button-primary" value="🧹 Borrar Caché Ahora">
</form>
</div>
<?php
}
// Acción del botón
add_action('admin_init', function () {
if (isset($_POST['cachemaster_clear']) && check_admin_referer('cachemaster_action', 'cachemaster_nonce')) {
if (!current_user_can('manage_options')) return;
if (isset($_POST['clear_transients'])) {
global $wpdb;
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_%'");
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_site_transient_%'");
}
if (isset($_POST['clear_object_cache']) && function_exists('wp_cache_flush')) {
wp_cache_flush();
}
if (isset($_POST['clear_cache_files'])) {
$cache_dirs = [WP_CONTENT_DIR . '/cache/'];
foreach ($cache_dirs as $dir) {
if (is_dir($dir)) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
@$todo($fileinfo->getRealPath());
}
}
}
}
add_action('admin_notices', function () {
echo '<div class="notice notice-success is-dismissible"><p><strong>✅ Caché borrada exitosamente.</strong></p></div>';
});
}
});
// Agregar botón en la barra superior del admin
add_action('admin_bar_menu', function($admin_bar) {
if (!current_user_can('manage_options')) return;
$admin_bar->add_menu(array(
'id' => 'cachemaster-clear',
'title' => '🧹 Borrar Caché',
'href' => wp_nonce_url(admin_url('?cachemaster=1'), 'cachemaster_adminbar')
));
}, 100);
// Ejecutar acción desde el botón
add_action('init', function() {
if (
is_admin() &&
isset($_GET['cachemaster']) &&
$_GET['cachemaster'] == 1 &&
current_user_can('manage_options') &&
check_admin_referer('cachemaster_adminbar')
) {
global $wpdb;
// Transients
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_%'");
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_site_transient_%'");
// Object cache
if (function_exists('wp_cache_flush')) {
wp_cache_flush();
}
// Cache de archivos
$cache_dirs = [WP_CONTENT_DIR . '/cache/'];
foreach ($cache_dirs as $dir) {
if (is_dir($dir)) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
@$todo($fileinfo->getRealPath());
}
}
}
add_action('admin_notices', function () {
echo '<div class="notice notice-success is-dismissible"><p><strong>✅ Caché borrada exitosamente desde la barra superior.</strong></p></div>';
});
}
});