PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 2.3.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v2.3.1
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / functions / replace_macro.php

replace_macro.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 2.3.1, at includes/functions/replace_macro.php

541 lines 19.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly
4 }
5
6 global $current_user, $wpscfunction,$wpdb;
7
8 $wpsc_ticket_data = $wpscfunction->get_ticket($ticket_id);
9 $ln_flag = false;
10 $tdec_flag = false;
11 $lr_flag = false;
12
13 preg_match_all("/{[^}]*}/" ,$str,$matches);
14 $matches = array_unique($matches[0]);
15
16 foreach($matches as $match){
17
18 $field = get_term_by('slug',str_replace("{}"," ",$match),'wpsc_ticket_custom_fields');
19 if(!$field) continue;
20
21 $tf_type = get_term_meta( $field->term_id, 'wpsc_tf_type', true);
22
23 if($tf_type > '0') {
24 switch ($tf_type) {
25 case '1':
26 case '2':
27 case '4':
28 case '9':
29 $text = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true);
30 $str = str_replace('{'.$field->slug.'}', $text, $str);
31 break;
32
33 case '3':
34 $text = $wpscfunction->get_ticket_meta($ticket_id,$field->slug);
35 if($text){
36 $str = str_replace('{'.$field->slug.'}', implode(', ',$text), $str);
37 } else {
38 $str = str_replace('{'.$field->slug.'}', '', $str);
39 }
40 break;
41
42 case '5':
43 $text = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true);
44 $str = str_replace('{'.$field->slug.'}', nl2br($text), $str);
45 break;
46
47 case '6':
48 $date = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true);
49 $str = str_replace('{'.$field->slug.'}', $this->datetimeToCalenderFormat($date), $str);
50 break;
51
52 case '7':
53 $text = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true);
54 $text = $text ? '<a href="'.$text.'" target="_blank">'.$text.'</a>' : '';
55 $str = str_replace('{'.$field->slug.'}', $text, $str);
56 break;
57
58 case '8':
59 $text = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true);
60 $text = $text ? '<a href="mailto:'.$text.'" target="_blank">'.$text.'</a>' : '';
61 $str = str_replace('{'.$field->slug.'}', $text, $str);
62 break;
63
64 case '10':
65 $attachments = $wpscfunction->get_ticket_meta($ticket_id, $field->slug);
66 $auth_id = $wpscfunction->get_ticket_fields($ticket_id,'ticket_auth_code');
67 $attach_url = array();
68 if($attachments){
69 foreach( $attachments as $attachment ):
70 $attach = array();
71 $attach_meta = get_term_meta($attachment);
72 foreach ($attach_meta as $key => $value) {
73 $attach[$key] = $value[0];
74 }
75 $upload_dir = wp_upload_dir();
76
77 $wpsp_file = get_term_meta($attachment,'wpsp_file');
78
79 if ($wpsp_file) {
80 $file_url = $upload_dir['baseurl'] . '/wpsp/'. $attach['save_file_name'];
81 }else {
82 if( isset($attach['is_restructured']) && $attach['is_restructured']){
83 $updated_time = get_term_meta($attachment,'time_uploaded',true);
84 $time = strtotime($updated_time);
85 $month = date("m",$time);
86 $year = date("Y",$time);
87 $file_url = $upload_dir['baseurl'] . '/wpsc/'.$year.'/'.$month.'/'. $attach['save_file_name'];
88 }else{
89 $file_url = $upload_dir['baseurl'] . '/wpsc/'. $attach['save_file_name'];
90 }
91 }
92
93 $download_url = home_url('/').'?wpsc_attachment='.$attachment.'&tid='.$ticket_id.'&tac='.$auth_id;
94 $attach_url[] = '<a href="'.$download_url.'" target="_blank">'.$attach['filename'].'</a>';
95
96 endforeach;
97
98 $str = str_replace('{'.$field->slug.'}', implode('<br />',$attach_url), $str);
99
100 } else {
101 $str = str_replace('{'.$field->slug.'}', ' ', $str);
102 }
103 break;
104
105 case '18':
106 $date = $wpscfunction->get_ticket_meta( $ticket_id, $field->slug, true );
107 if($date){
108 $str = str_replace('{'.$field->slug.'}', $date, $str);
109 } else {
110 $str = str_replace('{'.$field->slug.'}', '', $str);
111 }
112 break;
113
114 case '21':
115 $time = $wpscfunction->get_ticket_meta($ticket_id, $field->slug, true);
116 $time_format = get_term_meta($field->term_id, 'wpsc_time_format',true);
117
118 if($time){
119 if($time_format == '12'){
120 $str = str_replace('{'.$field->slug.'}', date("h:i:s a", strtotime($time)), $str);
121 } else{
122 $str = str_replace('{'.$field->slug.'}', $time, $str);
123 }
124 }else{
125 $str = str_replace('{'.$field->slug.'}', '', $str);
126 }
127 break;
128
129 default :
130 $str = apply_filters('wpsc_replace_macro_custom',$str,$ticket_id,$tf_type,$field);
131 break;
132 }
133 } else{
134
135 switch($match){
136
137 // Ticket ID
138 case '{ticket_id}':
139 $str = str_replace('{ticket_id}', $ticket_id, $str);
140 break;
141
142 // Ticket Status
143 case '{ticket_status}':
144
145 $ticket_status = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['ticket_status'] ) ? $this->get_status_name($wpsc_ticket_data['ticket_status']) : '';
146 $str = str_replace('{ticket_status}', $ticket_status, $str);
147 break;
148
149 // Ticket Category
150 case '{ticket_category}' :
151 $ticket_category = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['ticket_category'] ) ? $this->get_category_name($wpsc_ticket_data['ticket_category']) : '';
152 $str = str_replace('{ticket_category}', $ticket_category, $str);
153 break;
154
155 // Ticket Priority
156 case '{ticket_priority}':
157 $ticket_priority = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['ticket_priority'] ) ? $this->get_priority_name($wpsc_ticket_data['ticket_priority']) : '';
158 $str = str_replace('{ticket_priority}',$ticket_priority, $str);
159 break;
160
161 // Customer Name
162 case '{customer_name}':
163 $customer_name = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['customer_name'] ) ? stripslashes($wpsc_ticket_data['customer_name']) : '';
164 $str = str_replace('{customer_name}',$customer_name, $str);
165 break;
166
167 // Customer Email
168 case '{customer_email}':
169 $customer_email = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['customer_email'] ) ? $wpsc_ticket_data['customer_email'] : '';
170 $str = str_replace('{customer_email}',$customer_email, $str);
171 break;
172
173 // Subject
174 case '{ticket_subject}':
175 $ticket_subject = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['ticket_subject'] ) ? $wpsc_ticket_data['ticket_subject'] : '';
176 $str = str_replace('{ticket_subject}', stripslashes($ticket_subject), $str);
177 break;
178
179 //User Type
180 case '{user_type}':
181 $user_type = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['user_type'] ) ? $wpsc_ticket_data['user_type'] : '';
182 $str = str_replace('{user_type}', $user_type, $str);
183 break;
184
185 // Ticket Description
186 case '{ticket_description}':
187
188 $ticket_description = $this->get_ticket_description($ticket_id);
189 if (strpos($str, 'ticket_description') !== false) {
190 $tdec_flag = true;
191 }
192 $str = str_replace('{ticket_description}', $ticket_description['description'], $str);
193 break;
194
195 // Last Reply
196 case '{last_reply}':
197 $last_reply = $this->get_last_reply($ticket_id);
198 if (strpos($str, 'last_reply') !== false) {
199 $lr_flag = true;
200 }
201 $str = str_replace('{last_reply}', $last_reply['description'],$str);
202 break;
203
204 // Last Reply User Name
205 case '{last_reply_user_name}':
206 $last_reply = $this->get_last_reply($ticket_id);
207 $str = str_replace('{last_reply_user_name}', $last_reply['user_name'], $str);
208 break;
209
210 // Last Reply User Email
211 case '{last_reply_user_email}':
212 $last_reply = $this->get_last_reply($ticket_id);
213 $str = str_replace('{last_reply_user_email}', $last_reply['user_email'], $str);
214 break;
215
216 // Last Note
217 case '{last_note}':
218 $last_note = $this->get_last_note($ticket_id);
219
220 if (strpos($str, 'last_note') !== false) {
221 $ln_flag = true;
222 }
223 $str = str_replace('{last_note}', $last_note['description'], $str);
224 break;
225
226 // Last Note User Name
227 case '{last_note_user_name}':
228 $last_note = $this->get_last_note($ticket_id);
229 $str = str_replace('{last_note_user_name}', $last_note['user_name'], $str);
230 break;
231
232 // Last Note User Email
233 case '{last_note_user_email}':
234 $last_note = $this->get_last_note($ticket_id);
235 $str = str_replace('{last_note_user_email}', $last_note['user_email'], $str);
236 break;
237
238 // Current User Name
239 case '{current_user_name}':
240 $str = str_replace('{current_user_name}', $current_user->display_name, $str);
241 break;
242
243 // Current User Email
244 case '{current_user_email}':
245 $str = str_replace('{current_user_email}', $current_user->user_email, $str);
246 break;
247
248 // Ticket History
249 case '{ticket_history}':
250 $ticket_history = $this->get_ticket_history($ticket_id);
251 $str = str_replace('{ticket_history}', $ticket_history, $str);
252 break;
253
254 // Assigned Agents
255 case '{assigned_agent}':
256 $assigned_agents = $this->get_assigned_agent_names($ticket_id);
257 $str = str_replace('{assigned_agent}', $assigned_agents, $str);
258 break;
259
260 // Previously Assigned agents
261 case '{previously_assigned_agent}':
262 $previously_assigned_agent = $this->get_previously_assigned_agents_names($ticket_id);
263 $str = str_replace('{previously_assigned_agent}', $previously_assigned_agent, $str);
264 break;
265
266 // Date created
267 case '{date_created}':
268 $date_created = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['date_created'] ) ? get_date_from_gmt( $wpsc_ticket_data['date_created'] ,$this->time_elapsed_timestamp($wpsc_ticket_data['date_created']) ) : '';
269 $str = str_replace('{date_created}',$date_created, $str);
270 break;
271
272 // Date updated
273 case '{date_updated}':
274 $date_updated = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['date_updated'] ) ? get_date_from_gmt($wpsc_ticket_data['date_updated']) : '';
275 $str = str_replace('{date_updated}', $date_updated , $str);
276 break;
277
278 // Agent created
279 case '{agent_created}':
280 $agent_created = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['agent_created'] ) ? $this->get_agent_name($wpsc_ticket_data['agent_created']) : '';
281 $str = str_replace('{agent_created}',$agent_created, $str);
282 break;
283
284 // All ticket history
285 case '{ticket_history_all}':
286 $ticket_history_all = $this->get_ticket_history_all($ticket_id);
287 $str = str_replace('{ticket_history_all}', $ticket_history_all,$str);
288 break;
289
290 // All ticket history with all notes
291 case '{ticket_history_all_with_notes}':
292 if ($current_user && $current_user->has_cap('wpsc_agent')) {
293
294 $ticket_history_all_with_notes = $this->get_ticket_history_all_with_notes($ticket_id);
295 $str = str_replace('{ticket_history_all_with_notes}', $ticket_history_all_with_notes, $str);
296
297 } else {
298 $str = str_replace('{ticket_history_all_with_notes}',' ',$str);
299 }
300 break;
301
302 // All ticket history of notes
303 case '{ticket_notes_history}':
304 if ($current_user && $current_user->has_cap('wpsc_agent')) {
305
306 $ticket_notes_history = $this->ticket_notes_history($ticket_id);
307 $str = str_replace('{ticket_notes_history}',$ticket_notes_history , $str);
308
309 } else {
310 $str = str_replace('{ticket_notes_history}',' ', $str);
311 }
312 break;
313
314 // Ticket URL
315 case '{ticket_url}':
316
317 $ticket_auth_code = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['ticket_auth_code'] ) ? $wpsc_ticket_data['ticket_auth_code'] : '';
318 $ticket_url = get_permalink(get_option('wpsc_support_page_id')).'?support_page=open_ticket&ticket_id='.$ticket_id.'&auth_code='.$ticket_auth_code;
319 $ticket_url = apply_filters('wpsc_ticket_url',$ticket_url,$ticket_id);
320 $ticket_url = '<a href="'.$ticket_url.'" target="_blank">'.$ticket_url.'</a>';
321 $str = str_replace('{ticket_url}', $ticket_url, $str);
322 break;
323
324 // All ticket history with logs
325 case '{ticket_history_all_with_logs}':
326 if ($current_user && $current_user->has_cap('wpsc_agent')) {
327
328 $ticket_history_all_with_logs = $this->get_ticket_history_all_with_logs($ticket_id);
329 $str = str_replace('{ticket_history_all_with_logs}', $ticket_history_all_with_logs, $str);
330
331 }else{
332 $str = str_replace('{ticket_history_all_with_logs}', ' ', $str);
333 }
334 break;
335
336 // All ticket history with notes and log
337 case '{ticket_history_all_with_notes_and_logs}':
338 if ( $current_user && $current_user->has_cap('wpsc_agent')) {
339
340 $ticket_history_all_with_notes_and_logs = $this->get_ticket_history_all_with_notes_and_logs($ticket_id);
341 $str = str_replace('{ticket_history_all_with_notes_and_logs}', $ticket_history_all_with_notes_and_logs, $str);
342
343 } else {
344 $str = str_replace('{ticket_history_all_with_notes_and_logs}',' ', $str);
345 }
346 break;
347
348 // Ticket notes history with logs
349 case '{ticket_notes_history_with_logs}':
350 if ($current_user && $current_user->has_cap('wpsc_agent')) {
351
352 $ticket_notes_history_with_logs = $this->get_ticket_notes_history_with_logs($ticket_id);
353 $str = str_replace('{ticket_notes_history_with_logs}', $ticket_notes_history_with_logs, $str);
354
355 }else {
356 $str = str_replace('{ticket_notes_history_with_logs}',' ', $str);
357 }
358 break;
359
360 //Date Closed
361 case '{date_closed}':
362 $date_closed = $wpscfunction->time_elapsed_timestamp($wpscfunction->get_ticket_meta($ticket_id, 'date_closed', true));
363 $str = str_replace('{date_closed}', $date_closed, $str);
364 break;
365
366 case '{customer_first_name}':
367 $customer_email = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['customer_email'] ) ? stripslashes($wpsc_ticket_data['customer_email']) : '';
368 $user = get_user_by( 'email', $customer_email );
369 if($user){
370 $first_name = get_user_meta( $user->ID,'first_name', true );
371 if($first_name){
372 $str = str_replace('{customer_first_name}',$first_name, $str);
373 }else{
374 $customer_name = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['customer_name'] ) ? stripslashes($wpsc_ticket_data['customer_name']) : '';
375 $str = str_replace('{customer_first_name}',$customer_name, $str);
376 }
377 }else{
378 $customer_name = (!empty($wpsc_ticket_data)) && isset( $wpsc_ticket_data['customer_name'] ) ? stripslashes($wpsc_ticket_data['customer_name']) : '';
379 $str = str_replace('{customer_first_name}',$customer_name, $str);
380 }
381 break;
382
383 case '{last_reply_user_first_name}':
384 $last_reply = $this->get_last_reply($ticket_id);
385 $user = get_user_by('email',$last_reply['user_email']);
386 if( $user ){
387 $first_name = get_user_meta( $user->ID,'first_name', true );
388 if($first_name){
389 $str = str_replace('{last_reply_user_first_name}',$first_name, $str);
390 }else{
391 $str = str_replace('{last_reply_user_first_name}',$last_reply['user_name'], $str);
392 }
393 }else{
394 $str = str_replace('{last_reply_user_first_name}', $last_reply['user_name'], $str);
395 }
396 break;
397
398 case '{current_user_first_name}':
399 $first_name = get_user_meta( $current_user->ID,'first_name', true );
400 if($first_name){
401 $str = str_replace('{current_user_first_name}',$first_name, $str);
402 }else{
403 $str = str_replace('{current_user_first_name}',$current_user->display_name, $str);
404 }
405 break;
406
407 case '{last_note_user_first_name}':
408 $last_note = $this->get_last_note($ticket_id);
409 $user = get_user_by('email',$last_note['user_email']);
410 if( $user ){
411 $first_name = get_user_meta( $user->ID,'first_name', true );
412 if($first_name){
413 $str = str_replace('{last_note_user_first_name}',$first_name, $str);
414 }else{
415 $str = str_replace('{last_note_user_first_name}',$last_note['user_name'], $str);
416 }
417 }else{
418 $str = str_replace('{last_note_user_first_name}', $last_note['user_name'], $str);
419 }
420 break;
421
422 default:
423 break;
424 }
425 }
426 }
427
428 if($tdec_flag){
429 if(isset($ticket_description['thread_id'])){
430 $report_thread_id = $ticket_description['thread_id'];
431 $report_attachments = get_post_meta($report_thread_id,'attachments',true);
432 $auth_id = $wpsc_ticket_data['ticket_auth_code'];
433 $ticket_id = get_post_meta($report_thread_id,'ticket_id',true);
434 $attach_url = array();
435 if($report_attachments){
436 $attach_url[] = __('Attachments','supportcandy');
437 foreach( $report_attachments as $attachment ):
438 $attach = array();
439 $attach_meta = get_term_meta($attachment);
440 foreach ($attach_meta as $key => $value) {
441 $attach[$key] = $value[0];
442 }
443 $upload_dir = wp_upload_dir();
444
445 $wpsp_file = get_term_meta($attachment,'wpsp_file');
446
447 if ($wpsp_file) {
448 $file_url = $upload_dir['baseurl'] . '/wpsp/'. $attach['save_file_name'];
449 }else {
450 if( isset($attach['is_restructured']) && $attach['is_restructured']){
451 $updated_time = get_term_meta($attachment,'time_uploaded',true);
452 $time = strtotime($updated_time);
453 $month = date("m",$time);
454 $year = date("Y",$time);
455 $file_url = $upload_dir['baseurl'] . '/wpsc/'.$year.'/'.$month.'/'. $attach['save_file_name'];
456 }else{
457 $file_url = $upload_dir['baseurl'] . '/wpsc/'. $attach['save_file_name'];
458 }
459 }
460
461 $download_url = home_url('/').'?wpsc_attachment='.$attachment.'&tid='.$ticket_id.'&tac='.$auth_id;
462 $attach_url[] = '<a style="text-decoration:none;" href="'.$download_url.'" target="_blank">'.$attach['filename'].'</a>';
463 endforeach;
464 }
465 $str = $str.implode("<br>",$attach_url);
466 }
467 }
468 if($lr_flag){
469 $last_reply_thread_id = $last_reply['thread_id'];
470 $last_reply_attachments = get_post_meta($last_reply_thread_id,'attachments',true);
471 $auth_id = $wpsc_ticket_data['ticket_auth_code'];
472 $ticket_id = get_post_meta($last_reply_thread_id,'ticket_id',true);
473 $attach_url = array();
474 if($last_reply_attachments){
475 $attach_url[] = __('Attachments','supportcandy');
476 foreach( $last_reply_attachments as $attachment ):
477 $attach = array();
478 $attach_meta = get_term_meta($attachment);
479 foreach ($attach_meta as $key => $value) {
480 $attach[$key] = $value[0];
481 }
482 $upload_dir = wp_upload_dir();
483 if( isset($attach['is_restructured']) && $attach['is_restructured']){
484 $updated_time = get_term_meta($attachment,'time_uploaded',true);
485 $time = strtotime($updated_time);
486 $month = date("m",$time);
487 $year = date("Y",$time);
488 $file_url = $upload_dir['baseurl'] . '/wpsc/'.$year.'/'.$month.'/'.$attach['save_file_name'];
489 }else{
490 $file_url = $upload_dir['baseurl'] . '/wpsc/'.$attach['save_file_name'];
491 }
492
493 $download_url = home_url('/').'?wpsc_attachment='.$attachment.'&tid='.$ticket_id.'&tac='.$auth_id;
494 $attach_url[] = '<a style="text-decoration:none;" href="'.$download_url.'" target="_blank">'.$attach['filename'].'</a>';
495 endforeach;
496 }
497 $str = $str.implode("<br>",$attach_url);
498 }
499
500 if($ln_flag){
501 $last_note_thread_id = $last_note['thread_id'];
502 $last_note_attachments = get_post_meta($last_note_thread_id,'attachments',true);
503 $auth_id = $wpsc_ticket_data['ticket_auth_code'];
504 $ticket_id = get_post_meta($last_note_thread_id,'ticket_id',true);
505 $attach_url = array();
506 if($last_note_attachments){
507 $attach_url[] = __('Attachments','supportcandy');
508 foreach( $last_note_attachments as $attachment ):
509 $attach = array();
510 $attach_meta = get_term_meta($attachment);
511 foreach ($attach_meta as $key => $value) {
512 $attach[$key] = $value[0];
513 }
514 $upload_dir = wp_upload_dir();
515
516 $wpsp_file = get_term_meta($attachment,'wpsp_file');
517
518 if ($wpsp_file) {
519 $file_url = $upload_dir['baseurl'] . '/wpsp/'. $attach['save_file_name'];
520 }else {
521 if( isset($attach['is_restructured']) && $attach['is_restructured']){
522 $updated_time = get_term_meta($attachment,'time_uploaded',true);
523 $time = strtotime($updated_time);
524 $month = date("m",$time);
525 $year = date("Y",$time);
526 $file_url = $upload_dir['baseurl'] . '/wpsc/'.$year.'/'.$month.'/'. $attach['save_file_name'];
527 }else{
528 $file_url = $upload_dir['baseurl'] . '/wpsc/'. $attach['save_file_name'];
529 }
530 }
531
532 $download_url = home_url('/').'?wpsc_attachment='.$attachment.'&tid='.$ticket_id.'&tac='.$auth_id;
533 $attach_url[] = '<a style="text-decoration:none;" href="'.$download_url.'" target="_blank">'.$attach['filename'].'</a>';
534 endforeach;
535 }
536 $str = $str.implode("<br>",$attach_url);
537 }
538
539 $str = apply_filters('wpsc_replace_macro',$str,$ticket_id);
540
541