PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.3.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.3.0
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
199 lines 5.1 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 $form_info = $data->get_body();
132 $form_info_obj = json_decode( $form_info );
133
134 // Check if JSON decoding was successful and $form_info_obj is an object.
135 if ( json_last_error() !== JSON_ERROR_NONE || ! is_object( $form_info_obj ) ) {
136 wp_send_json_error(
137 [
138 'message' => __( 'Invalid JSON format.', 'sureforms' ),
139 ]
140 );
141 }
142
143 $required_properties = [ 'template_name', 'form_data' ];
144
145 // Check if required properties exist in the $form_info_obj.
146 foreach ( $required_properties as $property ) {
147 if ( ! property_exists( $form_info_obj, $property ) ) {
148 wp_send_json_error(
149 [
150 'message' => __( 'Missing required properties in form info.', 'sureforms' ),
151 ]
152 );
153 }
154 }
155
156 $title = $form_info_obj->template_name ?? '';
157 $content = $form_info_obj->form_data ?? '';
158 $template_metas = isset( $form_info_obj->template_metas ) ? (array) $form_info_obj->template_metas : [];
159
160 // if post content contains srfm/page-break block, then set _srfm_is_page_break meta to true.
161 if ( strpos( $content, 'srfm/page-break' ) !== false ) {
162 $template_metas['_srfm_is_page_break'] = [ 'true' ];
163 }
164
165 $post_id = wp_insert_post(
166 [
167 'post_title' => $title,
168 'post_content' => $content,
169 'post_status' => 'draft',
170 'post_type' => 'sureforms_form',
171 ]
172 );
173
174 if ( ! empty( $post_id ) ) {
175 if ( ! empty( $template_metas ) ) {
176 $default_post_metas = self::get_default_meta_keys();
177 $post_metas = array_merge( $default_post_metas, $template_metas );
178
179 foreach ( $post_metas as $meta_key => $meta_value ) {
180 add_post_meta( $post_id, $meta_key, $meta_value[0] );
181 }
182 }
183
184 return new WP_REST_Response(
185 [
186 'message' => __( 'SureForms Form created successfully.', 'sureforms' ),
187 'id' => $post_id,
188 ]
189 );
190 }
191 wp_send_json_error(
192 [
193 'message' => __( 'Error creating SureForms Form, ', 'sureforms' ),
194 ]
195 );
196 }
197
198 }
199