PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.3
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.3
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-custom-fields.php

class-wpsc-rest-custom-fields.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.3, at includes/rest-api/class-wpsc-rest-custom-fields.php

363 lines 9.2 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_Custom_Fields' ) ) :
7
8 final class WPSC_REST_Custom_Fields {
9
10 /**
11 * Array of custom field slugs which we do not want to expose to REST API
12 *
13 * @var array
14 */
15 public static $prevent_data = array();
16
17 /**
18 * Initialize this class
19 *
20 * @return void
21 */
22 public static function init() {
23
24 add_action( 'init', array( __CLASS__, 'load_prevent_data' ) );
25 add_action( 'wpsc_rest_register_routes', array( __CLASS__, 'register_routes' ) );
26 }
27
28 /**
29 * Load prevent data
30 *
31 * @return void
32 */
33 public static function load_prevent_data() {
34
35 self::$prevent_data = apply_filters(
36 'wpsc_rest_prevent_cf_data',
37 array(
38 'date_display_as',
39 'date_format',
40 'is_personal_info',
41 'allow_ticket_form',
42 'allow_my_profile',
43 'tl_width',
44 'load_order',
45 )
46 );
47 }
48
49 /**
50 * Register routes
51 *
52 * @return void
53 */
54 public static function register_routes() {
55
56 // list custom fields.
57 register_rest_route(
58 'supportcandy/v2',
59 '/custom-fields',
60 array(
61 'methods' => 'GET',
62 'callback' => array( __CLASS__, 'get_custom_fields' ),
63 'args' => array(
64 'per_page' => array(
65 'default' => 20,
66 'validate_callback' => array( 'WPSC_REST_API', 'validate_integer_value' ),
67 ),
68 'page' => array(
69 'default' => 1,
70 'validate_callback' => array( 'WPSC_REST_API', 'validate_integer_value' ),
71 ),
72 'filter' => array(
73 'default' => 'all',
74 'validate_callback' => array( __CLASS__, 'validate_filter' ),
75 ),
76 ),
77 'permission_callback' => 'is_user_logged_in',
78 ),
79 );
80
81 // list individual custom field.
82 register_rest_route(
83 'supportcandy/v2',
84 '/custom-fields/(?P<id>\d+)',
85 array(
86 'methods' => 'GET',
87 'callback' => array( __CLASS__, 'get_individual_custom_field' ),
88 'args' => array(
89 'id' => array(
90 'validate_callback' => array( __CLASS__, 'validate_cf_id' ),
91 ),
92 ),
93 'permission_callback' => 'is_user_logged_in',
94 ),
95 );
96
97 // list custom field options.
98 register_rest_route(
99 'supportcandy/v2',
100 '/custom-fields/(?P<id>\d+)/options',
101 array(
102 'methods' => 'GET',
103 'callback' => array( __CLASS__, 'get_options' ),
104 'args' => array(
105 'id' => array(
106 'validate_callback' => array( __CLASS__, 'validate_cf_id' ),
107 ),
108 ),
109 'permission_callback' => 'is_user_logged_in',
110 ),
111 );
112
113 // individual custom field option.
114 register_rest_route(
115 'supportcandy/v2',
116 '/custom-fields/(?P<id>\d+)/options/(?P<option_id>\d+)',
117 array(
118 'methods' => 'GET',
119 'callback' => array( __CLASS__, 'get_individual_option' ),
120 'args' => array(
121 'id' => array(
122 'validate_callback' => array( __CLASS__, 'validate_cf_id' ),
123 ),
124 'option_id' => array(
125 'validate_callback' => array( __CLASS__, 'validate_option_id' ),
126 ),
127 ),
128 'permission_callback' => 'is_user_logged_in',
129 ),
130 );
131 }
132
133 /**
134 * Custom field collection
135 *
136 * @param WP_REST_Request $request - request object.
137 * @return WP_Error|WP_REST_Response
138 */
139 public static function get_custom_fields( $request ) {
140
141 $filter = $request->get_param( 'filter' );
142
143 $filters = array(
144 'items_per_page' => $request->get_param( 'per_page' ),
145 'page_no' => $request->get_param( 'page' ),
146 'meta_query' => array(
147 'relation' => 'AND',
148 ),
149 );
150
151 switch ( $filter ) {
152
153 case 'ticket_fields':
154 $filters['meta_query'][] = array(
155 'slug' => 'field',
156 'compare' => '=',
157 'val' => 'ticket',
158 );
159 break;
160
161 case 'agentonly_fields':
162 $filters['meta_query'][] = array(
163 'slug' => 'field',
164 'compare' => '=',
165 'val' => 'agentonly',
166 );
167 break;
168
169 case 'customer_fields':
170 $filters['meta_query'][] = array(
171 'slug' => 'field',
172 'compare' => '=',
173 'val' => 'customer',
174 );
175 break;
176
177 default:
178 $filters = apply_filters( 'wpsc_rest_cf_query_filter', $filters, $filter );
179 }
180
181 $custom_fields = WPSC_Custom_Field::find( $filters, false );
182 $custom_fields['results'] = array_map(
183 fn( $cf ) => self::modify_response( $cf ),
184 $custom_fields['results']
185 );
186
187 return new WP_REST_Response( $custom_fields, 200 );
188 }
189
190 /**
191 * Single custom field
192 *
193 * @param WP_REST_Request $request - request object.
194 * @return WP_Error|WP_REST_Response
195 */
196 public static function get_individual_custom_field( $request ) {
197
198 $cf = new WPSC_Custom_Field( $request->get_param( 'id' ) );
199 $data = self::modify_response( $cf->to_array() );
200 return new WP_REST_Response( $data, 200 );
201 }
202
203 /**
204 * Custom field options
205 *
206 * @param WP_REST_Request $request - request object.
207 * @return WP_Error|WP_REST_Response
208 */
209 public static function get_options( $request ) {
210
211 $cf = new WPSC_Custom_Field( $request->get_param( 'id' ) );
212 if ( ! $cf->type::$has_options ) {
213 return new WP_Error( 'invalid_custom_field', 'This custom field does not have any options!', array( 'status' => 400 ) );
214 }
215 $options = $cf->get_options();
216 $data = array();
217 foreach ( $options as $option ) {
218 $data[] = array(
219 'id' => intval( $option->id ),
220 'name' => $option->name,
221 );
222 }
223 return new WP_REST_Response( $data, 200 );
224 }
225
226 /**
227 * Single option
228 *
229 * @param WP_REST_Request $request - request object.
230 * @return WP_Error|WP_REST_Response
231 */
232 public static function get_individual_option( $request ) {
233
234 $option = new WPSC_Option( $request->get_param( 'option_id' ) );
235 $data = array(
236 'id' => intval( $option->id ),
237 'name' => $option->name,
238 );
239 return new WP_REST_Response( $data, 200 );
240 }
241
242 /**
243 * Modify custom field data appropreate for client side
244 *
245 * @param array $cf - custom field array.
246 * @return array
247 */
248 public static function modify_response( $cf ) {
249
250 if ( is_object( $cf ) ) {
251 $cf = $cf->to_array();
252 }
253 $object = WPSC_Custom_Field::get_cf_by_slug( $cf['slug'] );
254
255 foreach ( $cf as $key => $value ) {
256
257 // remove if listed in prevent list.
258 if ( in_array( $key, self::$prevent_data ) ) {
259 unset( $cf[ $key ] );
260 continue;
261 }
262
263 // convert has multiple values to array.
264 if ( WPSC_Custom_Field::$schema[ $key ]['has_multiple_val'] ) {
265 if ( $value ) {
266 $cf[ $key ] = array_filter(
267 array_map(
268 fn( $val ) => is_numeric( $val ) ? intval( $val ) : $val,
269 explode( '|', $value )
270 )
271 );
272 } else {
273 $cf[ $key ] = array();
274 }
275 continue;
276 }
277
278 // empty date value.
279 if ( $value == '0000-00-00 00:00:00' ) {
280 $cf[ $key ] = '';
281 }
282
283 // cast numeric fields into integer.
284 if ( is_numeric( $value ) ) {
285 $cf[ $key ] = intval( $value );
286 }
287 }
288
289 $cf['has_options'] = $object->type::$has_options;
290
291 return $cf;
292 }
293
294 /**
295 * Validate filter
296 *
297 * @param string $param - parameter value.
298 * @param WP_REST_Request $request - request object.
299 * @param string $key - filter key.
300 * @return boolean
301 */
302 public static function validate_filter( $param, $request, $key ) {
303
304 $current_user = WPSC_Current_User::$current_user;
305
306 $filters = apply_filters(
307 'wpsc_rest_custom_field_filters',
308 array( 'all', 'ticket_fields', 'agentonly_fields', 'customer_fields' )
309 );
310
311 if ( $param == 'agentonly_fields' && ! $current_user->is_agent ) {
312 return false;
313 }
314
315 return in_array( $param, $filters );
316 }
317
318 /**
319 * Validate id
320 *
321 * @param string $param - parameter value.
322 * @param WP_REST_Request $request - request object.
323 * @param string $key - filter key.
324 * @return boolean
325 */
326 public static function validate_cf_id( $param, $request, $key ) {
327
328 $error = new WP_Error( 'invalid_id', 'Invalid custom field id', array( 'status' => 400 ) );
329 $cf = new WPSC_Custom_Field( $param );
330 return $cf->id ? true : $error;
331 }
332
333 /**
334 * Validate option id
335 *
336 * @param string $param - parameter value.
337 * @param WP_REST_Request $request - request object.
338 * @param string $key - filter key.
339 * @return boolean
340 */
341 public static function validate_option_id( $param, $request, $key ) {
342
343 $cf = new WPSC_Custom_Field( $request->get_param( 'id' ) );
344 if ( ! $cf->id ) {
345 return new WP_Error( 'invalid_id', 'Invalid custom field id', array( 'status' => 400 ) );
346 }
347 if ( ! $cf->type::$has_options ) {
348 return new WP_Error( 'invalid_custom_field', 'This custom field does not have any options!', array( 'status' => 400 ) );
349 }
350 $option = new WPSC_Option( $param );
351 if ( ! $option->id ) {
352 return new WP_Error( 'invalid_option_id', 'Invalid option id', array( 'status' => 400 ) );
353 }
354 if ( $option->custom_field->id != $cf->id ) {
355 return new WP_Error( 'invalid_option_id', 'Option does not belong to given custom field!', array( 'status' => 400 ) );
356 }
357 return true;
358 }
359 }
360 endif;
361
362 WPSC_REST_Custom_Fields::init();
363