PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.5
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.5
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Hooks / Handlers / CleanupHandler.php

CleanupHandler.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.5, at app/Hooks/Handlers/CleanupHandler.php

175 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Hooks\Handlers;
4
5 use FluentSupport\App\Models\Activity;
6 use FluentSupport\App\Models\AIActivityLogs;
7 use FluentSupport\App\Models\Attachment;
8 use FluentSupport\App\Models\Meta;
9 use FluentSupport\App\Services\EmailNotification\Settings;
10 use FluentSupport\App\Services\Helper;
11 use FluentSupport\App\Services\Includes\FileSystem;
12 use FluentSupport\App\Services\Integrations\Maintenance;
13
14 class CleanupHandler
15 {
16 public function initHourlyTasks()
17 {
18 $this->cleanLiveActivities();
19 $this->maybeDeleteOldTempFiles();
20 }
21
22 public function initDailyTasks()
23 {
24 $this->cleanActivityLogs();
25
26 $this->cleanAIActivityLogs();
27 }
28
29 protected function cleanLiveActivities()
30 {
31 // Delete All Live Activity older than 24 hours
32 $oldDateTime = gmdate('Y-m-d H:i:s', current_time('timestamp') - 86400);
33
34 Meta::where('key', '_live_activity')
35 ->where('object_type', 'ticket_meta')
36 ->where('updated_at', '<', $oldDateTime)
37 ->delete();
38 }
39
40 protected function cleanActivityLogs()
41 {
42 $settings = Helper::getOption('_activity_settings', []);
43
44 if (!$settings && empty($settings['delete_days'])) {
45 $settings['delete_days'] = 14;
46 }
47
48 $oldDateTime = gmdate('Y-m-d H:i:s', current_time('timestamp') - ($settings['delete_days'] * 86400));
49
50 Activity::where('created_at', '<', $oldDateTime)->delete();
51 }
52
53 protected function cleanAIActivityLogs()
54 {
55 if ((!defined('FLUENT_SUPPORT_PRO_DIR_FILE') || !Helper::openAIIntegrationStatus())
56 && !Helper::fluentBotIntegrationStatus()) {
57 return;
58 }
59
60 $settings = Helper::getOption('_ai_activity_settings', []);
61
62 $defaultDays = 14;
63
64 if (!empty($settings['delete_days'])) {
65 $defaultDays = (int)$settings['delete_days'];
66 }
67
68 $oldDateTime = gmdate('Y-m-d H:i:s', current_time('timestamp') - ($defaultDays * 86400));
69
70 AIActivityLogs::where('created_at', '<', $oldDateTime)->delete();
71 }
72
73 public function maybeDeleteAttachmentsOnClose($ticket)
74 {
75 $settings = (new Settings())->globalBusinessSettings();
76 if ($settings['del_files_on_close'] == 'yes') {
77 $this->deleteTicketAttachments($ticket);
78 }
79 }
80
81 public function deleteTicketAttachments($ticket)
82 {
83 $uploadDir = wp_upload_dir();
84 $dir = $uploadDir['basedir'] . FLUENT_SUPPORT_UPLOAD_DIR;
85
86 $attachments = Attachment::where('ticket_id', $ticket->id)->get();
87
88 if (!$attachments->isEmpty()) {
89 $ticketDir = $dir . '/ticket_' . $ticket->id;
90 if (is_dir($ticketDir)) {
91 $this->deleteDir($ticketDir);
92 }
93
94 foreach ($attachments as $attachment) {
95 if ($attachment->driver != 'local') {
96 do_action('fluent_support/delete_remote_attachment_' . $attachment->driver, $attachment, $ticket->id);
97 } else if (file_exists($attachment->file_path)) {
98 wp_delete_file($attachment->file_path);
99 }
100 }
101
102 Attachment::where('ticket_id', $ticket->id)->delete();
103 }
104
105 }
106
107 private function deleteDir($dir)
108 {
109 if (!class_exists('\WP_Filesystem_Direct')) {
110 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
111 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');
112 }
113
114 $fileSystemDirect = new \WP_Filesystem_Direct(false);
115 $fileSystemDirect->rmdir($dir, true);
116 }
117
118 public function maybeMaintanceTask()
119 {
120 (new Maintenance())->maybeProcessData();
121
122 $this->maybeRemoveOldScheuledActionLogs();
123 }
124
125 private function maybeDeleteOldTempFiles()
126 {
127 $dir = FileSystem::getDir();
128
129 // loop through files in directory
130 foreach (glob($dir . '/temp_files/*') as $filename) {
131 // check if file was created before last 2 hours
132 if (time() - filectime($filename) >= 7200) { // 2 hours
133 wp_delete_file($filename); // delete file
134 }
135 }
136 }
137
138
139 function maybeRemoveOldScheuledActionLogs($group_slug = 'fluent-support', $days_old = 7)
140 {
141 global $wpdb;
142
143 // Get the timestamp for 7 days ago
144 $cutoff_date = gmdate('Y-m-d H:i:s', strtotime("-{$days_old} days"));
145
146 // Get the group ID
147 $group_id = $wpdb->get_var($wpdb->prepare(
148 "SELECT group_id FROM {$wpdb->prefix}actionscheduler_groups WHERE slug = %s",
149 $group_slug
150 ));
151
152 if (!$group_id) {
153 return false; // Group not found
154 }
155
156 // Delete old actions and their associated logs
157 $deleted = $wpdb->query($wpdb->prepare("
158 DELETE a, l
159 FROM {$wpdb->prefix}actionscheduler_actions a
160 LEFT JOIN {$wpdb->prefix}actionscheduler_logs l ON a.action_id = l.action_id
161 WHERE a.group_id = %d
162 AND a.status IN ('complete', 'failed')
163 AND a.scheduled_date_gmt < %s", $group_id, $cutoff_date));
164
165 // Clean up orphaned claims
166 $wpdb->query("
167 DELETE c
168 FROM {$wpdb->prefix}actionscheduler_claims c
169 LEFT JOIN {$wpdb->prefix}actionscheduler_actions a ON c.claim_id = a.claim_id
170 WHERE a.action_id IS NULL");
171
172 return $deleted;
173 }
174 }
175