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 / rest-api / class-wpsc-rest-sf-ratings.php

class-wpsc-rest-sf-ratings.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.4, at includes/rest-api/class-wpsc-rest-sf-ratings.php

115 lines 2.7 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_SF_Ratings' ) ) :
7
8 final class WPSC_REST_SF_Ratings {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 add_action( 'wpsc_rest_register_routes', array( __CLASS__, 'register_routes' ) );
18 }
19
20 /**
21 * Register routes
22 *
23 * @return void
24 */
25 public static function register_routes() {
26
27 // list ratings.
28 register_rest_route(
29 'supportcandy/v2',
30 '/ratings',
31 array(
32 'methods' => 'GET',
33 'callback' => array( __CLASS__, 'get_ratings' ),
34 'permission_callback' => 'is_user_logged_in',
35 ),
36 );
37
38 // list individual rating.
39 register_rest_route(
40 'supportcandy/v2',
41 '/ratings/(?P<id>\d+)',
42 array(
43 'methods' => 'GET',
44 'callback' => array( __CLASS__, 'get_individual_rating' ),
45 'args' => array(
46 'id' => array(
47 'validate_callback' => array( __CLASS__, 'validate_id' ),
48 ),
49 ),
50 'permission_callback' => 'is_user_logged_in',
51 ),
52 );
53 }
54
55 /**
56 * Ratings collection
57 *
58 * @param WP_REST_Request $request - request object.
59 * @return WP_Error|WP_REST_Response
60 */
61 public static function get_ratings( $request ) {
62
63 $ratings = WPSC_SF_Rating::find( array( 'items_per_page' => 0 ), false )['results'];
64 $data = array();
65 foreach ( $ratings as $rating ) {
66 $data[] = self::modify_response( $rating );
67 }
68 return new WP_REST_Response( $data, 200 );
69 }
70
71 /**
72 * Single rating
73 *
74 * @param WP_REST_Request $request - request object.
75 * @return WP_Error|WP_REST_Response
76 */
77 public static function get_individual_rating( $request ) {
78
79 $rating = new WPSC_SF_Rating( $request->get_param( 'id' ) );
80 $data = self::modify_response( $rating->to_array() );
81 return new WP_REST_Response( $data, 200 );
82 }
83
84 /**
85 * Modify response data appropreate for client side
86 *
87 * @param array $rating - response array.
88 * @return array
89 */
90 public static function modify_response( $rating ) {
91
92 unset( $rating['load_order'] );
93 $rating['id'] = intval( $rating['id'] );
94 return $rating;
95 }
96
97 /**
98 * Validate id
99 *
100 * @param string $param - parameter value.
101 * @param WP_REST_Request $request - request object.
102 * @param string $key - filter key.
103 * @return boolean
104 */
105 public static function validate_id( $param, $request, $key ) {
106
107 $error = new WP_Error( 'invalid_id', 'Invalid rating id', array( 'status' => 400 ) );
108 $rating = new WPSC_SF_Rating( $param );
109 return $rating->id ? true : $error;
110 }
111 }
112 endif;
113
114 WPSC_REST_SF_Ratings::init();
115