PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
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
← All changes | includes/class-wpo-uninstall.php +164 -26 3.7.0 → 4.7.0 View file →
@@ -6,10 +6,19 @@
6 6 class WPO_Uninstall {
7 7
8 8 /**
9 9 * Actions to be performed upon plugin uninstallation
10 + *
11 + * @return void
10 12 */
11 - public static function actions() {
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 +
12 21 WP_Optimize()->get_gzip_compression()->disable();
13 22 WP_Optimize()->get_browser_cache()->disable();
14 23 WP_Optimize()->get_options()->delete_all_options();
15 24 WP_Optimize()->get_minify()->plugin_uninstall();
@@ -14,18 +23,44 @@
14 23 WP_Optimize()->get_options()->delete_all_options();
15 24 WP_Optimize()->get_minify()->plugin_uninstall();
16 25 WP_Optimize()->get_options()->wipe_settings();
17 26 WP_Optimize()->delete_transients_and_semaphores();
27 + WP_Optimize()->get_table_management()->delete_plugin_tables();
18 28 Updraft_Tasks_Activation::uninstall(WPO_PLUGIN_SLUG);
19 29 self::delete_wpo_folder();
20 -
21 - $htaccess_file = self::get_upload_basedir() . '.htaccess';
22 - if (is_file($htaccess_file) && 0 === filesize($htaccess_file)) {
23 - unlink($htaccess_file);
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);
24 54 }
25 -
26 - wp_clear_scheduled_hook('process_smush_tasks');
27 - WP_Optimize()->wpo_cron_deactivate();
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 + }
28 63 }
29 64
30 65 /**
31 66 * Returns absolute path to uploads folder
@@ -31,9 +66,9 @@
31 66 * Returns absolute path to uploads folder
32 67 *
33 68 * @return string
34 69 */
35 - private static function get_upload_basedir() {
70 + private static function get_upload_basedir(): string {
36 71 $upload_dir = wp_get_upload_dir();
37 72 return trailingslashit($upload_dir['basedir']);
38 73 }
39 74
@@ -39,11 +74,11 @@
39 74
40 75 /**
41 76 * Returns an array of sub folders in `uploads/wpo` folder
42 77 *
43 - * @return array
78 + * @return array<string>
44 79 */
45 - private static function get_wpo_sub_folders() {
80 + private static function get_wpo_sub_folders(): array {
46 81 $sub_folders = array(
47 82 'add-type',
48 83 'content-digest',
49 84 'crash-tester',
@@ -54,29 +89,132 @@
54 89 'rewrite',
55 90 'server-signature',
56 91 'logs',
57 92 );
58 - return apply_filters('wpo_uploads_sub_folders', $sub_folders);
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;
59 99 }
60 100
61 101 /**
62 - * Delete `uploads/wpo` sub folders and if it is empty delete the folder itself
102 + * Returns an array of known files in `uploads/wpo` root folder
103 + *
104 + * @return array<string>
63 105 */
64 - public static function delete_wpo_folder() {
65 - $wpo_folder = self::get_upload_basedir() . trailingslashit('wpo');
66 - require_once WPO_PLUGIN_MAIN_PATH . 'cache/file-based-page-cache-functions.php';
67 - if (is_dir($wpo_folder)) {
68 - $wpo_sub_folders = self::get_wpo_sub_folders();
69 - foreach ($wpo_sub_folders as $folder) {
70 - wpo_delete_files($wpo_folder . $folder);
71 - }
106 + private static function get_wpo_root_files(): array {
107 + return array(
108 + 'wpo-plugins-tables-list.json'
109 + );
110 + }
72 111
73 - $files = @scandir($wpo_folder); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warning if it arises due to race condition
74 - if (false === $files) return;
75 - if (2 === count($files)) {
76 - @rmdir($wpo_folder); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress error due to file permission issues
77 - }
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;
78 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');
79 217 }
80 218 }
81 219
82 220 endif;