WPRocketUninstall.php
6.36 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
<?php
use WP_Rocket\Engine\Optimization\RUCSS\Database\Tables\UsedCSS;
use WP_Rocket\Engine\Preload\Database\Tables\Cache;
/**
* Manages the deletion of WP Rocket data and files on uninstall.
*/
class WPRocketUninstall {
/**
* Path to the cache folder.
*
* @var string
*/
private $cache_path;
/**
* Path to the config folder.
*
* @var string
*/
private $config_path;
/**
* WP Rocket options.
*
* @var array
*/
private $options = [
'wp_rocket_settings',
'rocket_analytics_notice_displayed',
'rocketcdn_user_token',
'rocketcdn_process',
'wp_rocket_hide_deactivation_form',
];
/**
* WP Rocket transients.
*
* @var array
*/
private $transients = [
'wp_rocket_customer_data',
'rocket_notice_missing_tags',
'rocket_clear_cache',
'rocket_check_key_errors',
'rocket_send_analytics_data',
'rocket_critical_css_generation_process_running',
'rocket_critical_css_generation_process_complete',
'rocket_critical_css_generation_triggered',
'rocketcdn_status',
'rocketcdn_pricing',
'rocketcdn_purge_cache_response',
'rocket_cloudflare_ips',
'rocket_cloudflare_is_api_keys_valid',
'rocket_preload_triggered',
'rocket_preload_complete',
'rocket_preload_complete_time',
'rocket_preload_errors',
'rocket_database_optimization_process',
'rocket_database_optimization_process_complete',
'wp_rocket_no_licence',
'rocket_hide_deactivation_form',
'wpr_preload_running',
'rocket_preload_as_tables_count',
'wpr_dynamic_lists',
];
/**
* WP Rocket scheduled events.
*
* @var array
*/
private $events = [
'rocket_purge_time_event',
'rocket_database_optimization_time_event',
'rocket_cache_dir_size_check',
'rocketcdn_check_subscription_status_event',
'rocket_cron_deactivate_cloudflare_devmode',
];
/**
* WP Rocket cache directories.
*
* @var array
*/
private $cache_dirs = [
'wp-rocket',
'min',
'busting',
'critical-css',
'used-css',
];
/**
* WP Rocket Post MetaData Entries
*
* @var array
*/
private $post_meta = [
'minify_css',
'minify_js',
'cdn',
'lazyload',
'lazyload_iframes',
'async_css',
'defer_all_js',
'delay_js',
'remove_unused_css',
];
/**
* Instance of RUCSS used_css table.
*
* @var WP_Rocket\Engine\Optimization\RUCSS\Database\Tables\UsedCSS
*/
private $rucss_usedcss_table;
/**
* Instance of Preload rocket_cache table.
*
* @var Cache
*/
private $rocket_cache;
/**
* Constructor.
*
* @param string $cache_path Path to the cache folder.
* @param string $config_path Path to the config folder.
* @param UsedCSS $rucss_usedcss_table RUCSS used_css table.
* @param Cache $rocket_cache Preload rocket_cache table.
*/
public function __construct( $cache_path, $config_path, $rucss_usedcss_table, $rocket_cache ) {
$this->cache_path = trailingslashit( $cache_path );
$this->config_path = $config_path;
$this->rucss_usedcss_table = $rucss_usedcss_table;
$this->rocket_cache = $rocket_cache;
}
/**
* Deletes all plugin data and files on uninstall.
*
* @since 3.5.2
* @author Remy Perona
*
* @return void
*/
public function uninstall() {
$this->delete_plugin_data();
$this->delete_cache_files();
$this->delete_config_files();
$this->drop_rucss_database_tables();
$this->delete_preload_table();
}
/**
* Drop RUCSS database tables.
*
* @return void
*/
private function drop_rucss_database_tables() {
// If the table exist, then drop the table.
$this->drop_rucss_current_site_tables();
if ( ! is_multisite() ) {
return;
}
foreach ( get_sites( [ 'fields' => 'ids' ] ) as $site_id ) {
switch_to_blog( $site_id );
$this->drop_rucss_current_site_tables();
restore_current_blog();
}
}
/**
* Drop RUCSS tables for current active site.
*/
private function drop_rucss_current_site_tables() {
if ( $this->rucss_usedcss_table->exists() ) {
$this->rucss_usedcss_table->uninstall();
}
}
/**
* Deletes WP Rocket options, transients and events.
*
* @since 3.5.2
* @author Remy Perona
*
* @return void
*/
private function delete_plugin_data() {
delete_site_transient( 'wp_rocket_update_data' );
// Delete all user meta related to WP Rocket.
delete_metadata( 'user', '', 'rocket_boxes', '', true );
// Delete all post meta related to WP Rocket.
foreach ( $this->post_meta as $post_meta ) {
delete_post_meta_by_key( "_rocket_exclude_{$post_meta}" );
}
array_walk( $this->transients, 'delete_transient' );
array_walk( $this->options, 'delete_option' );
foreach ( $this->events as $event ) {
wp_clear_scheduled_hook( $event );
}
}
/**
* Deletes all WP Rocket cache files.
*
* @since 3.5.2
* @author Remy Perona
*
* @return void
*/
private function delete_cache_files() {
foreach ( $this->cache_dirs as $dir ) {
$this->delete( $this->cache_path . $dir );
}
}
/**
* Deletes all WP Rocket config files.
*
* @since 3.5.2
* @author Remy Perona
*
* @return void
*/
private function delete_config_files() {
$this->delete( $this->config_path );
}
/**
* Drop preload tables.
*/
private function delete_preload_table() {
// If the table exist, then drop the table.
if ( $this->rocket_cache->exists() ) {
$this->rocket_cache->uninstall();
}
if ( ! is_multisite() ) {
return;
}
foreach ( get_sites( [ 'fields' => 'ids' ] ) as $site_id ) {
switch_to_blog( $site_id );
if ( $this->rocket_cache->exists() ) {
$this->rocket_cache->uninstall();
}
restore_current_blog();
}
}
/**
* Recursively deletes files and directories.
*
* @since 3.5.2
* @author Remy Perona
*
* @param string $file Path to file or directory.
*/
private function delete( $file ) {
if ( ! is_dir( $file ) ) {
@unlink( $file ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
return;
}
try {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $file, FilesystemIterator::SKIP_DOTS ),
RecursiveIteratorIterator::CHILD_FIRST
);
} catch ( UnexpectedValueException $e ) {
return;
}
foreach ( $iterator as $item ) {
if ( $item->isDir() ) {
@rmdir( $item ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
continue;
}
@unlink( $item ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
}
@rmdir( $file ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
}
}