PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.4
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.4
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / Migrator / Classes / ContactForm7Migrator.php

ContactForm7Migrator.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.4, at app/Services/Migrator/Classes/ContactForm7Migrator.php

553 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\Migrator\Classes;
4
5 use FluentForm\App\Modules\Form\Form;
6
7 use FluentForm\Framework\Helpers\ArrayHelper;
8
9 class ContactForm7Migrator extends BaseMigrator
10 {
11 public function __construct()
12 {
13 $this->key = 'contactform7';
14 $this->title = 'Contact Form 7';
15 $this->shortcode = 'contact_form_7';
16 }
17
18 public function exist()
19 {
20 return !!defined('WPCF7_PLUGIN');
21 }
22
23 protected function getForms()
24 {
25 $forms = [];
26 $postItems = get_posts(['post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1]);
27 foreach ($postItems as $form) {
28 $forms[] = [
29 'ID' => $form->ID,
30 'name' => $form->post_title,
31 ];
32 }
33 return $forms;
34 }
35
36 public function getFields($form)
37 {
38 $formPostMeta = get_post_meta($form['ID'], '_form', true);
39 $formMetaDataArray = preg_split('/\r\n|\r|\n/', $formPostMeta);
40
41 //remove all label and empty line
42 $formattedArray = $this->removeLabelsAndNewLine($formMetaDataArray);
43
44 //format array with field label and remove quiz field
45 $fieldStringArray = $this->formatFieldArray($formattedArray);
46
47 // format fields as fluent forms field
48 return $this->formatAsFluentField($fieldStringArray);
49 }
50
51 public function getSubmitBttn($args)
52 {
53 return [
54 'uniqElKey' => 'submit-' . time(),
55 'element' => 'button',
56 'attributes' => [
57 'type' => 'submit',
58 'class' => '',
59 'id' => ''
60 ],
61 'settings' => [
62 'align' => 'left',
63 'button_style' => 'default',
64 'container_class' => '',
65 'help_message' => '',
66 'background_color' => '#1a7efb',
67 'button_size' => 'md',
68 'color' => '#ffffff',
69 'button_ui' => [
70 'type' => 'default',
71 'text' => $args['label'],
72 'img_url' => ''
73 ],
74 'normal_styles' => [
75 'backgroundColor' => '#1a7efb',
76 'borderColor' => '#1a7efb',
77 'color' => '#ffffff',
78 'borderRadius' => '',
79 'minWidth' => ''
80 ],
81 'hover_styles' => [
82 'backgroundColor' => '#1a7efb',
83 'borderColor' => '#1a7efb',
84 'color' => '#ffffff',
85 'borderRadius' => '',
86 'minWidth' => ''
87 ],
88 'current_state' => "normal_styles"
89 ],
90 'editor_options' => [
91 'title' => 'Submit Button',
92 ],
93 ];
94 }
95
96 private function fieldTypeMap()
97 {
98 return [
99 'email' => 'email',
100 'text' => 'input_text',
101 'url' => 'input_url',
102 'tel' => 'phone',
103 'textarea' => 'input_textarea',
104 'number' => 'input_number',
105 'range' => 'rangeslider',
106 'date' => 'input_date',
107 'checkbox' => 'input_checkbox',
108 'radio' => 'input_radio',
109 'select' => 'select',
110 'multi_select' => 'multi_select',
111 'file' => 'input_file',
112 'acceptance' => 'terms_and_condition'
113 ];
114 }
115
116 protected function formatFieldData($args, $type)
117 {
118 switch ($type) {
119 case 'input_number':
120 $args['min'] = ArrayHelper::get($args, 'min', 0);
121 $args['max'] = ArrayHelper::get($args, 'max');
122 break;
123 case 'rangeslider':
124 $args['min'] = ArrayHelper::get($args, 'min', 0);
125 $args['max'] = ArrayHelper::get($args, 'max', 10);
126 $args['step'] = ArrayHelper::get($args, 'step',1);
127 break;
128 case 'input_date':
129 $args['format'] = "Y-m-d H:i";
130 break;
131 case 'select':
132 case 'input_radio':
133 case 'input_checkbox':
134 list($options, $defaultVal) = $this->getOptions(ArrayHelper::get($args, 'choices', []),
135 ArrayHelper::get($args, 'default', '')
136 );;
137 $args['options'] = $options;
138 if ($type == 'select') {
139 $isMulti = ArrayHelper::isTrue($args, 'multiple');
140 if ($isMulti) {
141 $args['multiple'] = true;
142 $args['value'] = $defaultVal;
143 } else {
144 $args['value'] = is_array($defaultVal) ? array_shift($defaultVal) : "";
145 }
146 } elseif ($type == 'input_checkbox') {
147 $args['value'] = $defaultVal;
148 } elseif ($type == 'input_radio') {
149 $args['value'] = is_array($defaultVal) ? array_shift($defaultVal) : "";
150 }
151 break;
152 case 'input_file':
153 $args['allowed_file_types'] = $this->getFileTypes($args, 'allowed_file_types');
154 $args['max_size_unit'] = ArrayHelper::get($args, 'max_size_unit');
155 $max_size = ArrayHelper::get($args, 'max_file_size') ?: 1;
156 if ($args['max_size_unit'] === 'MB') {
157 $args['max_file_size'] = ceil($max_size * 1048576); // 1MB = 1048576 Bytes
158 }
159 $args['max_file_count'] = '1';
160 $args['upload_btn_text'] = 'File Upload';
161 break;
162 case 'terms_and_condition':
163 if (ArrayHelper::get($args, 'tnc_html') !== '') {
164 $args['tnc_html'] = ArrayHelper::get($args, 'tnc_html',
165 'I have read and agree to the Terms and Conditions and Privacy Policy.'
166 );
167 $args['required'] = true;
168 }
169 break;
170 default :
171 break;
172 }
173
174 return $args;
175 }
176
177 protected function getOptions($options, $default)
178 {
179 $formattedOptions = [];
180 $defaults = [];
181 foreach ($options as $key => $option) {
182 $formattedOption = [
183 'label' => $option,
184 'value' => $option,
185 'image' => '',
186 'calc_value' => '',
187 'id' => $key + 1,
188 ];
189 if (strpos($default, '_') !== false) {
190 $defaults = explode('_', $default);
191 foreach ($defaults as $defaultValue) {
192 if ($formattedOption['id'] == $defaultValue) {
193 $defaults[] = $formattedOption['value'];
194 }
195 }
196 } else {
197 $defaults = $default;
198 }
199 $formattedOptions[] = $formattedOption;
200 }
201
202 return [$formattedOptions, $defaults];
203 }
204
205 protected function getFileTypes($field, $arg)
206 {
207 // All Supported File Types in Fluent Forms
208 $allFileTypes = [
209 "image/*|jpg|jpeg|gif|png|bmp",
210 "audio/*|mp3|wav|ogg|oga|wma|mka|m4a|ra|mid|midi|mpga",
211 "video/*|avi|divx|flv|mov|ogv|mkv|mp4|m4v|mpg|mpeg|mpe|video/quicktime|qt",
212 "pdf",
213 "text/*|doc|ppt|pps|xls|mdb|docx|xlsx|pptx|odt|odp|ods|odg|odc|odb|odf|rtf|txt",
214 "zip|gz|gzip|rar|7z",
215 "exe",
216 "csv"
217 ];
218
219 $formattedTypes = explode('|', ArrayHelper::get($field, $arg, ''));
220 $fileTypeOptions = [];
221
222 foreach ($formattedTypes as $format) {
223 foreach ($allFileTypes as $fileTypes) {
224 if (!empty($format) && strpos($fileTypes, $format) !== false) {
225 if (strpos($fileTypes, '/*|') !== false) {
226 $fileTypes = explode('/*|', $fileTypes)[1];
227 }
228 $fileTypeOptions[] = $fileTypes;
229 }
230 }
231 }
232
233 return array_unique($fileTypeOptions);
234 }
235
236 protected function getFormName($form)
237 {
238 return $form['name'];
239 }
240
241 protected function getFormMetas($form)
242 {
243 $formObject = new Form(wpFluentForm());
244 $defaults = $formObject->getFormsDefaultSettings();
245
246 return [
247 'formSettings' => [
248 'confirmation' => ArrayHelper::get($defaults, 'confirmation'),
249 'restrictions' => ArrayHelper::get($defaults, 'restrictions'),
250 'layout' => ArrayHelper::get($defaults, 'layout'),
251 ],
252 'advancedValidationSettings' => $this->getAdvancedValidation(),
253 'delete_entry_on_submission' => 'no',
254 'notifications' => $this->getNotifications(),
255 'step_data_persistency_status' => 'no',
256 'form_save_state_status' => 'no'
257 ];
258 }
259
260 protected function getFormId($form)
261 {
262 return $form['ID'];
263 }
264
265 public function getFormsFormatted()
266 {
267 $forms = [];
268 $items = $this->getForms();
269 foreach ($items as $item) {
270 $forms[] = [
271 'name' => $item['name'],
272 'id' => $item['ID'],
273 'imported_ff_id' => $this->isAlreadyImported($item),
274 ];
275 }
276
277 return $forms;
278 }
279
280 private function getNotifications()
281 {
282 return [
283 'name' => __('Admin Notification Email', 'fluentform'),
284 'sendTo' => [
285 'type' => 'email',
286 'email' => '{wp.admin_email}',
287 'field' => '',
288 'routing' => [],
289 ],
290 'fromName' => '',
291 'fromEmail' => '',
292 'replyTo' => '',
293 'bcc' => '',
294 'subject' => __('New Form Submission', 'fluentform'),
295 'message' => '<p>{all_data}</p><p>This form submitted at: {embed_post.permalink}</p>',
296 'conditionals' => [],
297 'enabled' => false
298 ];
299 }
300
301 private function getAdvancedValidation()
302 {
303 return [
304 'status' => false,
305 'type' => 'all',
306 'conditions' => [
307 [
308 'field' => '',
309 'operator' => '=',
310 'value' => ''
311 ]
312 ],
313 'error_message' => '',
314 'validation_type' => 'fail_on_condition_met'
315 ];
316 }
317
318 private function removeLabelsAndNewLine($formMetaDataArray)
319 {
320 $formattedArray = [];
321 foreach ($formMetaDataArray as $formMetaString) {
322 if (!empty($formMetaString)) {
323 if (strpos($formMetaString, '<label>') !== false || strpos($formMetaString, '</label>') !== false) {
324 $formMetaString = trim(str_replace(['<label>', '</label>'], '', $formMetaString));
325 }
326 $formattedArray[] = $formMetaString;
327 }
328 }
329
330 return $formattedArray;
331 }
332
333 private function formatFieldArray($formattedArray)
334 {
335 $fieldStringArray = [];
336 foreach ($formattedArray as $formattedKey => &$formattedValue) {
337 preg_match_all('/\[[^\]]*\]/', $formattedValue, $fieldStringMatches);
338 $fieldString = isset($fieldStringMatches[0][0]) ? $fieldStringMatches[0][0] : '';
339
340 if (preg_match('/\[(.*?)\](.*?)\[.*?\]/', $formattedValue, $withoutBracketMatches)) {
341 $withoutBracketString = isset($withoutBracketMatches[2]) ? trim($withoutBracketMatches[2]) : '';
342 $fieldString = str_replace(']', ' "' . $withoutBracketString . '"]', $fieldString);
343 }
344
345 if (strpos($formattedValue, '[quiz') !== 0) {
346 if (strpos($formattedValue, '[') === false) {
347 if (
348 isset($formattedArray[$formattedKey + 1]) &&
349 strpos($formattedArray[$formattedKey + 1], '[') !== false
350 ) {
351 $fieldStringArray[] = $formattedValue . $formattedArray[$formattedKey + 1];
352 unset($formattedArray[$formattedKey + 1]);
353 }
354 } else {
355 $fieldStringArray[] = $fieldString;
356 unset($formattedArray[$formattedKey]);
357 }
358 }
359 }
360
361 return $fieldStringArray;
362 }
363
364 private function formatAsFluentField($fieldStringArray)
365 {
366 $fluentFields = [];
367 $submitBtn = [];
368
369 foreach ($fieldStringArray as $fieldKey => &$fieldValue) {
370 $fieldLabel = '';
371 $fieldPlaceholder = '';
372 $fieldAutoComplete = '';
373 $fieldMinLength = '';
374 $fieldMaxLength = '';
375 $fieldSize = '';
376 $fieldStep = '';
377 $fieldMultipleValues = [];
378 $fieldMin = '';
379 $fieldMax = '';
380 $fieldDefault = '';
381 $fieldMultiple = false;
382 $fieldFileTypes = '';
383 $fieldMaxFileSize = '';
384 $fieldFileSizeUnit = 'KB';
385 $tncHtml = '';
386
387 $fieldString = '';
388
389 if (preg_match('/^(.*?)\[/', $fieldValue, $matches)) {
390 $fieldLabel = isset($matches[1]) ? $matches[1] : '';
391 }
392
393 if (preg_match('/\[([^]]+)\]/', $fieldValue, $matches)) {
394 $fieldString = isset($matches[1]) ? $matches[1] : '';
395 }
396
397 $words = preg_split('/\s+/', $fieldString);
398 $fieldRequired = isset($words[0]) && strpos($words[0], '*') !== false ?? true;
399 $fieldElement = isset($words[0]) ? trim($words[0], '*') : '';
400 $fieldName = isset($words[1]) ? trim($words[1]) : '';
401
402 if ($fieldElement === 'submit') {
403 preg_match_all('/(["\'])(.*?)\1/', $fieldString, $matches);
404
405 $submitBtn = $this->getSubmitBttn([
406 'uniqElKey' => $fieldElement . '-' . time(),
407 'label' => isset($matches[2][0]) ? $matches[2][0] : 'Submit'
408 ]);
409
410 continue;
411 }
412
413 if ($fieldElement === 'select' && strpos($fieldString, 'multiple') !== false) {
414 $fieldMultiple = true;
415 }
416
417 if (preg_match('/min:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
418 $fieldMin = isset($matches[1]) ? $matches[1] : '';
419 }
420
421 if (preg_match('/max:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
422 $fieldMax = isset($matches[1]) ? $matches[1] : '';
423 }
424
425 if (preg_match('/minlength:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
426 $fieldMinLength = isset($matches[1]) ? $matches[1] : '';
427 }
428
429 if (preg_match('/maxlength:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
430 $fieldMaxLength = isset($matches[1]) ? $matches[1] : '';
431 }
432
433 if (preg_match('/size:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
434 $fieldSize = isset($matches[1]) ? $matches[1] : '';
435 }
436
437 if (preg_match('/step:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
438 $fieldStep = isset($matches[1]) ? $matches[1] : '';
439 }
440
441 if (preg_match('/(?:placeholder|watermark) "([a-zA-Z0-9]+)"/', $fieldString, $matches)) {
442 $fieldPlaceholder = isset($matches[1]) ? $matches[1] : '';
443 }
444
445 if (preg_match('/filetypes:([a-zA-Z0-9|]+)/', $fieldString, $matches)) {
446 $fieldFileTypes = isset($matches[1]) ? $matches[1] : '';
447 }
448
449 if (preg_match_all('/(["\'])(.*?)\1/', $fieldString, $matches)) {
450 if (isset($matches[2])) {
451 if (count($matches[2]) > 1) {
452 $fieldMultipleValues = $matches[2];
453 } else {
454 if (count($matches[2]) === 1) {
455 $fieldAutoComplete = isset($matches[2][0]) ? $matches[2][0] : '';
456 }
457 }
458 }
459 }
460
461 if (preg_match('/default:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
462 $fieldDefault = isset($matches[1]) ? $matches[1] : '';
463 }
464
465 if (preg_match('/limit:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
466 $fieldMaxFileSize = isset($matches[1]) ? $matches[1] : '1mb';
467
468 if (strpos($fieldMaxFileSize, 'mb') !== false) {
469 $fieldFileSizeUnit = 'MB';
470 }
471
472 $fieldMaxFileSize = str_replace(['mb', 'kb'], '', $fieldMaxFileSize);
473 }
474
475 if (preg_match('/autocomplete:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
476 $fieldAutoComplete = isset($matches[1]) ? $matches[1] : '';
477 }
478
479 if ($fieldElement === 'acceptance') {
480 $tncHtml = $fieldAutoComplete;
481 }
482
483 if (!$fieldLabel) {
484 $fieldLabel = $fieldElement;
485 }
486
487 $fieldType = ArrayHelper::get($this->fieldTypeMap(), $fieldElement);
488
489 $args = [
490 'uniqElKey' => 'el_' . $fieldKey . time(),
491 'type' => $fieldType,
492 'index' => $fieldKey,
493 'required' => $fieldRequired,
494 'label' => $fieldLabel,
495 'name' => $fieldName,
496 'placeholder' => $fieldPlaceholder,
497 'class' => '',
498 'value' => $fieldAutoComplete,
499 'help_message' => '',
500 'container_class' => '',
501 'prefix' => '',
502 'suffix' => '',
503 'min' => $fieldMin,
504 'max' => $fieldMax,
505 'minlength' => $fieldMinLength,
506 'maxlength' => $fieldMaxLength,
507 'size' => $fieldSize,
508 'step' => $fieldStep,
509 'choices' => $fieldMultipleValues,
510 'default' => $fieldDefault,
511 'multiple' => $fieldMultiple,
512 'allowed_file_types' => $fieldFileTypes,
513 'max_file_size' => $fieldMaxFileSize,
514 'max_size_unit' => $fieldFileSizeUnit,
515 'tnc_html' => $tncHtml
516 ];
517
518 $fields = $this->formatFieldData($args, $fieldType);
519
520 if ($fieldMultiple) {
521 $fieldType = 'multi_select';
522 }
523
524 if ($fieldData = $this->getFluentClassicField($fieldType, $fields)) {
525 $fluentFields['fields'][$args['index']] = $fieldData;
526 }
527 }
528
529 $fluentFields['submitButton'] = $submitBtn;
530
531 return $fluentFields;
532 }
533
534 public function getEntries($formId)
535 {
536 if (class_exists('Flamingo_Inbound_Message')) {
537 $post = get_post($formId);
538 $formName = $post->post_name;
539 $allPosts = \Flamingo_Inbound_Message::find(['channel' => $formName]);
540 $entries = [];
541
542 foreach ($allPosts as $post) {
543 $entries[] = $post->fields;
544 }
545
546 return $entries;
547 }
548 wp_send_json_error([
549 'message' => __("Please install and active Flamingo", 'fluentform')
550 ], 422);
551 }
552 }
553