PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.13
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.13
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.13, at app/Services/Migrator/Classes/ContactForm7Migrator.php

555 lines 19.8 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 // $allFileTypes is lowercase; CF7's filetypes attribute is author-entered.
224 $format = strtolower($format);
225 foreach ($allFileTypes as $fileTypes) {
226 if (!empty($format) && strpos($fileTypes, $format) !== false) {
227 if (strpos($fileTypes, '/*|') !== false) {
228 $fileTypes = explode('/*|', $fileTypes)[1];
229 }
230 $fileTypeOptions[] = $fileTypes;
231 }
232 }
233 }
234
235 return array_unique($fileTypeOptions);
236 }
237
238 protected function getFormName($form)
239 {
240 return $form['name'];
241 }
242
243 protected function getFormMetas($form)
244 {
245 $formObject = new Form(wpFluentForm());
246 $defaults = $formObject->getFormsDefaultSettings();
247
248 return [
249 'formSettings' => [
250 'confirmation' => ArrayHelper::get($defaults, 'confirmation'),
251 'restrictions' => ArrayHelper::get($defaults, 'restrictions'),
252 'layout' => ArrayHelper::get($defaults, 'layout'),
253 ],
254 'advancedValidationSettings' => $this->getAdvancedValidation(),
255 'delete_entry_on_submission' => 'no',
256 'notifications' => $this->getNotifications(),
257 'step_data_persistency_status' => 'no',
258 'form_save_state_status' => 'no'
259 ];
260 }
261
262 protected function getFormId($form)
263 {
264 return $form['ID'];
265 }
266
267 public function getFormsFormatted()
268 {
269 $forms = [];
270 $items = $this->getForms();
271 foreach ($items as $item) {
272 $forms[] = [
273 'name' => $item['name'],
274 'id' => $item['ID'],
275 'imported_ff_id' => $this->isAlreadyImported($item),
276 ];
277 }
278
279 return $forms;
280 }
281
282 private function getNotifications()
283 {
284 return [
285 'name' => __('Admin Notification Email', 'fluentform'),
286 'sendTo' => [
287 'type' => 'email',
288 'email' => '{wp.admin_email}',
289 'field' => '',
290 'routing' => [],
291 ],
292 'fromName' => '',
293 'fromEmail' => '',
294 'replyTo' => '',
295 'bcc' => '',
296 'subject' => __('New Form Submission', 'fluentform'),
297 'message' => '<p>{all_data}</p><p>This form submitted at: {embed_post.permalink}</p>',
298 'conditionals' => [],
299 'enabled' => false
300 ];
301 }
302
303 private function getAdvancedValidation()
304 {
305 return [
306 'status' => false,
307 'type' => 'all',
308 'conditions' => [
309 [
310 'field' => '',
311 'operator' => '=',
312 'value' => ''
313 ]
314 ],
315 'error_message' => '',
316 'validation_type' => 'fail_on_condition_met'
317 ];
318 }
319
320 private function removeLabelsAndNewLine($formMetaDataArray)
321 {
322 $formattedArray = [];
323 foreach ($formMetaDataArray as $formMetaString) {
324 if (!empty($formMetaString)) {
325 if (strpos($formMetaString, '<label>') !== false || strpos($formMetaString, '</label>') !== false) {
326 $formMetaString = trim(str_replace(['<label>', '</label>'], '', $formMetaString));
327 }
328 $formattedArray[] = $formMetaString;
329 }
330 }
331
332 return $formattedArray;
333 }
334
335 private function formatFieldArray($formattedArray)
336 {
337 $fieldStringArray = [];
338 foreach ($formattedArray as $formattedKey => &$formattedValue) {
339 preg_match_all('/\[[^\]]*\]/', $formattedValue, $fieldStringMatches);
340 $fieldString = isset($fieldStringMatches[0][0]) ? $fieldStringMatches[0][0] : '';
341
342 if (preg_match('/\[(.*?)\](.*?)\[.*?\]/', $formattedValue, $withoutBracketMatches)) {
343 $withoutBracketString = isset($withoutBracketMatches[2]) ? trim($withoutBracketMatches[2]) : '';
344 $fieldString = str_replace(']', ' "' . $withoutBracketString . '"]', $fieldString);
345 }
346
347 if (strpos($formattedValue, '[quiz') !== 0) {
348 if (strpos($formattedValue, '[') === false) {
349 if (
350 isset($formattedArray[$formattedKey + 1]) &&
351 strpos($formattedArray[$formattedKey + 1], '[') !== false
352 ) {
353 $fieldStringArray[] = $formattedValue . $formattedArray[$formattedKey + 1];
354 unset($formattedArray[$formattedKey + 1]);
355 }
356 } else {
357 $fieldStringArray[] = $fieldString;
358 unset($formattedArray[$formattedKey]);
359 }
360 }
361 }
362
363 return $fieldStringArray;
364 }
365
366 private function formatAsFluentField($fieldStringArray)
367 {
368 $fluentFields = [];
369 $submitBtn = [];
370
371 foreach ($fieldStringArray as $fieldKey => &$fieldValue) {
372 $fieldLabel = '';
373 $fieldPlaceholder = '';
374 $fieldAutoComplete = '';
375 $fieldMinLength = '';
376 $fieldMaxLength = '';
377 $fieldSize = '';
378 $fieldStep = '';
379 $fieldMultipleValues = [];
380 $fieldMin = '';
381 $fieldMax = '';
382 $fieldDefault = '';
383 $fieldMultiple = false;
384 $fieldFileTypes = '';
385 $fieldMaxFileSize = '';
386 $fieldFileSizeUnit = 'KB';
387 $tncHtml = '';
388
389 $fieldString = '';
390
391 if (preg_match('/^(.*?)\[/', $fieldValue, $matches)) {
392 $fieldLabel = isset($matches[1]) ? $matches[1] : '';
393 }
394
395 if (preg_match('/\[([^]]+)\]/', $fieldValue, $matches)) {
396 $fieldString = isset($matches[1]) ? $matches[1] : '';
397 }
398
399 $words = preg_split('/\s+/', $fieldString);
400 $fieldRequired = isset($words[0]) && strpos($words[0], '*') !== false ?? true;
401 $fieldElement = isset($words[0]) ? trim($words[0], '*') : '';
402 $fieldName = isset($words[1]) ? trim($words[1]) : '';
403
404 if ($fieldElement === 'submit') {
405 preg_match_all('/(["\'])(.*?)\1/', $fieldString, $matches);
406
407 $submitBtn = $this->getSubmitBttn([
408 'uniqElKey' => $fieldElement . '-' . time(),
409 'label' => isset($matches[2][0]) ? $matches[2][0] : 'Submit'
410 ]);
411
412 continue;
413 }
414
415 if ($fieldElement === 'select' && strpos($fieldString, 'multiple') !== false) {
416 $fieldMultiple = true;
417 }
418
419 if (preg_match('/min:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
420 $fieldMin = isset($matches[1]) ? $matches[1] : '';
421 }
422
423 if (preg_match('/max:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
424 $fieldMax = isset($matches[1]) ? $matches[1] : '';
425 }
426
427 if (preg_match('/minlength:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
428 $fieldMinLength = isset($matches[1]) ? $matches[1] : '';
429 }
430
431 if (preg_match('/maxlength:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
432 $fieldMaxLength = isset($matches[1]) ? $matches[1] : '';
433 }
434
435 if (preg_match('/size:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
436 $fieldSize = isset($matches[1]) ? $matches[1] : '';
437 }
438
439 if (preg_match('/step:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
440 $fieldStep = isset($matches[1]) ? $matches[1] : '';
441 }
442
443 if (preg_match('/(?:placeholder|watermark) "([a-zA-Z0-9]+)"/', $fieldString, $matches)) {
444 $fieldPlaceholder = isset($matches[1]) ? $matches[1] : '';
445 }
446
447 if (preg_match('/filetypes:([a-zA-Z0-9|]+)/', $fieldString, $matches)) {
448 $fieldFileTypes = isset($matches[1]) ? $matches[1] : '';
449 }
450
451 if (preg_match_all('/(["\'])(.*?)\1/', $fieldString, $matches)) {
452 if (isset($matches[2])) {
453 if (count($matches[2]) > 1) {
454 $fieldMultipleValues = $matches[2];
455 } else {
456 if (count($matches[2]) === 1) {
457 $fieldAutoComplete = isset($matches[2][0]) ? $matches[2][0] : '';
458 }
459 }
460 }
461 }
462
463 if (preg_match('/default:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
464 $fieldDefault = isset($matches[1]) ? $matches[1] : '';
465 }
466
467 if (preg_match('/limit:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
468 $fieldMaxFileSize = isset($matches[1]) ? $matches[1] : '1mb';
469
470 if (strpos($fieldMaxFileSize, 'mb') !== false) {
471 $fieldFileSizeUnit = 'MB';
472 }
473
474 $fieldMaxFileSize = str_replace(['mb', 'kb'], '', $fieldMaxFileSize);
475 }
476
477 if (preg_match('/autocomplete:([a-zA-Z0-9]+)/', $fieldString, $matches)) {
478 $fieldAutoComplete = isset($matches[1]) ? $matches[1] : '';
479 }
480
481 if ($fieldElement === 'acceptance') {
482 $tncHtml = $fieldAutoComplete;
483 }
484
485 if (!$fieldLabel) {
486 $fieldLabel = $fieldElement;
487 }
488
489 $fieldType = ArrayHelper::get($this->fieldTypeMap(), $fieldElement);
490
491 $args = [
492 'uniqElKey' => 'el_' . $fieldKey . time(),
493 'type' => $fieldType,
494 'index' => $fieldKey,
495 'required' => $fieldRequired,
496 'label' => $fieldLabel,
497 'name' => $fieldName,
498 'placeholder' => $fieldPlaceholder,
499 'class' => '',
500 'value' => $fieldAutoComplete,
501 'help_message' => '',
502 'container_class' => '',
503 'prefix' => '',
504 'suffix' => '',
505 'min' => $fieldMin,
506 'max' => $fieldMax,
507 'minlength' => $fieldMinLength,
508 'maxlength' => $fieldMaxLength,
509 'size' => $fieldSize,
510 'step' => $fieldStep,
511 'choices' => $fieldMultipleValues,
512 'default' => $fieldDefault,
513 'multiple' => $fieldMultiple,
514 'allowed_file_types' => $fieldFileTypes,
515 'max_file_size' => $fieldMaxFileSize,
516 'max_size_unit' => $fieldFileSizeUnit,
517 'tnc_html' => $tncHtml
518 ];
519
520 $fields = $this->formatFieldData($args, $fieldType);
521
522 if ($fieldMultiple) {
523 $fieldType = 'multi_select';
524 }
525
526 if ($fieldData = $this->getFluentClassicField($fieldType, $fields)) {
527 $fluentFields['fields'][$args['index']] = $fieldData;
528 }
529 }
530
531 $fluentFields['submitButton'] = $submitBtn;
532
533 return $fluentFields;
534 }
535
536 public function getEntries($formId)
537 {
538 if (class_exists('Flamingo_Inbound_Message')) {
539 $post = get_post($formId);
540 $formName = $post->post_name;
541 $allPosts = \Flamingo_Inbound_Message::find(['channel' => $formName]);
542 $entries = [];
543
544 foreach ($allPosts as $post) {
545 $entries[] = $post->fields;
546 }
547
548 return $entries;
549 }
550 wp_send_json_error([
551 'message' => __("Please install and active Flamingo", 'fluentform')
552 ], 422);
553 }
554 }
555