PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.7.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.7.1
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 / create-new-form.php
create-new-form.php
217 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Create new Form with Template and return the form ID.
4 *
5 * @package sureforms.
6 * @since 0.0.1
7 */
8
9 namespace SRFM\Inc;
10
11 use SRFM\Inc\Traits\Get_Instance;
12 use WP_Error;
13 use WP_Post_Type;
14 use WP_REST_Response;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 /**
21 * Create New Form.
22 *
23 * @since 0.0.1
24 */
25 class Create_New_Form {
26 use Get_Instance;
27
28 /**
29 * Constructor
30 *
31 * @since 0.0.1
32 */
33 public function __construct() {
34 add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] );
35 }
36
37 /**
38 * Add custom API Route create-new-form.
39 *
40 * @return void
41 * @since 0.0.1
42 */
43 public function register_custom_endpoint() {
44 register_rest_route(
45 'sureforms/v1',
46 '/create-new-form',
47 [
48 'methods' => 'POST',
49 'callback' => [ $this, 'create_form' ],
50 'permission_callback' => [ $this, 'get_items_permissions_check' ],
51 ]
52 );
53 }
54
55 /**
56 * Checks whether a given request has permission to create new forms.
57 *
58 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
59 * @since 0.0.1
60 */
61 public function get_items_permissions_check() {
62 if ( current_user_can( 'edit_posts' ) ) {
63 return true;
64 }
65 foreach ( get_post_types( [ 'show_in_rest' => true ], 'objects' ) as $post_type ) {
66 /**
67 * The post type.
68 *
69 * @var WP_Post_Type $post_type
70 */
71 if ( current_user_can( $post_type->cap->edit_posts ) ) {
72 return true;
73 }
74 }
75
76 return new \WP_Error(
77 'rest_cannot_view',
78 __( 'Sorry, you are not allowed to create forms.', 'sureforms' ),
79 [ 'status' => \rest_authorization_required_code() ]
80 );
81 }
82
83 /**
84 * Get default post metas for form when creating using template.
85 *
86 * @return array<string, array<int, int|string>> Default meta keys.
87 * @since 0.0.2
88 */
89 public static function get_default_meta_keys() {
90 return [
91 '_srfm_submit_button_text' => [ 'Submit' ],
92 '_srfm_use_label_as_placeholder' => [ '' ],
93 '_srfm_single_page_form_title' => [ 1 ],
94 '_srfm_instant_form' => [ '' ],
95 '_srfm_form_container_width' => [ 650 ],
96 '_srfm_bg_type' => [ 'image' ],
97 '_srfm_bg_image' => [ '' ],
98 '_srfm_cover_image' => [ '' ],
99 '_srfm_bg_color' => [ '#ffffff' ],
100 '_srfm_submit_width_backend' => [ 'max-content' ],
101 '_srfm_submit_alignment' => [ 'left' ],
102 '_srfm_submit_alignment_backend' => [ '100%' ],
103 '_srfm_submit_width' => [ '' ],
104 '_srfm_inherit_theme_button' => [ '' ],
105 '_srfm_additional_classes' => [ '' ],
106 '_srfm_submit_type' => [ 'message' ],
107 '_srfm_form_recaptcha' => [ 'none' ],
108 ];
109 }
110
111 /**
112 * Create new form post from selected template.
113 *
114 * @param \WP_REST_Request $data Form Markup Data.
115 *
116 * @return WP_Error|WP_REST_Response
117 * @since 0.0.1
118 */
119 public static function create_form( $data ) {
120 $nonce = Helper::get_string_value( $data->get_header( 'X-WP-Nonce' ) );
121 $nonce = sanitize_text_field( $nonce );
122
123 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
124 wp_send_json_error(
125 [
126 'message' => __( 'Nonce verification failed.', 'sureforms' ),
127 ]
128 );
129 }
130
131 if ( ! is_user_logged_in() ) {
132 // Return error if user is not logged in.
133 wp_send_json_error(
134 [
135 'message' => esc_html__( 'You must be logged in to make this request.', 'sureforms' ),
136 ]
137 );
138 }
139
140 if ( ! current_user_can( 'manage_options' ) ) {
141 // Return error if user does not have permissions to manage options.
142 wp_send_json_error(
143 [
144 'message' => esc_html__( 'You do not have permissions to manage options.', 'sureforms' ),
145 ]
146 );
147 }
148
149 $form_info = $data->get_body();
150 $form_info_obj = json_decode( $form_info );
151
152 // Check if JSON decoding was successful and $form_info_obj is an object.
153 if ( json_last_error() !== JSON_ERROR_NONE || ! is_object( $form_info_obj ) ) {
154 wp_send_json_error(
155 [
156 'message' => __( 'Invalid JSON format.', 'sureforms' ),
157 ]
158 );
159 }
160
161 $required_properties = [ 'template_name', 'form_data' ];
162
163 // Check if required properties exist in the $form_info_obj.
164 foreach ( $required_properties as $property ) {
165 if ( ! property_exists( $form_info_obj, $property ) ) {
166 wp_send_json_error(
167 [
168 'message' => __( 'Missing required properties in form info.', 'sureforms' ),
169 ]
170 );
171 }
172 }
173
174 $title = $form_info_obj->template_name ?? '';
175 $content = $form_info_obj->form_data ?? '';
176
177 // Create post metas for the creating form.
178 $post_metas = apply_filters(
179 'srfm_modify_ai_post_metas',
180 [],
181 $form_info_obj,
182 );
183
184 $post_id = wp_insert_post(
185 [
186 'post_title' => $title,
187 'post_content' => $content,
188 'meta_input' => $post_metas,
189 'post_status' => 'draft',
190 'post_type' => 'sureforms_form',
191 ]
192 );
193
194 if ( ! empty( $post_id ) ) {
195
196 /**
197 * Update _srfm_is_ai_generated meta to true.
198 * If the request is coming here then the form is AI generated.
199 */
200 update_post_meta( $post_id, '_srfm_is_ai_generated', true );
201
202 return new WP_REST_Response(
203 [
204 'message' => __( 'SureForms Form created successfully.', 'sureforms' ),
205 'id' => $post_id,
206 ]
207 );
208 }
209 wp_send_json_error(
210 [
211 'message' => __( 'Error creating SureForms Form, ', 'sureforms' ),
212 ]
213 );
214 }
215
216 }
217