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 / settings / general-settings / class-wpsc-gs-general.php

class-wpsc-gs-general.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.4, at includes/admin/settings/general-settings/class-wpsc-gs-general.php

445 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_GS_General' ) ) :
7
8 final class WPSC_GS_General {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // User interface.
18 add_action( 'wp_ajax_wpsc_get_gs_general', array( __CLASS__, 'load_settings_ui' ) );
19 add_action( 'wp_ajax_wpsc_set_gs_general', array( __CLASS__, 'save_settings' ) );
20 add_action( 'wp_ajax_wpsc_reset_gs_general', array( __CLASS__, 'reset_settings' ) );
21
22 // Apply ticket status after customer or agent reply.
23 add_action( 'wpsc_post_reply', array( __CLASS__, 'change_ticket_status' ) );
24
25 // after new agent role added.
26 add_action( 'wpsc_after_add_agent_role', array( __CLASS__, 'after_add_new_role' ) );
27
28 // after agent role cloned.
29 add_action( 'wpsc_after_clone_agent_role', array( __CLASS__, 'after_clone_role' ), 10, 2 );
30
31 // after delete agent role.
32 add_action( 'wpsc_after_delete_agent_role', array( __CLASS__, 'after_delete_role' ) );
33 }
34
35 /**
36 * Reset settings
37 *
38 * @return void
39 */
40 public static function reset() {
41
42 $roles = get_option( 'wpsc-agent-roles' );
43 $allow_close_ticket = array( 'customer' );
44 $allow_create_ticket = array( 'registered-user' );
45 foreach ( $roles as $key => $role ) {
46 $allow_close_ticket[] = $key;
47 $allow_create_ticket[] = $key;
48 }
49
50 $general = apply_filters(
51 'wpsc_gs_general',
52 array(
53 'ticket-status-after-customer-reply' => 3,
54 'ticket-status-after-agent-reply' => 2,
55 'close-ticket-status' => 4,
56 'reply-form-position' => 'top',
57 'default-date-format' => 'Y-m-d H:i:s',
58 'ticket-alice' => 'Ticket #',
59 'allow-close-ticket' => $allow_close_ticket,
60 'allow-ar-thread-email' => array( 1, 2 ),
61 'allow-create-ticket' => $allow_create_ticket,
62 'allowed-search-fields' => array( 'id', 'customer', 'subject', 'threads' ),
63 )
64 );
65 update_option( 'wpsc-gs-general', $general );
66 }
67
68 /**
69 * Settings user interface
70 *
71 * @return void
72 */
73 public static function load_settings_ui() {
74
75 if ( ! WPSC_Functions::is_site_admin() ) {
76 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
77 }
78 $statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'];
79 $settings = get_option( 'wpsc-gs-general', array() );
80 $roles = get_option( 'wpsc-agent-roles' );
81 $visibility = array(
82 'registered-user' => esc_attr__( 'Registered user', 'supportcandy' ),
83 'guest' => esc_attr__( 'Guest', 'supportcandy' ),
84 );
85 foreach ( $roles as $key => $role ) :
86 $visibility[ $key ] = $role['label'];
87 endforeach;
88
89 $allow_close_ticket = array(
90 'customer' => esc_attr__( 'Customer', 'supportcandy' ),
91 );
92 foreach ( $roles as $key => $role ) :
93 $allow_close_ticket[ $key ] = $role['label'];
94 endforeach;?>
95
96 <form action="#" onsubmit="return false;" class="wpsc-frm-gs-general">
97 <div class="wpsc-dock-container">
98 <?php
99 printf(
100 /* translators: Click here to see the documentation */
101 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
102 '<a href="https://supportcandy.net/docs/general-settings/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
103 );
104 ?>
105 </div>
106 <div class="wpsc-input-group">
107 <div class="label-container">
108 <label for=""><?php esc_attr_e( 'Ticket status after customer reply', 'supportcandy' ); ?></label>
109 </div>
110 <select id="wpsc-tsacr" name="ticket-status-after-customer-reply">
111 <option value="0"><?php esc_attr_e( 'Do not change', 'supportcandy' ); ?></option>
112 <?php
113 foreach ( $statuses as $status ) :
114 ?>
115 <option <?php selected( $status->id, $settings['ticket-status-after-customer-reply'] ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option>
116 <?php
117 endforeach;
118 ?>
119 </select>
120 <script>
121 jQuery('#wpsc-tsacr').selectWoo({
122 allowClear: true,
123 placeholder: ""
124 });
125 </script>
126 </div>
127 <div class="wpsc-input-group">
128 <div class="label-container">
129 <label for=""><?php esc_attr_e( 'Ticket status after agent reply', 'supportcandy' ); ?></label>
130 </div>
131 <select id="wpsc-tsaar" name="ticket-status-after-agent-reply">
132 <option value="0"><?php esc_attr_e( 'Do not change', 'supportcandy' ); ?></option>
133 <?php
134 foreach ( $statuses as $status ) :
135 ?>
136 <option <?php selected( $status->id, $settings['ticket-status-after-agent-reply'] ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option>
137 <?php
138 endforeach;
139 ?>
140 </select>
141 <script>
142 jQuery('#wpsc-tsaar').selectWoo({
143 allowClear: true,
144 placeholder: ""
145 });
146 </script>
147 </div>
148 <div class="wpsc-input-group">
149 <div class="label-container">
150 <label for=""><?php esc_attr_e( 'Close ticket status', 'supportcandy' ); ?></label>
151 </div>
152 <select id="wpsc-cts" name="close-ticket-status">
153 <?php
154 foreach ( $statuses as $status ) :
155 ?>
156 <option <?php selected( $status->id, $settings['close-ticket-status'] ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option>
157 <?php
158 endforeach;
159 ?>
160 </select>
161 <script>
162 jQuery('#wpsc-cts').selectWoo({
163 allowClear: true,
164 placeholder: ""
165 });
166 </script>
167 </div>
168 <div class="wpsc-input-group">
169 <div class="label-container">
170 <label for=""><?php esc_attr_e( 'Reply form position', 'supportcandy' ); ?></label>
171 </div>
172 <select id="wpsc-rfp" name="reply-form-position">
173 <option <?php selected( $settings['reply-form-position'], 'top' ); ?> value="top"><?php esc_attr_e( 'Top', 'supportcandy' ); ?></option>
174 <option <?php selected( $settings['reply-form-position'], 'bottom' ); ?> value="bottom"><?php esc_attr_e( 'Bottom', 'supportcandy' ); ?></option>
175 </select>
176 </div>
177 <div class="wpsc-input-group">
178 <div class="label-container">
179 <label for=""><?php esc_attr_e( 'Default date format', 'supportcandy' ); ?></label>
180 </div>
181 <input id="wpsc-ddf" type="text" name="default-date-format" value="<?php echo esc_attr( $settings['default-date-format'] ); ?>">
182 </div>
183 <div class="wpsc-input-group">
184 <div class="label-container">
185 <label for=""><?php esc_attr_e( 'Ticket alice', 'supportcandy' ); ?></label>
186 </div>
187 <input id="wpsc-ta" type="text" name="ticket-alice" value="<?php echo esc_attr( $settings['ticket-alice'] ); ?>">
188 </div>
189 <div class="wpsc-input-group">
190 <div class="label-container">
191 <label for=""><?php esc_attr_e( 'Allow close ticket', 'supportcandy' ); ?></label>
192 </div>
193 <select id="wpsc-allow-close-ticket" multiple name="allow-close-ticket[]">
194 <?php
195 foreach ( $allow_close_ticket as $key => $role ) :
196 $selected = in_array( $key, $settings['allow-close-ticket'] ) ? 'selected' : ''
197 ?>
198 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role ); ?></option>
199 <?php
200 endforeach;
201 ?>
202 </select>
203 <script>jQuery('#wpsc-allow-close-ticket').selectWoo();</script>
204 </div>
205 <div class="wpsc-input-group">
206 <div class="label-container">
207 <label for=""><?php esc_attr_e( 'Allow create new ticket', 'supportcandy' ); ?></label>
208 </div>
209 <select id="wpsc-allow-create-ticket" multiple name="allow-create-ticket[]">
210 <?php
211 foreach ( $visibility as $key => $role ) :
212 $selected = in_array( $key, $settings['allow-create-ticket'] ) ? 'selected' : ''
213 ?>
214 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role ); ?></option>
215 <?php
216 endforeach;
217 ?>
218 </select>
219 <script>jQuery('#wpsc-allow-create-ticket').selectWoo();</script>
220 </div>
221 <div class="wpsc-input-group">
222 <div class="label-container">
223 <label for=""><?php esc_attr_e( 'Thread email visibility', 'supportcandy' ); ?></label>
224 </div>
225 <select multiple id="wpsc-select-agent-roles" name="allow-ar-thread-email[]" placeholder="search agent roles">
226 <?php
227 foreach ( $roles as $key => $role ) :
228 ?>
229 <option <?php echo in_array( $key, $settings['allow-ar-thread-email'] ) ? 'selected="selected"' : ''; ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
230 <?php
231 endforeach;
232 ?>
233 </select>
234 </div>
235 <div class="wpsc-input-group">
236 <div class="label-container">
237 <label for=""><?php esc_attr_e( 'Select allowed search fields', 'supportcandy' ); ?></label>
238 </div>
239 <select multiple id="wpsc-select-allowed-search-fields" name="allowed-search-fields[]">
240 <option <?php echo in_array( 'threads', $settings['allowed-search-fields'] ) ? 'selected' : ''; ?> value="threads"><?php esc_attr_e( 'Threads', 'supportcandy' ); ?></option>
241 <?php
242 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
243 if ( ! ( property_exists( $cf->type, 'is_search' ) && $cf->type::$is_search ) || $cf->field == 'usergroup' ) {
244 continue;
245 }
246 $selected = in_array( $cf->slug, $settings['allowed-search-fields'] ) ? 'selected' : '';
247 ?>
248 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $cf->slug ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
249 <?php
250 }
251 ?>
252 </select>
253 </div>
254 <script>
255 jQuery('#wpsc-select-agent-roles, #wpsc-select-allowed-search-fields').selectWoo({
256 allowClear: false,
257 placeholder: ""
258 });
259 </script>
260 <?php do_action( 'wpsc_gs_general' ); ?>
261 <input type="hidden" name="action" value="wpsc_set_gs_general">
262 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_gs_general' ) ); ?>">
263 </form>
264 <div class="setting-footer-actions">
265 <button
266 class="wpsc-button normal primary margin-right"
267 onclick="wpsc_set_gs_general(this);">
268 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button>
269 <button
270 class="wpsc-button normal secondary"
271 onclick="wpsc_reset_gs_general(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_reset_gs_general' ) ); ?>');">
272 <?php esc_attr_e( 'Reset default', 'supportcandy' ); ?></button>
273 </div>
274 <?php
275 wp_die();
276 }
277
278 /**
279 * Save settings
280 *
281 * @return void
282 */
283 public static function save_settings() {
284
285 if ( check_ajax_referer( 'wpsc_set_gs_general', '_ajax_nonce', false ) != 1 ) {
286 wp_send_json_error( 'Unauthorized request!', 401 );
287 }
288
289 if ( ! WPSC_Functions::is_site_admin() ) {
290 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
291 }
292
293 $general = apply_filters(
294 'wpsc_set_gs_general',
295 array(
296 'ticket-status-after-customer-reply' => isset( $_POST['ticket-status-after-customer-reply'] ) ? intval( $_POST['ticket-status-after-customer-reply'] ) : '',
297 'ticket-status-after-agent-reply' => isset( $_POST['ticket-status-after-agent-reply'] ) ? intval( $_POST['ticket-status-after-agent-reply'] ) : '',
298 'close-ticket-status' => isset( $_POST['close-ticket-status'] ) ? intval( $_POST['close-ticket-status'] ) : 4,
299 'reply-form-position' => isset( $_POST['reply-form-position'] ) ? sanitize_text_field( wp_unslash( $_POST['reply-form-position'] ) ) : 'top',
300 'default-date-format' => isset( $_POST['default-date-format'] ) ? sanitize_text_field( wp_unslash( $_POST['default-date-format'] ) ) : 'Y-m-d',
301 'ticket-alice' => isset( $_POST['ticket-alice'] ) ? sanitize_text_field( wp_unslash( $_POST['ticket-alice'] ) ) : 'Ticket #',
302 'allow-close-ticket' => isset( $_POST['allow-close-ticket'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allow-close-ticket'] ) ) ) : array(),
303 'allow-ar-thread-email' => isset( $_POST['allow-ar-thread-email'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allow-ar-thread-email'] ) ) ) : array(),
304 'allow-create-ticket' => isset( $_POST['allow-create-ticket'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allow-create-ticket'] ) ) ) : array(),
305 'allowed-search-fields' => isset( $_POST['allowed-search-fields'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allowed-search-fields'] ) ) ) : array(),
306 )
307 );
308 update_option( 'wpsc-gs-general', $general );
309 wp_die();
310 }
311
312 /**
313 * Reset settings to default
314 *
315 * @return void
316 */
317 public static function reset_settings() {
318
319 if ( check_ajax_referer( 'wpsc_reset_gs_general', '_ajax_nonce', false ) != 1 ) {
320 wp_send_json_error( 'Unauthorized request!', 401 );
321 }
322
323 if ( ! WPSC_Functions::is_site_admin() ) {
324 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
325 }
326 self::reset();
327 wp_die();
328 }
329
330 /**
331 * Change status after agent or customer reply
332 *
333 * @param WPSC_Thread $thread - thread object.
334 * @return void
335 */
336 public static function change_ticket_status( $thread ) {
337
338 $gs = get_option( 'wpsc-gs-general' );
339 $reply_profile = WPSC_Individual_Ticket::$reply_profile;
340
341 if ( $reply_profile == 'agent' ) {
342
343 if ( ! $gs['ticket-status-after-agent-reply'] ) {
344 return;
345 }
346 $prev = $thread->ticket->status->id;
347 $new = intval( $gs['ticket-status-after-agent-reply'] ) ? $gs['ticket-status-after-agent-reply'] : $prev;
348 if ( $prev == $new ) {
349 return;
350 }
351 WPSC_Individual_Ticket::change_status( $prev, $new, 0 ); // customer id given as '0' because this is system changing the status based on settings.
352
353 } else {
354
355 if ( ! $gs['ticket-status-after-customer-reply'] ) {
356 return;
357 }
358 $prev = $thread->ticket->status->id;
359 $new = intval( $gs['ticket-status-after-customer-reply'] ) ? $gs['ticket-status-after-customer-reply'] : $prev;
360 if ( $prev == $new ) {
361 return;
362 }
363 WPSC_Individual_Ticket::change_status( $prev, $new, 0 ); // customer id given as '0' because this is system changing the status based on settings.
364 }
365 }
366
367 /**
368 * After new agent role added add that role in allow create ticket and thread visibility settings
369 *
370 * @param integer $role_id - agent role id.
371 * @return void
372 */
373 public static function after_add_new_role( $role_id ) {
374
375 $gs = get_option( 'wpsc-gs-general' );
376 $allow_ticket = $gs['allow-create-ticket'];
377 $allow_thread = $gs['allow-ar-thread-email'];
378 $allow_close = $gs['allow-close-ticket'];
379
380 $allow_ticket[] = $role_id;
381 $allow_thread[] = $role_id;
382 $allow_close[] = $role_id;
383
384 $gs['allow-create-ticket'] = $allow_ticket;
385 $gs['allow-ar-thread-email'] = $allow_thread;
386 $gs['allow-close-ticket'] = $allow_close;
387 update_option( 'wpsc-gs-general', $gs );
388 }
389
390 /**
391 * After agent role cloned, add the new role wherever the source role was allowed.
392 *
393 * @param integer $new_role_id - newly cloned agent role id.
394 * @param integer $source_role_id - source agent role id that was cloned.
395 * @return void
396 */
397 public static function after_clone_role( $new_role_id, $source_role_id ) {
398
399 $gs = get_option( 'wpsc-gs-general' );
400
401 foreach ( array( 'allow-create-ticket', 'allow-ar-thread-email', 'allow-close-ticket' ) as $key ) {
402
403 $allowed = isset( $gs[ $key ] ) ? $gs[ $key ] : array();
404 if ( in_array( $source_role_id, $allowed ) ) {
405 $allowed[] = $new_role_id;
406 $gs[ $key ] = $allowed;
407 }
408 }
409
410 update_option( 'wpsc-gs-general', $gs );
411 }
412
413 /**
414 * After agent role deleted remove that role in allow create ticket and thread visibility settings
415 *
416 * @param integer $role_id - agent role id.
417 * @return void
418 */
419 public static function after_delete_role( $role_id ) {
420
421 $gs = get_option( 'wpsc-gs-general' );
422 $allow_ticket = $gs['allow-create-ticket'];
423 $allow_thread = $gs['allow-ar-thread-email'];
424 $allow_close = $gs['allow-close-ticket'];
425
426 if ( in_array( $role_id, $allow_ticket ) ) {
427 unset( $allow_ticket[ $role_id ] );
428 }
429 if ( in_array( $role_id, $allow_thread ) ) {
430 unset( $allow_thread[ $role_id ] );
431 }
432 if ( in_array( $role_id, $allow_close ) ) {
433 unset( $allow_close[ $role_id ] );
434 }
435
436 $gs['allow-create-ticket'] = $allow_ticket;
437 $gs['allow-ar-thread-email'] = $allow_thread;
438 $gs['allow-close-ticket'] = $allow_close;
439 update_option( 'wpsc-gs-general', $gs );
440 }
441 }
442 endif;
443
444 WPSC_GS_General::init();
445