| @@ -6,18 +6,16 @@ | ||
| 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() { | |
| 12 | - $is_premium_active = WP_Optimize()->is_active('premium'); | |
| 13 | - $is_free_active = WP_Optimize()->is_active(); | |
| 13 | + public static function actions(): void { | |
| 14 | + self::require_dependency(); | |
| 14 | 15 | |
| 15 | - if (!$is_premium_active) { | |
| 16 | - self::delete_premium_cache_data(); | |
| 17 | - } | |
| 18 | - | |
| 19 | - if ($is_premium_active || $is_free_active) { | |
| 16 | + self::maybe_delete_premium_cache_data(); | |
| 17 | + if (self::is_plugin_active()) { | |
| 20 | 18 | return; |
| 21 | 19 | } |
| 22 | 20 | |
| 23 | 21 | WP_Optimize()->get_gzip_compression()->disable(); |
| @@ -28,16 +26,19 @@ | ||
| 28 | 26 | WP_Optimize()->delete_transients_and_semaphores(); |
| 29 | 27 | WP_Optimize()->get_table_management()->delete_plugin_tables(); |
| 30 | 28 | Updraft_Tasks_Activation::uninstall(WPO_PLUGIN_SLUG); |
| 31 | 29 | self::delete_wpo_folder(); |
| 30 | + self::delete_htaccess_file_in_uploads_folder(); | |
| 31 | + self::cron_deactivate(); | |
| 32 | + } | |
| 32 | 33 | |
| 33 | - $htaccess_file = self::get_upload_basedir() . '.htaccess'; | |
| 34 | - if (is_file($htaccess_file) && 0 === filesize($htaccess_file)) { | |
| 35 | - wp_delete_file($htaccess_file); | |
| 36 | - } | |
| 37 | - | |
| 38 | - wp_clear_scheduled_hook('process_smush_tasks'); | |
| 39 | - WP_Optimize()->wpo_cron_deactivate(); | |
| 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'; | |
| 40 | 41 | } |
| 41 | 42 | |
| 42 | 43 | /** |
| 43 | 44 | * Delete cache data created by premium-only features. |
| @@ -43,10 +44,12 @@ | ||
| 43 | 44 | * Delete cache data created by premium-only features. |
| 44 | 45 | * |
| 45 | 46 | * Only runs when premium is not active, to avoid wiping premium data |
| 46 | 47 | * while the premium version is still installed. |
| 48 | + * | |
| 49 | + * @return void | |
| 47 | 50 | */ |
| 48 | - private static function delete_premium_cache_data() { | |
| 51 | + private static function delete_premium_cache_data(): void { | |
| 49 | 52 | if (class_exists('WPO_Gravatar_Data')) { |
| 50 | 53 | wpo_delete_files(WPO_Gravatar_Data::WPO_CACHE_GRAVATAR_DIR); |
| 51 | 54 | } |
| 52 | 55 | |
| @@ -63,9 +66,9 @@ | ||
| 63 | 66 | * Returns absolute path to uploads folder |
| 64 | 67 | * |
| 65 | 68 | * @return string |
| 66 | 69 | */ |
| 67 | - private static function get_upload_basedir() { | |
| 70 | + private static function get_upload_basedir(): string { | |
| 68 | 71 | $upload_dir = wp_get_upload_dir(); |
| 69 | 72 | return trailingslashit($upload_dir['basedir']); |
| 70 | 73 | } |
| 71 | 74 | |
| @@ -71,11 +74,11 @@ | ||
| 71 | 74 | |
| 72 | 75 | /** |
| 73 | 76 | * Returns an array of sub folders in `uploads/wpo` folder |
| 74 | 77 | * |
| 75 | - * @return array | |
| 78 | + * @return array<string> | |
| 76 | 79 | */ |
| 77 | - private static function get_wpo_sub_folders() { | |
| 80 | + private static function get_wpo_sub_folders(): array { | |
| 78 | 81 | $sub_folders = array( |
| 79 | 82 | 'add-type', |
| 80 | 83 | 'content-digest', |
| 81 | 84 | 'crash-tester', |
| @@ -86,8 +89,12 @@ | ||
| 86 | 89 | 'rewrite', |
| 87 | 90 | 'server-signature', |
| 88 | 91 | 'logs', |
| 89 | 92 | ); |
| 93 | + | |
| 94 | + /** | |
| 95 | + * @var array<string> $filtered_sub_folders | |
| 96 | + */ | |
| 90 | 97 | $filtered_sub_folders = apply_filters('wpo_uploads_sub_folders', $sub_folders); |
| 91 | 98 | return is_array($filtered_sub_folders) ? $filtered_sub_folders : $sub_folders; |
| 92 | 99 | } |
| 93 | 100 | |
| @@ -93,11 +100,11 @@ | ||
| 93 | 100 | |
| 94 | 101 | /** |
| 95 | 102 | * Returns an array of known files in `uploads/wpo` root folder |
| 96 | 103 | * |
| 97 | - * @return array | |
| 104 | + * @return array<string> | |
| 98 | 105 | */ |
| 99 | - private static function get_wpo_root_files() { | |
| 106 | + private static function get_wpo_root_files(): array { | |
| 100 | 107 | return array( |
| 101 | 108 | 'wpo-plugins-tables-list.json' |
| 102 | 109 | ); |
| 103 | 110 | } |
| @@ -103,36 +110,111 @@ | ||
| 103 | 110 | } |
| 104 | 111 | |
| 105 | 112 | /** |
| 106 | 113 | * Delete `uploads/wpo` folder, its known sub folders, and known files |
| 114 | + * | |
| 115 | + * @return void | |
| 107 | 116 | */ |
| 108 | - public static function delete_wpo_folder() { | |
| 109 | - $wpo_folder = self::get_upload_basedir() . trailingslashit('wpo'); | |
| 110 | - require_once WPO_PLUGIN_MAIN_PATH . 'cache/file-based-page-cache-functions.php'; | |
| 117 | + public static function delete_wpo_folder(): void { | |
| 118 | + self::require_dependency(); | |
| 119 | + $wpo_folder = self::get_wpo_folder(); | |
| 111 | 120 | |
| 112 | 121 | if (!is_dir($wpo_folder)) { |
| 113 | 122 | return; |
| 114 | 123 | } |
| 115 | 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 | |
| 116 | 127 | $wpo_sub_folders = self::get_wpo_sub_folders(); |
| 117 | 128 | foreach ($wpo_sub_folders as $folder) { |
| 118 | 129 | wpo_delete_files($wpo_folder . $folder); |
| 119 | 130 | } |
| 131 | + self::delete_wpo_root_files(); | |
| 120 | 132 | |
| 121 | - // Delete known root-level files | |
| 122 | - $wpo_root_files = self::get_wpo_root_files(); | |
| 123 | - foreach ($wpo_root_files as $file) { | |
| 124 | - wpo_delete_files($wpo_folder . $file, false); | |
| 125 | - } | |
| 133 | + self::maybe_delete_empty_folder($wpo_folder); | |
| 134 | + } | |
| 126 | 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 { | |
| 127 | 144 | // phpcs:disable |
| 128 | 145 | // Generic.PHP.NoSilencedErrors.Discouraged -- suppress warning if it arises due to race condition |
| 129 | 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 | |
| 130 | 148 | $files = @scandir($wpo_folder); |
| 131 | 149 | if (false !== $files && 2 === count($files)) { |
| 132 | 150 | @rmdir($wpo_folder); |
| 133 | 151 | } |
| 134 | 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'); | |
| 135 | 217 | } |
| 136 | 218 | } |
| 137 | 219 | |
| 138 | 220 | endif; |