PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.8
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.8
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.4.8, at includes/class-wpsc-macros.php

1,118 lines 30.2 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 'items_per_page' => 0,
548 'meta_query' => array(
549 'relation' => 'AND',
550 array(
551 'slug' => 'ticket',
552 'compare' => '=',
553 'val' => $ticket->id,
554 ),
555 array(
556 'slug' => 'type',
557 'compare' => 'IN',
558 'val' => array( 'report', 'reply' ),
559 ),
560 array(
561 'slug' => 'is_active',
562 'compare' => '=',
563 'val' => 1,
564 ),
565 ),
566 'orderby' => 'id',
567 'order' => 'DESC',
568 );
569
570 $threads = WPSC_Thread::find( $filters );
571
572 if ( $threads['total_items'] ) {
573
574 $history = array_filter(
575 array_map(
576 fn ( $thread ) => $thread->get_history_macro(),
577 $threads['results']
578 )
579 );
580 $history = implode( '<br><hr><br>', $history );
581 return str_replace( '{{ticket_history_all}}', $history, $str );
582
583 } else {
584
585 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
586 return str_replace( '{{ticket_history_all}}', $history, $str );
587 }
588 }
589
590 /**
591 * Replace ticket notes history
592 *
593 * @param string $str - string to replace tags in.
594 * @param WPSC_Ticket $ticket - ticket object.
595 * @return string
596 */
597 public static function replace_ticket_notes_history( $str, $ticket ) {
598
599 $filters = array(
600 'meta_query' => array(
601 'relation' => 'AND',
602 array(
603 'slug' => 'ticket',
604 'compare' => '=',
605 'val' => $ticket->id,
606 ),
607 array(
608 'slug' => 'type',
609 'compare' => '=',
610 'val' => 'note',
611 ),
612 array(
613 'slug' => 'is_active',
614 'compare' => '=',
615 'val' => 1,
616 ),
617 ),
618 'orderby' => 'id',
619 'order' => 'DESC',
620 );
621
622 $threads = WPSC_Thread::find( $filters );
623
624 if ( $threads['total_items'] ) {
625
626 $history = array_filter(
627 array_map(
628 fn ( $thread ) => $thread->get_history_macro(),
629 $threads['results']
630 )
631 );
632 $history = implode( '<br><hr><br>', $history );
633 return str_replace( '{{ticket_notes_history}}', $history, $str );
634
635 } else {
636
637 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
638 return str_replace( '{{ticket_notes_history}}', $history, $str );
639 }
640 }
641
642 /**
643 * Replace ticket history all with notes
644 *
645 * @param string $str - string to replace tags in.
646 * @param WPSC_Ticket $ticket - ticket object.
647 * @return string
648 */
649 public static function replace_ticket_history_all_with_notes( $str, $ticket ) {
650
651 $filters = array(
652 'items_per_page' => 0,
653 'meta_query' => array(
654 'relation' => 'AND',
655 array(
656 'slug' => 'ticket',
657 'compare' => '=',
658 'val' => $ticket->id,
659 ),
660 array(
661 'slug' => 'type',
662 'compare' => 'IN',
663 'val' => array( 'report', 'reply', 'note' ),
664 ),
665 array(
666 'slug' => 'is_active',
667 'compare' => '=',
668 'val' => 1,
669 ),
670 ),
671 'orderby' => 'id',
672 'order' => 'DESC',
673 );
674
675 $threads = WPSC_Thread::find( $filters );
676
677 if ( $threads['total_items'] ) {
678
679 $history = array_filter(
680 array_map(
681 fn ( $thread ) => $thread->get_history_macro(),
682 $threads['results']
683 )
684 );
685 $history = implode( '<div></div><br><hr><br><div></div>', $history );
686 return str_replace( '{{ticket_history_all_with_notes}}', $history, $str );
687
688 } else {
689
690 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
691 return str_replace( '{{ticket_history_all_with_notes}}', $history, $str );
692 }
693 }
694
695 /**
696 * Replace ticket history all with logs
697 *
698 * @param string $str - string to replace tags in.
699 * @param WPSC_Ticket $ticket - ticket object.
700 * @return string
701 */
702 public static function replace_ticket_history_all_with_logs( $str, $ticket ) {
703
704 $filters = array(
705 'items_per_page' => 0,
706 'meta_query' => array(
707 'relation' => 'AND',
708 array(
709 'slug' => 'ticket',
710 'compare' => '=',
711 'val' => $ticket->id,
712 ),
713 array(
714 'slug' => 'type',
715 'compare' => 'IN',
716 'val' => array( 'report', 'reply', 'log' ),
717 ),
718 array(
719 'slug' => 'is_active',
720 'compare' => '=',
721 'val' => 1,
722 ),
723 ),
724 'orderby' => 'id',
725 'order' => 'DESC',
726 );
727
728 $threads = WPSC_Thread::find( $filters );
729
730 if ( $threads['total_items'] ) {
731
732 $history = array_filter(
733 array_map(
734 fn ( $thread ) => $thread->get_history_macro(),
735 $threads['results']
736 )
737 );
738 $history = implode( '<br><hr><br>', $history );
739 return str_replace( '{{ticket_history_all_with_logs}}', $history, $str );
740
741 } else {
742
743 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
744 return str_replace( '{{ticket_history_all_with_logs}}', $history, $str );
745 }
746 }
747
748 /**
749 * Replace ticket history all with logs and notes
750 *
751 * @param string $str - string to replace tags in.
752 * @param WPSC_Ticket $ticket - ticket object.
753 * @return string
754 */
755 public static function replace_ticket_history_all_with_notes_and_logs( $str, $ticket ) {
756
757 $filters = array(
758 'items_per_page' => 0,
759 'meta_query' => array(
760 'relation' => 'AND',
761 array(
762 'slug' => 'ticket',
763 'compare' => '=',
764 'val' => $ticket->id,
765 ),
766 array(
767 'slug' => 'type',
768 'compare' => 'IN',
769 'val' => array( 'report', 'reply', 'note', 'log' ),
770 ),
771 array(
772 'slug' => 'is_active',
773 'compare' => '=',
774 'val' => 1,
775 ),
776 ),
777 'orderby' => 'id',
778 'order' => 'DESC',
779 );
780
781 $threads = WPSC_Thread::find( $filters );
782
783 if ( $threads['total_items'] ) {
784
785 $history = array_filter(
786 array_map(
787 fn ( $thread ) => $thread->get_history_macro(),
788 $threads['results']
789 )
790 );
791 $history = implode( '<br><hr><br>', $history );
792 return str_replace( '{{ticket_history_all_with_notes_and_logs}}', $history, $str );
793
794 } else {
795
796 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
797 return str_replace( '{{ticket_history_all_with_notes_and_logs}}', $history, $str );
798 }
799 }
800
801 /**
802 * Replace ticket notes history with logs
803 *
804 * @param string $str - string to replace tags in.
805 * @param WPSC_Ticket $ticket - ticket object.
806 * @return string
807 */
808 public static function replace_ticket_notes_history_with_logs( $str, $ticket ) {
809
810 $filters = array(
811 'meta_query' => array(
812 'relation' => 'AND',
813 array(
814 'slug' => 'ticket',
815 'compare' => '=',
816 'val' => $ticket->id,
817 ),
818 array(
819 'slug' => 'type',
820 'compare' => 'IN',
821 'val' => array( 'note', 'log' ),
822 ),
823 array(
824 'slug' => 'is_active',
825 'compare' => '=',
826 'val' => 1,
827 ),
828 ),
829 'orderby' => 'id',
830 'order' => 'DESC',
831 );
832
833 $threads = WPSC_Thread::find( $filters );
834
835 if ( $threads['total_items'] ) {
836
837 $history = array_filter(
838 array_map(
839 fn ( $thread ) => $thread->get_history_macro(),
840 $threads['results']
841 )
842 );
843 $history = implode( '<br><hr><br>', $history );
844 return str_replace( '{{ticket_notes_history_with_logs}}', $history, $str );
845
846 } else {
847
848 $history = esc_attr__( 'Not Applicable', 'supportcandy' );
849 return str_replace( '{{ticket_notes_history_with_logs}}', $history, $str );
850 }
851 }
852
853 /**
854 * Replace ticket URL
855 *
856 * @param string $str - string to replace tags in.
857 * @param WPSC_Ticket $ticket - ticket object.
858 * @return string
859 */
860 public static function replace_ticket_url( $str, $ticket ) {
861
862 $ticket_url = '<a class="wpsc_link" href="' . $ticket->get_url() . '" target="_blank">' . $ticket->get_url() . '</a>';
863 $str = str_replace( '{{ticket_url}}', $ticket_url, $str );
864 return $str;
865 }
866
867 /**
868 * Replace ticket URL
869 *
870 * @param string $str - string to replace tags in.
871 * @param WPSC_Ticket $ticket - ticket object.
872 * @return string
873 */
874 public static function replace_ticket_url_plain( $str, $ticket ) {
875
876 $str = str_replace( '{{ticket_url_plain}}', $ticket->get_url(), $str );
877 return $str;
878 }
879
880 /**
881 * Replace ticket last reply
882 *
883 * @param string $str - string to replace tags in.
884 * @param WPSC_Ticket $ticket - ticket object.
885 * @return string
886 */
887 public static function replace_last_reply( $str, $ticket ) {
888
889 $thread = $ticket->get_last_reply();
890 if ( $thread ) {
891 $str = str_replace( '{{last_reply}}', $thread->get_printable_string(), $str );
892 } else {
893 $str = str_replace(
894 '{{last_reply}}',
895 esc_attr__( 'Not Applicable', 'supportcandy' ),
896 $str
897 );
898 }
899
900 return $str;
901 }
902
903 /**
904 * Replace ticket last reply user name
905 *
906 * @param string $str - string to replace tags in.
907 * @param WPSC_Ticket $ticket - ticket object.
908 * @return string
909 */
910 public static function replace_last_reply_user_name( $str, $ticket ) {
911
912 $thread = $ticket->get_last_reply();
913 if ( ! $thread ) {
914 return str_replace( '{{last_reply_user_name}}', '', $str );
915 }
916
917 $str = str_replace( '{{last_reply_user_name}}', $thread->customer->name, $str );
918 return $str;
919 }
920
921 /**
922 * Replace ticket last reply user email
923 *
924 * @param string $str - string to replace tags in.
925 * @param WPSC_Ticket $ticket - ticket object.
926 * @return string
927 */
928 public static function replace_last_reply_user_email( $str, $ticket ) {
929
930 $thread = $ticket->get_last_reply();
931 if ( ! $thread ) {
932 return str_replace( '{{last_reply_user_email}}', '', $str );
933 }
934
935 $str = str_replace( '{{last_reply_user_email}}', $thread->customer->email, $str );
936 return $str;
937 }
938
939 /**
940 * Replace ticket last reply user first name
941 *
942 * @param string $str - string to replace tags in.
943 * @param WPSC_Ticket $ticket - ticket object.
944 * @return string
945 */
946 public static function replace_last_reply_user_first_name( $str, $ticket ) {
947
948 $thread = $ticket->get_last_reply();
949 if ( ! $thread ) {
950 return str_replace( '{{last_reply_user_first_name}}', '', $str );
951 }
952
953 $first_name = $thread->customer->name;
954 if ( $thread->customer->user ) {
955 $first_name = get_user_meta( $thread->customer->user->ID, 'first_name', true );
956 if ( ! $first_name ) {
957 $first_name = $thread->customer->name;
958 }
959 }
960
961 $str = str_replace( '{{last_reply_user_first_name}}', $first_name, $str );
962 return $str;
963 }
964
965 /**
966 * Replace ticket last note
967 *
968 * @param string $str - string to replace tags in.
969 * @param WPSC_Ticket $ticket - ticket object.
970 * @return string
971 */
972 public static function replace_last_note( $str, $ticket ) {
973
974 $thread = $ticket->get_last_note();
975 if ( $thread ) {
976 $str = str_replace( '{{last_note}}', $thread->get_printable_string(), $str );
977 } else {
978 $str = str_replace(
979 '{{last_note}}',
980 esc_attr__( 'Not Applicable', 'supportcandy' ),
981 $str
982 );
983 }
984
985 return $str;
986 }
987
988 /**
989 * Replace ticket last note user name
990 *
991 * @param string $str - string to replace tags in.
992 * @param WPSC_Ticket $ticket - ticket object.
993 * @return string
994 */
995 public static function replace_last_note_user_name( $str, $ticket ) {
996
997 $thread = $ticket->get_last_note();
998 if ( ! $thread ) {
999 return str_replace( '{{last_note_user_name}}', '', $str );
1000 }
1001
1002 $str = str_replace( '{{last_note_user_name}}', $thread->customer->name, $str );
1003 return $str;
1004 }
1005
1006 /**
1007 * Replace ticket last note user email
1008 *
1009 * @param string $str - string to replace tags in.
1010 * @param WPSC_Ticket $ticket - ticket object.
1011 * @return string
1012 */
1013 public static function replace_last_note_user_email( $str, $ticket ) {
1014
1015 $thread = $ticket->get_last_note();
1016 if ( ! $thread ) {
1017 return str_replace( '{{last_note_user_email}}', '', $str );
1018 }
1019
1020 $str = str_replace( '{{last_note_user_email}}', $thread->customer->email, $str );
1021 return $str;
1022 }
1023
1024 /**
1025 * Replace ticket last note user first name
1026 *
1027 * @param string $str - string to replace tags in.
1028 * @param WPSC_Ticket $ticket - ticket object.
1029 * @return string
1030 */
1031 public static function replace_last_note_user_first_name( $str, $ticket ) {
1032
1033 $thread = $ticket->get_last_note();
1034 if ( ! $thread ) {
1035 return str_replace( '{{last_note_user_first_name}}', '', $str );
1036 }
1037
1038 $first_name = $thread->customer->name;
1039 if ( $thread->customer->user ) {
1040 $first_name = get_user_meta( $thread->customer->user->ID, 'first_name', true );
1041 if ( ! $first_name ) {
1042 $first_name = $thread->customer->name;
1043 }
1044 }
1045
1046 $str = str_replace( '{{last_note_user_first_name}}', $first_name, $str );
1047 return $str;
1048 }
1049
1050 /**
1051 * Replace current user name
1052 *
1053 * @param string $str - string to replace tags in.
1054 * @param WPSC_Ticket $ticket - ticket object.
1055 * @return string
1056 */
1057 public static function replace_current_user_name( $str, $ticket ) {
1058
1059 $current_user = WPSC_Current_User::$current_user;
1060 if ( $current_user->is_customer ) {
1061 $str = str_replace( '{{current_user_name}}', $current_user->customer->name, $str );
1062 } else {
1063 $str = str_replace( '{{current_user_name}}', esc_attr__( 'Not Applicable', 'supportcandy' ), $str );
1064 }
1065
1066 return $str;
1067 }
1068
1069 /**
1070 * Replace current user email
1071 *
1072 * @param string $str - string to replace tags in.
1073 * @param WPSC_Ticket $ticket - ticket object.
1074 * @return string
1075 */
1076 public static function replace_current_user_email( $str, $ticket ) {
1077
1078 $current_user = WPSC_Current_User::$current_user;
1079 if ( $current_user->is_customer ) {
1080 $str = str_replace( '{{current_user_email}}', $current_user->customer->email, $str );
1081 } else {
1082 $str = str_replace( '{{current_user_email}}', esc_attr__( 'Not Applicable', 'supportcandy' ), $str );
1083 }
1084
1085 return $str;
1086 }
1087
1088 /**
1089 * Replace current user first name
1090 *
1091 * @param string $str - string to replace tags in.
1092 * @param WPSC_Ticket $ticket - ticket object.
1093 * @return string
1094 */
1095 public static function replace_current_user_first_name( $str, $ticket ) {
1096
1097 $current_user = WPSC_Current_User::$current_user;
1098
1099 if ( $current_user->is_customer ) {
1100 $first_name = $current_user->customer->name;
1101 if ( $current_user->customer->user ) {
1102 $first_name = get_user_meta( $current_user->user->ID, 'first_name', true );
1103 if ( ! $first_name ) {
1104 $first_name = $current_user->customer->name;
1105 }
1106 }
1107 $str = str_replace( '{{current_user_first_name}}', $first_name, $str );
1108 } else {
1109 $str = str_replace( '{{current_user_first_name}}', esc_attr__( 'Not Applicable', 'supportcandy' ), $str );
1110 }
1111
1112 return $str;
1113 }
1114 }
1115 endif;
1116
1117 WPSC_Macros::init();
1118