PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.9
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 / admin / email-notifications / class-wpsc-en-settings-tn.php

class-wpsc-en-settings-tn.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.9, at includes/admin/email-notifications/class-wpsc-en-settings-tn.php

966 lines 33.1 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_EN_Settings_TN' ) ) :
7
8 final class WPSC_EN_Settings_TN {
9
10 /**
11 * Ticket events
12 *
13 * @var array
14 */
15 public static $events = array();
16
17 /**
18 * Ignore current user recipient from these events
19 *
20 * @var array
21 */
22 public static $ignore_current_user = array();
23
24 /**
25 * Initialize this class
26 */
27 public static function init() {
28
29 // ignore cft.
30 add_action( 'init', array( __CLASS__, 'load_ignore_list' ) );
31
32 // list.
33 add_action( 'wp_ajax_wpsc_get_ticket_notifications', array( __CLASS__, 'get_ticket_notifications' ) );
34
35 // add new.
36 add_action( 'wp_ajax_wpsc_en_get_add_ticket_notification', array( __CLASS__, 'get_add_ticket_notification' ) );
37 add_action( 'wp_ajax_wpsc_en_set_add_ticket_notification', array( __CLASS__, 'set_add_ticket_notification' ) );
38
39 // edit.
40 add_action( 'wp_ajax_wpsc_en_get_edit_ticket_notification', array( __CLASS__, 'get_edit_ticket_notification' ) );
41 add_action( 'wp_ajax_wpsc_en_set_edit_ticket_notification', array( __CLASS__, 'set_edit_ticket_notification' ) );
42
43 // delete.
44 add_action( 'wp_ajax_wpsc_en_delete_ticket_notification', array( __CLASS__, 'delete_ticket_notification' ) );
45
46 // clone.
47 add_action( 'wp_ajax_wpsc_en_get_clone_ticket_notification', array( __CLASS__, 'get_clone_ticket_notification' ) );
48 add_action( 'wp_ajax_wpsc_en_set_clone_ticket_notification', array( __CLASS__, 'set_clone_ticket_notification' ) );
49
50 // enable or disable.
51 add_action( 'wp_ajax_wpsc_en_enable_disable_template', array( __CLASS__, 'enable_disable_template' ) );
52
53 // filter conditions.
54 add_filter( 'wpsc_en_conditions', array( __CLASS__, 'filter_conditions' ) );
55
56 // filter triggers.
57 add_filter( 'wpsc_en_triggers', array( __CLASS__, 'filter_triggers' ) );
58 }
59
60 /**
61 * Set ignore custom field types for email notifications
62 *
63 * @return void
64 */
65 public static function load_ignore_list() {
66
67 self::$ignore_current_user = apply_filters(
68 'wpsc_en_ignore_current_user_recipient_events',
69 array( 'create-ticket' )
70 );
71 }
72
73 /**
74 * Ticket notification list
75 *
76 * @return void
77 */
78 public static function get_ticket_notifications() {
79
80 if ( ! WPSC_Functions::is_site_admin() ) {
81 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
82 }
83
84 $email_templates = get_option( 'wpsc-email-templates', array() );
85 $unique_id = uniqid( 'wpsc_' );?>
86 <div class="wpsc-setting-header">
87 <h2><?php esc_attr_e( 'Ticket Notifications', 'supportcandy' ); ?></h2>
88 </div>
89 <div class="wpsc-setting-section-body">
90 <div class="wpsc-dock-container">
91 <?php
92 printf(
93 /* translators: Click here to see the documentation */
94 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
95 '<a href="https://supportcandy.net/docs/ticket-notifications/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
96 );
97 ?>
98 </div>
99 <table class="ticket-notification-list wpsc-setting-tbl">
100 <thead>
101 <tr>
102 <th><?php esc_attr_e( 'Title', 'supportcandy' ); ?></th>
103 <th><?php esc_attr_e( 'Trigger', 'supportcandy' ); ?></th>
104 <th><?php esc_attr_e( 'Status', 'supportcandy' ); ?></th>
105 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
106 </tr>
107 </thead>
108 <tbody>
109 <?php
110 if ( $email_templates ) :
111 foreach ( $email_templates as $index => $et ) :
112 if ( ! array_key_exists( $et['event'], WPSC_Triggers::$triggers ) ) {
113 continue;
114 }
115 ?>
116 <tr>
117 <td><?php echo esc_attr( $et['title'] ); ?></td>
118 <td><?php echo esc_attr( WPSC_Triggers::$triggers[ $et['event'] ] ); ?></td>
119 <td><?php echo esc_attr( $et['is_enable'] ) == 1 ? esc_attr__( 'Enabled', 'supportcandy' ) : esc_attr__( 'Disabled', 'supportcandy' ); ?></td>
120 <td>
121 <?php
122 if ( $et['is_enable'] ) {
123 ?>
124 <span class="wpsc-link" onclick="wpsc_en_enable_disable_template(<?php echo esc_attr( $index ); ?>, '0', '<?php echo esc_attr( wp_create_nonce( 'wpsc_en_enable_disable_template' ) ); ?>')"><?php esc_attr_e( 'Disable', 'supportcandy' ); ?></span> |
125 <?php
126 } else {
127 ?>
128 <span class="wpsc-link" onclick="wpsc_en_enable_disable_template(<?php echo esc_attr( $index ); ?>, '1', '<?php echo esc_attr( wp_create_nonce( 'wpsc_en_enable_disable_template' ) ); ?>')"><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></span> |
129 <?php
130 }
131 ?>
132 <span class="wpsc-link" onclick="wpsc_en_get_edit_ticket_notification(<?php echo esc_attr( $index ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_en_get_edit_ticket_notification' ) ); ?>')"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></span> |
133 <span class="wpsc-link" onclick="wpsc_en_get_clone_ticket_notification(<?php echo esc_attr( $index ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_en_get_clone_ticket_notification' ) ); ?>')"><?php esc_attr_e( 'Clone', 'supportcandy' ); ?></span> |
134 <span class="wpsc-link" onclick="wpsc_en_delete_ticket_notification(<?php echo esc_attr( $index ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_en_delete_ticket_notification' ) ); ?>')"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></span>
135 </td>
136 </tr>
137 <?php
138 endforeach;
139 endif;
140 ?>
141 </tbody>
142 </table>
143 <script>
144 jQuery('table.ticket-notification-list').DataTable({
145 ordering: false,
146 pageLength: 20,
147 bLengthChange: false,
148 columnDefs: [
149 { targets: -1, searchable: false },
150 { targets: '_all', className: 'dt-left' }
151 ],
152 layout: {
153 topStart: {
154 buttons: [
155 {
156 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
157 className: 'wpsc-button small primary',
158 action: function ( e, dt, node, config ) {
159
160 wpsc_show_modal();
161 var data = { action: 'wpsc_en_get_add_ticket_notification' };
162 jQuery.post(
163 supportcandy.ajax_url,
164 data,
165 function (response) {
166
167 jQuery( '.wpsc-modal-header' ).text( response.title );
168 jQuery( '.wpsc-modal-body' ).html( response.body );
169 jQuery( '.wpsc-modal-footer' ).html( response.footer );
170
171 wpsc_show_modal_inner_container();
172 }
173 );
174 }
175 }
176 ],
177 },
178 },
179 language: supportcandy.translations.datatables
180 });
181 </script>
182 </div>
183 <?php
184 wp_die();
185 }
186
187 /**
188 * Get add ticket notification modal
189 *
190 * @return void
191 */
192 public static function get_add_ticket_notification() {
193
194 if ( ! WPSC_Functions::is_site_admin() ) {
195 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
196 }
197
198 $title = esc_attr__( 'Add new', 'supportcandy' );
199 $roles = get_option( 'wpsc-agent-roles', array() );
200 ob_start();
201 ?>
202 <form action="#" onsubmit="return false;" class="wpsc-frm-add-en">
203 <div class="wpsc-input-group">
204 <div class="label-container">
205 <label for="">
206 <?php esc_attr_e( 'Title', 'supportcandy' ); ?>
207 <span class="required-char">*</span>
208 </label>
209 </div>
210 <input id="title" type="text" name="title" autocomplete="off">
211 </div>
212 <?php WPSC_Triggers::print( 'event', 'wpsc_en_triggers', '', true ); ?>
213 <?php do_action( 'wpsc_get_add_ticket_notification' ); ?>
214 <input type="hidden" name="action" value="wpsc_en_set_add_ticket_notification">
215 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_en_set_add_ticket_notification' ) ); ?>">
216 </form>
217 <?php
218 $body = ob_get_clean();
219
220 ob_start();
221 ?>
222 <button class="wpsc-button small primary" onclick="wpsc_en_set_add_ticket_notification(this);">
223 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
224 </button>
225 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
226 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
227 </button>
228 <?php
229 do_action( 'wpsc_get_edit_agent_footer' );
230 $footer = ob_get_clean();
231
232 $response = array(
233 'title' => $title,
234 'body' => $body,
235 'footer' => $footer,
236 );
237 wp_send_json( $response );
238 }
239
240 /**
241 * Set add ticket notification
242 *
243 * @return void
244 */
245 public static function set_add_ticket_notification() {
246
247 if ( check_ajax_referer( 'wpsc_en_set_add_ticket_notification', '_ajax_nonce', false ) != 1 ) {
248 wp_send_json_error( 'Unauthorized request!', 401 );
249 }
250
251 if ( ! WPSC_Functions::is_site_admin() ) {
252 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
253 }
254
255 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '';
256 if ( ! $title ) {
257 wp_send_json_error( 'Bad request', 400 );
258 }
259
260 $event = isset( $_POST['event'] ) ? sanitize_text_field( wp_unslash( $_POST['event'] ) ) : '';
261 if ( ! $event || ! WPSC_Triggers::is_valid( 'wpsc_en_triggers', $event ) ) {
262 wp_send_json_error( 'Bad request', 400 );
263 }
264
265 $et = array(
266 'is_enable' => 1,
267 'title' => $title,
268 'event' => $event,
269 'conditions' => '',
270 'subject' => '',
271 'body' => array(
272 'text' => '',
273 'editor' => 'html',
274 ),
275 'to' => array(
276 'general-recipients' => array(),
277 'agent-roles' => array(),
278 'custom' => array(),
279 ),
280 'cc' => array(
281 'general-recipients' => array(),
282 'agent-roles' => array(),
283 'custom' => array(),
284 ),
285 'bcc' => array(
286 'general-recipients' => array(),
287 'agent-roles' => array(),
288 'custom' => array(),
289 ),
290 );
291
292 $email_templates = get_option( 'wpsc-email-templates', array() );
293 if ( count( $email_templates ) == 0 ) {
294
295 $email_templates[1] = $et;
296
297 } else {
298
299 $email_templates[] = $et;
300 }
301 update_option( 'wpsc-email-templates', $email_templates );
302
303 end( $email_templates );
304 $index = key( $email_templates );
305
306 $nonce = wp_create_nonce( 'wpsc_en_get_edit_ticket_notification' );
307 wp_send_json(
308 array(
309 'index' => $index,
310 'nonce' => $nonce,
311 )
312 );
313 wp_die();
314 }
315
316 /**
317 * Get edit email notification
318 *
319 * @return void
320 */
321 public static function get_edit_ticket_notification() {
322
323 if ( check_ajax_referer( 'wpsc_en_get_edit_ticket_notification', '_ajax_nonce', false ) != 1 ) {
324 wp_send_json_error( 'Unauthorized request!', 401 );
325 }
326
327 if ( ! WPSC_Functions::is_site_admin() ) {
328 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
329 }
330
331 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : 0;
332 if ( ! $template_id ) {
333 wp_send_json_error( 'Bad request', 400 );
334 }
335
336 $email_templates = get_option( 'wpsc-email-templates' );
337 $et = array_key_exists( $template_id, $email_templates ) ? $email_templates[ $template_id ] : null;
338 if ( ! $et ) {
339 wp_send_json_error( 'Bad request', 400 );
340 }
341 ?>
342
343 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-en">
344
345 <div class="wpsc-input-group">
346 <div class="label-container">
347 <label for="">
348 <?php esc_attr_e( 'Title', 'supportcandy' ); ?>
349 <span class="required-char">*</span>
350 </label>
351 </div>
352 <input type="text" name="title" value="<?php echo esc_attr( $et['title'] ); ?>" autocomplete="off">
353 </div>
354
355 <?php WPSC_Triggers::print( 'event', 'wpsc_en_triggers', $et['event'], true, '', true ); ?>
356
357 <?php WPSC_Ticket_Conditions::print( 'conditions', 'wpsc_en_conditions', $et['conditions'] ); ?>
358
359 <div class="wpsc-input-group">
360 <div class="label-container">
361 <label for="">
362 <?php esc_attr_e( 'Subject', 'supportcandy' ); ?>
363 <span class="required-char">*</span>
364 </label>
365 </div>
366 <?php
367 $en_tn_subject = $et['subject'] ? WPSC_Translations::get( 'wpsc-en-tn-subject-' . $template_id, stripslashes( $et['subject'] ) ) : stripslashes( $et['subject'] );
368 ?>
369 <input type="text" name="subject" value="<?php echo esc_attr( $en_tn_subject ); ?>" autocomplete="off">
370 </div>
371
372 <div class="wpsc-input-group">
373 <div class="label-container">
374 <label for="">
375 <?php esc_attr_e( 'Body', 'supportcandy' ); ?>
376 <span class="required-char">*</span>
377 </label>
378 </div>
379 <div class="textarea-container ">
380 <div class = "wpsc_tinymce_editor_btns">
381 <div class="inner-container">
382 <button class="visual wpsc-switch-editor <?php echo esc_attr( $et['body']['editor'] ) == 'html' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_tinymce(this, 'wpsc-en-body','wpsc_en_body');"><?php esc_attr_e( 'Visual', 'supportcandy' ); ?></button>
383 <button class="text wpsc-switch-editor <?php echo esc_attr( $et['body']['editor'] ) == 'text' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_textarea(this, 'wpsc-en-body')"><?php esc_attr_e( 'Text', 'supportcandy' ); ?></button>
384 </div>
385 </div>
386 <?php
387 $en_tn_body = $et['body']['text'] ? WPSC_Translations::get( 'wpsc-en-tn-body-' . $template_id, stripslashes( $et['body']['text'] ) ) : stripslashes( $et['body']['text'] );
388 ?>
389 <textarea id="wpsc-en-body" name="body" class="wpsc_textarea"><?php echo esc_attr( $en_tn_body ); ?></textarea>
390 <div class="wpsc-it-editor-action-container">
391 <div class="actions">
392 <div class="wpsc-editor-actions">
393 <span class="wpsc-link" onclick="wpsc_get_macros()"><?php esc_attr_e( 'Insert Macro', 'supportcandy' ); ?></span>
394 </div>
395 </div>
396 </div>
397 </div>
398 <script>
399 <?php
400 if ( $et['body']['editor'] == 'html' ) :
401 ?>
402 jQuery('.wpsc-switch-editor.visual').trigger('click');
403 <?php
404 endif;
405 ?>
406
407 function wpsc_get_tinymce(el, selector, body_id){
408 jQuery(el).parent().find('.text').removeClass('active');
409 jQuery(el).addClass('active');
410 tinymce.remove('#'+selector);
411 tinymce.init({
412 selector:'#'+selector,
413 body_id: body_id,
414 menubar: false,
415 statusbar: false,
416 height : '200',
417 plugins: [
418 'lists link image directionality'
419 ],
420 image_advtab: true,
421 toolbar: 'bold italic underline blockquote | alignleft aligncenter alignright | bullist numlist | rtl | link image',
422 directionality: '<?php echo is_rtl() ? 'rtl' : 'ltr'; ?>',
423 branding: false,
424 autoresize_bottom_margin: 20,
425 browser_spellcheck : true,
426 relative_urls : false,
427 remove_script_host : false,
428 convert_urls : false,
429 setup: function (editor) {
430 }
431 });
432 jQuery('#editor').val('html');
433 }
434
435 function wpsc_get_textarea(el, selector){
436 jQuery(el).parent().find('.visual').removeClass('active');
437 jQuery(el).addClass('active');
438 tinymce.remove('#'+selector);
439 jQuery('#editor').val('text');
440 }
441 </script>
442 </div>
443
444 <div class="wpsc-input-group">
445 <div class="label-container">
446 <label for="">
447 <?php esc_attr_e( 'To address', 'supportcandy' ); ?>
448 <span class="required-char">*</span>
449 </label>
450 </div>
451 <?php echo esc_attr( self::print_recipient_options( $et, 'to' ) ); ?>
452 </div>
453
454 <div class="wpsc-input-group">
455 <div class="label-container">
456 <label for="">CC</label>
457 </div>
458 <?php echo esc_attr( self::print_recipient_options( $et, 'cc' ) ); ?>
459 </div>
460
461 <div class="wpsc-input-group">
462 <div class="label-container">
463 <label for="">BCC</label>
464 </div>
465 <?php echo esc_attr( self::print_recipient_options( $et, 'bcc' ) ); ?>
466 </div>
467
468 <?php do_action( 'wpsc_get_edit_ticket_notification', $et ); ?>
469 <input type="hidden" name="action" value="wpsc_en_set_edit_ticket_notification">
470 <input type="hidden" name="template_id" value="<?php echo esc_attr( $template_id ); ?>">
471 <input type="hidden" id="editor" name="editor" value="<?php echo esc_attr( $et['body']['editor'] ); ?>">
472 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_en_set_edit_ticket_notification' ) ); ?>">
473
474 </form>
475
476 <div class="setting-footer-actions">
477 <button
478 class="wpsc-button normal primary"
479 onclick="wpsc_en_set_edit_ticket_notification();">
480 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
481 </button>
482 <button
483 class="wpsc-button normal secondary"
484 onclick="wpsc_get_ticket_notifications();">
485 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
486 </button>
487 </div>
488 <?php
489
490 wp_die();
491 }
492
493 /**
494 * Set edit ticket notification
495 */
496 public static function set_edit_ticket_notification() {
497
498 if ( check_ajax_referer( 'wpsc_en_set_edit_ticket_notification', '_ajax_nonce', false ) != 1 ) {
499 wp_send_json_error( 'Unauthorized request!', 401 );
500 }
501
502 if ( ! WPSC_Functions::is_site_admin() ) {
503 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
504 }
505
506 // template id.
507 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : 0;
508 if ( ! $template_id ) {
509 wp_send_json_error( 'Bad request', 400 );
510 }
511
512 // template.
513 $email_templates = get_option( 'wpsc-email-templates' );
514 $et = array_key_exists( $template_id, $email_templates ) ? $email_templates[ $template_id ] : null;
515 if ( ! $et ) {
516 wp_send_json_error( 'Bad request', 400 );
517 }
518
519 // title.
520 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '';
521 if ( ! $title ) {
522 wp_send_json_error( 'Bad request', 400 );
523 }
524 $et['title'] = $title;
525
526 // conditions.
527 $conditions = isset( $_POST['conditions'] ) ? sanitize_text_field( wp_unslash( $_POST['conditions'] ) ) : '';
528 if ( ! WPSC_Ticket_Conditions::is_valid_input_conditions( 'wpsc_en_conditions', $conditions ) ) {
529 wp_send_json_error( 'Bad request', 400 );
530 }
531 $et['conditions'] = $conditions;
532
533 // subject.
534 $subject = isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : '';
535 if ( ! $subject ) {
536 wp_send_json_error( 'Bad request', 400 );
537 }
538 $et['subject'] = $subject;
539
540 // body.
541 $body = isset( $_POST['body'] ) ? wp_kses_post( wp_unslash( $_POST['body'] ) ) : '';
542 if ( ! $body ) {
543 wp_send_json_error( 'Bad request', 400 );
544 }
545 $et['body'] = array(
546 'text' => $body,
547 'editor' => isset( $_POST['editor'] ) ? sanitize_text_field( wp_unslash( $_POST['editor'] ) ) : 'html',
548 );
549
550 // to.
551 $general_recipients = isset( $_POST['to'] ) && isset( $_POST['to']['general-recipients'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['to']['general-recipients'] ) ) ) : array();
552 $agent_roles = isset( $_POST['to'] ) && isset( $_POST['to']['agent-roles'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['to']['agent-roles'] ) ) ) : array();
553 $custom = isset( $_POST['to'] ) && isset( $_POST['to']['custom'] ) ? sanitize_textarea_field( wp_unslash( $_POST['to']['custom'] ) ) : '';
554 $custom = array_unique( array_filter( array_map( 'sanitize_email', explode( PHP_EOL, $custom ) ) ) );
555 if ( ! ( $general_recipients || $agent_roles || $custom ) ) {
556 wp_send_json_error( 'Bad request', 400 );
557 }
558 $et['to'] = array(
559 'general-recipients' => $general_recipients,
560 'agent-roles' => $agent_roles,
561 'custom' => $custom,
562 );
563
564 // cc.
565 $general_recipients = isset( $_POST['cc'] ) && isset( $_POST['cc']['general-recipients'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['cc']['general-recipients'] ) ) ) : array();
566 $agent_roles = isset( $_POST['cc'] ) && isset( $_POST['cc']['agent-roles'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['cc']['agent-roles'] ) ) ) : array();
567 $custom = isset( $_POST['cc'] ) && isset( $_POST['cc']['custom'] ) ? sanitize_textarea_field( wp_unslash( $_POST['cc']['custom'] ) ) : '';
568 $custom = array_unique( array_filter( array_map( 'sanitize_email', explode( PHP_EOL, $custom ) ) ) );
569 $et['cc'] = array(
570 'general-recipients' => $general_recipients,
571 'agent-roles' => $agent_roles,
572 'custom' => $custom,
573 );
574
575 // bcc.
576 $general_recipients = isset( $_POST['bcc'] ) && isset( $_POST['bcc']['general-recipients'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['bcc']['general-recipients'] ) ) ) : array();
577 $agent_roles = isset( $_POST['bcc'] ) && isset( $_POST['bcc']['agent-roles'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['bcc']['agent-roles'] ) ) ) : array();
578 $custom = isset( $_POST['bcc'] ) && isset( $_POST['bcc']['custom'] ) ? sanitize_textarea_field( wp_unslash( $_POST['bcc']['custom'] ) ) : '';
579 $custom = array_unique( array_filter( array_map( 'sanitize_email', explode( PHP_EOL, $custom ) ) ) );
580 $et['bcc'] = array(
581 'general-recipients' => $general_recipients,
582 'agent-roles' => $agent_roles,
583 'custom' => $custom,
584 );
585
586 $email_templates[ $template_id ] = apply_filters( 'wpsc_en_set_edit_et', $et );
587 update_option( 'wpsc-email-templates', $email_templates );
588
589 // remove string translations.
590 WPSC_Translations::remove( 'wpsc-en-tn-subject-' . $template_id );
591 WPSC_Translations::remove( 'wpsc-en-tn-body-' . $template_id );
592
593 // add string translations.
594 WPSC_Translations::add( 'wpsc-en-tn-subject-' . $template_id, stripslashes( $et['subject'] ) );
595 WPSC_Translations::add( 'wpsc-en-tn-body-' . $template_id, stripslashes( $et['body']['text'] ) );
596
597 wp_die();
598 }
599
600 /**
601 * Print recipient options for "to", "cc" and "bcc"
602 *
603 * @param array $et -email templete.
604 * @param string $type - one of "to", "cc" and "bcc".
605 * @return void
606 */
607 public static function print_recipient_options( $et, $type ) {
608
609 // general recipients.
610 $gereral_recipients = array(
611 'customer' => esc_attr__( 'Customer', 'supportcandy' ),
612 'assignee' => esc_attr__( 'Assignee', 'supportcandy' ),
613 );
614
615 if ( $et['event'] == 'change-assignee' ) {
616 $gereral_recipients['prev-assignee'] = esc_attr__( 'Previous Assignee', 'supportcandy' );
617 }
618
619 if ( ! in_array( $et['event'], self::$ignore_current_user ) ) {
620 $gereral_recipients['current-user'] = esc_attr__( 'Current User', 'supportcandy' );
621 }
622
623 $gereral_recipients['add-recipients'] = esc_attr__( 'Additional Recipients', 'supportcandy' );
624 $gereral_recipients = apply_filters( 'wpsc_en_general_recipients', $gereral_recipients );
625
626 // agent roles.
627 $agent_roles = get_option( 'wpsc-agent-roles' );
628 $unique_id = uniqid( 'wpsc_' );
629
630 $etgr = $et[ $type ]['general-recipients'];
631 $etar = $et[ $type ]['agent-roles'];
632 $etc = implode( PHP_EOL, $et[ $type ]['custom'] );
633 ?>
634
635 <div style="border: none;box-shadow: 0 0 3px 0 #a4b0be;border-radius: 5px;padding: 15px 15px 0 15px; background-color:#eee;">
636 <div class="wpsc-input-group">
637 <div class="label-container"><label for=""><?php esc_attr_e( 'General Recipients', 'supportcandy' ); ?></label></div>
638 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $type ); ?>[general-recipients][]" multiple>
639 <?php
640 foreach ( $gereral_recipients as $key => $label ) {
641 $selected = in_array( $key, $etgr ) ? 'selected' : ''
642 ?>
643 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $label ); ?></option>
644 <?php
645 }
646 ?>
647 </select>
648 </div>
649 <div class="wpsc-input-group">
650 <div class="label-container"><label for=""><?php esc_attr_e( 'Agent Roles', 'supportcandy' ); ?></label></div>
651 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $type ); ?>[agent-roles][]" multiple>
652 <?php
653 foreach ( $agent_roles as $key => $role ) {
654 $selected = in_array( $key, $etar ) ? 'selected' : ''
655 ?>
656 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
657 <?php
658 }
659 ?>
660 </select>
661 </div>
662 <div class="wpsc-input-group">
663 <div class="label-container"><label for=""><?php esc_attr_e( 'Custom Email Addresses (one per line)', 'supportcandy' ); ?></label></div>
664 <textarea name="<?php echo esc_attr( $type ); ?>[custom]"><?php echo esc_attr( $etc ); ?></textarea>
665 </div>
666 </div>
667 <script type="text/javascript">
668 jQuery('.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
669 allowClear: true,
670 placeholder: "<?php esc_attr_e( 'Click to select options', 'supportcandy' ); ?>"
671 });
672 </script>
673 <?php
674 }
675
676 /**
677 * Delete email notification
678 *
679 * @return void
680 */
681 public static function delete_ticket_notification() {
682
683 if ( check_ajax_referer( 'wpsc_en_delete_ticket_notification', '_ajax_nonce', false ) != 1 ) {
684 wp_send_json_error( 'Unauthorized request!', 401 );
685 }
686
687 if ( ! WPSC_Functions::is_site_admin() ) {
688 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
689 }
690
691 // template id.
692 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : 0;
693 if ( ! $template_id ) {
694 wp_send_json_error( 'Bad request', 400 );
695 }
696
697 // template.
698 $email_templates = get_option( 'wpsc-email-templates' );
699 $et = array_key_exists( $template_id, $email_templates ) ? $email_templates[ $template_id ] : null;
700 if ( ! $et ) {
701 wp_send_json_error( 'Bad request', 400 );
702 }
703
704 unset( $email_templates[ $template_id ] );
705 update_option( 'wpsc-email-templates', $email_templates );
706
707 // remove string translations.
708 WPSC_Translations::remove( 'wpsc-en-tn-body-' . $template_id );
709 WPSC_Translations::remove( 'wpsc-en-tn-subject-' . $template_id );
710
711 wp_die();
712 }
713
714 /**
715 * Enable or Disable email notification
716 *
717 * @return void
718 */
719 public static function enable_disable_template() {
720
721 if ( check_ajax_referer( 'wpsc_en_enable_disable_template', '_ajax_nonce', false ) != 1 ) {
722 wp_send_json_error( 'Unauthorized request!', 401 );
723 }
724
725 if ( ! WPSC_Functions::is_site_admin() ) {
726 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
727 }
728
729 // template id.
730 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : 0;
731 if ( ! $template_id ) {
732 wp_send_json_error( 'Bad request', 400 );
733 }
734
735 // status.
736 $status = isset( $_POST['status'] ) ? intval( $_POST['status'] ) : 0;
737
738 $email_templates = get_option( 'wpsc-email-templates' );
739 $et = array_key_exists( $template_id, $email_templates ) ? $email_templates[ $template_id ] : null;
740 if ( ! $et ) {
741 wp_send_json_error( 'Bad request', 400 );
742 }
743
744 $et['is_enable'] = $status;
745 $email_templates[ $template_id ] = apply_filters( 'wpsc_en_enable_disable_et', $et );
746 update_option( 'wpsc-email-templates', $email_templates );
747
748 wp_die();
749 }
750
751 /**
752 * Filter conditions for email templates
753 *
754 * @param array $conditions - all possible ticket conditions.
755 * @return array
756 */
757 public static function filter_conditions( $conditions ) {
758
759 $ignore_list = apply_filters(
760 'wpsc_en_conditions_ignore_list',
761 array(
762 'cft' => array(
763 'df_last_reply_by',
764 'df_last_reply_on',
765 'cf_woo_order',
766 'cf_woo_subscription',
767 'cf_edd_order',
768 'cf_tutor_order',
769 'cf_learnpress_order',
770 'cf_lifter_order',
771 ), // custom field types.
772 'other' => array(), // other(custom) condition slug.
773 )
774 );
775
776 foreach ( $conditions as $slug => $item ) {
777
778 if ( $item['type'] == 'cf' ) {
779
780 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
781 if ( in_array( $cf->type::$slug, $ignore_list['cft'] ) ) {
782 unset( $conditions[ $slug ] );
783 }
784 } elseif ( in_array( $slug, $ignore_list['other'] ) ) {
785 unset( $conditions[ $slug ] );
786 }
787 }
788
789 return $conditions;
790 }
791
792 /**
793 * Filter triggers for email notification templates
794 *
795 * @param array $triggers - all possible triggers.
796 * @return array
797 */
798 public static function filter_triggers( $triggers ) {
799
800 $ignore_list = apply_filters( 'wpsc_en_triggers_ignore_list', array() );
801 foreach ( $triggers as $key => $val ) {
802 if ( in_array( $key, $ignore_list ) ) {
803 unset( $triggers[ $key ] );
804 }
805 }
806 return $triggers;
807 }
808
809 /**
810 * Get add clone ticket notification modal
811 *
812 * @return void
813 */
814 public static function get_clone_ticket_notification() {
815
816 if ( check_ajax_referer( 'wpsc_en_get_clone_ticket_notification', '_ajax_nonce', false ) != 1 ) {
817 wp_send_json_error( 'Unauthorized request!', 401 );
818 }
819
820 if ( ! WPSC_Functions::is_site_admin() ) {
821 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
822 }
823
824 // template id.
825 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : 0;
826 if ( ! $template_id ) {
827 wp_send_json_error( 'Bad request', 400 );
828 }
829
830 $email_templates = get_option( 'wpsc-email-templates' );
831 $et = array_key_exists( $template_id, $email_templates ) ? $email_templates[ $template_id ] : null;
832 if ( ! $et ) {
833 wp_send_json_error( 'Bad request', 400 );
834 }
835
836 $title = esc_attr__( 'Clone notification', 'supportcandy' );
837 ob_start();
838 ?>
839 <form action="#" onsubmit="return false;" class="wpsc-en-add-clone">
840 <div class="wpsc-input-group">
841 <div class="label-container">
842 <label for="">
843 <?php esc_attr_e( 'Title', 'supportcandy' ); ?>
844 <span class="required-char">*</span>
845 </label>
846 </div>
847 <input type="text" name="title" value="<?php echo esc_attr( $et['title'] ); ?>" autocomplete="off">
848 </div>
849 <?php WPSC_Triggers::print( 'event', 'wpsc_en_triggers', $et['event'], true, '', false ); ?>
850 <?php do_action( 'wpsc_get_clone_ticket_notification' ); ?>
851 <input type="hidden" name="template_id" value="<?php echo esc_attr( $template_id ); ?>">
852 <input type="hidden" name="action" value="wpsc_en_set_clone_ticket_notification">
853 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_en_set_clone_ticket_notification' ) ); ?>">
854 </form>
855 <?php
856 $body = ob_get_clean();
857
858 ob_start();
859 ?>
860 <button class="wpsc-button small primary" onclick="wpsc_en_set_clone_ticket_notification(this);">
861 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
862 </button>
863 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
864 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
865 </button>
866 <?php
867 $footer = ob_get_clean();
868
869 $response = array(
870 'title' => $title,
871 'body' => $body,
872 'footer' => $footer,
873 );
874 wp_send_json( $response );
875 }
876
877 /**
878 * Set clone ticket notification
879 *
880 * @return void
881 */
882 public static function set_clone_ticket_notification() {
883
884 if ( check_ajax_referer( 'wpsc_en_set_clone_ticket_notification', '_ajax_nonce', false ) != 1 ) {
885 wp_send_json_error( 'Unauthorized request!', 401 );
886 }
887
888 if ( ! WPSC_Functions::is_site_admin() ) {
889 wp_send_json_error( 'Unauthorized!', 401 );
890 }
891
892 // template id.
893 $template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : 0;
894 if ( ! $template_id ) {
895 wp_send_json_error( 'Bad request', 400 );
896 }
897
898 // template.
899 $email_templates = get_option( 'wpsc-email-templates' );
900 $et = array_key_exists( $template_id, $email_templates ) ? $email_templates[ $template_id ] : null;
901 if ( ! $et ) {
902 wp_send_json_error( 'Bad request', 400 );
903 }
904
905 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( ( $_POST['title'] ) ) ) : '';
906 if ( ! $title ) {
907 wp_send_json_error( 'Bad Request', 400 );
908 }
909
910 $event = isset( $_POST['event'] ) ? sanitize_text_field( wp_unslash( ( $_POST['event'] ) ) ) : '';
911 if ( ! $event ) {
912 wp_send_json_error( 'Bad Request', 400 );
913 }
914
915 $clone = array(
916 'title' => $title,
917 'event' => $event,
918 'is_enable' => 0,
919 'subject' => $et['subject'],
920 'body' => array(
921 'text' => $et['body']['text'],
922 'editor' => $et['body']['editor'],
923 ),
924 'to' => array(
925 'general-recipients' => $et['to']['general-recipients'],
926 'agent-roles' => $et['to']['agent-roles'],
927 'custom' => $et['to']['custom'],
928 ),
929 'cc' => array(
930 'general-recipients' => $et['cc']['general-recipients'],
931 'agent-roles' => $et['cc']['agent-roles'],
932 'custom' => $et['cc']['custom'],
933 ),
934 'bcc' => array(
935 'general-recipients' => $et['bcc']['general-recipients'],
936 'agent-roles' => $et['bcc']['agent-roles'],
937 'custom' => $et['bcc']['custom'],
938 ),
939 'conditions' => $et['conditions'],
940 );
941
942 $clone = apply_filters( 'wpsc_en_clone_ticket_notification', $clone, $et );
943
944 $email_templates[] = $clone;
945
946 WPSC_Translations::add( 'wpsc-en-tn-subject-' . array_key_last( $email_templates ), stripslashes( $et['subject'] ) );
947 WPSC_Translations::add( 'wpsc-en-tn-body-' . array_key_last( $email_templates ), stripslashes( $et['body']['text'] ) );
948
949 update_option( 'wpsc-email-templates', $email_templates );
950
951 end( $email_templates );
952 $index = key( $email_templates );
953
954 $nonce = wp_create_nonce( 'wpsc_en_get_edit_ticket_notification' );
955 wp_send_json(
956 array(
957 'index' => $index,
958 'nonce' => $nonce,
959 )
960 );
961 }
962 }
963 endif;
964
965 WPSC_EN_Settings_TN::init();
966