advanced-database-cleaner
Last commit date
assets
7 months ago
includes
7 months ago
languages
7 months ago
LICENSE.txt
7 months ago
README.txt
7 months ago
advanced-db-cleaner.php
7 months ago
constants.php
7 months ago
index.php
7 months ago
uninstall.php
7 months ago
uninstall.php
269 lines
| 1 | <?php |
| 2 | |
| 3 | // If uninstall not called from WordPress, exit. |
| 4 | if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | class ADBC_Uninstall { |
| 9 | |
| 10 | // List of all ADBC options to delete on uninstall |
| 11 | private static $adbc_options = [ |
| 12 | 'adbc_plugin_settings' => '', |
| 13 | 'adbc_plugin_scan_info_options' => '', |
| 14 | 'adbc_plugin_scan_info_tables' => '', |
| 15 | 'adbc_plugin_scan_info_cron_jobs' => '', |
| 16 | 'adbc_plugin_scan_info_users_meta' => '', |
| 17 | 'adbc_plugin_scan_info_posts_meta' => '', |
| 18 | 'adbc_plugin_scan_info_transients' => '', |
| 19 | 'adbc_plugin_should_stop_scan_options' => '', |
| 20 | 'adbc_plugin_should_stop_scan_tables' => '', |
| 21 | 'adbc_plugin_should_stop_scan_cron_jobs' => '', |
| 22 | 'adbc_plugin_should_stop_scan_users_meta' => '', |
| 23 | 'adbc_plugin_should_stop_scan_posts_meta' => '', |
| 24 | 'adbc_plugin_should_stop_scan_transients' => '', |
| 25 | 'adbc_plugin_automation' => '', |
| 26 | 'adbc_plugin_license_key' => '', |
| 27 | 'adbc_plugin_license_key_license' => '', |
| 28 | ]; |
| 29 | |
| 30 | // List of all ADBC transients to delete on uninstall |
| 31 | private static $adbc_transients = [ |
| 32 | '_transient_adbc_plugin_tables_to_repair' => 'adbc_plugin_tables_to_repair', |
| 33 | ]; |
| 34 | |
| 35 | // List of all ADBC cron jobs to unschedule on uninstall |
| 36 | private static $adbc_cron_jobs = [ |
| 37 | 'adbc_cron_analytics', |
| 38 | 'adbc_cron_automation', |
| 39 | ]; |
| 40 | |
| 41 | /** |
| 42 | * Run the uninstall process. |
| 43 | * |
| 44 | * This method checks if the uninstall is being run from the premium or free version, |
| 45 | * and deletes plugin data accordingly to avoid data loss when switching between versions. |
| 46 | * |
| 47 | * If uninstalling the premium version and a new free version (>= 4.0.0) exists, it keeps |
| 48 | * useful data such as settings and automation tasks. Otherwise, it deletes all plugin data. |
| 49 | * |
| 50 | * If uninstalling the free version and the premium version does not exist, it deletes all plugin data. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public static function run() { |
| 55 | |
| 56 | // check if we are in the premium |
| 57 | if ( file_exists( __DIR__ . '/includes/premium' ) ) { |
| 58 | |
| 59 | // keep useful data if the the new free version is uninstalled (>= 4.0.0), otherwise delete everything |
| 60 | if ( self::is_new_free_version_exists() ) { |
| 61 | |
| 62 | // Delete all options except the adbc_plugin_settings and the automation options |
| 63 | foreach ( self::$adbc_options as $option_name => $_ ) { |
| 64 | if ( $option_name !== 'adbc_plugin_settings' && $option_name !== 'adbc_plugin_automation' ) { |
| 65 | delete_option( $option_name ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // delete the scan and the analytics folders with their contents |
| 70 | $adbc_upload_folder = self::get_adbc_upload_folder_path(); |
| 71 | $scan_folder = $adbc_upload_folder . '/scan'; |
| 72 | $analytics_folder = $adbc_upload_folder . '/analytics'; |
| 73 | $automation_folder = $adbc_upload_folder . '/automation_events'; |
| 74 | $addons_activity_file = $adbc_upload_folder . '/addons_activity.log'; |
| 75 | $addons_activity_dictionary_file = $adbc_upload_folder . '/addons_activity_dictionary.log'; |
| 76 | |
| 77 | self::delete_folder( $scan_folder ); |
| 78 | self::delete_folder( $analytics_folder ); |
| 79 | self::delete_folder( $automation_folder ); |
| 80 | |
| 81 | if ( file_exists( $addons_activity_file ) ) |
| 82 | unlink( $addons_activity_file ); |
| 83 | |
| 84 | if ( file_exists( $addons_activity_dictionary_file ) ) |
| 85 | unlink( $addons_activity_dictionary_file ); |
| 86 | |
| 87 | // Unschedule crons |
| 88 | wp_unschedule_hook( 'adbc_cron_analytics' ); |
| 89 | |
| 90 | // unschedule automation crons only if the new version is deactivated |
| 91 | if ( ! is_plugin_active( 'advanced-database-cleaner/advanced-db-cleaner.php' ) ) { |
| 92 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 93 | } |
| 94 | |
| 95 | } else { |
| 96 | |
| 97 | // delete all plugin data |
| 98 | self::delete_all_plugin_data(); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | } else { // we are in the free |
| 103 | |
| 104 | // if the premium version doesn't exist, delete everything |
| 105 | if ( ! self::is_premium_version_exists() ) { |
| 106 | self::delete_all_plugin_data(); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Recursively delete a folder and its contents. |
| 115 | * |
| 116 | * @param string $folder The folder path to delete. |
| 117 | * |
| 118 | * @return bool True on success, false on failure. |
| 119 | */ |
| 120 | private static function delete_folder( $folder ) { |
| 121 | |
| 122 | if ( ! is_dir( $folder ) ) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | $files = array_diff( scandir( $folder ), [ '.', '..' ] ); // get all files/folders |
| 127 | |
| 128 | foreach ( $files as $file ) { |
| 129 | $path = $folder . DIRECTORY_SEPARATOR . $file; |
| 130 | |
| 131 | if ( is_dir( $path ) ) { |
| 132 | self::delete_folder( $path ); // recursion for subfolders |
| 133 | } else { |
| 134 | unlink( $path ); // delete file |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return rmdir( $folder ); // delete the folder itself |
| 139 | |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the ADBC upload folder path. |
| 144 | * |
| 145 | * @return string The ADBC upload folder path. |
| 146 | */ |
| 147 | private static function get_adbc_upload_folder_path() { |
| 148 | |
| 149 | // Get upload folder security code to delete the folder |
| 150 | $settings = get_option( 'adbc_plugin_settings', [] ); |
| 151 | $security_code = isset( $settings['security_code'] ) ? $settings['security_code'] : ''; |
| 152 | $upload_folder = wp_upload_dir()['basedir'] . '/adbc_uploads_F_' . $security_code; |
| 153 | |
| 154 | return $upload_folder; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Delete the ADBC upload folder. |
| 160 | * |
| 161 | * @return void |
| 162 | */ |
| 163 | private static function delete_adbc_upload_folder() { |
| 164 | $upload_folder = self::get_adbc_upload_folder_path(); |
| 165 | self::delete_folder( $upload_folder ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Delete all ADBC options. |
| 170 | * |
| 171 | * @return void |
| 172 | */ |
| 173 | private static function delete_all_adbc_options() { |
| 174 | foreach ( self::$adbc_options as $option_name => $_ ) { |
| 175 | delete_option( $option_name ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Delete all ADBC transients. |
| 181 | * |
| 182 | * @return void |
| 183 | */ |
| 184 | private static function delete_all_adbc_transients() { |
| 185 | foreach ( self::$adbc_transients as $full_transient_name => $transient_name ) { |
| 186 | delete_transient( $transient_name ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Unschedule all ADBC cron jobs. |
| 192 | * |
| 193 | * @return void |
| 194 | */ |
| 195 | private static function unschedule_all_cron_jobs() { |
| 196 | foreach ( self::$adbc_cron_jobs as $cron_job ) { |
| 197 | wp_unschedule_hook( $cron_job ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Delete all plugin data: options, transients, upload folder. |
| 203 | * |
| 204 | * @return void |
| 205 | */ |
| 206 | private static function delete_all_plugin_data() { |
| 207 | self::delete_adbc_upload_folder(); |
| 208 | self::delete_all_adbc_transients(); |
| 209 | self::delete_all_adbc_options(); |
| 210 | self::unschedule_all_cron_jobs(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Check if a new free version (>= 4.0.0) exists. |
| 215 | * |
| 216 | * @return bool True if a new free version exists, false otherwise. |
| 217 | */ |
| 218 | private static function is_new_free_version_exists() { |
| 219 | |
| 220 | // Ensure plugin functions are loaded |
| 221 | if ( ! function_exists( 'get_plugins' ) ) { |
| 222 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 223 | } |
| 224 | |
| 225 | $plugins = get_plugins(); |
| 226 | |
| 227 | $free_slug = 'advanced-database-cleaner/advanced-db-cleaner.php'; |
| 228 | |
| 229 | if ( isset( $plugins[ $free_slug ] ) ) { |
| 230 | $free_version = $plugins[ $free_slug ]['Version']; |
| 231 | |
| 232 | // Compare version |
| 233 | if ( version_compare( $free_version, '4.0.0', '>=' ) ) { |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return false; |
| 239 | |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Check if the premium version exists. |
| 244 | * |
| 245 | * @return bool True if the premium version exists, false otherwise. |
| 246 | */ |
| 247 | private static function is_premium_version_exists() { |
| 248 | |
| 249 | // Ensure plugin functions are loaded |
| 250 | if ( ! function_exists( 'get_plugins' ) ) { |
| 251 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 252 | } |
| 253 | |
| 254 | $plugins = get_plugins(); |
| 255 | |
| 256 | $premium_slug = 'advanced-database-cleaner-premium/advanced-db-cleaner.php'; |
| 257 | |
| 258 | if ( isset( $plugins[ $premium_slug ] ) ) { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | return false; |
| 263 | |
| 264 | } |
| 265 | |
| 266 | } |
| 267 | |
| 268 | // Run the uninstall process |
| 269 | ADBC_Uninstall::run(); |