PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.0.3
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.0.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 in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 1.0.3, at inc/rest-api.php

165 lines 4.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 * Check if user can edit posts
62 *
63 * @since 0.0.7
64 * @return bool
65 */
66 public function can_edit_posts() {
67 return current_user_can( 'edit_posts' );
68 }
69
70 /**
71 * Checks whether the value is boolean or not.
72 *
73 * @param mixed $value value to be checked.
74 * @since 0.0.8
75 * @return bool
76 */
77 public function sanitize_boolean_field( $value ) {
78 return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
79 }
80
81 /**
82 * Generate the block slugs as per the request by parsing the post content.
83 *
84 * @param \WP_REST_Request $request Full details about the request.
85 * @since 0.0.7
86 * @return void
87 */
88 public function generate_block_slugs_by_content( $request ) {
89 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
90
91 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
92 wp_send_json_error( __( 'Nonce verification failed.', 'sureforms' ) );
93 }
94
95 $slugs = [];
96 $updated = false;
97 $params = $request->get_params();
98
99 if ( empty( $params['formID'] ) ) {
100 wp_send_json_error( __( 'Invalid request. Form ID missing.', 'sureforms' ) );
101 }
102
103 $form = get_post( absint( $params['formID'] ) );
104 $content = ! empty( $params['content'] ) ? wp_kses_post( $params['content'] ) : '';
105
106 if ( ! is_null( $form ) ) {
107 Helper::process_blocks( parse_blocks( $form->post_content ), $slugs, $updated );
108 }
109
110 Helper::process_blocks( parse_blocks( $content ), $slugs, $updated, '', true );
111
112 wp_send_json_success( $slugs );
113 }
114
115 /**
116 * Get endpoints
117 *
118 * @since 0.0.7
119 * @return array<array<mixed>>
120 */
121 private function get_endpoints() {
122 return [
123 'generate-block-slugs' => [
124 'methods' => 'POST',
125 'callback' => [ $this, 'generate_block_slugs_by_content' ],
126 'permission_callback' => [ $this, 'can_edit_posts' ],
127 ],
128 'generate-form' => [
129 'methods' => 'POST',
130 'callback' => [ AI_Form_Builder::get_instance(), 'generate_ai_form' ],
131 'permission_callback' => [ $this, 'can_edit_posts' ],
132 'args' => [
133 'use_system_message' => [
134 'sanitize_callback' => [ $this, 'sanitize_boolean_field' ],
135 ],
136 ],
137 ],
138 // This route is used to map the AI response to SureForms fields markup.
139 'map-fields' => [
140 'methods' => 'POST',
141 'callback' => [ Field_Mapping::get_instance(), 'generate_gutenberg_fields_from_questions' ],
142 'permission_callback' => [ $this, 'can_edit_posts' ],
143 ],
144 // This route is used to initiate auth process when user tries to authenticate on billing portal.
145 'initiate-auth' => [
146 'methods' => 'GET',
147 'callback' => [ AI_Auth::get_instance(), 'get_auth_url' ],
148 'permission_callback' => [ $this, 'can_edit_posts' ],
149 ],
150 // This route is to used to decrypt the access key and save it in the database.
151 'handle-access-key' => [
152 'methods' => 'POST',
153 'callback' => [ AI_Auth::get_instance(), 'handle_access_key' ],
154 'permission_callback' => [ $this, 'can_edit_posts' ],
155 ],
156 // This route is to get the form submissions for the last 30 days.
157 'entries-chart-data' => [
158 'methods' => 'GET',
159 'callback' => [ Entries::get_instance(), 'get_entries_chart_data' ],
160 'permission_callback' => [ $this, 'can_edit_posts' ],
161 ],
162 ];
163 }
164 }
165