PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.2.0
Fluent Support – Helpdesk & Customer Support Ticket System v2.2.0
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 2.2.0, at app/Hooks/Handlers/CleanupHandler.php

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