forked from rhubarbgroup/redis-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis-cache.php
393 lines (267 loc) · 12.1 KB
/
redis-cache.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/*
Plugin Name: Redis Object Cache
Plugin URI: https://wordpress.org/plugins/redis-cache/
Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, HHVM, replication, clustering and WP-CLI.
Version: 1.3.8
Text Domain: redis-cache
Domain Path: /languages
Author: Till Krüss
Author URI: https://till.im/
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/includes/wp-cli-commands.php';
}
class RedisObjectCache {
private $page;
private $screen = 'settings_page_redis-cache';
private $actions = array( 'enable-cache', 'disable-cache', 'flush-cache', 'update-dropin' );
public function __construct() {
load_plugin_textdomain( 'redis-cache', false, 'redis-cache/languages' );
register_activation_hook( __FILE__, 'wp_cache_flush' );
$this->page = is_multisite() ? 'settings.php?page=redis-cache' : 'options-general.php?page=redis-cache';
add_action( 'deactivate_plugin', array( $this, 'on_deactivation' ) );
add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', array( $this, 'add_admin_menu_page' ) );
add_action( 'admin_notices', array( $this, 'show_admin_notices' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
add_action( 'load-' . $this->screen, array( $this, 'do_admin_actions' ) );
add_action( 'load-' . $this->screen, array( $this, 'add_admin_page_notices' ) );
add_filter( sprintf(
'%splugin_action_links_%s',
is_multisite() ? 'network_admin_' : '',
plugin_basename( __FILE__ )
), array( $this, 'add_plugin_actions_links' ) );
}
public function add_admin_menu_page() {
// add sub-page to "Settings"
add_submenu_page(
is_multisite() ? 'settings.php' : 'options-general.php',
__( 'Redis Object Cache', 'redis-cache'),
__( 'Redis', 'redis-cache'),
is_multisite() ? 'manage_network_options' : 'manage_options',
'redis-cache',
array( $this, 'show_admin_page' )
);
}
public function show_admin_page() {
// request filesystem credentials?
if ( isset( $_GET[ '_wpnonce' ], $_GET[ 'action' ] ) ) {
$action = $_GET[ 'action' ];
foreach ( $this->actions as $name ) {
// verify nonce
if ( $action === $name && wp_verify_nonce( $_GET[ '_wpnonce' ], $action ) ) {
$url = wp_nonce_url( network_admin_url( add_query_arg( 'action', $action, $this->page ) ), $action );
if ( $this->initialize_filesystem( $url ) === false ) {
return; // request filesystem credentials
}
}
}
}
// show admin page
require_once plugin_dir_path( __FILE__ ) . '/includes/admin-page.php';
}
public function show_servers_list() {
require_once plugin_dir_path( __FILE__ ) . '/includes/servers-list.php';
$table = new Servers_List;
$table->prepare_items();
$table->display();
}
public function add_plugin_actions_links( $links ) {
// add settings link to plugin actions
return array_merge(
array( sprintf( '<a href="%s">Settings</a>', network_admin_url( $this->page ) ) ),
$links
);
}
public function enqueue_admin_styles( $hook_suffix ) {
if ( $hook_suffix === $this->screen ) {
$plugin = get_plugin_data( __FILE__ );
wp_enqueue_style( 'redis-cache', plugin_dir_url( __FILE__ ) . 'includes/admin-page.css', null, $plugin[ 'Version' ] );
}
}
public function object_cache_dropin_exists() {
return file_exists( WP_CONTENT_DIR . '/object-cache.php' );
}
public function validate_object_cache_dropin() {
if ( ! $this->object_cache_dropin_exists() ) {
return false;
}
$dropin = get_plugin_data( WP_CONTENT_DIR . '/object-cache.php' );
$plugin = get_plugin_data( plugin_dir_path( __FILE__ ) . '/includes/object-cache.php' );
if ( strcmp( $dropin[ 'PluginURI' ], $plugin[ 'PluginURI' ] ) !== 0 ) {
return false;
}
return true;
}
public function get_status() {
if ( ! $this->object_cache_dropin_exists() ) {
return __( 'Disabled', 'redis-cache' );
}
if ( $this->validate_object_cache_dropin() ) {
if ( $this->get_redis_status() ) {
return __( 'Connected', 'redis-cache' );
}
if ( $this->get_redis_status() === false ) {
return __( 'Not Connected', 'redis-cache' );
}
}
return __( 'Unknown', 'redis-cache' );
}
public function get_redis_status() {
global $wp_object_cache;
if ( $this->validate_object_cache_dropin() ) {
return $wp_object_cache->redis_status();
}
return;
}
public function get_redis_client_name() {
global $wp_object_cache;
if ( isset( $wp_object_cache->redis_client ) ) {
return $wp_object_cache->redis_client;
}
if ( defined( 'WP_REDIS_CLIENT' ) ) {
return WP_REDIS_CLIENT;
}
return null;
}
public function get_redis_cachekey_prefix() {
return defined( 'WP_CACHE_KEY_SALT' ) ? WP_CACHE_KEY_SALT : null;
}
public function get_redis_maxttl() {
return defined( 'WP_REDIS_MAXTTL' ) ? WP_REDIS_MAXTTL : null;
}
public function show_admin_notices() {
// only show admin notices to users with the right capability
if ( ! current_user_can( is_multisite() ? 'manage_network_options' : 'manage_options' ) ) {
return;
}
if ( $this->object_cache_dropin_exists() ) {
$url = wp_nonce_url( network_admin_url( add_query_arg( 'action', 'update-dropin', $this->page ) ), 'update-dropin' );
if ( $this->validate_object_cache_dropin() ) {
$dropin = get_plugin_data( WP_CONTENT_DIR . '/object-cache.php' );
$plugin = get_plugin_data( plugin_dir_path( __FILE__ ) . '/includes/object-cache.php' );
if ( version_compare( $dropin[ 'Version' ], $plugin[ 'Version' ], '<' ) ) {
$message = sprintf( __( 'The Redis object cache drop-in is outdated. Please <a href="%s">update it now</a>.', 'redis-cache' ), $url );
}
} else {
$message = sprintf( __( 'An unknown object cache drop-in was found. To use Redis, <a href="%s">please replace it now</a>.', 'redis-cache' ), $url );
}
if ( isset( $message ) ) {
printf( '<div class="update-nag">%s</div>', $message );
}
}
}
public function add_admin_page_notices() {
// show PHP version warning
if ( version_compare( PHP_VERSION, '5.4.0', '<' ) ) {
add_settings_error( '', 'redis-cache', __( 'This plugin requires PHP 5.4 or greater.', 'redis-cache' ) );
}
// show action success/failure messages
if ( isset( $_GET[ 'message' ] ) ) {
switch ( $_GET[ 'message' ] ) {
case 'cache-enabled':
$message = __( 'Object cache enabled.', 'redis-cache' );
break;
case 'enable-cache-failed':
$error = __( 'Object cache could not be enabled.', 'redis-cache' );
break;
case 'cache-disabled':
$message = __( 'Object cache disabled.', 'redis-cache' );
break;
case 'disable-cache-failed':
$error = __( 'Object cache could not be disabled.', 'redis-cache' );
break;
case 'cache-flushed':
$message = __( 'Object cache flushed.', 'redis-cache' );
break;
case 'flush-cache-failed':
$error = __( 'Object cache could not be flushed.', 'redis-cache' );
break;
case 'dropin-updated':
$message = __( 'Updated object cache drop-in and enabled Redis object cache.', 'redis-cache' );
break;
case 'update-dropin-failed':
$error = __( 'Object cache drop-in could not be updated.', 'redis-cache' );
break;
}
add_settings_error( '', 'redis-cache', isset( $message ) ? $message : $error, isset( $message ) ? 'updated' : 'error' );
}
}
public function do_admin_actions() {
global $wp_filesystem;
if ( isset( $_GET[ '_wpnonce' ], $_GET[ 'action' ] ) ) {
$action = $_GET[ 'action' ];
// verify nonce
foreach ( $this->actions as $name ) {
if ( $action === $name && ! wp_verify_nonce( $_GET[ '_wpnonce' ], $action ) ) {
return;
}
}
if ( in_array( $action, $this->actions ) ) {
$url = wp_nonce_url( network_admin_url( add_query_arg( 'action', $action, $this->page ) ), $action );
if ( $action === 'flush-cache' ) {
$message = wp_cache_flush() ? 'cache-flushed' : 'flush-cache-failed';
}
// do we have filesystem credentials?
if ( $this->initialize_filesystem( $url, true ) ) {
switch ( $action ) {
case 'enable-cache':
$result = $wp_filesystem->copy( plugin_dir_path( __FILE__ ) . '/includes/object-cache.php', WP_CONTENT_DIR . '/object-cache.php', true );
do_action( 'redis_object_cache_enable', $result );
$message = $result ? 'cache-enabled' : 'enable-cache-failed';
break;
case 'disable-cache':
$result = $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
do_action( 'redis_object_cache_disable', $result );
$message = $result ? 'cache-disabled' : 'disable-cache-failed';
break;
case 'update-dropin':
$result = $wp_filesystem->copy( plugin_dir_path( __FILE__ ) . '/includes/object-cache.php', WP_CONTENT_DIR . '/object-cache.php', true );
do_action( 'redis_object_cache_update_dropin', $result );
$message = $result ? 'dropin-updated' : 'update-dropin-failed';
break;
}
}
// redirect if status `$message` was set
if ( isset( $message ) ) {
wp_safe_redirect( network_admin_url( add_query_arg( 'message', $message, $this->page ) ) );
exit;
}
}
}
}
public function initialize_filesystem( $url, $silent = false ) {
if ( $silent ) {
ob_start();
}
if ( ( $credentials = request_filesystem_credentials( $url ) ) === false ) {
if ( $silent ) {
ob_end_clean();
}
return false;
}
if ( ! WP_Filesystem( $credentials ) ) {
request_filesystem_credentials( $url );
if ( $silent ) {
ob_end_clean();
}
return false;
}
return true;
}
public function on_deactivation( $plugin ) {
global $wp_filesystem;
if ( $plugin === plugin_basename( __FILE__ ) ) {
wp_cache_flush();
if ( $this->validate_object_cache_dropin() && $this->initialize_filesystem( '', true ) ) {
$wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
}
}
}
}
$GLOBALS[ 'redisObjectCache' ] = new RedisObjectCache;