PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.8
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.8
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 / custom-field-types / class-wpsc-df-additional-recipients.php

class-wpsc-df-additional-recipients.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.8, at includes/custom-field-types/class-wpsc-df-additional-recipients.php

565 lines 16.6 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_DF_Additional_Recipients' ) ) :
7
8 final class WPSC_DF_Additional_Recipients {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_add_recipients';
16
17 /**
18 * Set whether this custom field type is of type date
19 *
20 * @var boolean
21 */
22 public static $is_date = false;
23
24 /**
25 * Set whether this custom field type has applicable to date range
26 *
27 * @var boolean
28 */
29 public static $has_date_range = false;
30
31 /**
32 * Set whether this custom field type has multiple values
33 *
34 * @var boolean
35 */
36 public static $has_multiple_val = true;
37
38 /**
39 * Data type for column created in tickets table
40 *
41 * @var string
42 */
43 public static $data_type = 'TEXT NULL DEFAULT NULL';
44
45 /**
46 * Set whether this custom field type has reference to other class
47 *
48 * @var boolean
49 */
50 public static $has_ref = false;
51
52 /**
53 * Reference class for this custom field type so that its value(s) return with object or array of objects automatically. Empty string indicate no reference.
54 *
55 * @var string
56 */
57 public static $ref_class = '';
58
59 /**
60 * Set whether this custom field field type is system default (no fields can be created from it).
61 *
62 * @var boolean
63 */
64 public static $is_default = true;
65
66 /**
67 * Set whether this field type has extra information that can be used in ticket form, edit custom fields, etc.
68 *
69 * @var boolean
70 */
71 public static $has_extra_info = true;
72
73 /**
74 * Set whether this custom field type can accept personal info.
75 *
76 * @var boolean
77 */
78 public static $has_personal_info = false;
79
80 /**
81 * Set whether fields created from this custom field type is allowed in create ticket form
82 *
83 * @var boolean
84 */
85 public static $is_ctf = true;
86
87 /**
88 * Set whether fields created from this custom field type is allowed in ticket list
89 *
90 * @var boolean
91 */
92 public static $is_list = false;
93
94 /**
95 * Set whether fields created from this custom field type is allowed in ticket filter
96 *
97 * @var boolean
98 */
99 public static $is_filter = false;
100
101 /**
102 * Set whether fields created from this custom field type can be given character limits
103 *
104 * @var boolean
105 */
106 public static $has_char_limit = false;
107
108 /**
109 * Set whether fields created from this custom field type has custom options set in options table
110 *
111 * @var boolean
112 */
113 public static $has_options = false;
114
115 /**
116 * Set whether fields created from this custom field type can be auto-filled
117 *
118 * @var boolean
119 */
120 public static $is_auto_fill = false;
121
122 /**
123 * Set whether fields created from this custom field type can be available for ticket list sorting
124 *
125 * @var boolean
126 */
127 public static $is_sort = false;
128
129 /**
130 * Set whether fields created from this custom field type can have placeholder
131 *
132 * @var boolean
133 */
134 public static $is_placeholder = true;
135
136 /**
137 * Set whether fields created from this custom field type is applicable for visibility conditions in create ticket form
138 *
139 * @var boolean
140 */
141 public static $is_visibility_conditions = false;
142
143 /**
144 * Set whether fields created from this custom field type is applicable for macros
145 *
146 * @var boolean
147 */
148 public static $has_macro = true;
149
150 /**
151 * Set whether fields of this custom field type is applicalbe for search on ticket list page.
152 *
153 * @var boolean
154 */
155 public static $is_search = true;
156
157 /**
158 * Initialize the class
159 *
160 * @return void
161 */
162 public static function init() {
163
164 // Get object of this class.
165 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
166
167 // Ticket model.
168 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
169
170 // ticket form.
171 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
172 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
173 add_action( 'wpsc_js_clear_value_hidden_fields', array( __CLASS__, 'js_clear_value_hidden_fields' ) );
174
175 // create ticket data for rest api.
176 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
177 }
178
179 /**
180 * Load current class to reference classes
181 *
182 * @param array $classes - Associative array of class names indexed by its slug.
183 * @return array
184 */
185 public static function load_ref_class( $classes ) {
186
187 $classes[ self::$slug ] = array(
188 'class' => __CLASS__,
189 'save-key' => 'id',
190 );
191 return $classes;
192 }
193
194 /**
195 * Add ticket search compatibility for fields of this custom field type.
196 *
197 * @param array $sql - Array of sql peices that can be joined later.
198 * @param array $filter - User filter.
199 * @param array $custom_fields - Custom fields array applicable for search.
200 * @param string $search - search string.
201 * @param array $allowed_search_fields - Allowed search fields.
202 * @return array
203 */
204 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
205
206 if ( isset( $custom_fields[ self::$slug ] ) ) {
207 foreach ( $custom_fields[ self::$slug ] as $cf ) {
208 if ( in_array( $cf->slug, $allowed_search_fields ) ) {
209 $sql[] = 'CONVERT( t.assigned_agent USING utf8) LIKE \'%' . $search . '%\'';
210 }
211 }
212 }
213
214 return $sql;
215 }
216
217 /**
218 * Print ticket form field
219 *
220 * @param WPSC_Custom_Field $cf - Custom field object.
221 * @param array $tff - Array of ticket form field settings for this field.
222 * @return string
223 */
224 public static function print_tff( $cf, $tff ) {
225
226 ob_start();?>
227 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>" data-slug="<?php echo esc_attr( $cf->slug ); ?>">
228 <div class="wpsc-tff-label">
229 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
230 <?php
231 if ( $tff['is-required'] ) :
232 ?>
233 <span class="required-indicator">*</span>
234 <?php
235 endif;
236 ?>
237 </div>
238 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
239 <textarea
240 type="text"
241 name="<?php echo esc_attr( $cf->slug ); ?>"
242 placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>"
243 onchange="wpsc_check_tff_visibility()"
244 autocomplete="off"></textarea>
245 </div>
246 <?php
247 return ob_get_clean();
248 }
249
250 /**
251 * Validate this type field in create ticket
252 *
253 * @return void
254 */
255 public static function js_validate_ticket_form() {
256 ?>
257
258 case '<?php echo esc_attr( self::$slug ); ?>':
259 var val = customField.find('textarea').first().val().trim();
260 if (customField.hasClass('required') && !val) {
261 isValid = false;
262 alert(supportcandy.translations.req_fields_missing);
263 }
264 break;
265 <?php
266 echo PHP_EOL;
267 }
268
269 /**
270 * Return custom field value in $_POST
271 * Ignore phpcs nonce issue as we already checked where it is called from.
272 *
273 * @param string $slug - Custom field slug.
274 * @param mixed $cf - Custom field object or false.
275 * @return mixed
276 */
277 public static function get_tff_value( $slug, $cf = false ) {
278
279 return isset( $_POST[ $slug ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $slug ] ) ) : ''; // phpcs:ignore
280 }
281
282 /**
283 * Check and return custom field value for new ticket to be created.
284 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
285 * Ignore phpcs nonce issue as we already checked where it is called from.
286 *
287 * @param array $data - Array of values to to stored in ticket in an insert function.
288 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
289 * @param boolean $is_my_profile - Whether it or not it is created from my-profile. This function is used by create ticket as well as my-profile. Due to customer fields handling is done same way, this flag gives apportunity to identify where it being called.
290 * @return array
291 */
292 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
293
294 if ( $is_my_profile ) {
295 return;
296 }
297
298 if ( isset( $custom_fields[ self::$slug ] ) ) {
299 $new = array();
300 foreach ( $custom_fields[ self::$slug ] as $cf ) {
301
302 $recipients = self::get_tff_value( $cf->slug );
303 if ( ! $recipients ) {
304 continue;
305 }
306 $recipients = array_filter( array_map( 'sanitize_email', explode( PHP_EOL, $recipients ) ) );
307 $data[ $cf->slug ] = $recipients ? implode( '|', $recipients ) : '';
308 }
309 }
310 return $data;
311 }
312
313 /**
314 * Set create ticket data for rest api request
315 *
316 * @param array $data - create ticket data array.
317 * @param WP_REST_Request $request - rest request object.
318 * @param array $custom_fields - custom field objects indexed by unique custom field types.
319 * @return array
320 */
321 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
322
323 $cf = WPSC_Custom_Field::get_cf_by_slug( 'add_recipients' );
324 $add_recipients = array_unique(
325 array_filter(
326 array_map(
327 fn( $email ) => WPSC_Functions::sanitize_email( trim( $email ) ),
328 explode( ',', sanitize_text_field( $request->get_param( 'add_recipients' ) ) )
329 )
330 )
331 );
332 $data['add_recipients'] = implode( '|', $add_recipients );
333 return $data;
334 }
335
336 /**
337 * Print generic input field for this type.
338 *
339 * @param WPSC_Custom_Field $cf - Custom field object.
340 * @param mixed $value - optional. Input field will be printed with given value if given.
341 * @return void
342 */
343 public static function print_cf_input( $cf, $value = '' ) {
344
345 $add_recipients = implode( PHP_EOL, explode( '|', $value ) );
346 ?>
347 <div class="wpsc-tff wpsc-sm-12 wpsc-md-12 wpsc-lg-12 wpsc-visible wpsc-xs-12" data-cft="<?php echo esc_attr( self::$slug ); ?>">
348 <div class="wpsc-tff-label">
349 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
350 </div>
351 <?php
352 $extra_info = stripslashes( $cf->extra_info );
353 if ( $extra_info ) {
354 ?>
355 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
356 <?php
357 }
358 ?>
359 <textarea
360 name="<?php echo esc_attr( $cf->slug ); ?>"
361 rows="5"
362 placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>"><?php echo esc_attr( $add_recipients ); ?></textarea>
363 </div>
364 <?php
365 }
366
367 /**
368 * Get value for generic field from post.
369 * Ignore phpcs nonce issue as we already checked where it is called from.
370 *
371 * @param WPSC_Custom_Field $cf - custom field object.
372 * @return string
373 */
374 public static function get_cf_input_val( $cf ) {
375
376 $value = isset( $_POST[ $cf->slug ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $cf->slug ] ) ) : ''; // phpcs:ignore
377 $value = explode( PHP_EOL, $value );
378 return $value ? implode( '|', $value ) : '';
379 }
380
381 /**
382 * Returns value of edit custom field
383 *
384 * @param WPSC_Custom_Field $cf - custom field object.
385 * @return mixed
386 */
387 public static function get_edit_cf_val( $cf ) {
388
389 // ignore phpcs nonce issue as we already checked where it is called from.
390 $recipients = isset( $_POST[ $cf->slug ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $cf->slug ] ) ) : ''; // phpcs:ignore
391 if ( ! $recipients ) {
392 return '';
393 }
394 $recipients = array_filter( array_map( 'sanitize_email', explode( PHP_EOL, $recipients ) ) );
395 return $recipients ? implode( '|', $recipients ) : '';
396 }
397
398 /**
399 * Print edit custom field properties
400 *
401 * @param WPSC_Custom_Fields $cf - custom field object.
402 * @param string $field_class - class name of field category.
403 * @return void
404 */
405 public static function get_edit_custom_field_properties( $cf, $field_class ) {
406
407 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
408 ?>
409 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
410 <div class="label-container">
411 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
412 </div>
413 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
414 </div>
415 <?php
416 endif;
417
418 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
419 ?>
420 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
421 <div class="label-container">
422 <label for="">
423 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
424 </label>
425 </div>
426 <input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off">
427 </div>
428 <?php
429 endif;
430 }
431
432 /**
433 * Set custom field properties. Can be used by add/edit custom field.
434 * Ignore phpcs nonce issue as we already checked where it is called from.
435 *
436 * @param WPSC_Custom_Field $cf - custom field object.
437 * @param string $field_class - class of field category.
438 * @return void
439 */
440 public static function set_cf_properties( $cf, $field_class ) {
441
442 // extra info.
443 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
444 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
445 }
446
447 // placeholder!
448 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) {
449 $cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore
450 }
451
452 // save!
453 $cf->save();
454
455 // update widget title!
456 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
457 $ticket_widgets['additional-recipients']['title'] = $cf->name;
458 update_option( 'wpsc-ticket-widget', $ticket_widgets );
459 }
460
461 /**
462 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
463 *
464 * @param WPSC_Custom_Field $cf - custom field object.
465 * @param WPSC_Ticket $ticket - ticket object.
466 * @param string $module - module name.
467 * @return string
468 */
469 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
470
471 $recipients = $ticket->{$cf->slug};
472 $value = $recipients ? implode( ', ', $recipients ) : '';
473 return apply_filters( 'wpsc_ticket_field_val_add_recipients', $value, $cf, $ticket, $module );
474 }
475
476 /**
477 * Print ticket value for given custom field on widget
478 *
479 * @param WPSC_Custom_Field $cf - custom field object.
480 * @param WPSC_Ticket $ticket - ticket object.
481 * @return void
482 */
483 public static function print_widget_ticket_field_val( $cf, $ticket ) {
484
485 $recipients = $ticket->{$cf->slug};
486 if ( $recipients ) {
487 foreach ( $recipients as $recipient ) {
488 ?>
489 <div><?php echo esc_attr( $recipient ); ?></div>
490 <?php
491 }
492 } else {
493 esc_attr_e( 'Not Applicable', 'supportcandy' );
494 }
495 }
496
497 /**
498 * Print given value for custom field
499 *
500 * @param WPSC_Custom_Field $cf - custom field object.
501 * @param mixed $val - value to convert and print.
502 * @return void
503 */
504 public static function print_val( $cf, $val ) {
505
506 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
507 echo $val ? esc_attr( implode( ', ', $val ) ) : esc_attr__( 'None', 'supportcandy' );
508 }
509
510 /**
511 * Return printable value for history log macro
512 *
513 * @param WPSC_Custom_Field $cf - custom field object.
514 * @param mixed $val - value to convert and return.
515 * @return string
516 */
517 public static function get_history_log_val( $cf, $val ) {
518
519 ob_start();
520 self::print_val( $cf, $val );
521 return ob_get_clean();
522 }
523
524 /**
525 * Return default value for custom field of this type
526 *
527 * @param WPSC_Custom_Field $cf - custom field object.
528 * @return mixed
529 */
530 public static function get_default_value( $cf ) {
531
532 return '';
533 }
534
535 /**
536 * Return given value for custom field
537 *
538 * @param WPSC_Custom_Field $cf - custom field object.
539 * @param mixed $val - value to convert and print.
540 * @return string
541 */
542 public static function get_field_value( $cf, $val ) {
543
544 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
545 return $val ? esc_attr( implode( ', ', $val ) ) : esc_attr__( 'None', 'supportcandy' );
546 }
547
548 /**
549 * Clear value of hidden fields
550 *
551 * @return void
552 */
553 public static function js_clear_value_hidden_fields() {
554 ?>
555 case '<?php echo esc_attr( self::$slug ); ?>':
556 customField.find('textarea').first().val('');
557 break;
558 <?php
559 echo PHP_EOL;
560 }
561 }
562 endif;
563
564 WPSC_DF_Additional_Recipients::init();
565