| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Activity; |
| 6 |
use FluentSupport\App\Models\Attachment; |
| 7 |
use FluentSupport\App\Models\Meta; |
| 8 |
use FluentSupport\App\Services\Helper; |
| 9 |
use FluentSupport\App\Services\Includes\FileSystem; |
| 10 |
|
| 11 |
class CleanupHandler |
| 12 |
{ |
| 13 |
public function initHourlyTasks() |
| 14 |
{ |
| 15 |
$this->cleanLiveActivities(); |
| 16 |
} |
| 17 |
|
| 18 |
public function initDailyTasks() |
| 19 |
{ |
| 20 |
$this->cleanActivityLogs(); |
| 21 |
} |
| 22 |
|
| 23 |
protected function cleanLiveActivities() |
| 24 |
{ |
| 25 |
// Delete All Live Activity older than 24 hours |
| 26 |
$oldDateTime = date('Y-m-d H:i:s', strtotime(current_time('mysql')) - 86400); |
| 27 |
|
| 28 |
Meta::where('key', '_live_activity') |
| 29 |
->where('object_type', 'ticket_meta') |
| 30 |
->where('updated_at', '<', $oldDateTime) |
| 31 |
->delete(); |
| 32 |
} |
| 33 |
|
| 34 |
protected function cleanActivityLogs() |
| 35 |
{ |
| 36 |
$settings = Helper::getOption('_activity_settings', []); |
| 37 |
|
| 38 |
if (!$settings && empty($settings['delete_days'])) { |
| 39 |
$settings['delete_days'] = 14; |
| 40 |
} |
| 41 |
|
| 42 |
$oldDateTime = date('Y-m-d H:i:s', strtotime(current_time('mysql')) - ($settings['delete_days'] * 86400)); |
| 43 |
|
| 44 |
Activity::where('created_at', '<', $oldDateTime)->delete(); |
| 45 |
} |
| 46 |
|
| 47 |
public function deleteTicketAttachments($ticket) |
| 48 |
{ |
| 49 |
$uploadDir = wp_upload_dir(); |
| 50 |
$dir = $uploadDir['basedir'] . FLUENT_SUPPORT_UPLOAD_DIR; |
| 51 |
|
| 52 |
$attachments = Attachment::where('ticket_id', $ticket->id)->get(); |
| 53 |
|
| 54 |
if (!$attachments->isEmpty()) { |
| 55 |
$ticketDir = $dir . '/ticket_' . $ticket->id; |
| 56 |
if (is_dir($ticketDir)) { |
| 57 |
$this->deleteDir($ticketDir); |
| 58 |
} |
| 59 |
|
| 60 |
foreach ($attachments as $attachment) { |
| 61 |
if ($attachment->driver != 'local') { |
| 62 |
continue; |
| 63 |
} |
| 64 |
if (file_exists($attachment->file_path)) { |
| 65 |
@unlink($attachment->file_path); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
Attachment::where('ticket_id', $ticket->id)->delete(); |
| 70 |
} |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
private function deleteDir($dir) |
| 75 |
{ |
| 76 |
if (!class_exists('\WP_Filesystem_Direct')) { |
| 77 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
| 78 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'); |
| 79 |
} |
| 80 |
$fileSystemDirect = new \WP_Filesystem_Direct(false); |
| 81 |
$fileSystemDirect->rmdir($dir, true); |
| 82 |
} |
| 83 |
} |
| 84 |
|