-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathRoot_Environment.php
285 lines (248 loc) · 7.81 KB
/
Root_Environment.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
<?php
/**
* File: Root_Environment.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Root_Environment
*
* phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
*/
class Root_Environment {
/**
* Fixes the environment configuration in the WordPress admin panel.
*
* @param Config $config W3TC Config containing relevant settings.
* @param bool $force_all_checks Whether to force all environment checks.
*
* @return void
*
* @throws Util_Environment_Exceptions If one or more handlers fail during the fix process.
*/
public function fix_in_wpadmin( $config, $force_all_checks = false ) {
$exs = new Util_Environment_Exceptions();
$fix_on_event = false;
if ( Util_Environment::is_wpmu() && Util_Environment::blog_id() !== 0 ) {
$md5_string = $config->get_md5();
if ( get_transient( 'w3tc_config_changes' ) !== $md5_string ) {
$fix_on_event = true;
set_transient( 'w3tc_config_changes', $md5_string, 3600 );
}
}
// call plugin-related handlers.
foreach ( $this->get_handlers() as $h ) {
try {
$h->fix_on_wpadmin_request( $config, $force_all_checks );
if ( $fix_on_event ) {
$this->fix_on_event( $config, 'admin_request' );
}
} catch ( Util_Environment_Exceptions $ex ) {
$exs->push( $ex );
}
}
try {
do_action( 'w3tc_environment_fix_on_wpadmin_request', $config, $force_all_checks );
} catch ( Util_Environment_Exceptions $ex ) {
$exs->push( $ex );
}
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}
/**
* Fixes the environment configuration based on specific events.
*
* @param Config $config W3TC Config containing relevant settings.
* @param string $event Name of the triggering event.
* @param Config|null $old_config Optional old W3TC Config object for comparison.
*
* @return void
*
* @throws Util_Environment_Exceptions If one or more handlers fail during the fix process.
*/
public function fix_on_event( $config, $event, $old_config = null ) {
$exs = new Util_Environment_Exceptions();
// call plugin-related handlers.
foreach ( $this->get_handlers() as $h ) {
try {
$h->fix_on_event( $config, $event );
} catch ( Util_Environment_Exceptions $ex ) {
$exs->push( $ex );
}
}
try {
do_action( 'w3tc_environment_fix_on_event', $config, $event );
} catch ( Util_Environment_Exceptions $ex ) {
$exs->push( $ex );
}
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}
/**
* Fixes the environment configuration after deactivation.
*
* @return void
*
* @throws Util_Environment_Exceptions If one or more handlers fail during the fix process.
*/
public function fix_after_deactivation() {
$exs = new Util_Environment_Exceptions();
// call plugin-related handlers.
foreach ( $this->get_handlers() as $h ) {
try {
$h->fix_after_deactivation();
} catch ( Util_Environment_Exceptions $ex ) {
$exs->push( $ex );
}
}
try {
do_action( 'w3tc_environment_fix_after_deactivation' );
} catch ( Util_Environment_Exceptions $ex ) {
$exs->push( $ex );
}
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}
/**
* Retrieves the rules required for the environment configuration.
*
* @param Config $config W3TC Config containing relevant settings.
*
* @return array Array of descriptors for required rewrite rules.
*/
public function get_required_rules( $config ) {
$required_rules = array();
foreach ( $this->get_handlers() as $h ) {
if ( method_exists( $h, 'get_required_rules' ) ) {
$required_rules_current = $h->get_required_rules( $config );
if ( ! is_null( $required_rules_current ) ) {
$required_rules = array_merge( $required_rules, $required_rules_current );
}
}
}
$required_rules = apply_filters( 'w3tc_environment_get_required_rules', $required_rules, $config );
$rewrite_rules_descriptors = array();
foreach ( $required_rules as $descriptor ) {
$filename = $descriptor['filename'];
if ( isset( $rewrite_rules_descriptors[ $filename ] ) ) {
$content = $rewrite_rules_descriptors[ $filename ]['content'];
} else {
$content = array();
}
if ( ! isset( $descriptor['position'] ) ) {
$content[] = $descriptor['content'];
} else {
$position = $descriptor['position'];
if ( isset( $content[ $position ] ) ) {
$content[ $position ] .= $descriptor['content'];
} else {
$content[ $position ] = $descriptor['content'];
}
}
$rewrite_rules_descriptors[ $filename ] = array(
'filename' => $filename,
'content' => $content,
);
}
$rewrite_rules_descriptors_out = array();
foreach ( $rewrite_rules_descriptors as $filename => $descriptor ) {
$rewrite_rules_descriptors_out[ $filename ] = array(
'filename' => $filename,
'content' => implode( '', $descriptor['content'] ),
);
}
ksort( $rewrite_rules_descriptors_out );
return $rewrite_rules_descriptors_out;
}
/**
* Retrieves the list of environment handlers.
*
* @return array Array of handler objects for managing environment configurations.
*/
private function get_handlers() {
$a = array(
new Generic_Environment(),
new Minify_Environment(),
new PgCache_Environment(),
new BrowserCache_Environment(),
new ObjectCache_Environment(),
new DbCache_Environment(),
new Cdn_Environment(),
new Extension_ImageService_Environment(),
new Extension_AlwaysCached_Environment(),
);
return $a;
}
/**
* Retrieves additional instructions for the environment configuration.
*
* @param Config $config W3TC Config containing relevant settings.
*
* @return array Array of descriptors for additional instructions, grouped by area.
*/
public function get_other_instructions( $config ) {
$instructions_descriptors = array();
foreach ( $this->get_handlers() as $h ) {
if ( method_exists( $h, 'get_instructions' ) ) {
$instructions = $h->get_instructions( $config );
if ( ! is_null( $instructions ) ) {
foreach ( $instructions as $descriptor ) {
$area = $descriptor['area'];
$instructions_descriptors[ $area ][] = array(
'title' => $descriptor['title'],
'content' => $descriptor['content'],
);
}
}
}
}
return $instructions_descriptors;
}
/**
* Deletes all W3 Total Cache data from the database.
*
* phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
* phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
* phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log
*
* @since 2.8.3
*
* @param Config $config W3TC Config containing relevant settings.
*
* @return void
*/
public static function delete_plugin_data( $config ) {
global $wpdb;
$license_key = $config->get_string( 'plugin.license_key' );
if ( ! empty( $license_key ) ) {
Licensing_Core::deactivate_license( $license_key );
}
// Define prefixes for options and transients.
$prefixes = array(
'w3tc_', // General options prefix.
'w3tcps_', // Additional options prefix.
'_transient_w3tc_', // Transient prefix.
'_transient_timeout_w3tc_', // Transient timeout prefix.
);
// Delete options and transients with defined prefixes.
foreach ( $prefixes as $prefix ) {
$query = 'SELECT `option_name` FROM ' . $wpdb->options . ' WHERE `option_name` LIKE "' . $prefix . '%";';
$option_names = $wpdb->get_col( $query ); // phpcs:ignore WordPress.DB.PreparedSQL
foreach ( $option_names as $option_name ) {
delete_option( $option_name );
}
}
// Remove plugin-created directories.
$directories = array(
defined( 'W3TC_CACHE_DIR' ) ? W3TC_CACHE_DIR : WP_CONTENT_DIR . '/cache',
defined( 'W3TC_CONFIG_DIR' ) ? W3TC_CONFIG_DIR : WP_CONTENT_DIR . '/w3tc-config',
);
foreach ( $directories as $dir ) {
Util_File::rmdir( $dir );
}
}
}