WPCache.php
8.93 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
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
<?php
namespace WP_Rocket\Engine\Cache;
use WP_Rocket\Engine\Activation\ActivationInterface;
use WP_Rocket\Engine\Deactivation\DeactivationInterface;
class WPCache implements ActivationInterface, DeactivationInterface {
/**
* Filesystem instance.
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* Instantiate the class
*
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
*/
public function __construct( $filesystem ) {
$this->filesystem = $filesystem;
}
/**
* Performs these actions during the plugin activation
*
* @return void
*/
public function activate() {
add_action( 'rocket_activation', [ $this, 'update_wp_cache' ] );
}
/**
* Performs these actions during the plugin deactivation
*
* @return void
*/
public function deactivate() {
add_action( 'rocket_deactivation', [ $this, 'update_wp_cache' ] );
add_filter( 'rocket_prevent_deactivation', [ $this, 'maybe_prevent_deactivation' ] );
}
/**
* Sets the WP_CACHE constant on (de)activation
*
* @since 3.6.3
*
* @param int $sites_number Number of WP Rocket config files found.
* @return void
*/
public function update_wp_cache( $sites_number = 0 ) {
if ( ! rocket_valid_key() ) {
return;
}
$value = true;
if ( 'rocket_deactivation' === current_filter() ) {
if ( is_multisite() && 0 !== $sites_number ) {
return;
}
$value = false;
}
$this->set_wp_cache_constant( $value );
}
/**
* Updates the causes array on deactivation if needed
*
* @since 3.6.3
*
* @param array $causes Array of causes to pass to the notice.
*/
public function maybe_prevent_deactivation( $causes ) {
if (
$this->find_wpconfig_path()
||
// This filter is documented in inc/Engine/Cache/WPCache.php.
! (bool) apply_filters( 'rocket_set_wp_cache_constant', true )
) {
return $causes;
}
$causes[] = 'wpconfig';
return $causes;
}
/**
* Set WP_CACHE constant to true if needed
*
* @since 3.6.1
*
* @return void
*/
public function maybe_set_wp_cache() {
if (
rocket_get_constant( 'DOING_AJAX' )
||
rocket_get_constant( 'DOING_AUTOSAVE' )
) {
return;
}
if ( rocket_get_constant( 'WP_CACHE' ) ) {
return;
}
$this->set_wp_cache_constant( true );
}
/**
* Sets the value of the WP_CACHE constant in wp-config.php
*
* @since 3.6.1
*
* @param bool $value The value to set for WP_CACHE constant.
* @return bool true on success, false otherwise.
*/
public function set_wp_cache_constant( $value ) {
if ( ! $this->should_set_wp_cache_constant( $value ) ) {
return false;
}
$config_file_path = $this->find_wpconfig_path();
if ( ! $config_file_path ) {
return false;
}
$config_file_contents = $this->filesystem->get_contents( $config_file_path );
$value = $value ? 'true' : 'false';
/**
* Filter allow to change the value of WP_CACHE constant
*
* @since 2.1
*
* @param string $value The value of WP_CACHE constant.
*/
$value = apply_filters( 'set_rocket_wp_cache_define', $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
$wp_cache_found = preg_match( '/^\s*define\(\s*\'WP_CACHE\'\s*,\s*(?<value>[^\s\)]*)\s*\)/m', $config_file_contents, $matches );
if (
! empty( $matches['value'] )
&&
$matches['value'] === $value
) {
return false;
}
$constant = $this->get_wp_cache_content( $value );
if ( ! $wp_cache_found ) {
$config_file_contents = preg_replace( '/(<\?php)/i', "<?php\r\n{$constant}\r\n", $config_file_contents, 1 );
} elseif ( ! empty( $matches['value'] ) && $matches['value'] !== $value ) {
$config_file_contents = preg_replace( '/^\s*define\(\s*\'WP_CACHE\'\s*,\s*([^\s\)]*)\s*\).+/m', $constant, $config_file_contents );
}
return $this->filesystem->put_contents( $config_file_path, $config_file_contents, rocket_get_filesystem_perms( 'file' ) );
}
/**
* Checks if we should set the WP_CACHE constant
*
* @since 3.6.1
*
* @param bool $value The value to set for WP_CACHE constant.
* @return bool
*/
private function should_set_wp_cache_constant( $value ) {
if ( ! $this->is_user_allowed() ) {
return false;
}
if (
true === $value
&&
rocket_get_constant( 'WP_CACHE' )
) {
return false;
}
/**
* Filters the writing of the WP_CACHE constant in wp-config.php
*
* @since 3.6.1
* @param bool $set True to allow writing, false otherwise.
*/
return (bool) apply_filters( 'rocket_set_wp_cache_constant', true );
}
/**
* Try to find the correct wp-config.php file, support one level up in file tree.
*
* @since 3.6.1
*
* @return string|bool The path of wp-config.php file or false if not found.
*/
private function find_wpconfig_path() {
/**
* Filter the wp-config's filename.
*
* @since 2.11
*
* @param string $filename The WP Config filename, without the extension.
*/
$config_file_name = apply_filters( 'rocket_wp_config_name', 'wp-config' );
$abspath = rocket_get_constant( 'ABSPATH' );
$config_file = "{$abspath}{$config_file_name}.php";
if ( $this->filesystem->is_writable( $config_file ) ) {
return $config_file;
}
$abspath_parent = dirname( $abspath ) . DIRECTORY_SEPARATOR;
$config_file_alt = "{$abspath_parent}{$config_file_name}.php";
if (
$this->filesystem->exists( $config_file_alt )
&&
$this->filesystem->is_writable( $config_file_alt )
&&
! $this->filesystem->exists( "{$abspath_parent}wp-settings.php" )
) {
return $config_file_alt;
}
// No writable file found.
return false;
}
/**
* This warning is displayed when the wp-config.php file isn't writable
*
* @since 3.6.1
*
* @return void
*/
public function notice_wp_config_permissions() {
global $pagenow;
if (
'plugins.php' === $pagenow
||
isset( $_GET['activate'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
) {
return;
}
if ( ! $this->is_user_allowed() ) {
return;
}
if ( rocket_get_constant( 'WP_CACHE' ) ) {
return;
}
// This filter is documented in inc/Engine/Cache/WPCache.php.
if ( ! (bool) apply_filters( 'rocket_set_wp_cache_constant', true ) ) {
return;
}
if ( $this->find_wpconfig_path() ) {
return;
}
$notice_name = 'rocket_warning_wp_config_permissions';
if (
in_array(
$notice_name,
(array) get_user_meta( get_current_user_id(), 'rocket_boxes', true ),
true
)
) {
return;
}
rocket_notice_html(
[
'status' => 'error',
'dismissible' => '',
'message' => rocket_notice_writing_permissions( 'wp-config.php' ),
'dismiss_button' => $notice_name,
'readonly_content' => $this->get_wp_cache_content(),
]
);
}
/**
* Checks if current user can perform the action
*
* @since 3.6.1
*
* @return bool
*/
private function is_user_allowed() {
return ( rocket_get_constant( 'WP_CLI', false ) || current_user_can( 'rocket_manage_options' ) ) && rocket_valid_key();
}
/**
* Gets the content to add to the wp-config.php file
*
* @since 3.6.1
*
* @param string $value Value for the WP_CACHE constant.
* @return string
*/
private function get_wp_cache_content( $value = 'true' ) {
$plugin_name = rocket_get_constant( 'WP_ROCKET_PLUGIN_NAME' );
return "define( 'WP_CACHE', {$value} ); // Added by {$plugin_name}";
}
/**
* Adds a Site Health check for the WP_CACHE constant value
*
* @since 3.6.1
*
* @param array $tests An array of tests to perform.
* @return array
*/
public function add_wp_cache_status_test( $tests ) {
// This filter is documented in inc/Engine/Cache/WPCache.php.
if ( ! (bool) apply_filters( 'rocket_set_wp_cache_constant', true ) ) {
return $tests;
}
$tests['direct']['wp_cache_status'] = [
'label' => __( 'WP_CACHE value', 'rocket' ),
'test' => [ $this, 'check_wp_cache_value' ],
];
return $tests;
}
/**
* Checks the WP_CACHE constant value and return the result for Site Health
*
* @since 3.6.1
*
* @return array
*/
public function check_wp_cache_value() {
$result = [
'badge' => [
'label' => __( 'Cache', 'rocket' ),
],
'description' => sprintf(
'<p>%s</p>',
__( 'The WP_CACHE constant needs to be set to true for WP Rocket cache to work properly', 'rocket' )
),
'actions' => '',
'test' => 'wp_cache_status',
];
$value = rocket_get_constant( 'WP_CACHE' );
if ( true === $value ) {
$result['label'] = __( 'WP_CACHE is set to true', 'rocket' );
$result['status'] = 'good';
$result['badge']['color'] = 'green';
return $result;
}
if ( null === $value ) {
$result['label'] = __( 'WP_CACHE is not set', 'rocket' );
$result['status'] = 'critical';
$result['badge']['color'] = 'red';
return $result;
}
if ( false === $value ) {
$result['label'] = __( 'WP_CACHE is set to false', 'rocket' );
$result['status'] = 'critical';
$result['badge']['color'] = 'red';
return $result;
}
return $result;
}
}