PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.7.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.7.1
2.12.8 2.12.7 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 All 98 releases
sureforms / inc / ai-form-builder / field-mapping.php

field-mapping.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 2.7.1, at inc/ai-form-builder/field-mapping.php

329 lines 13.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SureForms - AI Form Builder.
4 *
5 * @package sureforms
6 * @since 0.0.8
7 */
8
9 namespace SRFM\Inc\AI_Form_Builder;
10
11 use SRFM\Inc\Helper;
12 use SRFM\Inc\Traits\Get_Instance;
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * SureForms AI Form Builder Class.
21 */
22 class Field_Mapping {
23 use Get_Instance;
24
25 /**
26 * Generate Gutenberg Fields from AI data.
27 *
28 * @param \WP_REST_Request $request Full details about the request.
29 * @return string
30 */
31 public static function generate_gutenberg_fields_from_questions( $request ) {
32
33 // Get params from request.
34 $params = $request->get_params();
35
36 // check parama is empty or not and is an array and consist form_data key.
37 if ( empty( $params ) || ! is_array( $params ) || ! isset( $params['form_data'] ) || 0 === count( $params['form_data'] ) ) {
38 return '';
39 }
40
41 // Get questions from form data.
42 $form_data = $params['form_data'];
43 if ( empty( $form_data ) || ! is_array( $form_data ) ) {
44 return '';
45 }
46
47 $form = $form_data['form'];
48 if ( empty( $form ) || ! is_array( $form ) ) {
49 return '';
50 }
51
52 $form_fields = $form['formFields'];
53 // if questions is empty then return empty string.
54 if ( empty( $form_fields ) || ! is_array( $form ) ) {
55 return '';
56 }
57
58 // Initialize post content string.
59 $post_content = '';
60
61 $is_conversational = isset( $params['is_conversional'] ) ? filter_var( $params['is_conversional'], FILTER_VALIDATE_BOOLEAN ) : false;
62 $form_type = isset( $params['form_type'] ) ? Helper::get_string_value( $params['form_type'] ) : 'simple';
63
64 // Filer to skip fields while mapping the fields.
65 $skip_fields = apply_filters( 'srfm_ai_field_map_skip_fields', [], $is_conversational, $form_type );
66
67 // Loop through questions.
68 foreach ( $form_fields as $question ) {
69
70 // Check if question is empty then continue to next question.
71 if ( empty( $question ) || ! is_array( $question ) ) {
72 return '';
73 }
74
75 // Initialize common attributes.
76 $common_attributes = [
77 'block_id' => bin2hex( random_bytes( 4 ) ), // Generate random block_id.
78 'formId' => 0, // Set your formId here.
79 ];
80
81 // Merge common attributes with question attributes.
82 $merged_attributes = array_merge(
83 $common_attributes,
84 [
85 'label' => sanitize_text_field( $question['label'] ),
86 'required' => filter_var( $question['required'], FILTER_VALIDATE_BOOLEAN ),
87 'help' => isset( $question['helpText'] ) ? sanitize_text_field( $question['helpText'] ) : '',
88 'slug' => isset( $question['slug'] ) ? sanitize_text_field( $question['slug'] ) : '',
89 ]
90 );
91
92 // Apply filter to modify field type.
93 $field_type = apply_filters( 'srfm_ai_field_modify_field_type', $question['fieldType'], $question, $is_conversational, $form_type );
94
95 // Determine field type based on field_type.
96 switch ( $field_type ) {
97 case 'input':
98 case 'email':
99 case 'number':
100 case 'textarea':
101 case 'dropdown':
102 case 'checkbox':
103 case 'address':
104 case 'inline-button':
105 case 'gdpr':
106 case 'multi-choice':
107 case 'url':
108 case 'phone':
109 case 'payment':
110 // if payment block then map payment specific attributes.
111 if ( 'payment' === $field_type ) {
112 $merged_attributes['customerNameField'] = isset( $question['customerNameField'] ) ? sanitize_text_field( $question['customerNameField'] ) : '';
113 $merged_attributes['customerEmailField'] = isset( $question['customerEmailField'] ) ? sanitize_text_field( $question['customerEmailField'] ) : '';
114 $merged_attributes['paymentType'] = isset( $question['paymentType'] ) && in_array( $question['paymentType'], [ 'one-time', 'subscription' ], true ) ? sanitize_text_field( $question['paymentType'] ) : 'one-time';
115 $merged_attributes['subscriptionPlan'] = isset( $question['subscriptionPlan'] ) && is_array( $question['subscriptionPlan'] ) ? [
116 'name' => isset( $question['subscriptionPlan']['name'] ) ? sanitize_text_field( $question['subscriptionPlan']['name'] ) : 'Subscription Plan',
117 'interval' => isset( $question['subscriptionPlan']['interval'] ) && in_array( $question['subscriptionPlan']['interval'], [ 'day', 'week', 'month', 'year' ], true ) ? sanitize_text_field( $question['subscriptionPlan']['interval'] ) : 'month',
118 'billingCycles' => isset( $question['subscriptionPlan']['billingCycles'] ) ? sanitize_text_field( $question['subscriptionPlan']['billingCycles'] ) : 'ongoing',
119 ] : [
120 'name' => 'Subscription Plan',
121 'interval' => 'month',
122 'billingCycles' => 'ongoing',
123 ];
124 $merged_attributes['amountType'] = isset( $question['amountType'] ) && in_array( $question['amountType'], [ 'fixed', 'variable', 'user-choice' ], true ) ? sanitize_text_field( $question['amountType'] ) : 'fixed';
125 $merged_attributes['fixedAmount'] = isset( $question['fixedAmount'] ) && is_numeric( $question['fixedAmount'] ) ? floatval( $question['fixedAmount'] ) : 10;
126 $merged_attributes['minimumAmount'] = isset( $question['minimumAmount'] ) && is_numeric( $question['minimumAmount'] ) ? floatval( $question['minimumAmount'] ) : 0;
127 $merged_attributes['amountLabel'] = isset( $question['amountLabel'] ) ? sanitize_text_field( $question['amountLabel'] ) : 'Enter Amount';
128 $merged_attributes['variableAmountField'] = isset( $question['variableAmountField'] ) ? sanitize_text_field( $question['variableAmountField'] ) : '';
129 }
130
131 // Handle specific attributes for certain fields.
132 if ( 'dropdown' === $field_type && ! empty( $question['fieldOptions'] ) && is_array( $question['fieldOptions'] ) &&
133 ! empty( $question['fieldOptions'][0]['label'] )
134 ) {
135 $merged_attributes['options'] = $question['fieldOptions'];
136
137 if ( isset( $question['showValues'] ) ) {
138 $merged_attributes['showValues'] = filter_var( $question['showValues'], FILTER_VALIDATE_BOOLEAN );
139 }
140
141 // remove icon from options for the dropdown field.
142 foreach ( $merged_attributes['options'] as $key => $option ) {
143 if ( ! empty( $merged_attributes['options'][ $key ]['icon'] ) ) {
144 $merged_attributes['options'][ $key ]['icon'] = '';
145 }
146 }
147 }
148 if ( 'multi-choice' === $field_type ) {
149
150 // Remove duplicate icons and clear icons if all are the same.
151 $icons = array_column( $question['fieldOptions'], 'icon' );
152 $options = array_column( $question['fieldOptions'], 'optionTitle' );
153 $unique_icons = array_unique( $icons );
154 if ( count( $unique_icons ) === 1 || count( $options ) !== count( $icons ) ) {
155 foreach ( $question['fieldOptions'] as &$option ) {
156 $option['icon'] = '';
157 }
158 }
159
160 // Set options if they are valid.
161 if ( ! empty( $question['fieldOptions'][0]['optionTitle'] ) ) {
162 $merged_attributes['options'] = $question['fieldOptions'];
163 }
164
165 // Determine vertical layout based on icons.
166 if ( ! empty( $merged_attributes['options'] ) ) {
167 $merged_attributes['verticalLayout'] = array_reduce(
168 $merged_attributes['options'],
169 static fn( $carry, $option ) => $carry && ! empty( $option['icon'] ),
170 true
171 );
172 }
173
174 if ( isset( $question['showValues'] ) ) {
175 $merged_attributes['showValues'] = filter_var( $question['showValues'], FILTER_VALIDATE_BOOLEAN );
176 }
177
178 // Set single selection if provided.
179 if ( isset( $question['singleSelection'] ) ) {
180 $merged_attributes['singleSelection'] = filter_var( $question['singleSelection'], FILTER_VALIDATE_BOOLEAN );
181 }
182
183 // Set choiceWidth for options divisible by 3.
184 if ( ! empty( $merged_attributes['options'] ) && count( $merged_attributes['options'] ) % 3 === 0 ) {
185 $merged_attributes['choiceWidth'] = 33.33;
186 }
187 }
188 if ( 'phone' === $field_type ) {
189 $merged_attributes['autoCountry'] = true;
190 }
191
192 // Apply filter to modify merged attributes.
193 $merged_attributes = apply_filters( 'srfm_ai_form_builder_modify_merged_attributes', $merged_attributes, $question, $is_conversational, $form_type );
194
195 // if field type is needs to be skipped then skip that field.
196 if ( ! empty( $skip_fields ) && in_array( $field_type, $skip_fields, true ) ) {
197 break;
198 }
199
200 $post_content .= '<!-- wp:srfm/' . $field_type . ' ' . Helper::encode_json( $merged_attributes ) . ' /-->' . PHP_EOL;
201 break;
202 case 'slider':
203 case 'page-break':
204 case 'date-picker':
205 case 'time-picker':
206 case 'upload':
207 case 'hidden':
208 case 'rating':
209 case 'signature':
210 // If pro version is not active then do not add pro fields.
211 if ( ! defined( 'SRFM_PRO_VER' ) ) {
212 break;
213 }
214
215 if ( 'signature' === $field_type && defined( 'SRFM_PRO_PRODUCT' ) && SRFM_PRO_PRODUCT === 'SureForms Starter' ) {
216 // If the product is SureForms Starter then skip the signature field.
217 break;
218 }
219
220 // Handle specific attributes for certain pro fields.
221 if ( 'slider' === $field_type ) {
222 $merged_attributes['min'] = ! empty( $question['min'] ) ? filter_var( $question['min'], FILTER_VALIDATE_INT ) : 0;
223 $merged_attributes['max'] = ! empty( $question['max'] ) ? filter_var( $question['max'], FILTER_VALIDATE_INT ) : 100;
224 $merged_attributes['step'] = ! empty( $question['step'] ) ? filter_var( $question['step'], FILTER_VALIDATE_INT ) : 1;
225
226 $merged_attributes['prefixTooltip'] = ! empty( $question['prefixTooltip'] ) ? $question['prefixTooltip'] : '';
227 $merged_attributes['suffixTooltip'] = ! empty( $question['suffixTooltip'] ) ? $question['suffixTooltip'] : '';
228
229 // get min and max then diveide by 2 and round it.
230 $min = $merged_attributes['min'];
231 $max = $merged_attributes['max'];
232
233 if ( is_numeric( $min ) && is_numeric( $max ) ) {
234 $min = intval( $min );
235 $max = intval( $max );
236
237 // If min and max are same then set the value to 0.
238 $merged_attributes['numberDefaultValue'] = Helper::get_string_value( round( ( $min + $max ) / 2 ) );
239 }
240 }
241 if ( 'date-picker' === $field_type ) {
242 $merged_attributes['dateFormat'] = ! empty( $question['dateFormat'] ) ? sanitize_text_field( $question['dateFormat'] ) : 'mm/dd/yy';
243 $merged_attributes['min'] = ! empty( $question['minDate'] ) ? sanitize_text_field( $question['minDate'] ) : '';
244 $merged_attributes['max'] = ! empty( $question['maxDate'] ) ? sanitize_text_field( $question['maxDate'] ) : '';
245 }
246 if ( 'time-picker' === $field_type ) {
247 $merged_attributes['increment'] = ! empty( $question['increment'] ) ? filter_var( $question['increment'], FILTER_VALIDATE_INT ) : 30;
248 $merged_attributes['showTwelveHourFormat'] = ! empty( $question['showTwelveHourFormat'] ) ? filter_var( $question['useTwelveHourFormat'], FILTER_VALIDATE_BOOLEAN ) : false;
249 $merged_attributes['min'] = ! empty( $question['minTime'] ) ? sanitize_text_field( $question['minTime'] ) : '';
250 $merged_attributes['max'] = ! empty( $question['maxTime'] ) ? sanitize_text_field( $question['maxTime'] ) : '';
251 }
252 if ( 'rating' === $field_type ) {
253 $merged_attributes['iconShape'] = ! empty( $question['iconShape'] ) ? sanitize_text_field( $question['iconShape'] ) : 'star';
254 $merged_attributes['showText'] = ! empty( $question['showTooltip'] ) ? filter_var( $question['showTooltip'], FILTER_VALIDATE_BOOLEAN ) : false;
255 $merged_attributes['defaultRating'] = ! empty( $question['defaultRating'] ) ? filter_var( $question['defaultRating'], FILTER_VALIDATE_INT ) : 0;
256
257 if ( ! empty( $merged_attributes['showText'] ) ) {
258 foreach ( $question['tooltipValues'] as $tooltips ) {
259 $i = 0;
260 foreach ( $tooltips as $value ) {
261 $merged_attributes['ratingText'][ $i ] = ! empty( $value ) ? sanitize_text_field( $value ) : '';
262 $i++;
263 }
264 }
265 }
266 }
267 if ( 'upload' === $field_type ) {
268 if ( ! empty( $question['allowedTypes'] ) ) {
269 $allowed_types = str_replace( '.', '', $question['allowedTypes'] );
270
271 $allowed_types = explode( ',', $allowed_types );
272
273 $types_array = array_map(
274 static function( $type ) {
275 return [
276 'value' => trim( $type ),
277 'label' => trim( $type ),
278 ];
279 },
280 $allowed_types
281 );
282
283 $merged_attributes['allowedFormats'] = $types_array;
284 } else {
285 $merged_attributes['allowedFormats'] = [
286 [
287 'value' => 'jpg',
288 'label' => 'jpg',
289 ],
290 [
291 'value' => 'jpeg',
292 'label' => 'jpeg',
293 ],
294 [
295 'value' => 'gif',
296 'label' => 'gif',
297 ],
298 [
299 'value' => 'png',
300 'label' => 'png',
301 ],
302 [
303 'value' => 'pdf',
304 'label' => 'pdf',
305 ],
306 ];
307 }
308
309 $merged_attributes['fileSizeLimit'] = ! empty( $question['uploadSize'] ) ? filter_var( $question['uploadSize'], FILTER_VALIDATE_INT ) : 10;
310
311 $merged_attributes['multiple'] = ! empty( $question['multiUpload'] ) ? filter_var( $question['multiUpload'], FILTER_VALIDATE_BOOLEAN ) : false;
312
313 $merged_attributes['maxFiles'] = ! empty( $question['multiFilesNumber'] ) ? filter_var( $question['multiFilesNumber'], FILTER_VALIDATE_INT ) : 2;
314
315 }
316
317 $post_content .= '<!-- wp:srfm/' . $field_type . ' ' . Helper::encode_json( $merged_attributes ) . ' /-->' . PHP_EOL;
318 break;
319 default:
320 // Unsupported field type - fallback to input.
321 $post_content .= '<!-- wp:srfm/input ' . Helper::encode_json( $merged_attributes ) . ' /-->' . PHP_EOL;
322 }
323 }
324
325 return apply_filters( 'srfm_ai_form_builder_post_content', $post_content, $is_conversational, $form_type );
326 }
327
328 }
329