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
|
<?php /** * Redis caching class. * * @since 2.5.0 * @package Hummingbird\Core\Modules */
namespace Hummingbird\Core\Modules;
use Hummingbird\Core\Module; use Hummingbird\Core\Settings; use Hummingbird\Core\Traits\Module as ModuleContract; use Hummingbird\Core\Traits\WPConfig;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class Redis */ class Redis extends Module {
use ModuleContract; use WPConfig;
/** * Initializes the module. Always executed even if the module is deactivated. * * @since 2.5.0 */ public function init() { add_filter( 'wp_hummingbird_is_active_module_redis', array( $this, 'module_status' ) );
if ( $this->module_status( null ) ) { add_action( 'wphb_deactivate', array( $this, 'disable' ) ); } }
/** * Clear the module cache. * * @since 2.5.0 */ public function clear_cache() { wp_cache_flush(); }
/** * Clear APC and Opcache. * * @since 2.5.0 */ public function clear_apc_and_opcache() { if ( function_exists( 'opcache_reset' ) ) { opcache_reset(); }
if ( function_exists( 'apc_clear_cache' ) ) { apc_clear_cache(); } }
/** * Set enabled option value. * * @since 2.5.0 * * @param bool $value Value. * * @return array */ public function set_enabled_option( $value ) { $options = $this->get_options(); $options['enabled'] = $value; $this->update_options( $options ); return $options; }
/** * Enable cache. * * @since 2.5.0 * * @param string $host Redis host. * @param int $port Redis port. * @param string $password Password. */ public function enable( $host, $port, $password ) { $this->set_enabled_option( true );
$this->wpconfig_add( 'WPHB_REDIS_HOST', $host ); $this->wpconfig_add( 'WPHB_REDIS_PORT', $port );
if ( $password ) { $this->wpconfig_add( 'WPHB_REDIS_PASSWORD', $password ); }
$this->toggle_object_cache( true );
// Clear redis cache. $this->test_redis_connection( $host, $port, $password, true ); }
/** * Disable cache. * * @since 2.5.0 */ public function disable() { $this->set_enabled_option( false );
$this->wpconfig_remove( 'WPHB_REDIS_HOST' ); $this->wpconfig_remove( 'WPHB_REDIS_PORT' ); $this->wpconfig_remove( 'WPHB_REDIS_PASSWORD' );
$this->toggle_object_cache( false ); }
/** * Get module status. * * @since 2.5.0 * * @param bool $current Current status. * * @return bool */ public function module_status( $current ) { $options = $this->get_options(); return $options['enabled']; }
/** * Toggle object cache. * * Will copy object-cache.php file to wp-content folder. * * @since 2.5.0 * * @param bool $enable Enable or disable object cache. */ public function toggle_object_cache( $enable ) { // See if there's already an advanced-cache.php file in place. $object_cache = dirname( get_theme_root() ) . '/object-cache.php';
// Unlink file. if ( ! $enable && file_exists( $object_cache ) && false !== strpos( file_get_contents( $object_cache ), 'Hummingbird Redis Object Cache' ) ) { wp_cache_flush(); $this->set_enabled_option( false ); unlink( $object_cache ); }
if ( $enable && ! file_exists( $object_cache ) ) { // Try to add advanced-cache.php file. $hb_object_cache = WPHB_DIR_PATH . 'core/object-cache.php'; if ( ! file_exists( $hb_object_cache ) ) { $this->set_enabled_option( false ); return; }
copy( $hb_object_cache, $object_cache ); wp_cache_flush(); }
$this->clear_apc_and_opcache(); }
/** * Test redis connection with given auth info. * * @since 2.5.0 * * @param string $host Redis host. * @param int $port Redis port. * @param string $password Password. * @param bool $clear_cache Clear cache. * * @return array */ public function test_redis_connection( $host, $port, $password, $clear_cache = false ) { $parameters = array( 'host' => $host, 'port' => $port, 'timeout' => 5, 'read_timeout' => 5, 'retry_interval' => 0, );
$redis = null; $error = ''; $client = 'predis';
if ( class_exists( 'Redis' ) && 0 !== strcasecmp( 'predis', $client ) ) { $client = defined( 'HHVM_VERSION' ) ? 'hhvm' : 'pecl'; }
try { if ( 'hhvm' === $client ) { $redis = new \Redis(); $redis->connect( $parameters['host'], $parameters['port'], $parameters['timeout'], null, $parameters['retry_interval'] );
if ( $parameters['read_timeout'] ) { $redis->setOption( \Redis::OPT_READ_TIMEOUT, $parameters['read_timeout'] ); } }
if ( 'pecl' === $client ) { $phpredis_version = phpversion( 'redis' );
$redis = new \Redis(); if ( version_compare( $phpredis_version, '3.1.3', '>=' ) ) { $redis->connect( $parameters['host'], $parameters['port'], $parameters['timeout'], null, $parameters['retry_interval'], $parameters['read_timeout'] ); } else { $redis->connect( $parameters['host'], $parameters['port'], $parameters['timeout'], null, $parameters['retry_interval'] ); } }
if ( ( 'hhvm' === $client || 'pecl' === $client ) ) { $redis->auth( $password ); }
if ( 'predis' === $client ) { // Load bundled Predis library. if ( ! class_exists( '\\Predis\\Client' ) ) { $predis_autoload_file = WPHB_DIR_PATH . 'vendor/predis/predis/autoload.php'; if ( file_exists( $predis_autoload_file ) ) { include_once $predis_autoload_file; } else { throw new \Exception( 'Predis library not found. Re-install Hummingbird plugin or delete object-cache.php.' ); } }
$options = array();
if ( $parameters['read_timeout'] ) { $parameters['read_write_timeout'] = $parameters['read_timeout']; }
if ( $password ) { $options['parameters']['password'] = $password; }
$redis = new \Predis\Client( $parameters, $options ); $redis->connect(); }
$redis->ping();
$redis_connected = true;
if ( $clear_cache ) { $redis->flushdb(); } } catch ( \Exception $exception ) { $redis_connected = false;
$error = $exception->getMessage(); }
$result = array(); if ( $redis_connected ) { $result['status'] = 'success'; } else { $result['status'] = 'fail'; $result['error'] = $error; }
return $result; }
/** * Generate admin notice for Redis settings update. * * @since 2.5.0 * * @param string $updated Type. * * @return string */ public function get_update_notice( $updated ) { $vars = $this->get_status_related_vars(); $notice = '';
switch ( $updated ) { case 'redis-auth': if ( $vars['redis_connected'] ) { $notice = __( 'Redis is connected successfully.', 'wphb' ); } else { $notice = __( 'Redis is not connected.', 'wphb' ); } break; case 'redis-disconnect': if ( ! $vars['redis_connected'] ) { $notice = __( 'Redis was disconnected successfully.', 'wphb' ); } break; case 'redis-object-cache': if ( $vars['is_redis_object_cache'] ) { $notice = __( 'Object caching is enabled successfully.', 'wphb' ); } else { $notice = __( 'Object caching is disabled successfully.', 'wphb' ); } break; case 'redis-auth-2': default: $notice = __( 'Settings updated successfully.', 'wphb' ); break; }
return $notice; }
/** * Get redis status related vars and options * * @since 2.5.0 * * @return array */ public function get_status_related_vars() { static $vars = null; if ( $vars ) { return $vars; }
global $wp_object_cache;
$redis_connected = false; $connection_error = '';
if ( is_object( $wp_object_cache ) && method_exists( $wp_object_cache, 'redis_status' ) ) { $redis_connected = $wp_object_cache->redis_status(); if ( ! $redis_connected ) { $connection_error = ! empty( $wp_object_cache->redis_error ) ? $wp_object_cache->redis_error : ''; } $redis_connected = $redis_connected && defined( 'WPHB_REDIS_HOST' ); }
if ( method_exists( $this, 'can_continue' ) ) { if ( ! defined( 'WPHB_REDIS_HOST' ) && ! $this->can_continue() ) { $redis_connected = false; $connection_error = 'wp.config.php is not editable.'; } }
$options = Settings::get_settings( 'redis' );
$object_cache = file_exists( WP_CONTENT_DIR . '/object-cache.php' );
$vars = array( 'options' => $options, 'redis_connected' => $redis_connected, 'redis_enabled' => $options['enabled'], 'is_redis_object_cache' => $object_cache, 'disable_redis' => isset( $_SERVER['WPMUDEV_HOSTED'] ) && $_SERVER['WPMUDEV_HOSTED'], 'connection_error' => $connection_error, );
return $vars; }
}
|