PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.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 All 69 releases
fluent-support / app / Services / Tickets / Importer / AwesomeSupportTickets.php

AwesomeSupportTickets.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Services/Tickets/Importer/AwesomeSupportTickets.php

256 lines 7.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\Services\Tickets\Importer;
4
5 use FluentSupport\App\Models\Ticket;
6 use FluentSupport\App\Services\Helper;
7 use FluentSupport\App\Models\Attachment;
8 use FluentSupport\App\Models\Conversation;
9
10 class AwesomeSupportTickets extends BaseImporter
11 {
12 protected $handler = 'awesome-support';
13
14 public function stats()
15 {
16 $ticketsCount = $this->getCount();
17 $replyCount = $this->db->table('posts')
18 ->where('post_type', 'ticket_reply')
19 ->count();
20
21 return [
22 'name' => esc_html('Awesome Support'),
23 'tickets' => $ticketsCount,
24 'replies' => $replyCount,
25 'customers' => count( get_users( array( 'role' => 'wpas_user' ) ) ),
26 'handler' => $this->handler,
27 'last_migrated' => get_option('_fs_migrate_awesome_support')
28 ];
29 }
30
31 /**
32 * @param int $page
33 * @param string $handler
34 * @return array
35 */
36 public function doMigration( $page, $handler )
37 {
38 $this->handler = $handler;
39 $allCounts = $this->getCount();
40
41 if (!$allCounts) {
42 return [
43 'message' => __('Sorry, no tickets available for import.', 'fluent-support'),
44 'had_tickets' => 'no',
45 ];
46 }
47
48 $tickets = $this->getTickets($this->limit, $page);
49 $results = $this->migrateTickets($tickets);
50
51 $hasMore = $page * $this->limit <= $allCounts;
52
53 $remainingTickets = $allCounts - ($page * $this->limit);
54 $completedTickets = intval(($page / $allCounts) * 100);
55
56 $response = [
57 'handler' => $this->handler,
58 'insert_ids' => $results['inserts'],
59 'skips' => count($results['skips']),
60 'has_more' => $hasMore,
61 'completed' => $completedTickets,
62 'total_pages' => ceil($allCounts / $this->limit),
63 'imported_page' => $page,
64 'next_page' => $page + 1,
65 'total_tickets' => $allCounts,
66 'remaining' => $remainingTickets
67 ];
68
69 if (!$hasMore) {
70 $response['message'] = __('All tickets have been imported successfully', 'fluent-support');
71 update_option('_fs_migrate_awesome_support', current_time('mysql'), 'no');
72 }
73
74 return $response;
75 }
76
77 /**
78 * @param int $limit
79 * @param int $page
80 * @return array $tickets
81 */
82 public function getTickets( $limit, $page )
83 {
84 $args = [
85 'posts_per_page' => $limit,
86 'paged' => $page
87 ];
88
89 $tickets = \wpas_get_tickets('any', $args);
90 $formattedTickets = [];
91
92 if ($tickets){
93 foreach($tickets as $ticket){
94 $customerData = $this->resolvePerson($ticket->post_author);
95
96 if (!$customerData) {
97 $customerData = $this->getUnknownCustomerData($ticket->post_author);
98 }
99
100 $replies = $this->getReplies($ticket);
101
102 $ticketData = [
103 'origin_id' => intval($ticket->ID),
104 'replies' => $replies,
105 'source' => sanitize_text_field($this->handler),
106 'title' => sanitize_text_field($ticket->post_title),
107 'content' => wp_kses_post($ticket->post_content),
108 'mailbox_id' => intval($this->mailbox->id),
109 'response_count' => count($replies),
110 'status' => 'active',
111 'created_at' => $ticket->post_date,
112 'updated_at' => $ticket->post_modified,
113 'last_customer_response' => $ticket->post_modified,
114 'waiting_since' => $ticket->post_modified
115 ];
116
117 $ticketStatus = $this->getMeta('_wpas_status', $ticket->ID);
118
119 if ($ticketStatus == 'closed') {
120 $ticketData['status'] = 'closed';
121 $ticketData['resolved_at'] = $this->getMeta('_ticket_closed_on', $ticket->ID);
122 } else if (!$replies) {
123 $ticketData['status'] = 'new';
124 }
125
126 $customer = $this->getPerson($customerData, 'customer');
127
128 if(!$customer) {
129 continue;
130 }
131
132 $ticketData['customer'] = $customer;
133 $ticketData['attachments'] = $this->getAttachments($ticket->ID);
134
135 $formattedTickets[] = $ticketData;
136 }
137 }
138
139 return $formattedTickets;
140 }
141
142 /**
143 * @param string $metaKey
144 * @param int $postId
145 * @return mixed
146 */
147 public function getMeta($metaKey, $postId)
148 {
149 return get_post_meta($postId, $metaKey, true);
150 }
151
152 /**
153 * @param object $ticket
154 * @return array $formattedReplies
155 */
156 public function getReplies($ticket)
157 {
158 $replies = $this->db->table('posts')
159 ->where('post_type', 'ticket_reply')
160 ->where('post_parent', $ticket->ID)
161 ->oldest('ID')
162 ->get();
163
164 $formattedReplies = [];
165
166 foreach ($replies as $reply) {
167 $formattedReplies[] = [
168 'content' => $reply->post_content,
169 'conversation_type' => 'response',
170 'created_at' => $reply->post_date,
171 'updated_at' => $reply->post_modified,
172 'is_customer_reply' => ($ticket->post_author === $reply->post_author),
173 'user' => $this->resolvePerson($reply->post_author),
174 'attachments' => $this->getAttachments($reply->ID)
175 ];
176 }
177
178 return $formattedReplies;
179 }
180
181 protected function getAttachments($threadId)
182 {
183 $attachments = $this->db->table('posts')
184 ->where('post_type', 'attachment')
185 ->where('post_parent', $threadId)
186 ->oldest('ID')
187 ->get();
188
189 $formattedAttachments = [];
190
191 foreach ($attachments as $attachment) {
192 $formattedAttachments[] = [
193 'full_url' => sanitize_url($attachment->guid),
194 'title' => $attachment->post_title,
195 'file_path' => get_attached_file($attachment->ID),
196 'driver' => 'local',
197 'status' => 'active',
198 'file_type' => $attachment->post_mime_type
199 ];
200 }
201
202 return $formattedAttachments;
203 }
204
205 protected function getCount()
206 {
207 return count( \wpas_get_tickets( 'any' ) );
208 }
209
210 /**
211 * @param int $page
212 * @return array $response
213 */
214 public function deleteTickets($page)
215 {
216 $hasmore = true;
217
218 $args = [
219 'posts_per_page' => $this->limit,
220 'paged' => $page
221 ];
222
223 $tickets = \wpas_get_tickets('any', $args);
224
225 if (!$tickets) {
226 $hasmore = false;
227 }
228
229 if ($tickets) {
230 foreach($tickets as $ticket){
231 wp_delete_post($ticket->ID, true);
232
233 $this->db->table('posts')
234 ->whereIn('post_type', ['ticket_reply', 'ticket_history'])
235 ->where('post_parent', $ticket->ID)
236 ->oldest('ID')
237 ->delete();
238 }
239 }
240
241 $response = [
242 'has_more' => $hasmore,
243 'next_page' => (int) $page + 1
244 ];
245
246 if (!$hasmore) {
247 $response['message'] = __('All tickets have been deleted successfully', 'fluent-support');
248 }
249
250 return $response;
251 }
252
253 }
254
255
256