uninstall.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Fired when the plugin is uninstalled.
  4. *
  5. * @package QDR_Temporary_Shared_Storage
  6. * @since 1.0.0
  7. */
  8. // If uninstall not called from WordPress, then exit.
  9. if (!defined('WP_UNINSTALL_PLUGIN')) {
  10. exit;
  11. }
  12. // Define constants if not already defined
  13. if (!defined('QDR_TSS_PLUGIN_TABLE')) {
  14. define('QDR_TSS_PLUGIN_TABLE', 'qdr_temporary_shared_storage');
  15. }
  16. if (!defined('QDR_TSS_UPLOADS_DIR')) {
  17. $uploads_dir = wp_upload_dir();
  18. define('QDR_TSS_UPLOADS_DIR', $uploads_dir['basedir'] . '/qdr-shared-storage/');
  19. }
  20. // Delete plugin settings
  21. delete_option('qdr_tss_settings');
  22. delete_option('qdr_tss_version');
  23. // Drop custom database table
  24. global $wpdb;
  25. $table_name = $wpdb->prefix . QDR_TSS_PLUGIN_TABLE;
  26. $wpdb->query("DROP TABLE IF EXISTS $table_name");
  27. // Delete all files in upload directory
  28. function qdr_tss_delete_directory($dir) {
  29. if (!file_exists($dir)) {
  30. return true;
  31. }
  32. if (!is_dir($dir)) {
  33. return unlink($dir);
  34. }
  35. foreach (scandir($dir) as $item) {
  36. if ($item == '.' || $item == '..') {
  37. continue;
  38. }
  39. if (!qdr_tss_delete_directory($dir . DIRECTORY_SEPARATOR . $item)) {
  40. return false;
  41. }
  42. }
  43. return rmdir($dir);
  44. }
  45. // Delete upload directory
  46. qdr_tss_delete_directory(QDR_TSS_UPLOADS_DIR);
  47. // Delete download page
  48. $download_page = get_page_by_path('shared-file-download');
  49. if ($download_page) {
  50. wp_delete_post($download_page->ID, true);
  51. }
  52. // Clear scheduled tasks
  53. $timestamp = wp_next_scheduled('qdr_tss_purge_expired_files');
  54. if ($timestamp) {
  55. wp_unschedule_event($timestamp, 'qdr_tss_purge_expired_files');
  56. }
  57. // Clear any transients
  58. $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_qdr_tss_%'");
  59. $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_qdr_tss_%'");