advanced-database-cleaner
Last commit date
assets
1 day ago
includes
1 day ago
languages
1 day ago
LICENSE.txt
7 months ago
README.txt
1 day ago
advanced-db-cleaner.php
1 day ago
constants.php
4 months ago
index.php
1 day ago
uninstall.php
2 months ago
uninstall.php
488 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' => 'all', |
| 13 | 'adbc_plugin_automation' => 'all', |
| 14 | 'adbc_plugin_conflict_notice' => 'all', |
| 15 | 'adbc_plugin_scan_info_options' => 'premium_pro', |
| 16 | 'adbc_plugin_scan_info_tables' => 'premium_pro', |
| 17 | 'adbc_plugin_scan_info_cron_jobs' => 'premium_pro', |
| 18 | 'adbc_plugin_scan_info_users_meta' => 'premium_pro', |
| 19 | 'adbc_plugin_scan_info_posts_meta' => 'premium_pro', |
| 20 | 'adbc_plugin_scan_info_transients' => 'premium_pro', |
| 21 | 'adbc_plugin_should_stop_scan_options' => 'premium_pro', |
| 22 | 'adbc_plugin_should_stop_scan_tables' => 'premium_pro', |
| 23 | 'adbc_plugin_should_stop_scan_cron_jobs' => 'premium_pro', |
| 24 | 'adbc_plugin_should_stop_scan_users_meta' => 'premium_pro', |
| 25 | 'adbc_plugin_should_stop_scan_posts_meta' => 'premium_pro', |
| 26 | 'adbc_plugin_should_stop_scan_transients' => 'premium_pro', |
| 27 | 'adbc_plugin_license_key' => 'premium', |
| 28 | 'adbc_plugin_license_key_license' => 'premium', |
| 29 | 'adbc_plugin_license_key_pro' => 'pro', |
| 30 | 'adbc_plugin_license_key_pro_license' => 'pro', |
| 31 | 'adbc_plugin_pro_api_scan_balance' => 'pro', |
| 32 | ]; |
| 33 | |
| 34 | // List of all ADBC transients to delete on uninstall |
| 35 | private static $adbc_transients = [ |
| 36 | 'adbc_plugin_tables_to_repair' => 'all', |
| 37 | 'adbc_plugin_innodb_conversion_lock' => 'all', |
| 38 | 'adbc_plugin_post_types_dict_updated' => 'premium_pro', |
| 39 | ]; |
| 40 | |
| 41 | // List of all ADBC cron jobs to unschedule on uninstall |
| 42 | private static $adbc_cron_jobs = [ |
| 43 | 'adbc_cron_analytics' => 'premium_pro', |
| 44 | 'adbc_cron_automation' => 'all', |
| 45 | 'edd_sl_sdk_weekly_license_check_advanced-database-cleaner-premium' => 'premium', |
| 46 | 'edd_sl_sdk_weekly_license_check_advanced-database-cleaner-pro' => 'pro', |
| 47 | ]; |
| 48 | |
| 49 | /** |
| 50 | * Run the uninstall process. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public static function run() { |
| 55 | |
| 56 | $is_premium_version_exists = self::is_premium_version_exists(); |
| 57 | $is_new_free_version_exists = self::is_new_free_version_exists(); |
| 58 | $is_new_pro_version_exists = self::is_new_pro_version_exists(); |
| 59 | |
| 60 | $is_premium_version_active = is_plugin_active( 'advanced-database-cleaner-premium/advanced-db-cleaner.php' ); |
| 61 | $is_free_version_active = is_plugin_active( 'advanced-database-cleaner/advanced-db-cleaner.php' ); |
| 62 | $is_pro_version_active = is_plugin_active( 'advanced-database-cleaner-pro/advanced-db-cleaner.php' ); |
| 63 | |
| 64 | $current_plugin_slug = basename( __DIR__ ); |
| 65 | |
| 66 | self::add_edd_cache_option_to_options_list( $current_plugin_slug ); |
| 67 | |
| 68 | if ( $current_plugin_slug === 'advanced-database-cleaner-premium' ) { // we are in the premium version |
| 69 | |
| 70 | if ( $is_new_free_version_exists && $is_new_pro_version_exists ) { // free exists + pro exist |
| 71 | |
| 72 | self::clean_premium_data_only(); |
| 73 | |
| 74 | // unschedule automation crons if free is deactivated and pro is deactivated |
| 75 | if ( ! $is_free_version_active && ! $is_pro_version_active ) { |
| 76 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 77 | } |
| 78 | |
| 79 | } elseif ( $is_new_free_version_exists && ! $is_new_pro_version_exists ) { // free exists + pro doesn't exists |
| 80 | |
| 81 | self::clean_premium_and_pro_data_only(); |
| 82 | |
| 83 | // unschedule automation crons if free is deactivated |
| 84 | if ( ! $is_free_version_active ) { |
| 85 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 86 | } |
| 87 | |
| 88 | } elseif ( ! $is_new_free_version_exists && $is_new_pro_version_exists ) { // free doesn't exist + pro exists |
| 89 | |
| 90 | self::clean_premium_data_only(); |
| 91 | |
| 92 | // unschedule automation crons if pro is deactivated |
| 93 | if ( ! $is_pro_version_active ) { |
| 94 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 95 | } |
| 96 | |
| 97 | } else { // free doesn't exist + pro doesn't exist |
| 98 | |
| 99 | self::clean_all_data(); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | } elseif ( $current_plugin_slug === 'advanced-database-cleaner-pro' ) { // we are in the pro version |
| 104 | |
| 105 | if ( $is_new_free_version_exists && $is_premium_version_exists ) { // free exists + premium exists |
| 106 | |
| 107 | self::clean_pro_data_only(); |
| 108 | |
| 109 | // unschedule automation crons if free is deactivated and premium is deactivated |
| 110 | if ( ! $is_free_version_active && ! $is_premium_version_active ) { |
| 111 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 112 | } |
| 113 | |
| 114 | } elseif ( $is_new_free_version_exists && ! $is_premium_version_exists ) { // free exists + premium doesn't exist |
| 115 | |
| 116 | self::clean_premium_and_pro_data_only(); |
| 117 | |
| 118 | // unschedule automation crons if free is deactivated |
| 119 | if ( ! $is_free_version_active ) { |
| 120 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 121 | } |
| 122 | |
| 123 | } elseif ( ! $is_new_free_version_exists && $is_premium_version_exists ) { // free doesn't exist + premium exists |
| 124 | |
| 125 | self::clean_pro_data_only(); |
| 126 | |
| 127 | // unschedule automation crons if pro is deactivated |
| 128 | if ( ! $is_pro_version_active ) { |
| 129 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 130 | } |
| 131 | |
| 132 | } else { // free doesn't exist + premium doesn't exist |
| 133 | |
| 134 | self::clean_all_data(); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | } else { // we are in the free version |
| 139 | |
| 140 | if ( $is_premium_version_exists && $is_new_pro_version_exists ) { // premium exists + pro exists |
| 141 | |
| 142 | // unschedule automation crons if premium is deactivated and pro is deactivated |
| 143 | if ( ! $is_premium_version_active && ! $is_pro_version_active ) { |
| 144 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 145 | } |
| 146 | |
| 147 | } elseif ( $is_premium_version_exists && ! $is_new_pro_version_exists ) { // premium exists + pro doesn't exist |
| 148 | |
| 149 | self::clean_pro_data_only(); |
| 150 | |
| 151 | // unschedule automation crons if premium is deactivated |
| 152 | if ( ! $is_premium_version_active ) { |
| 153 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 154 | } |
| 155 | |
| 156 | } elseif ( ! $is_premium_version_exists && $is_new_pro_version_exists ) { // premium doesn't exist + pro exists |
| 157 | |
| 158 | self::clean_premium_data_only(); |
| 159 | |
| 160 | // unschedule automation crons if pro is deactivated |
| 161 | if ( ! $is_pro_version_active ) { |
| 162 | wp_unschedule_hook( 'adbc_cron_automation' ); |
| 163 | } |
| 164 | |
| 165 | } else { // premium doesn't exist + pro doesn't exist |
| 166 | |
| 167 | self::clean_all_data(); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | private static function clean_all_data() { |
| 176 | self::delete_all_plugin_data(); |
| 177 | } |
| 178 | |
| 179 | private static function clean_pro_data_only() { |
| 180 | |
| 181 | // delete pro only options |
| 182 | foreach ( self::$adbc_options as $option_name => $plugin_type ) { |
| 183 | if ( $plugin_type === 'pro' ) |
| 184 | delete_option( $option_name ); |
| 185 | } |
| 186 | |
| 187 | // delete pro only transients |
| 188 | foreach ( self::$adbc_transients as $transient_name => $plugin_type ) { |
| 189 | if ( $plugin_type === 'pro' ) |
| 190 | delete_transient( $transient_name ); |
| 191 | } |
| 192 | |
| 193 | // Unschedule pro only crons |
| 194 | foreach ( self::$adbc_cron_jobs as $cron_job => $plugin_type ) { |
| 195 | if ( $plugin_type === 'pro' ) |
| 196 | wp_unschedule_hook( $cron_job ); |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
| 201 | private static function clean_premium_data_only() { |
| 202 | |
| 203 | // delete premium only options |
| 204 | foreach ( self::$adbc_options as $option_name => $plugin_type ) { |
| 205 | if ( $plugin_type === 'premium' ) |
| 206 | delete_option( $option_name ); |
| 207 | } |
| 208 | |
| 209 | // delete premium only transients |
| 210 | foreach ( self::$adbc_transients as $transient_name => $plugin_type ) { |
| 211 | if ( $plugin_type === 'premium' ) |
| 212 | delete_transient( $transient_name ); |
| 213 | } |
| 214 | |
| 215 | // Unschedule premium only crons |
| 216 | foreach ( self::$adbc_cron_jobs as $cron_job => $plugin_type ) { |
| 217 | if ( $plugin_type === 'premium' ) |
| 218 | wp_unschedule_hook( $cron_job ); |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | private static function clean_premium_and_pro_data_only() { |
| 224 | |
| 225 | $data_types_to_delete = [ 'premium', 'pro', 'premium_pro' ]; |
| 226 | |
| 227 | // delete premium and pro only options |
| 228 | foreach ( self::$adbc_options as $option_name => $plugin_type ) { |
| 229 | if ( in_array( $plugin_type, $data_types_to_delete ) ) |
| 230 | delete_option( $option_name ); |
| 231 | } |
| 232 | |
| 233 | // delete premium and pro only transients |
| 234 | foreach ( self::$adbc_transients as $transient_name => $plugin_type ) { |
| 235 | if ( in_array( $plugin_type, $data_types_to_delete ) ) |
| 236 | delete_transient( $transient_name ); |
| 237 | } |
| 238 | |
| 239 | // Unschedule premium and pro only crons |
| 240 | foreach ( self::$adbc_cron_jobs as $cron_job => $plugin_type ) { |
| 241 | if ( in_array( $plugin_type, $data_types_to_delete ) ) |
| 242 | wp_unschedule_hook( $cron_job ); |
| 243 | } |
| 244 | |
| 245 | // delete the scan and the analytics folders with their contents |
| 246 | $adbc_upload_folder = self::get_adbc_upload_folder_path(); |
| 247 | $scan_folder = $adbc_upload_folder . '/scan'; |
| 248 | $analytics_folder = $adbc_upload_folder . '/analytics'; |
| 249 | $automation_folder = $adbc_upload_folder . '/automation_events'; |
| 250 | $addons_activity_file = $adbc_upload_folder . '/addons_activity.log'; |
| 251 | $addons_activity_dictionary_file = $adbc_upload_folder . '/addons_activity_dictionary.log'; |
| 252 | $registered_post_types_dictionary_file = $adbc_upload_folder . '/registered_post_types_dictionary.txt'; |
| 253 | |
| 254 | self::delete_folder( $scan_folder ); |
| 255 | self::delete_folder( $analytics_folder ); |
| 256 | self::delete_folder( $automation_folder ); |
| 257 | |
| 258 | if ( file_exists( $addons_activity_file ) ) |
| 259 | wp_delete_file( $addons_activity_file ); |
| 260 | |
| 261 | if ( file_exists( $addons_activity_dictionary_file ) ) |
| 262 | wp_delete_file( $addons_activity_dictionary_file ); |
| 263 | |
| 264 | if ( file_exists( $registered_post_types_dictionary_file ) ) |
| 265 | wp_delete_file( $registered_post_types_dictionary_file ); |
| 266 | |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Recursively delete a folder and its contents. |
| 271 | * |
| 272 | * @param string $folder The folder path to delete. |
| 273 | * |
| 274 | * @return bool True on success, false on failure. |
| 275 | */ |
| 276 | private static function delete_folder( $folder ) { |
| 277 | |
| 278 | if ( ! is_dir( $folder ) ) { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | $files = array_diff( scandir( $folder ), [ '.', '..' ] ); // get all files/folders |
| 283 | |
| 284 | foreach ( $files as $file ) { |
| 285 | $path = $folder . DIRECTORY_SEPARATOR . $file; |
| 286 | |
| 287 | if ( is_dir( $path ) ) { |
| 288 | self::delete_folder( $path ); // recursion for subfolders |
| 289 | } else { |
| 290 | wp_delete_file( $path ); // delete file |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return rmdir( $folder ); // delete the folder itself |
| 295 | |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Get the ADBC upload folder path. |
| 300 | * |
| 301 | * @return string The ADBC upload folder path. |
| 302 | */ |
| 303 | private static function get_adbc_upload_folder_path() { |
| 304 | |
| 305 | // Get upload folder security code to delete the folder |
| 306 | $settings = get_option( 'adbc_plugin_settings', [] ); |
| 307 | $security_code = isset( $settings['security_code'] ) ? $settings['security_code'] : ''; |
| 308 | $upload_folder = wp_upload_dir()['basedir'] . '/adbc_uploads_F_' . $security_code; |
| 309 | |
| 310 | return $upload_folder; |
| 311 | |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Delete the ADBC upload folder. |
| 316 | * |
| 317 | * @return void |
| 318 | */ |
| 319 | private static function delete_adbc_upload_folder() { |
| 320 | $upload_folder = self::get_adbc_upload_folder_path(); |
| 321 | self::delete_folder( $upload_folder ); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Delete all ADBC options. |
| 326 | * |
| 327 | * @return void |
| 328 | */ |
| 329 | private static function delete_all_adbc_options() { |
| 330 | foreach ( self::$adbc_options as $option_name => $_ ) { |
| 331 | delete_option( $option_name ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Delete all ADBC transients. |
| 337 | * |
| 338 | * @return void |
| 339 | */ |
| 340 | private static function delete_all_adbc_transients() { |
| 341 | foreach ( self::$adbc_transients as $transient_name => $_ ) { |
| 342 | delete_transient( $transient_name ); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Unschedule all ADBC cron jobs. |
| 348 | * |
| 349 | * @return void |
| 350 | */ |
| 351 | private static function unschedule_all_cron_jobs() { |
| 352 | foreach ( self::$adbc_cron_jobs as $cron_job => $_ ) { |
| 353 | wp_unschedule_hook( $cron_job ); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Delete all plugin data: options, transients, upload folder. |
| 359 | * |
| 360 | * @return void |
| 361 | */ |
| 362 | private static function delete_all_plugin_data() { |
| 363 | self::delete_adbc_upload_folder(); |
| 364 | self::delete_all_adbc_transients(); |
| 365 | self::delete_all_adbc_options(); |
| 366 | self::unschedule_all_cron_jobs(); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Check if a new free version (>= 4.0.0) exists. |
| 371 | * |
| 372 | * @return bool True if a new free version exists, false otherwise. |
| 373 | */ |
| 374 | private static function is_new_free_version_exists() { |
| 375 | |
| 376 | // Ensure plugin functions are loaded |
| 377 | if ( ! function_exists( 'get_plugins' ) ) { |
| 378 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 379 | } |
| 380 | |
| 381 | $plugins = get_plugins(); |
| 382 | |
| 383 | $free_slug = 'advanced-database-cleaner/advanced-db-cleaner.php'; |
| 384 | |
| 385 | if ( isset( $plugins[ $free_slug ] ) ) { |
| 386 | $free_version = $plugins[ $free_slug ]['Version']; |
| 387 | |
| 388 | // Compare version |
| 389 | if ( version_compare( $free_version, '4.0.0', '>=' ) ) { |
| 390 | return true; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return false; |
| 395 | |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Check if the premium version exists. |
| 400 | * |
| 401 | * @return bool True if the premium version exists, false otherwise. |
| 402 | */ |
| 403 | private static function is_premium_version_exists() { |
| 404 | |
| 405 | // Ensure plugin functions are loaded |
| 406 | if ( ! function_exists( 'get_plugins' ) ) { |
| 407 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 408 | } |
| 409 | |
| 410 | $plugins = get_plugins(); |
| 411 | |
| 412 | $premium_slug = 'advanced-database-cleaner-premium/advanced-db-cleaner.php'; |
| 413 | |
| 414 | if ( isset( $plugins[ $premium_slug ] ) ) { |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | return false; |
| 419 | |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Check if the pro version exists. |
| 424 | * |
| 425 | * @return bool True if the pro version exists, false otherwise. |
| 426 | */ |
| 427 | private static function is_new_pro_version_exists() { |
| 428 | |
| 429 | // Ensure plugin functions are loaded |
| 430 | if ( ! function_exists( 'get_plugins' ) ) { |
| 431 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 432 | } |
| 433 | |
| 434 | $plugins = get_plugins(); |
| 435 | |
| 436 | $pro_slug = 'advanced-database-cleaner-pro/advanced-db-cleaner.php'; |
| 437 | |
| 438 | if ( isset( $plugins[ $pro_slug ] ) ) { |
| 439 | $pro_version = $plugins[ $pro_slug ]['Version']; |
| 440 | |
| 441 | // Compare version |
| 442 | if ( version_compare( $pro_version, '4.0.0', '>=' ) ) { |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | } |
| 447 | |
| 448 | return false; |
| 449 | |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Add the EDD updater cache option to the options list. |
| 454 | * |
| 455 | * Added for ADBC integration so we can compute the cache key |
| 456 | * without instantiating the updater and adding listeners. |
| 457 | * |
| 458 | * @param string $slug Plugin slug. |
| 459 | * |
| 460 | * @return void |
| 461 | */ |
| 462 | private static function add_edd_cache_option_to_options_list( $slug ) { |
| 463 | |
| 464 | $plugin_version_type = $slug === 'advanced-database-cleaner-pro' ? 'pro' : 'premium'; |
| 465 | $license_option_name = $plugin_version_type === 'pro' ? 'adbc_plugin_license_key_pro' : 'adbc_plugin_license_key'; |
| 466 | $license = get_option( $license_option_name, '' ); |
| 467 | |
| 468 | if ( empty( $license ) ) |
| 469 | return; |
| 470 | |
| 471 | $key = md5( |
| 472 | wp_json_encode( |
| 473 | array( |
| 474 | (string) $slug, |
| 475 | (string) $license, |
| 476 | 0, |
| 477 | ) |
| 478 | ) |
| 479 | ); |
| 480 | |
| 481 | self::$adbc_options[ "edd_sl_{$key}" ] = $plugin_version_type; |
| 482 | |
| 483 | } |
| 484 | |
| 485 | } |
| 486 | |
| 487 | // Run the uninstall process |
| 488 | ADBC_Uninstall::run(); |