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
← All changes | app/Services/Tickets/TicketService.php +123 -33 2.3.0 → 2.4.5 View file →
@@ -12,14 +12,13 @@
12 12
13 13 class TicketService
14 14 {
15 15 /**
16 - * this function will set the ticket status as closed
17 - * @param $ticket
18 - * @param $person
16 + * @param \FluentSupport\App\Models\Ticket $ticket
17 + * @param \FluentSupport\App\Models\Person $person
19 18 * @param string $internalNote
20 - * @param bool $silently
21 - * @return mixed
19 + * @param string $silently 'yes' skips the close hooks.
20 + * @return \FluentSupport\App\Models\Ticket
22 21 */
23 22
24 23 public function close($ticket, $person, $internalNote = '', $silently = 'no')
25 24 {
@@ -38,9 +37,8 @@
38 37 if (!$internalNote) {
39 38 $internalNote = __('Ticket has been closed', 'fluent-support');
40 39 }
41 40
42 - //Keep track in conversation
43 41 Conversation::create([
44 42 'ticket_id' => $ticket->id,
45 43 'person_id' => $person->id,
46 44 'conversation_type' => 'internal_info',
@@ -50,9 +48,13 @@
50 48
51 49 return $ticket;
52 50 }
53 51
54 - public function reopen($ticket, $person)
52 + /**
53 + * @param bool $deferHooks Skip the reopen hooks so a caller running inside a
54 + * transaction can fire them after commit (fireReopenHooks()).
55 + */
56 + public function reopen($ticket, $person, $deferHooks = false)
55 57 {
56 58 if ($ticket->status == 'closed') {
57 59 $ticket->status = 'active';
58 60 $ticket->waiting_since = current_time('mysql');
@@ -58,17 +60,12 @@
58 60 $ticket->waiting_since = current_time('mysql');
59 61 $ticket->resolved_at = null;
60 62 $ticket->save();
61 63
62 - /*
63 - * Action on ticket reopen
64 - *
65 - * @since v1.0.0
66 - * @param object $ticket
67 - * @param object $person
68 - */
69 - do_action('fluent_support/ticket_reopen', $ticket, $person);
70 - do_action('fluent_support/ticket_reopen_by_' . $person->person_type, $ticket, $person);
64 + if (!$deferHooks) {
65 + $this->fireReopenHooks($ticket, $person);
66 + }
67 +
71 68 Conversation::create([
72 69 'ticket_id' => $ticket->id,
73 70 'person_id' => $person->id,
74 71 'conversation_type' => 'internal_info',
@@ -78,8 +75,21 @@
78 75
79 76 return $ticket;
80 77 }
81 78
79 + public function fireReopenHooks($ticket, $person)
80 + {
81 + /*
82 + * Fires when a closed ticket is reopened.
83 + *
84 + * @since v1.0.0
85 + * @param object $ticket
86 + * @param object $person
87 + */
88 + do_action('fluent_support/ticket_reopen', $ticket, $person);
89 + do_action('fluent_support/ticket_reopen_by_' . $person->person_type, $ticket, $person);
90 + }
91 +
82 92 public function onAgentChange($ticket, $person)
83 93 {
84 94 do_action('fluent_support/ticket_agent_change', $ticket, $person);
85 95 Conversation::create([
@@ -85,11 +95,13 @@
85 95 Conversation::create([
86 96 'ticket_id' => $ticket->id,
87 97 'person_id' => $person->id,
88 98 'conversation_type' => 'internal_info',
89 - 'content' => $ticket->agent->user_id !== $person->user_id ?
90 - $person->full_name . __(' assigned ', 'fluent-support') . $ticket->agent->full_name . __(' in this ticket', 'fluent-support') :
91 - $person->full_name .__( ' assign this ticket to self', 'fluent-support')
99 + 'content' => $ticket->agent->user_id !== $person->user_id
100 + /* translators: %1$s is the name of the person who assigned the ticket, %2$s is the assigned agent's name */
101 + ? sprintf(__('%1$s assigned %2$s to this ticket', 'fluent-support'), $person->full_name, $ticket->agent->full_name)
102 + /* translators: %s is the agent's name */
103 + : sprintf(__('%s assigned this ticket to themselves', 'fluent-support'), $person->full_name)
92 104 ]);
93 105
94 106 return $person;
95 107 }
@@ -94,23 +106,35 @@
94 106 return $person;
95 107 }
96 108
97 109 /**
98 - * This `addTicketAttachments` method is responsible for adding attachments to ticket
110 + * Move the ticket's inline images and pending uploads into the ticket folder.
111 + *
99 112 * @param array $data
100 113 * @param array $disabledFields
101 114 * @param \FluentSupport\App\Models\Ticket $ticket
102 115 * @param object $customer
116 + * @param bool $onBehalf Filed by a remote portal for the customer; their uploads are the customer's.
103 117 * @return \FluentSupport\App\Models\Ticket $ticket
104 118 * @since 1.5.7
105 119 */
106 - public static function addTicketAttachments($data, $disabledFields, $ticket, $customer)
120 + public static function addTicketAttachments($data, $disabledFields, $ticket, $customer, $onBehalf = false)
107 121 {
108 122 Helper::tempImageMoveUploadDir($ticket->id, 'ticket-create', null, $customer->id);
109 123
110 124 if (($attachmentsHashes = Arr::get($data, 'attachments')) && !in_array('file_upload', $disabledFields)) {
125 + $uploader = Helper::getCurrentAgent() ?: $customer;
126 + $uploaderIds = [$uploader->id];
127 +
128 + // A remote portal acting on_behalf uploads as the customer it files for.
129 + if ($onBehalf) {
130 + $uploaderIds[] = $customer->id;
131 + }
132 +
111 133 $attachments = Attachment::whereIn('file_hash', $attachmentsHashes)
112 134 ->where('status', 'in-active')
135 + ->whereNull('ticket_id')
136 + ->whereIn('person_id', $uploaderIds)
113 137 ->get();
114 138
115 139 if ($attachments->isEmpty()) {
116 140 return $ticket;
@@ -145,10 +169,10 @@
145 169 return $ticket;
146 170 }
147 171
148 172 /**
149 - * Sanitize ticket data, create the ticket, handle attachments & custom fields, fire hooks.
150 - * Shared by TicketController::createTicket and ProTicketService::handleSplitTicket.
173 + * Sanitize and create the ticket, then handle attachments, custom fields and hooks.
174 + * Used by TicketController::createTicket and ProTicketService::handleSplitTicket.
151 175 *
152 176 * @param array $ticketData
153 177 * @param \FluentSupport\App\Models\Customer $customer
154 178 * @return \FluentSupport\App\Models\Ticket
@@ -163,14 +187,20 @@
163 187 } else {
164 188 $mailbox = MailBox::findOrFail($ticketData['mailbox_id']);
165 189 }
166 190
191 + // Empty when no agent is logged in, so cron, email piping and API callers pass.
192 + $restrictedBoxes = (new AgentTicketAccess())->getRestrictedMailboxIds();
193 +
194 + $this->assertMailboxAllowed($ticketData, $restrictedBoxes);
195 +
167 196 if (!empty($ticketData['product_id'])) {
168 197 $ticketData['product_source'] = 'local';
169 198 }
170 199
171 - $ticketData['title'] = sanitize_text_field(wp_unslash($ticketData['title']));
172 - $ticketData['content'] = wp_specialchars_decode(wp_unslash(wp_kses_post($ticketData['content'])));
200 + // Already unslashed at the request boundary. Don't unslash again or literal backslashes are lost.
201 + $ticketData['title'] = sanitize_text_field($ticketData['title']);
202 + $ticketData['content'] = wp_kses_post($ticketData['content']);
173 203
174 204 if (!empty($ticketData['priority'])) {
175 205 $ticketData['priority'] = sanitize_text_field($ticketData['priority']);
176 206 }
@@ -180,8 +210,11 @@
180 210 }
181 211
182 212 $ticketData = apply_filters('fluent_support/create_ticket_data', $ticketData, $customer);
183 213
214 + // Check again: a filter may have routed the ticket into a restricted box.
215 + $this->assertMailboxAllowed($ticketData, $restrictedBoxes);
216 +
184 217 do_action('fluent_support/before_ticket_create', $ticketData, $customer);
185 218
186 219 $createdTicket = Ticket::create($ticketData);
187 220
@@ -198,18 +231,29 @@
198 231 $isAgentInitiated = Arr::get($ticketData, 'agent_initiated') === 'yes';
199 232 $createdTicket->created_by = $agent->id;
200 233
201 234 if ($isAgentInitiated) {
202 - // Skip all ticket creation emails for agent-initiated tickets
203 - add_filter('fluent_support/should_send_notification', function ($shouldSend, $channel, $type) {
204 - if ($channel === 'email' && in_array($type, ['ticket_created_email_to_customer', 'ticket_created_email_to_admin', 'ticket_created_by_agent_email_to_customer'])) {
235 + // The agent's first response below sends its own email. Scoped to this
236 + // ticket and removed after its creation hooks, so later tickets in the
237 + // same request (MCP, imports, workflows) still get their emails.
238 + $suppressCreatedEmails = function ($shouldSend, $channel, $type, $ticket = null) use ($createdTicket) {
239 + if ($channel === 'email'
240 + && $ticket && (int) $ticket->id === (int) $createdTicket->id
241 + && in_array($type, ['ticket_created_email_to_customer', 'ticket_created_email_to_admin', 'ticket_created_by_agent_email_to_customer'], true)) {
205 242 return false;
206 243 }
207 244 return $shouldSend;
208 - }, 10, 3);
245 + };
246 + add_filter('fluent_support/should_send_notification', $suppressCreatedEmails, 10, 4);
209 247
210 - $initializedMessage = $agent->full_name . __(' initialized this ticket', 'fluent-support');
248 + // The channel it was sent from moves to the response; the ticket's source marks it as outreach.
249 + $channel = $createdTicket->source ?: 'web';
250 + $createdTicket->source = Ticket::SOURCE_AGENT_INITIATED;
211 251
252 + // A placeholder only: both apps show the agent's response as the opening message.
253 + /* translators: %s is the agent's name */
254 + $initializedMessage = sprintf(__('%s started this conversation', 'fluent-support'), $agent->full_name);
255 +
212 256 $responseContent = apply_filters('fluent_support/parse_smartcode_data', $createdTicket->content, [
213 257 'customer' => $customer,
214 258 'agent' => $agent,
215 259 'ticket' => $createdTicket,
@@ -214,17 +258,16 @@
214 258 'agent' => $agent,
215 259 'ticket' => $createdTicket,
216 260 ]);
217 261
218 - $responseContent = (new ResponseService())->maybeAppendSignature($responseContent, $agent);
219 262 $responseContent = wp_kses_post($responseContent);
220 263
221 - // Agent response: actual ticket content — sends reply email to customer
222 264 $agentResponse = Conversation::create([
223 265 'ticket_id' => $createdTicket->id,
224 266 'person_id' => $agent->id,
225 267 'conversation_type' => 'response',
226 - 'content' => $responseContent
268 + 'content' => $responseContent,
269 + 'source' => $channel,
227 270 ]);
228 271
229 272 if ($attachmentHashes = Arr::get($ticketData, 'attachments', [])) {
230 273 Attachment::where('ticket_id', $createdTicket->id)
@@ -237,8 +280,29 @@
237 280
238 281 $agentResponse->load('attachments');
239 282 }
240 283
284 + // The agent's message is the first response, so the ticket must say so:
285 + // the same counters ResponseService::createResponse() sets for an agent
286 + // reply, otherwise the ticket lists as new/unanswered and the agent's
287 + // next reply is measured as the first response.
288 + $now = current_time('mysql');
289 + // Unset in memory when the column default supplied it on insert.
290 + if (!$createdTicket->status || $createdTicket->status === 'new') {
291 + $createdTicket->status = 'active';
292 + }
293 + if (!$createdTicket->agent_id) {
294 + $createdTicket->agent_id = $agent->id;
295 + }
296 + $createdTicket->response_count = (int) $createdTicket->response_count + 1;
297 + $createdTicket->first_response_time = 0;
298 + $createdTicket->last_agent_response = $now;
299 + $createdTicket->waiting_since = $now;
300 +
301 + // The outreach email may render {{ticket.content}}, so expose the parsed body
302 + // during the hook. The ticket itself stores the "initialized" message.
303 + $createdTicket->content = $responseContent;
304 +
241 305 do_action('fluent_support/agent_initiated_ticket_response', $agentResponse, $createdTicket, $agent);
242 306
243 307 $createdTicket->content = $initializedMessage;
244 308
@@ -249,11 +313,37 @@
249 313
250 314 do_action('fluent_support/ticket_created', $createdTicket, $customer);
251 315 do_action('fluent_support/ticket_created_behalf_of_customer', $createdTicket, $customer, $agent);
252 316
317 + if (!empty($suppressCreatedEmails)) {
318 + remove_filter('fluent_support/should_send_notification', $suppressCreatedEmails, 10);
319 + }
320 +
253 321 return $createdTicket;
254 322 }
255 323
324 + /**
325 + * Block ticket creation in a business box the acting agent is restricted from.
326 + *
327 + * @param array $ticketData
328 + * @param array $restrictedBoxes Already int-normalized by AgentTicketAccess
329 + * @return void
330 + * @throws \Exception
331 + */
332 + protected function assertMailboxAllowed($ticketData, $restrictedBoxes)
333 + {
334 + if (empty($restrictedBoxes) || empty($ticketData['mailbox_id'])) {
335 + return;
336 + }
337 +
338 + if (in_array((int) $ticketData['mailbox_id'], $restrictedBoxes, true)) {
339 + throw new \Exception(
340 + esc_html__('Sorry, you do not have permission to create a ticket in this business box', 'fluent-support')
341 + );
342 + }
343 + }
344 +
345 + // No auth check here: callers handle it, and it also runs with no current user (API, GDPR erasure, cron).
256 346 public function deleteTicket($ticket, $agent = null)
257 347 {
258 348 if (!$agent) {
259 349 $agent = Helper::getAgentByUserId();