PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.12.3
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.12.3
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / rest-api.php
rest-api.php
332 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rest API Manager Class.
4 *
5 * @package sureforms.
6 */
7
8 namespace SRFM\Inc;
9
10 use SRFM\Inc\AI_Form_Builder\AI_Auth;
11 use SRFM\Inc\AI_Form_Builder\AI_Form_Builder;
12 use SRFM\Inc\AI_Form_Builder\Field_Mapping;
13 use SRFM\Inc\Database\Tables\Entries;
14 use SRFM\Inc\Traits\Get_Instance;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 /**
21 * Rest API handler class.
22 *
23 * @since 0.0.7
24 */
25 class Rest_Api {
26 use Get_Instance;
27
28 /**
29 * Constructor
30 *
31 * @since 0.0.7
32 * @return void
33 */
34 public function __construct() {
35 add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
36 }
37
38 /**
39 * Register endpoints
40 *
41 * @since 0.0.7
42 * @return void
43 */
44 public function register_endpoints() {
45
46 $prefix = 'sureforms';
47 $version_slug = 'v1';
48
49 $endpoints = $this->get_endpoints();
50
51 foreach ( $endpoints as $endpoint => $args ) {
52 register_rest_route(
53 $prefix . '/' . $version_slug,
54 $endpoint,
55 $args
56 );
57 }
58 }
59
60 /**
61 * Checks whether the value is boolean or not.
62 *
63 * @param mixed $value value to be checked.
64 * @since 0.0.8
65 * @return bool
66 */
67 public function sanitize_boolean_field( $value ) {
68 return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
69 }
70
71 /**
72 * Get the data for generating entries chart.
73 *
74 * @param \WP_REST_Request $request Full details about the request.
75 * @since 1.0.0
76 * @return array<mixed>
77 */
78 public function get_entries_chart_data( $request ) {
79 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
80
81 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
82 wp_send_json_error( __( 'Nonce verification failed.', 'sureforms' ) );
83 }
84
85 $params = $request->get_params();
86
87 if ( empty( $params ) ) {
88 wp_send_json_error( __( 'Request could not be processed.', 'sureforms' ) );
89 }
90
91 $after = is_array( $params ) && ! empty( $params['after'] ) ? sanitize_text_field( Helper::get_string_value( $params['after'] ) ) : '';
92 $before = is_array( $params ) && ! empty( $params['before'] ) ? sanitize_text_field( Helper::get_string_value( $params['before'] ) ) : '';
93
94 if ( empty( $after ) || empty( $before ) ) {
95 wp_send_json_error( __( 'Invalid date.', 'sureforms' ) );
96 }
97
98 $form = is_array( $params ) && ! empty( $params['form'] ) ? sanitize_text_field( Helper::get_string_value( $params['form'] ) ) : '';
99
100 $where = [
101 [
102 [
103 'key' => 'created_at',
104 'value' => $after,
105 'compare' => '>=',
106 ],
107 [
108 'key' => 'created_at',
109 'value' => $before,
110 'compare' => '<=',
111 ],
112 ],
113 ];
114
115 if ( ! empty( $form ) ) {
116 $where[0][] = [
117 'key' => 'form_id',
118 'value' => $form,
119 'compare' => '=',
120 ];
121 }
122
123 return Entries::get_instance()->get_results(
124 $where,
125 'created_at',
126 [ 'ORDER BY created_at DESC' ]
127 );
128 }
129
130 /**
131 * Get the data for all the forms.
132 *
133 * @param \WP_REST_Request $request Full details about the request.
134 * @since 1.7.0
135 * @return array<mixed>
136 */
137 public function get_form_data( $request ) {
138 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
139
140 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
141 wp_send_json_error( __( 'Nonce verification failed.', 'sureforms' ) );
142 }
143
144 $forms = Helper::get_instance()->get_sureforms();
145
146 return ! empty( $forms ) ? $forms : [];
147 }
148
149 /**
150 * Set onboarding completion status.
151 *
152 * @param \WP_REST_Request $request Full details about the request.
153 * @since 1.9.1
154 * @return \WP_REST_Response
155 */
156 public function set_onboarding_status( $request ) {
157 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
158
159 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
160 return new \WP_REST_Response(
161 [ 'error' => __( 'Nonce verification failed.', 'sureforms' ) ],
162 403
163 );
164 }
165
166 // Set the onboarding status to yes always.
167 Onboarding::get_instance()->set_onboarding_status( 'yes' );
168
169 // Get analytics data from request.
170 $analytics_data = $request->get_param( 'analyticsData' );
171
172 // Save analytics data if provided.
173 if ( $analytics_data ) {
174 // Use Helper::update_srfm_option instead of update_option.
175 Helper::update_srfm_option( 'onboarding_analytics', $analytics_data );
176 }
177
178 return new \WP_REST_Response( [ 'success' => true ] );
179 }
180
181 /**
182 * Get onboarding completion status.
183 *
184 * @param \WP_REST_Request $request Full details about the request.
185 * @since 1.9.1
186 * @return \WP_REST_Response
187 */
188 public function get_onboarding_status( $request ) {
189 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
190
191 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
192 return new \WP_REST_Response(
193 [ 'error' => __( 'Nonce verification failed.', 'sureforms' ) ],
194 403
195 );
196 }
197
198 $status = Onboarding::get_instance()->get_onboarding_status();
199
200 return new \WP_REST_Response( [ 'completed' => $status ] );
201 }
202
203 /**
204 * Get plugin status for specified plugin.
205 *
206 * @param \WP_REST_Request $request Full details about the request.
207 * @since 1.9.1
208 * @return \WP_REST_Response
209 */
210 public function get_plugin_status( $request ) {
211 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
212
213 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
214 return new \WP_REST_Response(
215 [ 'error' => __( 'Nonce verification failed.', 'sureforms' ) ],
216 403
217 );
218 }
219
220 $params = $request->get_params();
221 $plugin_slug = is_array( $params ) && isset( $params['plugin'] ) ?
222 sanitize_text_field( Helper::get_string_value( $params['plugin'] ) ) : '';
223
224 if ( empty( $plugin_slug ) ) {
225 return new \WP_REST_Response(
226 [ 'error' => __( 'Plugin slug is required.', 'sureforms' ) ],
227 400
228 );
229 }
230
231 $integrations = Helper::sureforms_get_integration();
232
233 if ( ! isset( $integrations[ $plugin_slug ] ) ) {
234 return new \WP_REST_Response(
235 [ 'error' => __( 'Plugin not found.', 'sureforms' ) ],
236 404
237 );
238 }
239
240 $plugin_data = $integrations[ $plugin_slug ];
241
242 // Get fresh status.
243 if ( is_array( $plugin_data ) && isset( $plugin_data['path'] ) ) {
244 $plugin_data['status'] = Helper::get_plugin_status( Helper::get_string_value( $plugin_data['path'] ) );
245 }
246
247 return new \WP_REST_Response( $plugin_data );
248 }
249
250 /**
251 * Get endpoints
252 *
253 * @since 0.0.7
254 * @return array<array<mixed>>
255 */
256 private function get_endpoints() {
257 /*
258 * @internal This filter is used to add custom endpoints.
259 * @since 1.2.0
260 * @param array<array<mixed>> $endpoints Endpoints.
261 */
262 return apply_filters(
263 'srfm_rest_api_endpoints',
264 [
265 'generate-form' => [
266 'methods' => 'POST',
267 'callback' => [ AI_Form_Builder::get_instance(), 'generate_ai_form' ],
268 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
269 'args' => [
270 'use_system_message' => [
271 'sanitize_callback' => [ $this, 'sanitize_boolean_field' ],
272 ],
273 ],
274 ],
275 // This route is used to map the AI response to SureForms fields markup.
276 'map-fields' => [
277 'methods' => 'POST',
278 'callback' => [ Field_Mapping::get_instance(), 'generate_gutenberg_fields_from_questions' ],
279 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
280 ],
281 // This route is used to initiate auth process when user tries to authenticate on billing portal.
282 'initiate-auth' => [
283 'methods' => 'GET',
284 'callback' => [ AI_Auth::get_instance(), 'get_auth_url' ],
285 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
286 ],
287 // This route is to used to decrypt the access key and save it in the database.
288 'handle-access-key' => [
289 'methods' => 'POST',
290 'callback' => [ AI_Auth::get_instance(), 'handle_access_key' ],
291 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
292 ],
293 // This route is to get the form submissions for the last 30 days.
294 'entries-chart-data' => [
295 'methods' => 'GET',
296 'callback' => [ $this, 'get_entries_chart_data' ],
297 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
298 ],
299 // This route is to get all forms data.
300 'form-data' => [
301 'methods' => 'GET',
302 'callback' => [ $this, 'get_form_data' ],
303 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
304 ],
305 // Onboarding endpoints.
306 'onboarding/set-status' => [
307 'methods' => 'POST',
308 'callback' => [ $this, 'set_onboarding_status' ],
309 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
310 ],
311 'onboarding/get-status' => [
312 'methods' => 'GET',
313 'callback' => [ $this, 'get_onboarding_status' ],
314 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
315 ],
316 // Plugin status endpoint.
317 'plugin-status' => [
318 'methods' => 'GET',
319 'callback' => [ $this, 'get_plugin_status' ],
320 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
321 'args' => [
322 'plugin' => [
323 'required' => true,
324 'sanitize_callback' => 'sanitize_text_field',
325 ],
326 ],
327 ],
328 ]
329 );
330 }
331 }
332