| 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\Traits\Get_Instance; |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* SureForms AI Form Builder Class. |
| 20 |
*/ |
| 21 |
class Field_Mapping { |
| 22 |
use Get_Instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Generate Gutenberg Fields from AI data. |
| 26 |
* |
| 27 |
* @param \WP_REST_Request $request Full details about the request. |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public static function generate_gutenberg_fields_from_questions( $request ) { |
| 31 |
|
| 32 |
// Get params from request. |
| 33 |
$params = $request->get_params(); |
| 34 |
|
| 35 |
// check parama is empty or not and is an array and consist form_data key. |
| 36 |
if ( empty( $params ) || ! is_array( $params ) || ! isset( $params['form_data'] ) || 0 === count( $params['form_data'] ) ) { |
| 37 |
return ''; |
| 38 |
} |
| 39 |
|
| 40 |
// Get questions from form data. |
| 41 |
$form_data = $params['form_data']; |
| 42 |
if ( empty( $form_data ) || ! is_array( $form_data ) ) { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
$form = $form_data['form']; |
| 47 |
if ( empty( $form ) || ! is_array( $form ) ) { |
| 48 |
return ''; |
| 49 |
} |
| 50 |
|
| 51 |
$form_fields = $form['formFields']; |
| 52 |
// if questions is empty then return empty string. |
| 53 |
if ( empty( $form_fields ) || ! is_array( $form ) ) { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
// Initialize post content string. |
| 58 |
$post_content = ''; |
| 59 |
|
| 60 |
// Loop through questions. |
| 61 |
foreach ( $form_fields as $question ) { |
| 62 |
|
| 63 |
// Check if question is empty then continue to next question. |
| 64 |
if ( empty( $question ) || ! is_array( $question ) ) { |
| 65 |
return ''; |
| 66 |
} |
| 67 |
|
| 68 |
// Initialize common attributes. |
| 69 |
$common_attributes = [ |
| 70 |
'block_id' => bin2hex( random_bytes( 4 ) ), // Generate random block_id. |
| 71 |
'formId' => 0, // Set your formId here. |
| 72 |
]; |
| 73 |
|
| 74 |
// Merge common attributes with question attributes. |
| 75 |
$merged_attributes = array_merge( |
| 76 |
$common_attributes, |
| 77 |
[ |
| 78 |
'label' => sanitize_text_field( $question['label'] ), |
| 79 |
'required' => filter_var( $question['required'], FILTER_VALIDATE_BOOLEAN ), |
| 80 |
'help' => sanitize_text_field( $question['helpText'] ), |
| 81 |
] |
| 82 |
); |
| 83 |
|
| 84 |
// Determine field type based on fieldType. |
| 85 |
switch ( $question['fieldType'] ) { |
| 86 |
case 'input': |
| 87 |
case 'email': |
| 88 |
case 'number': |
| 89 |
case 'textarea': |
| 90 |
case 'dropdown': |
| 91 |
case 'checkbox': |
| 92 |
case 'address': |
| 93 |
case 'inline-button': |
| 94 |
case 'gdpr': |
| 95 |
case 'multi-choice': |
| 96 |
case 'url': |
| 97 |
case 'phone': |
| 98 |
// Handle specific attributes for certain fields. |
| 99 |
if ( 'textarea' === $question['fieldType'] && ! empty( $question['helpText'] ) ) { |
| 100 |
$merged_attributes['textAreaHelpText'] = sanitize_text_field( $question['helpText'] ); |
| 101 |
} |
| 102 |
if ( 'dropdown' === $question['fieldType'] && ! empty( $question['fieldOptions'] ) && is_array( $question['fieldOptions'] ) && |
| 103 |
! empty( $question['fieldOptions'][0]['label'] ) |
| 104 |
) { |
| 105 |
$merged_attributes['options'] = $question['fieldOptions']; |
| 106 |
} |
| 107 |
if ( 'multi-choice' === $question['fieldType'] ) { |
| 108 |
if ( ! empty( $question['fieldOptions'] ) && is_array( $question['fieldOptions'] ) |
| 109 |
&& ! empty( $question['fieldOptions'][0]['optionTitle'] ) |
| 110 |
) { |
| 111 |
$merged_attributes['options'] = $question['fieldOptions']; |
| 112 |
} |
| 113 |
if ( ! empty( $question['singleSelection'] ) ) { |
| 114 |
$merged_attributes['singleSelection'] = filter_var( $question['singleSelection'], FILTER_VALIDATE_BOOLEAN ); |
| 115 |
} |
| 116 |
} |
| 117 |
// if checkbox then map help to checkboxHelpText. |
| 118 |
if ( 'checkbox' === $question['fieldType'] && ! empty( $question['helpText'] ) ) { |
| 119 |
$merged_attributes['checkboxHelpText'] = sanitize_text_field( $question['helpText'] ); |
| 120 |
} |
| 121 |
// if gdpr then map help to gdprHelpText. |
| 122 |
if ( 'gdpr' === $question['fieldType'] && ! empty( $question['helpText'] ) ) { |
| 123 |
$merged_attributes['gdprHelpText'] = sanitize_text_field( $question['helpText'] ); |
| 124 |
} |
| 125 |
if ( 'phone' === $question['fieldType'] ) { |
| 126 |
$merged_attributes['autoCountry'] = true; |
| 127 |
} |
| 128 |
|
| 129 |
$post_content .= '<!-- wp:srfm/' . $question['fieldType'] . ' ' . wp_json_encode( $merged_attributes ) . ' /-->' . PHP_EOL; |
| 130 |
break; |
| 131 |
case 'slider': |
| 132 |
case 'page-break': |
| 133 |
case 'date-picker': |
| 134 |
case 'time-picker': |
| 135 |
case 'upload': |
| 136 |
case 'hidden': |
| 137 |
case 'rating': |
| 138 |
// If pro version is not active then do not add pro fields. |
| 139 |
if ( ! defined( 'SRFM_PRO_VER' ) ) { |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
// Handle specific attributes for certain pro fields. |
| 144 |
if ( 'date-picker' === $question['fieldType'] ) { |
| 145 |
$merged_attributes['dateFormat'] = ! empty( $question['dateFormat'] ) ? sanitize_text_field( $question['dateFormat'] ) : 'mm/dd/yy'; |
| 146 |
$merged_attributes['min'] = ! empty( $question['minDate'] ) ? sanitize_text_field( $question['minDate'] ) : ''; |
| 147 |
$merged_attributes['max'] = ! empty( $question['maxDate'] ) ? sanitize_text_field( $question['maxDate'] ) : ''; |
| 148 |
} |
| 149 |
if ( 'time-picker' === $question['fieldType'] ) { |
| 150 |
$merged_attributes['increment'] = ! empty( $question['increment'] ) ? filter_var( $question['increment'], FILTER_VALIDATE_INT ) : 30; |
| 151 |
$merged_attributes['showTwelveHourFormat'] = ! empty( $question['showTwelveHourFormat'] ) ? filter_var( $question['useTwelveHourFormat'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 152 |
$merged_attributes['min'] = ! empty( $question['minTime'] ) ? sanitize_text_field( $question['minTime'] ) : ''; |
| 153 |
$merged_attributes['max'] = ! empty( $question['maxTime'] ) ? sanitize_text_field( $question['maxTime'] ) : ''; |
| 154 |
} |
| 155 |
if ( 'rating' === $question['fieldType'] ) { |
| 156 |
$merged_attributes['iconShape'] = ! empty( $question['iconShape'] ) ? sanitize_text_field( $question['iconShape'] ) : 'star'; |
| 157 |
$merged_attributes['showText'] = ! empty( $question['showTooltip'] ) ? filter_var( $question['showTooltip'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 158 |
$merged_attributes['defaultRating'] = ! empty( $question['defaultRating'] ) ? filter_var( $question['defaultRating'], FILTER_VALIDATE_INT ) : 0; |
| 159 |
|
| 160 |
if ( ! empty( $merged_attributes['showText'] ) ) { |
| 161 |
foreach ( $question['tooltipValues'] as $tooltips ) { |
| 162 |
$i = 0; |
| 163 |
foreach ( $tooltips as $key => $value ) { |
| 164 |
$merged_attributes['ratingText'][ $i ] = ! empty( $value ) ? sanitize_text_field( $value ) : ''; |
| 165 |
$i++; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
if ( 'upload' === $question['fieldType'] ) { |
| 171 |
if ( ! empty( $question['allowedTypes'] ) ) { |
| 172 |
$allowed_types = str_replace( '.', '', $question['allowedTypes'] ); |
| 173 |
|
| 174 |
$allowed_types = explode( ',', $allowed_types ); |
| 175 |
|
| 176 |
$types_array = array_map( |
| 177 |
function( $type ) { |
| 178 |
return [ |
| 179 |
'value' => $type, |
| 180 |
'label' => $type, |
| 181 |
]; |
| 182 |
}, |
| 183 |
$allowed_types |
| 184 |
); |
| 185 |
|
| 186 |
$merged_attributes['allowedFormats'] = $types_array; |
| 187 |
} else { |
| 188 |
$merged_attributes['allowedFormats'] = [ |
| 189 |
[ |
| 190 |
'value' => 'jpg', |
| 191 |
'label' => 'jpg', |
| 192 |
], |
| 193 |
[ |
| 194 |
'value' => 'jpeg', |
| 195 |
'label' => 'jpeg', |
| 196 |
], |
| 197 |
[ |
| 198 |
'value' => 'gif', |
| 199 |
'label' => 'gif', |
| 200 |
], |
| 201 |
[ |
| 202 |
'value' => 'png', |
| 203 |
'label' => 'png', |
| 204 |
], |
| 205 |
[ |
| 206 |
'value' => 'pdf', |
| 207 |
'label' => 'pdf', |
| 208 |
], |
| 209 |
]; |
| 210 |
} |
| 211 |
|
| 212 |
$merged_attributes['fileSizeLimit'] = ! empty( $question['uploadSize'] ) ? filter_var( $question['uploadSize'], FILTER_VALIDATE_INT ) : 10; |
| 213 |
|
| 214 |
$merged_attributes['multiple'] = ! empty( $question['multiUpload'] ) ? filter_var( $question['multiUpload'], FILTER_VALIDATE_BOOLEAN ) : false; |
| 215 |
|
| 216 |
$merged_attributes['maxFiles'] = ! empty( $question['multiFilesNumber'] ) ? filter_var( $question['multiFilesNumber'], FILTER_VALIDATE_INT ) : 2; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
$post_content .= '<!-- wp:srfm/' . $question['fieldType'] . ' ' . wp_json_encode( $merged_attributes ) . ' /-->' . PHP_EOL; |
| 221 |
break; |
| 222 |
default: |
| 223 |
// Unsupported field type - fallback to input. |
| 224 |
$post_content .= '<!-- wp:srfm/input ' . wp_json_encode( $merged_attributes ) . ' /-->' . PHP_EOL; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return $post_content; |
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
|