| @@ -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,26 +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(); |
| 18 | - // Using Updraft_Smush_Manager() throws `Call to undefined function` fatal error | |
| 19 | - Updraft_Smush_Manager::instance()->delete_log_files(); | |
| 27 | + WP_Optimize()->get_table_management()->delete_plugin_tables(); | |
| 20 | 28 | Updraft_Tasks_Activation::uninstall(WPO_PLUGIN_SLUG); |
| 21 | 29 | self::delete_wpo_folder(); |
| 30 | + self::delete_htaccess_file_in_uploads_folder(); | |
| 31 | + self::cron_deactivate(); | |
| 32 | + } | |
| 22 | 33 | |
| 23 | - $wpo_plugins_table_list = self::get_upload_basedir() . 'wpo-plugins-tables-list.json'; | |
| 24 | - if (is_file($wpo_plugins_table_list)) { | |
| 25 | - unlink($wpo_plugins_table_list); | |
| 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); | |
| 26 | 54 | } |
| 27 | - | |
| 28 | - $htaccess_file = self::get_upload_basedir() . '.htaccess'; | |
| 29 | - if (is_file($htaccess_file) && 0 === filesize($htaccess_file)) { | |
| 30 | - unlink($htaccess_file); | |
| 55 | + | |
| 56 | + if (class_exists('WP_Optimize_Minify_Analytics')) { | |
| 57 | + wpo_delete_files(WP_Optimize_Minify_Analytics::WPO_CACHE_GTAG_DIR); | |
| 31 | 58 | } |
| 32 | - | |
| 33 | - wp_clear_scheduled_hook('wpo_smush_clear_backup_images'); | |
| 34 | - wp_clear_scheduled_hook('wpo_minify_purge_old_cache'); | |
| 35 | - wp_clear_scheduled_hook('process_smush_tasks'); | |
| 59 | + | |
| 60 | + if (class_exists('WP_Optimize_Lazy_Load')) { | |
| 61 | + WP_Optimize_Lazy_Load::instance()->delete_image_cache(); | |
| 62 | + } | |
| 36 | 63 | } |
| 37 | 64 | |
| 38 | 65 | /** |
| 39 | 66 | * Returns absolute path to uploads folder |
| @@ -39,9 +66,9 @@ | ||
| 39 | 66 | * Returns absolute path to uploads folder |
| 40 | 67 | * |
| 41 | 68 | * @return string |
| 42 | 69 | */ |
| 43 | - private static function get_upload_basedir() { | |
| 70 | + private static function get_upload_basedir(): string { | |
| 44 | 71 | $upload_dir = wp_get_upload_dir(); |
| 45 | 72 | return trailingslashit($upload_dir['basedir']); |
| 46 | 73 | } |
| 47 | 74 | |
| @@ -47,11 +74,11 @@ | ||
| 47 | 74 | |
| 48 | 75 | /** |
| 49 | 76 | * Returns an array of sub folders in `uploads/wpo` folder |
| 50 | 77 | * |
| 51 | - * @return array | |
| 78 | + * @return array<string> | |
| 52 | 79 | */ |
| 53 | - private static function get_wpo_sub_folders() { | |
| 80 | + private static function get_wpo_sub_folders(): array { | |
| 54 | 81 | $sub_folders = array( |
| 55 | 82 | 'add-type', |
| 56 | 83 | 'content-digest', |
| 57 | 84 | 'crash-tester', |
| @@ -60,29 +87,134 @@ | ||
| 60 | 87 | 'images', |
| 61 | 88 | 'module-loaded', |
| 62 | 89 | 'rewrite', |
| 63 | 90 | 'server-signature', |
| 91 | + 'logs', | |
| 64 | 92 | ); |
| 65 | - 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; | |
| 66 | 99 | } |
| 67 | 100 | |
| 68 | 101 | /** |
| 69 | - * 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> | |
| 70 | 105 | */ |
| 71 | - public static function delete_wpo_folder() { | |
| 72 | - $wpo_folder = self::get_upload_basedir() . trailingslashit('wpo'); | |
| 73 | - require_once WPO_PLUGIN_MAIN_PATH . 'cache/file-based-page-cache-functions.php'; | |
| 74 | - if (is_dir($wpo_folder)) { | |
| 75 | - $wpo_sub_folders = self::get_wpo_sub_folders(); | |
| 76 | - foreach ($wpo_sub_folders as $folder) { | |
| 77 | - wpo_delete_files($wpo_folder . $folder); | |
| 78 | - } | |
| 79 | - $files = @scandir($wpo_folder); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warning if it arises due to race condition | |
| 80 | - if (false === $files) return; | |
| 81 | - if (2 === count($files)) { | |
| 82 | - @rmdir($wpo_folder); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress error due to file permission issues | |
| 83 | - } | |
| 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; | |
| 84 | 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'); | |
| 85 | 217 | } |
| 86 | 218 | } |
| 87 | 219 | |
| 88 | 220 | endif; |