PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.4
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.4
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / class-wpo-uninstall.php

class-wpo-uninstall.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.4, at includes/class-wpo-uninstall.php

221 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('Access denied.');
3
4 if (!class_exists('WPO_Uninstall')) :
5
6 class WPO_Uninstall {
7
8 /**
9 * Actions to be performed upon plugin uninstallation
10 *
11 * @return void
12 */
13 public static function actions(): void {
14 self::require_dependency();
15
16 self::maybe_delete_premium_cache_data();
17 if (self::is_plugin_active()) {
18 return;
19 }
20
21 WP_Optimize()->get_gzip_compression()->disable();
22 WP_Optimize()->get_browser_cache()->disable();
23 WP_Optimize()->get_options()->delete_all_options();
24 WP_Optimize()->get_minify()->plugin_uninstall();
25 WP_Optimize()->get_options()->wipe_settings();
26 WP_Optimize()->delete_transients_and_semaphores();
27 WP_Optimize()->get_table_management()->delete_plugin_tables();
28 Updraft_Tasks_Activation::uninstall(WPO_PLUGIN_SLUG);
29 self::delete_wpo_folder();
30 self::delete_htaccess_file_in_uploads_folder();
31 self::cron_deactivate();
32 }
33
34 /**
35 * `wpo_delete_files` defined in dependency
36 *
37 * @return void
38 */
39 private static function require_dependency(): void {
40 require_once WPO_PLUGIN_MAIN_PATH . 'cache/file-based-page-cache-functions.php';
41 }
42
43 /**
44 * Delete cache data created by premium-only features.
45 *
46 * Only runs when premium is not active, to avoid wiping premium data
47 * while the premium version is still installed.
48 *
49 * @return void
50 */
51 private static function delete_premium_cache_data(): void {
52 if (class_exists('WPO_Gravatar_Data')) {
53 wpo_delete_files(WPO_Gravatar_Data::WPO_CACHE_GRAVATAR_DIR);
54 }
55
56 if (class_exists('WP_Optimize_Minify_Analytics')) {
57 wpo_delete_files(WP_Optimize_Minify_Analytics::WPO_CACHE_GTAG_DIR);
58 }
59
60 if (class_exists('WP_Optimize_Lazy_Load')) {
61 WP_Optimize_Lazy_Load::instance()->delete_image_cache();
62 }
63 }
64
65 /**
66 * Returns absolute path to uploads folder
67 *
68 * @return string
69 */
70 private static function get_upload_basedir(): string {
71 $upload_dir = wp_get_upload_dir();
72 return trailingslashit($upload_dir['basedir']);
73 }
74
75 /**
76 * Returns an array of sub folders in `uploads/wpo` folder
77 *
78 * @return array<string>
79 */
80 private static function get_wpo_sub_folders(): array {
81 $sub_folders = array(
82 'add-type',
83 'content-digest',
84 'crash-tester',
85 'directory-index',
86 'header-set',
87 'images',
88 'module-loaded',
89 'rewrite',
90 'server-signature',
91 'logs',
92 );
93
94 /**
95 * @var array<string> $filtered_sub_folders
96 */
97 $filtered_sub_folders = apply_filters('wpo_uploads_sub_folders', $sub_folders);
98 return is_array($filtered_sub_folders) ? $filtered_sub_folders : $sub_folders;
99 }
100
101 /**
102 * Returns an array of known files in `uploads/wpo` root folder
103 *
104 * @return array<string>
105 */
106 private static function get_wpo_root_files(): array {
107 return array(
108 'wpo-plugins-tables-list.json'
109 );
110 }
111
112 /**
113 * Delete `uploads/wpo` folder, its known sub folders, and known files
114 *
115 * @return void
116 */
117 public static function delete_wpo_folder(): void {
118 self::require_dependency();
119 $wpo_folder = self::get_wpo_folder();
120
121 if (!is_dir($wpo_folder)) {
122 return;
123 }
124
125 // Even though `wpo_delete_files()` can delete recursively, we can't be sure `wpo` folder
126 // only contains our own files and folders, we need to be specific here about what is going to be deleted
127 $wpo_sub_folders = self::get_wpo_sub_folders();
128 foreach ($wpo_sub_folders as $folder) {
129 wpo_delete_files($wpo_folder . $folder);
130 }
131 self::delete_wpo_root_files();
132
133 self::maybe_delete_empty_folder($wpo_folder);
134 }
135
136 /**
137 * Removes given folder, if it is empty
138 *
139 * @param string $wpo_folder Path of folder to check, and delete
140 *
141 * @return void
142 */
143 private static function maybe_delete_empty_folder(string $wpo_folder): void {
144 // phpcs:disable
145 // Generic.PHP.NoSilencedErrors.Discouraged -- suppress warning if it arises due to race condition
146 // WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- Not applicable in this context
147 // If `wpo` is empty after deleting our own stuff, folder itself is ours, so remove it too
148 $files = @scandir($wpo_folder);
149 if (false !== $files && 2 === count($files)) {
150 @rmdir($wpo_folder);
151 }
152 // phpcs:enable
153 }
154
155 /**
156 * Deletes the .htaccess file in the uploads folder if it exists and is empty.
157 *
158 * @return void
159 */
160 private static function delete_htaccess_file_in_uploads_folder(): void {
161 $htaccess_file = self::get_upload_basedir() . '.htaccess';
162 if (is_file($htaccess_file) && 0 === filesize($htaccess_file)) {
163 wp_delete_file($htaccess_file);
164 }
165 }
166
167 /**
168 * Delete known root-level files
169 *
170 * @return void
171 */
172 private static function delete_wpo_root_files(): void {
173 $wpo_root_files = self::get_wpo_root_files();
174 $wpo_folder = self::get_wpo_folder();
175 foreach ($wpo_root_files as $file) {
176 wpo_delete_files($wpo_folder . $file, false);
177 }
178 }
179
180 /**
181 * Checks whether premium or free version of WPO plugin is active
182 *
183 * @return bool
184 */
185 private static function is_plugin_active(): bool {
186 return WP_Optimize()->is_active('premium') || WP_Optimize()->is_active();
187 }
188
189 /**
190 * Deletes premium cache data, if premium version of plugin is not active
191 *
192 * @return void
193 */
194 private static function maybe_delete_premium_cache_data(): void {
195 if (!WP_Optimize()->is_active('premium')) {
196 self::delete_premium_cache_data();
197 }
198 }
199
200 /**
201 * Deactivates cron jobs
202 *
203 * @return void
204 */
205 private static function cron_deactivate(): void {
206 wp_clear_scheduled_hook('process_smush_tasks');
207 WP_Optimize()->wpo_cron_deactivate();
208 }
209
210 /**
211 * Full path to `wpo` folder in uploads directory
212 *
213 * @return string
214 */
215 private static function get_wpo_folder(): string {
216 return self::get_upload_basedir() . trailingslashit('wpo');
217 }
218 }
219
220 endif;
221