| 1 |
<?php |
| 2 |
|
| 3 |
namespace DebugLogViewer\Admin\Controllers; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
use DebugLogViewer\Admin\Controllers\LogController; |
| 10 |
use DebugLogViewer\Admin\Controllers\LiveUpdatesController; |
| 11 |
use DebugLogViewer\Admin\Services\LogCleanupService; |
| 12 |
use DebugLogViewer\Admin\Models\LogModel; |
| 13 |
|
| 14 |
class ServiceController |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* Get all scheduled event option names matching pattern |
| 19 |
*/ |
| 20 |
private static function getScheduledEventOptions($pattern = null) |
| 21 |
{ |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
if ($pattern === null) { |
| 25 |
$pattern = strtoupper(LogController::SCHEDULE_MAIL_SEND . '_user_%'); |
| 26 |
} |
| 27 |
|
| 28 |
return $wpdb->get_results( |
| 29 |
$wpdb->prepare( |
| 30 |
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 31 |
$pattern |
| 32 |
) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
private static function cleanupScheduledEvents() |
| 37 |
{ |
| 38 |
$options = self::getScheduledEventOptions(); |
| 39 |
|
| 40 |
foreach ($options as $option) { |
| 41 |
(new LogController())->deleteWpScheduleEvent($option->option_name); |
| 42 |
} |
| 43 |
|
| 44 |
wp_clear_scheduled_hook(LogController::SCHEDULE_MAIL_SEND); |
| 45 |
delete_option(LiveUpdatesController::DEBUG_LOG_LAST_FILESIZE); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Delete multiple options from database |
| 50 |
*/ |
| 51 |
private static function deleteOptions(array $options) |
| 52 |
{ |
| 53 |
foreach ($options as $option) { |
| 54 |
delete_option($option); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public static function deactivationHandler() |
| 59 |
{ |
| 60 |
self::cleanupScheduledEvents(); |
| 61 |
CleanupController::deactivate(); |
| 62 |
} |
| 63 |
|
| 64 |
public static function uninstallHandler() |
| 65 |
{ |
| 66 |
self::cleanupScheduledEvents(); |
| 67 |
CleanupController::deactivate(); |
| 68 |
|
| 69 |
// Delete all plugin options using constants |
| 70 |
$cleanup_options = [ |
| 71 |
LogCleanupService::OPTION_MAX_LOG_SIZE, |
| 72 |
LogCleanupService::OPTION_CLEANUP_ENABLED, |
| 73 |
LogCleanupService::OPTION_CLEANUP_METHOD, |
| 74 |
LogCleanupService::OPTION_LAST_CLEANUP_TIME, |
| 75 |
LogCleanupService::OPTION_LAST_CLEANUP_SIZE, |
| 76 |
LogCleanupService::OPTION_RETENTION_PERCENT, |
| 77 |
LogCleanupService::OPTION_CLEANUP_SCHEDULE, |
| 78 |
LogCleanupService::OPTION_CLEANUP_NOTIFICATIONS, |
| 79 |
LogCleanupService::OPTION_CLEANUP_HISTORY, |
| 80 |
LogCleanupService::OPTION_CLEANUP_NEXT_RUN, |
| 81 |
]; |
| 82 |
|
| 83 |
$log_viewer_options = [ |
| 84 |
LogModel::LAST_POSITION_OPTION_NAME, |
| 85 |
LogModel::LOG_UPDATES_MODE_OPTION_NAME, |
| 86 |
LogModel::DATETIME_FORMAT_OPTION_NAME, |
| 87 |
LiveUpdatesController::DEBUG_LOG_LAST_FILESIZE, |
| 88 |
]; |
| 89 |
|
| 90 |
self::deleteOptions(array_merge($cleanup_options, $log_viewer_options)); |
| 91 |
|
| 92 |
// Delete user-specific alert notification options (pattern: SCHEDULE_MAIL_SEND_user_*) |
| 93 |
$options = self::getScheduledEventOptions(); |
| 94 |
foreach ($options as $option) { |
| 95 |
delete_option($option->option_name); |
| 96 |
} |
| 97 |
|
| 98 |
// Delete user meta (timezone stored per-user) |
| 99 |
delete_metadata('user', 0, 'dbg_lv_timezone', '', true); |
| 100 |
} |
| 101 |
} |
| 102 |
|