-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwee-object-cache.php
More file actions
252 lines (209 loc) · 6.06 KB
/
Copy pathtwee-object-cache.php
File metadata and controls
252 lines (209 loc) · 6.06 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Plugin Name: Twee Object Cache
* Plugin URI: https://github.com/TwistedAndy/twee-object-cache
* Description: A suite of high-performance, lightweight object caches built for large-scale WordPress projects.
* Author: Andrii Toniievych
* Author URI: https://www.linkedin.com/in/toniievych/
* Version: 1.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
add_action('admin_bar_menu', 'twee_object_cache_admin_bar', 100);
/**
* Clear object cache on activation
*/
register_activation_hook(__FILE__, function(): void {
wp_cache_flush();
});
/**
* Clear object cache on deactivation
*/
register_deactivation_hook(__FILE__, function(): void {
wp_cache_flush();
$dropin_file = WP_CONTENT_DIR . '/object-cache.php';
if (file_exists($dropin_file)) {
unlink($dropin_file);
}
});
/**
* Add a notice when it's not possible to activate the cache engine
*/
if (isset($_GET['twee_cache_error']) and $_GET['twee_cache_error'] === 'unreachable') {
add_action('admin_notices', function() {
$message = 'The selected cache engine is not reachable. Please check your configuration.';
if (function_exists('wp_admin_notice')) {
wp_admin_notice($message, ['type' => 'error', 'dismissible' => true,]);
} else {
echo '<div class="notice notice-error"><p>' . $message . '</p></div>';
}
});
}
/**
* Add the Object Cache menu in the WordPress Admin Bar
*/
function twee_object_cache_admin_bar($admin_bar): void
{
if (!current_user_can('manage_options')) {
return;
}
// Handle actions (enable, disable, flush)
$action = $_GET['twee_cache_action'] ?? '';
if ($action and wp_verify_nonce($_GET['_wpnonce'] ?? '', 'twee_cache_action')) {
$dropin_file = WP_CONTENT_DIR . '/object-cache.php';
$redirect_url = remove_query_arg(['twee_cache_action', '_wpnonce', 'twee_cache_error']);
if ($action === 'flush') {
wp_cache_flush();
} elseif ($action === 'disable' and file_exists($dropin_file)) {
unlink($dropin_file);
} elseif (strpos($action, 'enable_') === 0) {
$engine = str_replace('enable_', '', $action);
if (twee_object_cache_is_reachable($engine)) {
$source_file = plugin_dir_path(__FILE__) . $engine . '/object-cache.php';
if (file_exists($source_file)) {
copy($source_file, $dropin_file);
wp_cache_flush();
$redirect_url = add_query_arg([
'twee_cache_action' => 'flush',
'_wpnonce' => wp_create_nonce('twee_cache_action')
], $redirect_url);
}
} else {
$redirect_url = add_query_arg('twee_cache_error', 'unreachable', $redirect_url);
}
}
wp_redirect($redirect_url);
exit;
}
$admin_bar->add_node([
'id' => 'twee-object-cache',
'title' => 'Object Cache',
'href' => '#',
]);
if (file_exists(WP_CONTENT_DIR . '/object-cache.php')) {
global $wp_object_cache;
if (is_object($wp_object_cache) and method_exists($wp_object_cache, 'stats')) {
ob_start();
$wp_object_cache->stats();
$stats_html = ob_get_clean();
} else {
$stats_html = '';
}
if ($stats_html) {
$stats_html = strip_tags($stats_html, '<strong><br>');
$lines = preg_split('/<br\s*\/?>/i', $stats_html, -1, PREG_SPLIT_NO_EMPTY);
foreach ($lines as $line) {
if (strpos($line, 'Engine:') !== false) {
$admin_bar->add_node([
'id' => 'twee-cache-stats',
'parent' => 'twee-object-cache',
'title' => trim($line),
]);
break;
}
}
}
$flush_url = wp_nonce_url(add_query_arg('twee_cache_action', 'flush'), 'twee_cache_action');
$admin_bar->add_node([
'id' => 'twee-cache-flush',
'parent' => 'twee-object-cache',
'title' => 'Clean Cache',
'href' => $flush_url,
]);
$disable_url = wp_nonce_url(add_query_arg('twee_cache_action', 'disable'), 'twee_cache_action');
$admin_bar->add_node([
'id' => 'twee-cache-disable',
'parent' => 'twee-object-cache',
'title' => 'Disable Cache',
'href' => $disable_url,
]);
} else {
// Check available extensions
$engines = [
'memcached' => 'Memcached',
'redis' => 'Redis',
'apcu' => 'APCu'
];
$available = [];
foreach ($engines as $ext => $name) {
if (extension_loaded($ext)) {
$available[$ext] = $name;
}
}
if (empty($available)) {
$admin_bar->add_node([
'id' => 'twee-cache-no-ext',
'parent' => 'twee-object-cache',
'title' => 'No extensions available',
]);
} else {
foreach ($available as $ext => $name) {
$enable_url = wp_nonce_url(add_query_arg('twee_cache_action', 'enable_' . $ext), 'twee_cache_action');
$admin_bar->add_node([
'id' => 'twee-cache-enable-' . $ext,
'parent' => 'twee-object-cache',
'title' => 'Enable ' . $name,
'href' => $enable_url,
]);
}
}
}
}
/**
* Check if the selected cache engine is actually working
*/
function twee_object_cache_is_reachable($ext): bool
{
if ($ext === 'apcu') {
return (function_exists('apcu_enabled') and apcu_enabled());
}
if ($ext === 'memcached') {
if (!class_exists('Memcached')) {
return false;
}
$memcached = new Memcached();
global $memcached_servers;
if (!empty($memcached_servers)) {
$memcached->addServers($memcached_servers);
} else {
$memcached->addServer('127.0.0.1', 11211);
}
$stats = $memcached->getStats();
if (empty($stats)) {
return false;
}
foreach ($stats as $server => $data) {
if (isset($data['pid']) and $data['pid'] > 0) {
return true;
}
}
return false;
}
if ($ext === 'redis') {
if (!class_exists('Redis')) {
return false;
}
$redis = new Redis();
$host = defined('WP_REDIS_HOST') ? WP_REDIS_HOST : '127.0.0.1';
$port = defined('WP_REDIS_PORT') ? WP_REDIS_PORT : 6379;
$timeout = defined('WP_REDIS_TIMEOUT') ? WP_REDIS_TIMEOUT : 1;
try {
if (strpos($host, 'unix://') === 0 or strpos($host, '/') === 0) {
$connected = $redis->connect($host, 0, $timeout);
} else {
$connected = $redis->connect($host, (int) $port, $timeout);
}
if (!$connected) {
return false;
}
if (defined('WP_REDIS_PASSWORD') and !$redis->auth(WP_REDIS_PASSWORD)) {
return false;
}
return true;
} catch (Exception $e) {
return false;
}
}
return false;
}