PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.6
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.6
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 / class-wpsc-macros.php

class-wpsc-macros.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.6, at includes/class-wpsc-macros.php

1,114 lines 30.0 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 if ( ! class_exists( 'WPSC_Macros' ) ) :
7
8 final class WPSC_Macros {
9
10 /**
11 * All macros
12 *
13 * @var array
14 */
15 public static $macros;
16
17 /**
18 * Custom field type attachments macros
19 *
20 * @var array
21 */
22 public static $attachments = array();
23
24 /**
25 * Initialize this class
26 *
27 * @return void
28 */
29 public static function init() {
30
31 // Load macros.
32 add_action( 'init', array( __CLASS__, 'load_macros' ), 12 );
33
34 // get macros modal.
35 add_action( 'wp_ajax_wpsc_get_macros', array( __CLASS__, 'get_macros' ) );
36 add_action( 'wp_ajax_nopriv_wpsc_get_macros', array( __CLASS__, 'get_macros' ) );
37 }
38
39 /**
40 * Load maros on init wp event
41 *
42 * @return void
43 */
44 public static function load_macros() {
45
46 $macros = array(
47
48 array(
49 'tag' => '{{ticket_id}}',
50 'title' => esc_attr__( 'Ticket id', 'supportcandy' ),
51 ),
52 array(
53 'tag' => '{{customer_name}}',
54 'title' => esc_attr__( 'Customer name', 'supportcandy' ),
55 ),
56 array(
57 'tag' => '{{customer_first_name}}',
58 'title' => esc_attr__( 'Customer first name', 'supportcandy' ),
59 ),
60 array(
61 'tag' => '{{customer_email}}',
62 'title' => esc_attr__( 'Customer email addrees', 'supportcandy' ),
63 ),
64 array(
65 'tag' => '{{last_reply}}',
66 'title' => esc_attr__( 'Last reply', 'supportcandy' ),
67 ),
68 array(
69 'tag' => '{{last_reply_user_name}}',
70 'title' => esc_attr__( 'Last reply user name', 'supportcandy' ),
71 ),
72 array(
73 'tag' => '{{last_reply_user_email}}',
74 'title' => esc_attr__( 'Last reply user email', 'supportcandy' ),
75 ),
76 array(
77 'tag' => '{{last_reply_user_first_name}}',
78 'title' => esc_attr__( 'Last reply user first name', 'supportcandy' ),
79 ),
80 array(
81 'tag' => '{{last_note}}',
82 'title' => esc_attr__( 'Last note', 'supportcandy' ),
83 ),
84 array(
85 'tag' => '{{last_note_user_name}}',
86 'title' => esc_attr__( 'Last note user name', 'supportcandy' ),
87 ),
88 array(
89 'tag' => '{{last_note_user_email}}',
90 'title' => esc_attr__( 'Last note user email', 'supportcandy' ),
91 ),
92 array(
93 'tag' => '{{last_note_user_first_name}}',
94 'title' => esc_attr__( 'Last note user first name', 'supportcandy' ),
95 ),
96 array(
97 'tag' => '{{current_user_name}}',
98 'title' => esc_attr__( 'Current user name', 'supportcandy' ),
99 ),
100 array(
101 'tag' => '{{current_user_email}}',
102 'title' => esc_attr__( 'Current user email', 'supportcandy' ),
103 ),
104 array(
105 'tag' => '{{current_user_first_name}}',
106 'title' => esc_attr__( 'Current user first name', 'supportcandy' ),
107 ),
108 array(
109 'tag' => '{{ticket_history}}',
110 'title' => esc_attr__( 'Ticket history (last few report & reply excluding last reply)', 'supportcandy' ),
111 ),
112 array(
113 'tag' => '{{ticket_history_all}}',
114 'title' => esc_attr__( 'Ticket history (report & reply)', 'supportcandy' ),
115 ),
116 array(
117 'tag' => '{{ticket_notes_history}}',
118 'title' => esc_attr__( 'Ticket history (note)', 'supportcandy' ),
119 ),
120 array(
121 'tag' => '{{ticket_history_all_with_notes}}',
122 'title' => esc_attr__( 'Ticket history (report, reply & note)', 'supportcandy' ),
123 ),
124 array(
125 'tag' => '{{ticket_history_all_with_logs}}',
126 'title' => esc_attr__( 'Ticket history (report, reply & log)', 'supportcandy' ),
127 ),
128 array(
129 'tag' => '{{ticket_history_all_with_notes_and_logs}}',
130 'title' => esc_attr__( 'Ticket history (report, reply, note & log)', 'supportcandy' ),
131 ),
132 array(
133 'tag' => '{{ticket_notes_history_with_logs}}',
134 'title' => esc_attr__( 'Ticket history (note & log)', 'supportcandy' ),
135 ),
136 array(
137 'tag' => '{{ticket_url}}',
138 'title' => esc_attr__( 'Ticket URL', 'supportcandy' ),
139 ),
140 array(
141 'tag' => '{{ticket_url_plain}}',
142 'title' => esc_attr__( 'Returns the ticket URL without any HTML formatting.', 'supportcandy' ),
143 ),
144 );
145
146 // Add custom fields.
147 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
148
149 if ( ! (
150 class_exists( $cf->type ) &&
151 in_array( $cf->field, WPSC_CF_Settings::$allowed_modules['ticket-macro'] ) &&
152 $cf->type::$has_macro
153 ) ) {
154 continue;
155 }
156
157 $macros[] = array(
158 'tag' => '{{' . $cf->slug . '}}',
159 'title' => $cf->name,
160 );
161 }
162
163 self::$macros = apply_filters( 'wpsc_macros', $macros );
164 }
165
166 /**
167 * Get macros
168 *
169 * @return void
170 */
171 public static function get_macros() {
172
173 $current_user = WPSC_Current_User::$current_user;
174 if ( ! $current_user->is_agent ) {
175 wp_send_json_error( new WP_Error( '001', 'Bad request!' ), 400 );
176 }
177
178 $gs = get_option( 'wpsc-gs-general' );
179 $title = esc_attr__( 'Insert macro', 'supportcandy' );
180
181 // Sort macros.
182 $macros = self::$macros;
183 $macros = WPSC_Functions::array_sort( $macros, 'title', SORT_ASC );
184
185 // Unique ID.
186 $unique_id = uniqid( 'wpsc_' );
187
188 ob_start();?>
189
190 <div style="width: 100%;">
191 <table class="wpsc-setting-tbl wpscMacros">
192 <thead>
193 <tr>
194 <th><?php esc_attr_e( 'Title', 'supportcandy' ); ?></th>
195 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
196 </tr>
197 </thead>
198 <tbody>
199 <?php
200 foreach ( $macros as $macro ) {
201
202 if ( $macro['tag'] == '{{customer_email}}' && ! in_array( $current_user->agent->role, $gs['allow-ar-thread-email'] ) ) {
203 continue;
204 }
205 ?>
206 <tr>
207 <td class="insert-tag lable" data-label="<?php echo esc_attr( $macro['title'] ); ?>" data-tag="<?php echo esc_attr( $macro['tag'] ); ?>"><?php echo esc_attr( $macro['title'] ); ?></td>
208 <td>
209 <a class="copy-tag wpsc-link" title="<?php echo esc_attr_e( 'Copy Tag', 'supportcandy' ); ?>"><?php esc_attr_e( 'Copy', 'supportcandy' ); ?></a> |
210 <a class="insert-tag wpsc-link" title="<?php echo esc_attr_e( 'Insert Tag', 'supportcandy' ); ?>"><?php esc_attr_e( 'Insert', 'supportcandy' ); ?></a>
211 </td>
212 </tr>
213 <?php
214 }
215 ?>
216 </tbody>
217 </table>
218 </div>
219
220 <script>
221 var macroTable = jQuery('table.wpscMacros').DataTable({
222 ordering: false,
223 paging: false,
224 info: false,
225 order: [[1, "desc"]],
226 columnDefs: [
227 { targets: -1, searchable: false, orderable: false },
228 { targets: '_all', className: 'dt-left' }
229 ],
230 language: supportcandy.translations.datatables
231 });
232
233 jQuery(document).ready(function() {
234 jQuery('div.dt-search input', macroTable.table().container()).focus();
235 })
236
237 // Insert tag directly into editor
238 jQuery('.insert-tag').click(function(){
239
240 var text_to_insert = jQuery(this).closest('tr').find('td:first-child').data('tag');
241 var is_tinymce = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
242 if (is_tinymce) {
243 tinymce.activeEditor.execCommand('mceInsertContent', false, text_to_insert);
244 } else {
245 var $txt = jQuery(".wpsc_textarea");
246 var caretPos = $txt[0].selectionStart;
247 var textAreaTxt = $txt.val();
248 $txt.val(textAreaTxt.substring(0, caretPos) + text_to_insert + textAreaTxt.substring(caretPos));
249 }
250 wpsc_close_modal();
251 });
252 // Copy tag to clipboard
253 jQuery('.copy-tag').click(function(){
254
255 var text_to_copy = jQuery(this).closest('tr').find('td:first-child').data('tag');
256 var temp = jQuery("<input>");
257 jQuery("body").append(temp);
258 temp.val(text_to_copy).select();
259 document.execCommand("copy");
260 temp.remove();
261 wpsc_close_modal();
262 });
263 </script>
264 <?php
265 $body = ob_get_clean();
266
267 ob_start();
268 ?>
269 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
270 <?php esc_attr_e( 'Close', 'supportcandy' ); ?>
271 </button>
272 <?php
273 do_action( 'wpsc_get_after_macro' );
274 $footer = ob_get_clean();
275
276 $response = array(
277 'title' => $title,
278 'body' => $body,
279 'footer' => $footer,
280 );
281 wp_send_json( $response );
282 }
283
284 /**
285 * Replace macros in string
286 *
287 * @param string $str - String to replace macros in.
288 * @param WPSC_ticket $ticket - ticket object.
289 * @param string $module - module name.
290 * @return string
291 */
292 public static function replace( $str, $ticket, $module = '' ) {
293
294 // validate ticket object.
295 if ( is_object( $ticket ) && ! intval( $ticket->id ) ) {
296 return $str;
297 }
298
299 // get all macros within string so that will replace only matched.
300 preg_match_all( '/{(\w*)}/', $str, $matches );
301 $matches = isset( $matches[1] ) ? array_unique( $matches[1] ) : array();
302
303 // replace matched tags.
304 foreach ( $matches as $macro ) {
305
306 switch ( $macro ) {
307
308 case 'ticket_id':
309 $cf = WPSC_Custom_Field::get_cf_by_slug( 'id' );
310 $str = str_replace( '{{ticket_id}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
311 break;
312
313 case 'ticket_status':
314 $cf = WPSC_Custom_Field::get_cf_by_slug( 'status' );
315 $str = str_replace( '{{ticket_status}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
316 break;
317
318 case 'ticket_category':
319 $cf = WPSC_Custom_Field::get_cf_by_slug( 'category' );
320 $str = str_replace( '{{ticket_category}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
321 break;
322
323 case 'ticket_priority':
324 $cf = WPSC_Custom_Field::get_cf_by_slug( 'priority' );
325 $str = str_replace( '{{ticket_priority}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
326 break;
327
328 case 'ticket_subject':
329 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
330 $str = str_replace( '{{ticket_subject}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
331 break;
332
333 case 'customer_name':
334 $cf = WPSC_Custom_Field::get_cf_by_slug( 'customer' );
335 $str = str_replace( '{{customer_name}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
336 break;
337
338 case 'customer_email':
339 $cf = WPSC_Custom_Field::get_cf_by_slug( 'email' );
340 $str = str_replace( '{{customer_email}}', $cf->type::get_customer_field_val( $cf, $ticket->customer ), $str );
341 break;
342
343 case 'customer_first_name':
344 $str = self::replace_customer_first_name( $str, $ticket );
345 break;
346
347 case 'ticket_description':
348 $cf = WPSC_Custom_Field::get_cf_by_slug( 'description' );
349 $str = str_replace( '{{ticket_description}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
350 break;
351
352 case 'last_reply':
353 $str = self::replace_last_reply( $str, $ticket );
354 break;
355
356 case 'last_reply_user_name':
357 $str = self::replace_last_reply_user_name( $str, $ticket );
358 break;
359
360 case 'last_reply_user_email':
361 $str = self::replace_last_reply_user_email( $str, $ticket );
362 break;
363
364 case 'last_reply_user_first_name':
365 $str = self::replace_last_reply_user_first_name( $str, $ticket );
366 break;
367
368 case 'last_note':
369 $str = self::replace_last_note( $str, $ticket );
370 break;
371
372 case 'last_note_user_name':
373 $str = self::replace_last_note_user_name( $str, $ticket );
374 break;
375
376 case 'last_note_user_email':
377 $str = self::replace_last_note_user_email( $str, $ticket );
378 break;
379
380 case 'last_note_user_first_name':
381 $str = self::replace_last_note_user_first_name( $str, $ticket );
382 break;
383
384 case 'current_user_name':
385 $str = self::replace_current_user_name( $str, $ticket );
386 break;
387
388 case 'current_user_email':
389 $str = self::replace_current_user_email( $str, $ticket );
390 break;
391
392 case 'current_user_first_name':
393 $str = self::replace_current_user_first_name( $str, $ticket );
394 break;
395
396 case 'previously_assigned_agent':
397 $cf = WPSC_Custom_Field::get_cf_by_slug( 'prev_assignee' );
398 $str = str_replace( '{{' . $cf->slug . '}}', $cf->type::get_ticket_field_val( $cf, $ticket, $module ), $str );
399 break;
400
401 case 'ticket_history':
402 $str = self::replace_ticket_history( $str, $ticket );
403 break;
404
405 case 'ticket_history_all':
406 $str = self::replace_ticket_history_all( $str, $ticket );
407 break;
408
409 case 'ticket_notes_history':
410 $str = self::replace_ticket_notes_history( $str, $ticket );
411 break;
412
413 case 'ticket_history_all_with_notes':
414 $str = self::replace_ticket_history_all_with_notes( $str, $ticket );
415 break;
416
417 case 'ticket_history_all_with_logs':
418 $str = self::replace_ticket_history_all_with_logs( $str, $ticket );
419 break;
420
421 case 'ticket_history_all_with_notes_and_logs':
422 $str = self::replace_ticket_history_all_with_notes_and_logs( $str, $ticket );
423 break;
424
425 case 'ticket_notes_history_with_logs':
426 $str = self::replace_ticket_notes_history_with_logs( $str, $ticket );
427 break;
428
429 case 'ticket_url':
430 $str = self::replace_ticket_url( $str, $ticket );
431 break;
432
433 case 'ticket_url_plain':
434 $str = self::replace_ticket_url_plain( $str, $ticket );
435 break;
436
437 default:
438 // custom fields.
439 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
440
441 if (
442 $cf->type::$slug == 'cf_html' ||
443 $cf->slug != $macro
444 ) {
445 continue;
446 }
447
448 $val = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? $cf->type::get_ticket_field_val( $cf, $ticket, $module ) : $cf->type::get_customer_field_val( $cf, $ticket->customer );
449 $str = str_replace( '{{' . $cf->slug . '}}', $val, $str );
450 }
451
452 // filter tags.
453 $str = apply_filters( 'wpsc_replace_macros', $str, $ticket, $macro );
454 }
455 }
456
457 // return string after replacing found tags.
458 return $str;
459 }
460
461 /**
462 * Replace ticket customer first name
463 *
464 * @param string $str - string to replace tags in.
465 * @param WPSC_Ticket $ticket - ticket object.
466 * @return string
467 */
468 public static function replace_customer_first_name( $str, $ticket ) {
469
470 $first_name = $ticket->customer->name;
471 if ( $ticket->customer->user ) {
472 $first_name = get_user_meta( $ticket->customer->user->ID, 'first_name', true );
473 if ( ! $first_name ) {
474 $first_name = $ticket->customer->name;
475 }
476 }
477
478 $str = str_replace( '{{customer_first_name}}', $first_name, $str );
479 return $str;
480 }
481
482 /**
483 * Replace ticket history
484 *
485 * @param string $str - string to replace tags in.
486 * @param WPSC_Ticket $ticket - ticket object.
487 * @return string
488 */
489 public static function replace_ticket_history( $str, $ticket ) {
490
491 $advanced = get_option( 'wpsc-ms-advanced-settings' );
492 $filters = array(
493 'meta_query' => array(
494 'relation' => 'AND',
495 array(
496 'slug' => 'ticket',
497 'compare' => '=',
498 'val' => $ticket->id,
499 ),
500 array(
501 'slug' => 'type',
502 'compare' => 'IN',
503 'val' => array( 'report', 'reply' ),
504 ),
505 array(
506 'slug' => 'is_active',
507 'compare' => '=',
508 'val' => 1,
509 ),
510 ),
511 'orderby' => 'id',
512 'order' => 'DESC',
513 'items_per_page' => intval( $advanced['ticket-history-macro-threads'] ) + 1,
514 );
515
516 $threads = WPSC_Thread::find( $filters );
517
518 if ( $threads['total_items'] ) {
519
520 array_shift( $threads['results'] );
521 $history = array_filter(
522 array_map(
523 fn ( $thread ) => $thread->get_history_macro(),
524 $threads['results']
525 )
526 );
527 $history = implode( '<br><hr><br>', $history );
528 return str_replace( '{{ticket_history}}', $history, $str );
529
530 } else {
531
532 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
533 return str_replace( '{{ticket_history}}', $history, $str );
534 }
535 }
536
537 /**
538 * Replace ticket history all
539 *
540 * @param string $str - string to replace tags in.
541 * @param WPSC_Ticket $ticket - ticket object.
542 * @return string
543 */
544 public static function replace_ticket_history_all( $str, $ticket ) {
545
546 $filters = array(
547 'meta_query' => array(
548 'relation' => 'AND',
549 array(
550 'slug' => 'ticket',
551 'compare' => '=',
552 'val' => $ticket->id,
553 ),
554 array(
555 'slug' => 'type',
556 'compare' => 'IN',
557 'val' => array( 'report', 'reply' ),
558 ),
559 array(
560 'slug' => 'is_active',
561 'compare' => '=',
562 'val' => 1,
563 ),
564 ),
565 'orderby' => 'id',
566 'order' => 'DESC',
567 );
568
569 $threads = WPSC_Thread::find( $filters );
570
571 if ( $threads['total_items'] ) {
572
573 $history = array_filter(
574 array_map(
575 fn ( $thread ) => $thread->get_history_macro(),
576 $threads['results']
577 )
578 );
579 $history = implode( '<br><hr><br>', $history );
580 return str_replace( '{{ticket_history_all}}', $history, $str );
581
582 } else {
583
584 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
585 return str_replace( '{{ticket_history_all}}', $history, $str );
586 }
587 }
588
589 /**
590 * Replace ticket notes history
591 *
592 * @param string $str - string to replace tags in.
593 * @param WPSC_Ticket $ticket - ticket object.
594 * @return string
595 */
596 public static function replace_ticket_notes_history( $str, $ticket ) {
597
598 $filters = array(
599 'meta_query' => array(
600 'relation' => 'AND',
601 array(
602 'slug' => 'ticket',
603 'compare' => '=',
604 'val' => $ticket->id,
605 ),
606 array(
607 'slug' => 'type',
608 'compare' => '=',
609 'val' => 'note',
610 ),
611 array(
612 'slug' => 'is_active',
613 'compare' => '=',
614 'val' => 1,
615 ),
616 ),
617 'orderby' => 'id',
618 'order' => 'DESC',
619 );
620
621 $threads = WPSC_Thread::find( $filters );
622
623 if ( $threads['total_items'] ) {
624
625 $history = array_filter(
626 array_map(
627 fn ( $thread ) => $thread->get_history_macro(),
628 $threads['results']
629 )
630 );
631 $history = implode( '<br><hr><br>', $history );
632 return str_replace( '{{ticket_notes_history}}', $history, $str );
633
634 } else {
635
636 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
637 return str_replace( '{{ticket_notes_history}}', $history, $str );
638 }
639 }
640
641 /**
642 * Replace ticket history all with notes
643 *
644 * @param string $str - string to replace tags in.
645 * @param WPSC_Ticket $ticket - ticket object.
646 * @return string
647 */
648 public static function replace_ticket_history_all_with_notes( $str, $ticket ) {
649
650 $filters = array(
651 'meta_query' => array(
652 'relation' => 'AND',
653 array(
654 'slug' => 'ticket',
655 'compare' => '=',
656 'val' => $ticket->id,
657 ),
658 array(
659 'slug' => 'type',
660 'compare' => 'IN',
661 'val' => array( 'report', 'reply', 'note' ),
662 ),
663 array(
664 'slug' => 'is_active',
665 'compare' => '=',
666 'val' => 1,
667 ),
668 ),
669 'orderby' => 'id',
670 'order' => 'DESC',
671 );
672
673 $threads = WPSC_Thread::find( $filters );
674
675 if ( $threads['total_items'] ) {
676
677 $history = array_filter(
678 array_map(
679 fn ( $thread ) => $thread->get_history_macro(),
680 $threads['results']
681 )
682 );
683 $history = implode( '<div></div><br><hr><br><div></div>', $history );
684 return str_replace( '{{ticket_history_all_with_notes}}', $history, $str );
685
686 } else {
687
688 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
689 return str_replace( '{{ticket_history_all_with_notes}}', $history, $str );
690 }
691 }
692
693 /**
694 * Replace ticket history all with logs
695 *
696 * @param string $str - string to replace tags in.
697 * @param WPSC_Ticket $ticket - ticket object.
698 * @return string
699 */
700 public static function replace_ticket_history_all_with_logs( $str, $ticket ) {
701
702 $filters = array(
703 'meta_query' => array(
704 'relation' => 'AND',
705 array(
706 'slug' => 'ticket',
707 'compare' => '=',
708 'val' => $ticket->id,
709 ),
710 array(
711 'slug' => 'type',
712 'compare' => 'IN',
713 'val' => array( 'report', 'reply', 'log' ),
714 ),
715 array(
716 'slug' => 'is_active',
717 'compare' => '=',
718 'val' => 1,
719 ),
720 ),
721 'orderby' => 'id',
722 'order' => 'DESC',
723 );
724
725 $threads = WPSC_Thread::find( $filters );
726
727 if ( $threads['total_items'] ) {
728
729 $history = array_filter(
730 array_map(
731 fn ( $thread ) => $thread->get_history_macro(),
732 $threads['results']
733 )
734 );
735 $history = implode( '<br><hr><br>', $history );
736 return str_replace( '{{ticket_history_all_with_logs}}', $history, $str );
737
738 } else {
739
740 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
741 return str_replace( '{{ticket_history_all_with_logs}}', $history, $str );
742 }
743 }
744
745 /**
746 * Replace ticket history all with logs and notes
747 *
748 * @param string $str - string to replace tags in.
749 * @param WPSC_Ticket $ticket - ticket object.
750 * @return string
751 */
752 public static function replace_ticket_history_all_with_notes_and_logs( $str, $ticket ) {
753
754 $filters = array(
755 'meta_query' => array(
756 'relation' => 'AND',
757 array(
758 'slug' => 'ticket',
759 'compare' => '=',
760 'val' => $ticket->id,
761 ),
762 array(
763 'slug' => 'type',
764 'compare' => 'IN',
765 'val' => array( 'report', 'reply', 'note', 'log' ),
766 ),
767 array(
768 'slug' => 'is_active',
769 'compare' => '=',
770 'val' => 1,
771 ),
772 ),
773 'orderby' => 'id',
774 'order' => 'DESC',
775 );
776
777 $threads = WPSC_Thread::find( $filters );
778
779 if ( $threads['total_items'] ) {
780
781 $history = array_filter(
782 array_map(
783 fn ( $thread ) => $thread->get_history_macro(),
784 $threads['results']
785 )
786 );
787 $history = implode( '<br><hr><br>', $history );
788 return str_replace( '{{ticket_history_all_with_notes_and_logs}}', $history, $str );
789
790 } else {
791
792 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
793 return str_replace( '{{ticket_history_all_with_notes_and_logs}}', $history, $str );
794 }
795 }
796
797 /**
798 * Replace ticket notes history with logs
799 *
800 * @param string $str - string to replace tags in.
801 * @param WPSC_Ticket $ticket - ticket object.
802 * @return string
803 */
804 public static function replace_ticket_notes_history_with_logs( $str, $ticket ) {
805
806 $filters = array(
807 'meta_query' => array(
808 'relation' => 'AND',
809 array(
810 'slug' => 'ticket',
811 'compare' => '=',
812 'val' => $ticket->id,
813 ),
814 array(
815 'slug' => 'type',
816 'compare' => 'IN',
817 'val' => array( 'note', 'log' ),
818 ),
819 array(
820 'slug' => 'is_active',
821 'compare' => '=',
822 'val' => 1,
823 ),
824 ),
825 'orderby' => 'id',
826 'order' => 'DESC',
827 );
828
829 $threads = WPSC_Thread::find( $filters );
830
831 if ( $threads['total_items'] ) {
832
833 $history = array_filter(
834 array_map(
835 fn ( $thread ) => $thread->get_history_macro(),
836 $threads['results']
837 )
838 );
839 $history = implode( '<br><hr><br>', $history );
840 return str_replace( '{{ticket_notes_history_with_logs}}', $history, $str );
841
842 } else {
843
844 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
845 return str_replace( '{{ticket_notes_history_with_logs}}', $history, $str );
846 }
847 }
848
849 /**
850 * Replace ticket URL
851 *
852 * @param string $str - string to replace tags in.
853 * @param WPSC_Ticket $ticket - ticket object.
854 * @return string
855 */
856 public static function replace_ticket_url( $str, $ticket ) {
857
858 $ticket_url = '<a class="wpsc_link" href="' . $ticket->get_url() . '" target="_blank">' . $ticket->get_url() . '</a>';
859 $str = str_replace( '{{ticket_url}}', $ticket_url, $str );
860 return $str;
861 }
862
863 /**
864 * Replace ticket URL
865 *
866 * @param string $str - string to replace tags in.
867 * @param WPSC_Ticket $ticket - ticket object.
868 * @return string
869 */
870 public static function replace_ticket_url_plain( $str, $ticket ) {
871
872 $str = str_replace( '{{ticket_url_plain}}', $ticket->get_url(), $str );
873 return $str;
874 }
875
876 /**
877 * Replace ticket last reply
878 *
879 * @param string $str - string to replace tags in.
880 * @param WPSC_Ticket $ticket - ticket object.
881 * @return string
882 */
883 public static function replace_last_reply( $str, $ticket ) {
884
885 $thread = $ticket->get_last_reply();
886 if ( $thread ) {
887 $str = str_replace( '{{last_reply}}', $thread->get_printable_string(), $str );
888 } else {
889 $str = str_replace(
890 '{{last_reply}}',
891 esc_attr__( 'Not Applicable', 'supportcandy' ),
892 $str
893 );
894 }
895
896 return $str;
897 }
898
899 /**
900 * Replace ticket last reply user name
901 *
902 * @param string $str - string to replace tags in.
903 * @param WPSC_Ticket $ticket - ticket object.
904 * @return string
905 */
906 public static function replace_last_reply_user_name( $str, $ticket ) {
907
908 $thread = $ticket->get_last_reply();
909 if ( ! $thread ) {
910 return str_replace( '{{last_reply_user_name}}', '', $str );
911 }
912
913 $str = str_replace( '{{last_reply_user_name}}', $thread->customer->name, $str );
914 return $str;
915 }
916
917 /**
918 * Replace ticket last reply user email
919 *
920 * @param string $str - string to replace tags in.
921 * @param WPSC_Ticket $ticket - ticket object.
922 * @return string
923 */
924 public static function replace_last_reply_user_email( $str, $ticket ) {
925
926 $thread = $ticket->get_last_reply();
927 if ( ! $thread ) {
928 return str_replace( '{{last_reply_user_email}}', '', $str );
929 }
930
931 $str = str_replace( '{{last_reply_user_email}}', $thread->customer->email, $str );
932 return $str;
933 }
934
935 /**
936 * Replace ticket last reply user first name
937 *
938 * @param string $str - string to replace tags in.
939 * @param WPSC_Ticket $ticket - ticket object.
940 * @return string
941 */
942 public static function replace_last_reply_user_first_name( $str, $ticket ) {
943
944 $thread = $ticket->get_last_reply();
945 if ( ! $thread ) {
946 return str_replace( '{{last_reply_user_first_name}}', '', $str );
947 }
948
949 $first_name = $thread->customer->name;
950 if ( $thread->customer->user ) {
951 $first_name = get_user_meta( $thread->customer->user->ID, 'first_name', true );
952 if ( ! $first_name ) {
953 $first_name = $thread->customer->name;
954 }
955 }
956
957 $str = str_replace( '{{last_reply_user_first_name}}', $first_name, $str );
958 return $str;
959 }
960
961 /**
962 * Replace ticket last note
963 *
964 * @param string $str - string to replace tags in.
965 * @param WPSC_Ticket $ticket - ticket object.
966 * @return string
967 */
968 public static function replace_last_note( $str, $ticket ) {
969
970 $thread = $ticket->get_last_note();
971 if ( $thread ) {
972 $str = str_replace( '{{last_note}}', $thread->get_printable_string(), $str );
973 } else {
974 $str = str_replace(
975 '{{last_note}}',
976 esc_attr__( 'Not Applicable', 'supportcandy' ),
977 $str
978 );
979 }
980
981 return $str;
982 }
983
984 /**
985 * Replace ticket last note user name
986 *
987 * @param string $str - string to replace tags in.
988 * @param WPSC_Ticket $ticket - ticket object.
989 * @return string
990 */
991 public static function replace_last_note_user_name( $str, $ticket ) {
992
993 $thread = $ticket->get_last_note();
994 if ( ! $thread ) {
995 return str_replace( '{{last_note_user_name}}', '', $str );
996 }
997
998 $str = str_replace( '{{last_note_user_name}}', $thread->customer->name, $str );
999 return $str;
1000 }
1001
1002 /**
1003 * Replace ticket last note user email
1004 *
1005 * @param string $str - string to replace tags in.
1006 * @param WPSC_Ticket $ticket - ticket object.
1007 * @return string
1008 */
1009 public static function replace_last_note_user_email( $str, $ticket ) {
1010
1011 $thread = $ticket->get_last_note();
1012 if ( ! $thread ) {
1013 return str_replace( '{{last_note_user_email}}', '', $str );
1014 }
1015
1016 $str = str_replace( '{{last_note_user_email}}', $thread->customer->email, $str );
1017 return $str;
1018 }
1019
1020 /**
1021 * Replace ticket last note user first name
1022 *
1023 * @param string $str - string to replace tags in.
1024 * @param WPSC_Ticket $ticket - ticket object.
1025 * @return string
1026 */
1027 public static function replace_last_note_user_first_name( $str, $ticket ) {
1028
1029 $thread = $ticket->get_last_note();
1030 if ( ! $thread ) {
1031 return str_replace( '{{last_note_user_first_name}}', '', $str );
1032 }
1033
1034 $first_name = $thread->customer->name;
1035 if ( $thread->customer->user ) {
1036 $first_name = get_user_meta( $thread->customer->user->ID, 'first_name', true );
1037 if ( ! $first_name ) {
1038 $first_name = $thread->customer->name;
1039 }
1040 }
1041
1042 $str = str_replace( '{{last_note_user_first_name}}', $first_name, $str );
1043 return $str;
1044 }
1045
1046 /**
1047 * Replace current user name
1048 *
1049 * @param string $str - string to replace tags in.
1050 * @param WPSC_Ticket $ticket - ticket object.
1051 * @return string
1052 */
1053 public static function replace_current_user_name( $str, $ticket ) {
1054
1055 $current_user = WPSC_Current_User::$current_user;
1056 if ( $current_user->is_customer ) {
1057 $str = str_replace( '{{current_user_name}}', $current_user->customer->name, $str );
1058 } else {
1059 $str = str_replace( '{{current_user_name}}', esc_attr__( 'Not Applicable', 'supportcandy' ), $str );
1060 }
1061
1062 return $str;
1063 }
1064
1065 /**
1066 * Replace current user email
1067 *
1068 * @param string $str - string to replace tags in.
1069 * @param WPSC_Ticket $ticket - ticket object.
1070 * @return string
1071 */
1072 public static function replace_current_user_email( $str, $ticket ) {
1073
1074 $current_user = WPSC_Current_User::$current_user;
1075 if ( $current_user->is_customer ) {
1076 $str = str_replace( '{{current_user_email}}', $current_user->customer->email, $str );
1077 } else {
1078 $str = str_replace( '{{current_user_email}}', esc_attr__( 'Not Applicable', 'supportcandy' ), $str );
1079 }
1080
1081 return $str;
1082 }
1083
1084 /**
1085 * Replace current user first name
1086 *
1087 * @param string $str - string to replace tags in.
1088 * @param WPSC_Ticket $ticket - ticket object.
1089 * @return string
1090 */
1091 public static function replace_current_user_first_name( $str, $ticket ) {
1092
1093 $current_user = WPSC_Current_User::$current_user;
1094
1095 if ( $current_user->is_customer ) {
1096 $first_name = $current_user->customer->name;
1097 if ( $current_user->customer->user ) {
1098 $first_name = get_user_meta( $current_user->user->ID, 'first_name', true );
1099 if ( ! $first_name ) {
1100 $first_name = $current_user->customer->name;
1101 }
1102 }
1103 $str = str_replace( '{{current_user_first_name}}', $first_name, $str );
1104 } else {
1105 $str = str_replace( '{{current_user_first_name}}', esc_attr__( 'Not Applicable', 'supportcandy' ), $str );
1106 }
1107
1108 return $str;
1109 }
1110 }
1111 endif;
1112
1113 WPSC_Macros::init();
1114