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 / tickets / widgets / class-wpsc-itw-agentonly-fields.php

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

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