PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.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 / 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.3.9, at includes/admin/tickets/widgets/class-wpsc-itw-ticket-fields.php

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