PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
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 / tickets / widgets / class-wpsc-itw-ticket-fields.php

class-wpsc-itw-ticket-fields.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.4, at includes/admin/tickets/widgets/class-wpsc-itw-ticket-fields.php

441 lines 14.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_ITW_Ticket_Fields' ) ) :
7
8 final class WPSC_ITW_Ticket_Fields {
9
10 /**
11 * Ignore ticket custom field types for ticket fields
12 *
13 * @var array
14 */
15 public static $ignore_cft = array();
16
17 /**
18 * Initialize this class
19 *
20 * @return void
21 */
22 public static function init() {
23
24 // ignore cft.
25 add_action( 'init', array( __CLASS__, 'ignore_cft' ) );
26
27 // get edit ticket fields.
28 add_action( 'wp_ajax_wpsc_it_get_edit_ticket_fields', array( __CLASS__, 'it_get_edit_ticket_fields' ) );
29 add_action( 'wp_ajax_nopriv_wpsc_it_get_edit_ticket_fields', array( __CLASS__, 'it_get_edit_ticket_fields' ) );
30 add_action( 'wp_ajax_wpsc_it_set_edit_ticket_fields', array( __CLASS__, 'it_set_edit_ticket_fields' ) );
31 add_action( 'wp_ajax_nopriv_wpsc_it_set_edit_ticket_fields', array( __CLASS__, 'it_set_edit_ticket_fields' ) );
32
33 // ticket fields.
34 add_action( 'wp_ajax_wpsc_get_tw_ticket_fields', array( __CLASS__, 'get_tw_ticket_fields' ) );
35 add_action( 'wp_ajax_wpsc_set_tw_ticket_fields', array( __CLASS__, 'set_tw_ticket_fields' ) );
36 }
37
38 /**
39 * Set ignore custom field types for ticket fields
40 *
41 * @return void
42 */
43 public static function ignore_cft() {
44
45 self::$ignore_cft = apply_filters( 'wpsc_ignore_edit_ticket_field_cft', array( 'cf_html' ) );
46 }
47
48 /**
49 * Prints body of current widget
50 *
51 * @param WPSC_Ticket $ticket - ticket object.
52 * @param array $settings - widget settings.
53 * @return void
54 */
55 public static function print_widget( $ticket, $settings ) {
56
57 $current_user = WPSC_Current_User::$current_user;
58 if ( ! (
59 (
60 (
61 WPSC_Individual_Ticket::$view_profile == 'customer' ||
62 $ticket->customer->id == $current_user->customer->id
63 ) &&
64 $settings['allow-customer']
65 ) ||
66 ( WPSC_Individual_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) )
67 ) ) {
68 return;
69 }
70 $show_edit = (
71 $ticket->is_active &&
72 WPSC_Individual_Ticket::$view_profile == 'agent' &&
73 WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) &&
74 ! WPSC_Individual_Ticket::$is_restricted
75 );
76 self::render_widget( $ticket, $settings, $show_edit, false );
77 }
78
79 /**
80 * Prints body of current widget
81 *
82 * @param WPSC_Ticket $ticket - ticket object.
83 * @param array $settings - widget settings.
84 * @return void
85 */
86 public static function print_archive_widget( $ticket, $settings ) {
87
88 $current_user = WPSC_Current_User::$current_user;
89 if ( ! (
90 (
91 (
92 WPSC_Individual_Archive_Ticket::$view_profile == 'customer' ||
93 $ticket->customer->id == $current_user->customer->id
94 ) &&
95 $settings['allow-customer']
96 ) ||
97 ( WPSC_Individual_Archive_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) )
98 ) ) {
99 return;
100 }
101 self::render_widget( $ticket, $settings, false, true );
102 }
103
104 /**
105 * Render widget
106 *
107 * @param WPSC_Ticket|WPSC_Archive_Ticket $ticket - ticket object.
108 * @param array $settings - widget settings.
109 * @param bool $show_edit - show edit button.
110 * @param bool $is_archive - is archive ticket.
111 * @return void
112 */
113 private static function render_widget( $ticket, $settings, $show_edit, $is_archive ) {
114 $flag = false;
115 $title = $settings['title']
116 ? WPSC_Translations::get( 'wpsc-twt-ticket-fields', stripslashes( $settings['title'] ) )
117 : stripslashes( $settings['title'] );
118 ?>
119 <div class="wpsc-it-widget wpsc-itw-ticket-fields">
120 <div class="wpsc-widget-header">
121 <h2><?php echo esc_attr( $title ); ?></h2>
122 <?php
123 if ( $show_edit ) :
124 ?>
125 <span onclick="wpsc_it_get_edit_ticket_fields(<?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_it_get_edit_ticket_fields' ) ); ?>')"><?php WPSC_Icons::get( 'edit' ); ?></span>
126 <?php
127 endif;
128 ?>
129 <span class="wpsc-itw-toggle" data-widget="wpsc-itw-ticket-fields"><?php WPSC_Icons::get( 'chevron-up' ); ?></span>
130 </div>
131 <div class="wpsc-widget-body">
132 <?php
133 $cft_exclude = apply_filters( 'wpsc_it_widget_exclude_cft', array() );
134 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) :
135 if ( in_array( $cf->type::$slug, $cft_exclude ) ) {
136 continue;
137 }
138 if ( $cf->field == 'ticket' && ! $cf->type::$is_default && ( $ticket->{$cf->slug} || ( $cf->type::$slug == 'cf_number' && is_numeric( $ticket->{$cf->slug} ) ) ) ) :
139 if ( $cf->type::$is_date && ! is_object( $ticket->{$cf->slug} ) ) {
140 continue;
141 }
142 $flag = ! $flag ? true : $flag
143 ?>
144 <div class="info-list-item">
145 <div class="info-label"><?php echo esc_attr( $cf->name ); ?>:</div>
146 <div class="info-val">
147 <?php
148 $cf->type::print_widget_ticket_field_val( $cf, $ticket );
149 ?>
150 </div>
151 </div>
152 <?php
153 endif;
154 endforeach;
155 if ( ! $flag ) :
156 ?>
157 <div class="wpsc-widget-default"><?php esc_attr_e( 'Not Applicable', 'supportcandy' ); ?></div>
158 <?php
159 endif;
160 do_action( 'wpsc_itw_ticket_fields', $ticket )
161 ?>
162 </div>
163 </div>
164 <?php
165 }
166
167 /**
168 * Get edit ticket fields
169 *
170 * @return void
171 */
172 public static function it_get_edit_ticket_fields() {
173
174 if ( check_ajax_referer( 'wpsc_it_get_edit_ticket_fields', '_ajax_nonce', false ) != 1 ) {
175 wp_send_json_error( 'Unauthorized request!', 401 );
176 }
177
178 WPSC_Individual_Ticket::load_current_ticket();
179
180 $current_user = WPSC_Current_User::$current_user;
181 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) {
182 wp_send_json_error( 'Something went wrong!', 401 );
183 }
184
185 $ticket = WPSC_Individual_Ticket::$ticket;
186
187 $widgets = get_option( 'wpsc-ticket-widget' );
188 $title = $widgets['ticket-fields']['title'];
189
190 ob_start()
191 ?>
192 <form action="#" onsubmit="return false;" class="change-ticket-fields">
193
194 <div class="wpsc-input-group" style="align-items:flex-end;">
195 <input type="text" id="cft_search" placeholder="<?php esc_attr_e( 'Search...', 'supportcandy' ); ?>" autocomplete="off" style="max-width:200px;">
196 </div>
197
198 <div id="tf_cf_list">
199 <?php
200
201 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
202
203 if (
204 ! class_exists( $cf->type ) ||
205 $cf->type::$is_default ||
206 $cf->field != 'ticket' ||
207 in_array( $cf->type::$slug, self::$ignore_cft )
208 ) {
209 continue;
210 }
211
212 $cf->type::print_edit_ticket_cf( $cf, $ticket );
213 }
214
215 do_action( 'wpsc_get_tw_ticket_fields_body' );
216 ?>
217 </div>
218
219 <input type="hidden" name="action" value="wpsc_it_set_edit_ticket_fields">
220 <input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>">
221 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_ticket_fields' ) ); ?>">
222
223 <script>
224 jQuery('#cft_search').keyup(function(e) {
225 var val = jQuery(this).val().trim();
226 var regex = new RegExp(val, "i");
227 jQuery('.change-ticket-fields .wpsc-tff').each(function(){
228 var name = jQuery(this).find('.wpsc-tff-label').text();
229 if (name.search(regex) < 0) {
230 jQuery(this).hide();
231 } else {
232 jQuery(this).show();
233 }
234 })
235 });
236 </script>
237
238 </form>
239 <?php
240 do_action( 'wpsc_get_tw_ticket_fields_footer', $ticket );
241 $body = ob_get_clean();
242
243 ob_start();
244 ?>
245 <button class="wpsc-button small primary" onclick="wpsc_it_set_edit_ticket_fields(this, <?php echo esc_attr( $ticket->id ); ?>);">
246 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
247 </button>
248 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
249 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
250 </button>
251 <?php
252 $footer = ob_get_clean();
253
254 $response = array(
255 'title' => $title,
256 'body' => $body,
257 'footer' => $footer,
258 );
259 wp_send_json( $response );
260 }
261
262 /**
263 * Set edit ticket fields
264 *
265 * @return void
266 */
267 public static function it_set_edit_ticket_fields() {
268
269 if ( check_ajax_referer( 'wpsc_it_set_edit_ticket_fields', '_ajax_nonce', false ) != 1 ) {
270 wp_send_json_error( 'Unauthorized request!', 401 );
271 }
272
273 WPSC_Individual_Ticket::load_current_ticket();
274 if ( WPSC_Individual_Ticket::$is_restricted ) {
275 wp_send_json_error( 'Unauthorised request!', 401 );
276 }
277
278 $current_user = WPSC_Current_User::$current_user;
279 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) {
280 wp_send_json_error( 'Something went wrong!', 401 );
281 }
282
283 $ticket = clone WPSC_Individual_Ticket::$ticket;
284 if ( ! $ticket->is_active ) {
285 wp_send_json_error( 'Something went wrong!', 400 );
286 }
287
288 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
289
290 if (
291 ! class_exists( $cf->type ) ||
292 $cf->type::$is_default ||
293 $cf->field != 'ticket' ||
294 in_array( $cf->type::$slug, self::$ignore_cft )
295 ) {
296 continue;
297 }
298
299 $ticket = $cf->type::set_edit_ticket_cf( $cf, $ticket );
300 }
301
302 if ( $ticket != WPSC_Individual_Ticket::$ticket ) {
303
304 $ticket->date_updated = new DateTime();
305 $ticket->save();
306
307 do_action( 'wpsc_change_ticket_fields', WPSC_Individual_Ticket::$ticket, $ticket );
308 }
309
310 wp_die();
311 }
312
313 /**
314 * Get Ticket fields
315 */
316 public static function get_tw_ticket_fields() {
317
318 if ( ! WPSC_Functions::is_site_admin() ) {
319 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
320 }
321
322 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
323 $ticket_fields = $ticket_widgets['ticket-fields'];
324 $title = $ticket_fields['title'];
325 $roles = get_option( 'wpsc-agent-roles', array() );
326 ob_start();
327 ?>
328
329 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-ticket-fields">
330 <div class="wpsc-input-group">
331 <div class="label-container">
332 <label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label>
333 </div>
334 <input name="label" type="text" value="<?php echo esc_attr( $ticket_fields['title'] ); ?>" autocomplete="off">
335 </div>
336 <div class="wpsc-input-group">
337 <div class="label-container">
338 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
339 </div>
340 <select name="is_enable">
341 <option <?php selected( $ticket_fields['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
342 <option <?php selected( $ticket_fields['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
343 </select>
344 </div>
345 <div class="wpsc-input-group">
346 <div class="label-container">
347 <label for=""><?php esc_attr_e( 'Allowed for customer', 'supportcandy' ); ?></label>
348 </div>
349 <select id="allow-customer" name="allow-customer">
350 <option <?php selected( $ticket_fields['allow-customer'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
351 <option <?php selected( $ticket_fields['allow-customer'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
352 </select>
353 </div>
354 <div class="wpsc-input-group">
355 <div class="label-container">
356 <label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label>
357 </div>
358 <select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent...">
359 <?php
360 foreach ( $roles as $key => $role ) :
361 $selected = in_array( $key, $ticket_fields['allowed-agent-roles'] ) ? 'selected="selected"' : ''
362 ?>
363 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
364 <?php
365 endforeach;
366 ?>
367 </select>
368 </div>
369 <script>
370 jQuery('#wpsc-select-agents').selectWoo({
371 allowClear: false,
372 placeholder: ""
373 });
374 </script>
375 <?php do_action( 'wpsc_get_ticket_fields_body' ); ?>
376 <input type="hidden" name="action" value="wpsc_set_tw_ticket_fields">
377 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tw_ticket_fields' ) ); ?>">
378 </form>
379 <?php
380 $body = ob_get_clean();
381
382 ob_start();
383 ?>
384 <button class="wpsc-button small primary" onclick="wpsc_set_tw_ticket_fields(this);">
385 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
386 </button>
387 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
388 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
389 </button>
390 <?php
391 do_action( 'wpsc_get_tw_ticket_fields_widget_footer' );
392 $footer = ob_get_clean();
393
394 $response = array(
395 'title' => $title,
396 'body' => $body,
397 'footer' => $footer,
398 );
399 wp_send_json( $response );
400 }
401
402 /**
403 * Set Ticket fields
404 */
405 public static function set_tw_ticket_fields() {
406
407 if ( check_ajax_referer( 'wpsc_set_tw_ticket_fields', '_ajax_nonce', false ) != 1 ) {
408 wp_send_json_error( 'Unauthorized request!', 401 );
409 }
410
411 if ( ! WPSC_Functions::is_site_admin() ) {
412 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
413 }
414
415 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
416 if ( ! $label ) {
417 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
418 }
419
420 $is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
421 $allow_for_customer = isset( $_POST['allow-customer'] ) ? intval( $_POST['allow-customer'] ) : 0;
422 $agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array();
423
424 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
425
426 $ticket_widgets['ticket-fields']['title'] = $label;
427 $ticket_widgets['ticket-fields']['is_enable'] = $is_enable;
428 $ticket_widgets['ticket-fields']['allow-customer'] = $allow_for_customer;
429 $ticket_widgets['ticket-fields']['allowed-agent-roles'] = $agents;
430 update_option( 'wpsc-ticket-widget', $ticket_widgets );
431
432 // remove string translations.
433 WPSC_Translations::remove( 'wpsc-twt-ticket-fields' );
434 WPSC_Translations::add( 'wpsc-twt-ticket-fields', stripslashes( $label ) );
435 wp_die();
436 }
437 }
438 endif;
439
440 WPSC_ITW_Ticket_Fields::init();
441