PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.11.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.11.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
194 lines 4.9 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\AI_Form_Builder\AI_Helper;
12 use SRFM\Inc\Traits\Get_Instance;
13 use WP_Error;
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' => [ Helper::class, 'get_items_permissions_check' ],
51 ]
52 );
53 }
54
55 /**
56 * Get default post metas for form when creating using template.
57 *
58 * @return array<string, int|string|bool> Default meta keys.
59 * @since 2.0.0 updated the return type.
60 * @since 0.0.2
61 */
62 public static function get_default_meta_keys() {
63 $default_values = [
64 '_srfm_submit_button_text' => __( 'Submit', 'sureforms' ),
65 '_srfm_use_label_as_placeholder' => false,
66 '_srfm_single_page_form_title' => 1,
67 '_srfm_instant_form' => '',
68 '_srfm_form_container_width' => 650,
69 '_srfm_bg_type' => 'image',
70 '_srfm_bg_image' => '',
71 '_srfm_cover_image' => '',
72 '_srfm_bg_color' => '#ffffff',
73 '_srfm_submit_width_backend' => 'max-content',
74 '_srfm_submit_alignment' => 'left',
75 '_srfm_submit_alignment_backend' => '100%',
76 '_srfm_submit_width' => '',
77 '_srfm_inherit_theme_button' => false,
78 '_srfm_additional_classes' => '',
79 '_srfm_submit_type' => 'message',
80 '_srfm_form_recaptcha' => 'none',
81 '_srfm_is_inline_button' => false, // @since 2.0.0 -- adding required default value.
82 ];
83
84 return apply_filters( 'srfm_add_post_meta_defaults', $default_values );
85 }
86
87 /**
88 * Create new form post from selected template.
89 *
90 * @param \WP_REST_Request $data Form Markup Data.
91 *
92 * @return WP_Error|WP_REST_Response
93 * @since 0.0.1
94 */
95 public static function create_form( $data ) {
96 $nonce = Helper::get_string_value( $data->get_header( 'X-WP-Nonce' ) );
97 $nonce = sanitize_text_field( $nonce );
98
99 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
100 wp_send_json_error(
101 [
102 'message' => __( 'Nonce verification failed.', 'sureforms' ),
103 ]
104 );
105 }
106
107 $form_info = $data->get_body();
108 $form_info_obj = json_decode( $form_info );
109
110 // Check if JSON decoding was successful and $form_info_obj is an object.
111 if ( json_last_error() !== JSON_ERROR_NONE || ! is_object( $form_info_obj ) ) {
112 wp_send_json_error(
113 [
114 'message' => __( 'Invalid JSON format.', 'sureforms' ),
115 ]
116 );
117 }
118
119 $required_properties = [ 'template_name', 'form_data' ];
120
121 // Check if required properties exist in the $form_info_obj.
122 foreach ( $required_properties as $property ) {
123 if ( ! property_exists( $form_info_obj, $property ) ) {
124 wp_send_json_error(
125 [
126 'message' => __( 'Missing required properties in form info.', 'sureforms' ),
127 ]
128 );
129 }
130 }
131
132 $title = $form_info_obj->template_name ?? '';
133 $content = $form_info_obj->form_data ?? '';
134
135 // Create post metas for the creating form.
136 $post_metas = apply_filters(
137 'srfm_modify_ai_post_metas',
138 [],
139 $form_info_obj,
140 );
141
142 $post_id = wp_insert_post(
143 [
144 'post_title' => $title,
145 'post_content' => $content,
146 'meta_input' => $post_metas,
147 'post_status' => 'draft',
148 'post_type' => 'sureforms_form',
149 ],
150 true
151 );
152
153 if ( is_wp_error( $post_id ) ) {
154 // Pass the raw WP_Error through the shared sanitizer so any URLs,
155 // request IDs, or model names injected by a filter on
156 // wp_insert_post don't leak via the REST response. The raw message
157 // is still logged server-side (gated on WP_DEBUG / WP_DEBUG_LOG).
158 $sanitized = AI_Helper::sanitize_ai_error_message( $post_id->get_error_message(), 'wp_insert_post' );
159 if ( '' === $sanitized ) {
160 $sanitized = __( 'Error creating SureForms Form.', 'sureforms' );
161 }
162 return new WP_REST_Response(
163 [
164 'message' => $sanitized,
165 ],
166 500
167 );
168 }
169
170 if ( ! is_int( $post_id ) || $post_id <= 0 ) {
171 return new WP_REST_Response(
172 [
173 'message' => __( 'Error creating SureForms Form.', 'sureforms' ),
174 ],
175 500
176 );
177 }
178
179 /**
180 * Update _srfm_is_ai_generated meta to true.
181 * If the request is coming here then the form is AI generated.
182 */
183 update_post_meta( $post_id, '_srfm_is_ai_generated', true );
184
185 return new WP_REST_Response(
186 [
187 'message' => __( 'SureForms Form created successfully.', 'sureforms' ),
188 'id' => $post_id,
189 ]
190 );
191 }
192
193 }
194