PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 2.3.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v2.3.1
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 / rest_api / v1 / class-rest-v1-helper.php

class-rest-v1-helper.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 2.3.1, at includes/rest_api/v1/class-rest-v1-helper.php

601 lines 18.4 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_Rest_v1_Helper' ) ) :
7
8 final class WPSC_Rest_v1_Helper {
9
10 /**
11 * Check whether auth user has read permission of ticket.
12 */
13 public static function validate_ticket_read_permission( $request ){
14
15 global $current_user, $wpscfunction;
16
17 $params = $request->get_params();
18 $ticket_id = isset($params['id']) ? intval($params['id']) : 0;
19 $public_mode = get_option('wpsc_ticket_public_mode');
20 $ticket = $wpscfunction->get_ticket($ticket_id);
21
22 if( !( $ticket && $current_user && ( $current_user->user_email == $ticket['customer_email'] || $wpscfunction->has_permission('view_ticket',$ticket_id) || $public_mode) ) ){
23 return new WP_Error(
24 'unauthorized',
25 'You are not authorized to view this ticket.',
26 array(
27 'status' => 403,
28 )
29 );
30 }
31
32 return true;
33
34 }
35
36 /**
37 * Check whether auth user has reply permission or not
38 * @param [Array] $request Given by REST fucntionality
39 * @return boolean
40 */
41 public static function validate_ticket_reply_permission( $request ){
42
43 global $current_user, $wpscfunction;
44
45 $params = $request->get_params();
46 $ticket_id = isset($params['id']) ? intval($params['id']) : 0;
47 $public_mode = get_option('wpsc_ticket_public_mode');
48 $ticket = $wpscfunction->get_ticket($ticket_id);
49
50 if( !( $ticket && $current_user && ( $current_user->user_email == $ticket['customer_email'] || $wpscfunction->has_permission('reply_ticket',$ticket_id) || $public_mode) ) ){
51 return new WP_Error(
52 'unauthorized',
53 'You are not authorized to add reply to this ticket.',
54 array(
55 'status' => 403,
56 )
57 );
58 }
59
60 return true;
61
62 }
63
64 /**
65 * Check whether auth user has add note permission or not
66 * @param [Array] $request Given by REST fucntionality
67 * @return boolean
68 */
69 public static function validate_ticket_note_permission( $request ){
70
71 global $current_user, $wpscfunction;
72
73 $params = $request->get_params();
74 $ticket_id = isset($params['id']) ? intval($params['id']) : 0;
75 $ticket = $wpscfunction->get_ticket($ticket_id);
76
77 if( !( $ticket && $current_user && $current_user->has_cap('wpsc_agent') && $wpscfunction->has_permission('add_note',$ticket_id) ) ){
78 return new WP_Error(
79 'unauthorized',
80 'You are not authorized to add note to this ticket.',
81 array(
82 'status' => 403,
83 )
84 );
85 }
86
87 return true;
88
89 }
90
91 /**
92 * Validate whether guest ticket is enabled or not.
93 */
94 public static function validate_guest_user($request){
95
96 $secret_key_validate = SELF::validate_secret_key($request);
97 if (is_wp_error($secret_key_validate)) {
98 return $secret_key_validate;
99 }
100
101 $wpsc_allow_to_create_ticket = get_option('wpsc_allow_to_create_ticket');
102 if (!in_array('guest', $wpsc_allow_to_create_ticket)) {
103 return new WP_Error(
104 'guest_ticket_disabled',
105 'Guest ticket is not allowed. In this case, accessing form fields is not permitted.',
106 array(
107 'status' => 403,
108 )
109 );
110 }
111
112 return true;
113
114 }
115
116 /**
117 * Validate whether user is registered or not
118 */
119 public static function validate_registered_user($request){
120
121 global $current_user;
122
123 if($current_user->ID) {
124 return true;
125 }
126
127 return new WP_Error(
128 'unauthorized',
129 'Either incorrect auth token or auth user.',
130 array(
131 'status' => 403,
132 )
133 );
134
135 }
136
137 /**
138 * Check whether registered user attachment is valid or not
139 */
140 public static function validate_registered_user_attachment($request){
141
142 $is_registered = SELF::validate_registered_user($request);
143 if (is_wp_error($is_registered)) {
144 return $is_registered;
145 }
146
147 $is_valid = SELF::validate_attachment();
148 if (is_wp_error($is_valid)) {
149 return $is_valid;
150 }
151
152 return true;
153
154 }
155
156 /**
157 * Check whether registered user attachment is valid or not
158 */
159 public static function validate_guest_attachment($request){
160
161 $is_guest = SELF::validate_guest_user($request);
162 if (is_wp_error($is_guest)) {
163 return $is_guest;
164 }
165
166 // check whether guest attachments allowed or not
167 $guest_upload = get_option('wpsc_guest_can_upload_files');
168 if (!$guest_upload) {
169 return new WP_Error(
170 'unauthorized',
171 'Guest not allowed file attachments.',
172 array(
173 'status' => 403,
174 )
175 );
176 }
177
178 $is_valid = SELF::validate_attachment();
179 if (is_wp_error($is_valid)) {
180 return $is_valid;
181 }
182
183 return true;
184
185 }
186
187 /**
188 * Check file related validations
189 */
190 public static function validate_attachment(){
191
192 // see if file available or not
193 $file = $_FILES && isset($_FILES['file']) ? $_FILES['file'] : array();
194 if( count($file) == 0 ){
195 return new WP_Error(
196 'file_not_found',
197 'File not found or file too large error.',
198 array(
199 'status' => 403,
200 )
201 );
202 }
203
204 // See if size is within allowed limit or not
205 $max_attachment_limit = get_option('wpsc_attachment_max_filesize');
206 $file_size = isset($file['size']) && $file['size'] ? $file['size'] : 0;
207 $file_size = $file_size ? ($file_size/1000)/1000 : 0;
208 if( $file['tmp_name']=='' || $file_size > $max_attachment_limit ) {
209 return new WP_Error(
210 'file_size_error',
211 'file size exceeded allowed limit!',
212 array(
213 'status' => 403,
214 )
215 );
216 }
217
218 return true;
219
220 }
221
222 /**
223 * Validate whether user has agent capabilities or not
224 */
225 public static function validate_agent($request){
226
227 global $current_user;
228
229 if( $current_user->ID && $current_user->has_cap('wpsc_agent') ) {
230 return true;
231 }
232
233 if ( $current_user->ID ) {
234 return new WP_Error(
235 'unauthorized',
236 'You do not have sufficient permission to access agentonly fields.',
237 array(
238 'status' => 403,
239 )
240 );
241 }
242
243 return new WP_Error(
244 'unauthorized',
245 'Either incorrect auth token or auth user.',
246 array(
247 'status' => 403,
248 )
249 );
250
251 }
252
253 /**
254 * Validate secret key provided
255 */
256 public static function validate_secret_key( $request ){
257
258 $params = $request->get_params();
259 $secret_key = isset($params['secret_key']) ? sanitize_text_field($params['secret_key']) : '';
260 if( $secret_key == get_option('wpsc_rest_api_secret_key') ) {
261 return true;
262 } else {
263 return new WP_Error(
264 'invalid_secret_key',
265 'Invalid Secret Key Provided.',
266 array(
267 'status' => 403,
268 )
269 );
270 }
271
272 }
273
274 /**
275 * User login and return auth token
276 */
277 public static function user_login($request){
278
279 global $wpdb;
280
281 $params = $request->get_params();
282
283 if ( isset($params['username']) && $params['password'] ) {
284
285 $username = sanitize_text_field($params['username']);
286 $password = sanitize_text_field($params['password']);
287 $secret_key = isset($params['secret_key']) ? sanitize_text_field($params['secret_key']) : '';
288 $users = $wpdb->get_results("select * from {$wpdb->prefix}users WHERE user_login='".$username."' OR user_email='".$username."'");
289 if( count($users) && wp_check_password($password,$users[0]->user_pass,$users[0]->ID) ) {
290
291 $auth_token = md5( $secret_key . $users[0]->ID . $secret_key );
292 return array(
293 'auth_user' => $users[0]->ID,
294 'auth_token' => $auth_token
295 );
296
297 } else {
298
299 return new WP_Error(
300 'invalid_login_credential',
301 'Invalid username or password',
302 array(
303 'status' => 403,
304 )
305 );
306
307 }
308
309 } else {
310
311 return new WP_Error(
312 'missing_required_fields',
313 'Required fields are missing for the request.',
314 array(
315 'status' => 403,
316 )
317 );
318
319 }
320
321 }
322
323 /**
324 * Returns ticket form fields
325 */
326 public static function get_ticket_form_fields( $request ){
327
328 include WPSC_ABSPATH . 'includes/rest_api/v1/get_ticket_form_fields.php';
329 return $response;
330
331 }
332
333 /**
334 * Returns agentonly fields
335 */
336 public static function get_agentonly_fields( $request ){
337
338 include WPSC_ABSPATH . 'includes/rest_api/v1/get_agentonly_fields.php';
339 return $response;
340
341 }
342
343 /**
344 * Get all statuses. No authentication needed
345 */
346 public static function get_status_list( $request ){
347
348 include WPSC_ABSPATH . 'includes/rest_api/v1/get_status_list.php';
349 return $response;
350
351 }
352
353 /**
354 * Get status by id
355 */
356 public static function get_status( $request ){
357
358 include WPSC_ABSPATH . 'includes/rest_api/v1/get_status_by_id.php';
359 return $response;
360
361 }
362
363 /**
364 * Get all statuses. No authentication needed
365 */
366 public static function get_category_list( $request ){
367
368 include WPSC_ABSPATH . 'includes/rest_api/v1/get_category_list.php';
369 return $response;
370
371 }
372
373 /**
374 * Get status by id
375 */
376 public static function get_category( $request ){
377
378 include WPSC_ABSPATH . 'includes/rest_api/v1/get_category_by_id.php';
379 return $response;
380
381 }
382
383 /**
384 * Get all priorities. No authentication needed
385 */
386 public static function get_priority_list( $request ){
387
388 include WPSC_ABSPATH . 'includes/rest_api/v1/get_priority_list.php';
389 return $response;
390
391 }
392
393 /**
394 * Get status by id
395 */
396 public static function get_priority( $request ){
397
398 include WPSC_ABSPATH . 'includes/rest_api/v1/get_priority_by_id.php';
399 return $response;
400
401 }
402
403 /**
404 * Get ticket list
405 */
406 public static function get_tickets( $request ){
407
408 include WPSC_ABSPATH . 'includes/rest_api/v1/get_tickets.php';
409 return $response;
410
411 }
412
413 /**
414 * Get individual ticket
415 */
416 public static function get_individual_ticket( $request ){
417
418 $params = $request->get_params();
419 $ticket_id = isset($params['id']) ? intval($params['id']) : 0;
420 $response = array();
421 if($ticket_id){
422 $response = SELF::get_ticket_response($ticket_id);
423 }
424 return $response;
425
426 }
427
428 /**
429 * Get ticket threads
430 */
431 public static function get_ticket_threads( $request ){
432
433 include WPSC_ABSPATH . 'includes/rest_api/v1/get_ticket_threads.php';
434 return $response;
435
436 }
437
438 /**
439 * Get ticket response array for a ticket
440 */
441 public static function get_ticket_response( $ticket_id ) {
442
443 include WPSC_ABSPATH . 'includes/rest_api/v1/get_ticket_response.php';
444 return $response;
445
446 }
447
448 /**
449 * Get thread response array
450 */
451 public static function get_thread_response( $thread ){
452
453 include WPSC_ABSPATH . 'includes/rest_api/v1/get_thread_response.php';
454 return $response;
455
456 }
457
458 /**
459 * Get filters for logged in user
460 */
461 public static function get_filters(){
462
463 include WPSC_ABSPATH . 'includes/rest_api/v1/get_filters.php';
464 return $response;
465
466 }
467
468 /**
469 * Get last thread history id
470 */
471 public static function get_thread_history_id( $request ){
472
473 include WPSC_ABSPATH . 'includes/rest_api/v1/get_thread_history_id.php';
474 return $response;
475
476 }
477
478 /**
479 * Get history of threads from last history id
480 */
481 public static function get_thread_history( $request ){
482
483 include WPSC_ABSPATH . 'includes/rest_api/v1/get_thread_history.php';
484 return $response;
485
486 }
487
488 /**
489 * Get meta query restrict rules for current user
490 */
491 public static function get_tl_meta_query_restrict_rules(){
492
493 include WPSC_ABSPATH . 'includes/rest_api/v1/get_tl_meta_query_restrict_rules.php';
494 return $restrict_rules;
495
496 }
497
498 /**
499 * Create New Ticket
500 */
501 public static function create_new_ticket($request){
502
503 include WPSC_ABSPATH . 'includes/rest_api/v1/create_new_ticket.php';
504 return $response;
505 }
506
507 /**
508 * Submit ticket reply
509 */
510 public static function submit_reply($request){
511
512 include WPSC_ABSPATH . 'includes/rest_api/v1/submit_reply.php';
513 return $response;
514 }
515
516 /**
517 * Submit ticket note
518 */
519 public static function submit_note($request){
520
521 include WPSC_ABSPATH . 'includes/rest_api/v1/submit_note.php';
522 return $response;
523 }
524
525 /**
526 * Update ticket fields
527 */
528 public static function update_ticket_fields($request){
529
530 include WPSC_ABSPATH . 'includes/rest_api/v1/update_ticket_fields.php';
531 return $response;
532 }
533
534 /**
535 * Attach file and return attachment id
536 */
537 public static function file_attachment($request){
538
539 include WPSC_ABSPATH . 'includes/rest_api/v1/file_attachment.php';
540 return $response;
541
542 }
543
544 /**
545 * Attach file and return attachment id
546 */
547 public static function autocomplete_suggestions($request){
548
549 include WPSC_ABSPATH . 'includes/rest_api/v1/autocomplete_suggestions.php';
550 return $response;
551
552 }
553
554 /**
555 * Get all agents
556 */
557 public static function get_agents($request) {
558 include WPSC_ABSPATH . 'includes/rest_api/v1/get_agents.php';
559 return $response;
560 }
561
562 /**
563 * Get agent by id
564 */
565 public static function get_agent($request) {
566 include WPSC_ABSPATH . 'includes/rest_api/v1/get_agent_by_id.php';
567 return $response;
568 }
569
570 /**
571 * Return single agent response
572 */
573 public static function get_agent_info( $agent_id, $agent_roles ){
574
575 $agent = get_term_by('id', $agent_id, 'wpsc_agents');
576 $role_id = get_term_meta( $agent->term_id, 'role', true);
577
578 return array(
579 'id' => $agent->term_id,
580 'name' => get_term_meta( $agent->term_id, 'label', true),
581 'role' => array(
582 'id' => $role_id,
583 'name' => $agent_roles[$role_id]['label']
584 ),
585 );
586
587 }
588
589 /**
590 * Delete single ticket
591 */
592 public static function delete_ticket_by_id($request){
593 include WPSC_ABSPATH . 'includes/rest_api/v1/delete_ticket.php';
594 return $response;
595 }
596
597
598 }
599
600 endif;
601