PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.3
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.3
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 / NinjaFormsMigrator.php

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

614 lines 22.1 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
6 use FluentForm\App\Helpers\Helper;
7 use \FluentForm\App\Modules\Form\Form;
8 use \FluentForm\Framework\Helpers\ArrayHelper;
9
10 class NinjaFormsMigrator extends BaseMigrator
11 {
12
13 public function __construct()
14 {
15 $this->key = 'ninja_forms';
16 $this->title = 'Ninja Forms';
17 $this->shortcode = 'ninja_form';
18 }
19
20 /**
21 * Check if form type exists
22 * @return bool
23 */
24 public function exist()
25 {
26 return !!defined('NF_SERVER_URL');
27 }
28
29 /**
30 * Get array of all forms
31 * @return array Forms with fields
32 */
33 public function getForms()
34 {
35 $forms = [];
36 $items = (new \NF_Database_FormsController())->getFormsData();
37 foreach ($items as $item) {
38 $fields = Ninja_Forms()->form($item->id)->get_fields();
39 $field_settings = [];
40 foreach ($fields as $key => $field) {
41 $field_settings[$key] = $field->get_settings();
42 }
43 $forms[] = [
44 'ID' => $item->id,
45 'name' => $item->title,
46 'fields' => $field_settings,
47 ];
48 }
49 return $forms;
50 }
51
52 public function getForm($id)
53 {
54 $formModel = \Ninja_Forms()->form($id)->get();
55 if (!$formModel) {
56 return [];
57 }
58 $forms = $this->getForms();
59 $formArray = array_filter($forms, function ($form) use ($id) {
60 if ($form['ID'] == $id) {
61 return $form;
62 }
63 });
64 return array_shift($formArray);
65 }
66
67 public function getFormsFormatted()
68 {
69 $forms = [];
70 $items = $this->getForms();
71 foreach ($items as $item) {
72 $forms[] = [
73 'name' => $this->getFormName($item),
74 'id' => $this->getFormId($item),
75 'imported_ff_id' => $this->isAlreadyImported($item),
76 ];
77 }
78 return $forms;
79 }
80
81 /**
82 *
83 * Get formatted fields form array
84 * @param array $form
85 * @return array fluentform data formatted for database
86 */
87 public function getFields($form)
88 {
89 $fluentFields = [];
90 $fields = ArrayHelper::get($form, 'fields');
91
92 foreach ($fields as $field) {
93 list($type, $args) = $this->formatFieldData($field);
94 if ($value = $this->getFluentClassicField($type, $args)) {
95 $fluentFields[$field['key']] = $value;
96 } else {
97 if (ArrayHelper::get($field, 'type') != 'submit') {
98 $this->unSupportFields[] = ArrayHelper::get($field, 'label');
99 }
100 }
101 }
102
103 $submitBtn = $this->getSubmitBttn([
104 'uniqElKey' => $field['key'],
105 'label' => $field['label'],
106 'class' => $field['element_class'],
107 ]);
108 if (empty($fluentFields)) {
109 return false;
110 }
111 $returnData = [
112 'fields' => $fluentFields,
113 'submitButton' => $this->submitBtn
114 ];
115 return $returnData;
116 }
117
118 /**
119 * Format each field with proper data
120 * @param $field
121 * @return array required arguments for single field
122 */
123 protected function formatFieldData($field)
124 {
125
126 $type = ArrayHelper::get($this->fieldTypes(), $field['type'], '');
127
128 $args = [
129 'uniqElKey' => $field['key'] . '-' . time(),
130 'index' => $field['order'],
131 'required' => ArrayHelper::isTrue($field, 'required'),
132 'label' => $field['label'],
133 'name' => ArrayHelper::get($field, 'key'),
134 'placeholder' => ArrayHelper::get($field, 'placeholder', ''),
135 'class' => ArrayHelper::get($field, 'element_class', ''),
136 'value' => $this->dynamicShortcodeConverter(ArrayHelper::get($field, 'value', '')),
137 'help_message' => ArrayHelper::get($field, 'desc_text'),
138 'container_class' => ArrayHelper::get($field, 'container_class'),
139 ];
140 if (empty($args['name'])) {
141 $name = ArrayHelper::get($field, 'id');
142 $args['name'] = str_replace('.', '_', $name);
143 }
144
145
146 switch ($type) {
147
148 case 'select':
149 case 'input_radio':
150 case 'input_checkbox':
151 case 'multi_select':
152 $optionsData = $this->getOptions(ArrayHelper::get($field, 'options', []));
153 $args['options'] = ArrayHelper::get($optionsData, 'options');
154 $args['value'] = ArrayHelper::get($optionsData, 'selectedOption.0', '');
155
156 if ($field['type'] == 'listimage') {
157 $args['enable_image_input'] = true;
158 $optionsData = $this->getOptions(ArrayHelper::get($field, 'image_options', []), $hasImage = true);
159 $args['options'] = $optionsData['options'];
160 $args['value'] = ArrayHelper::get($optionsData, 'selectedOption.0', '');
161
162 if (ArrayHelper::isTrue($field, 'allow_multi_select')) {
163 $type = 'input_checkbox';
164 }
165
166 } else {
167 if ($field['type'] == 'checkbox' && empty($args['options'])) {
168 //single item checkbox
169 $arr = [
170 'label' => ArrayHelper::get($field, 'checked_value'),
171 'value' => ArrayHelper::get($field, 'checked_value'),
172 'calc_value' => '',
173 'id' => 0
174 ];
175 $args['options'] = [$arr];
176 } else {
177 if ($field['type'] == 'checkbox' && $args['allow_multi_select'] == 1) {
178 //img with multi item checkbox make it check box
179 $type = 'input_checkbox';
180 $args['value'] = ArrayHelper::get($optionsData, 'selectedOption');
181 }
182 }
183 }
184 if ($type == 'input_checkbox' || $type == 'multi_select') {
185 //array values
186 $args['value'] = ArrayHelper::get($optionsData, 'selectedOption', '');
187 }
188
189 break;
190 case 'input_date':
191 $args['format'] = Arrayhelper::get($field, 'date_format');
192 if ($args['format'] == 'default') {
193 $args['format'] = 'd/m/Y';
194 }
195 break;
196 case 'input_number':
197 $args['step'] = $field['num_step'];
198 $args['min'] = $field['num_min'];
199 $args['max'] = $field['num_max'];
200 break;
201 case 'input_hidden':
202 $args['value'] = ArrayHelper::get($field, 'default', '');
203 break;
204 case 'ratings':
205 $number = ArrayHelper::get($field, 'number_of_stars', 5);
206 $args['options'] = array_combine(range(1, $number), range(1, $number));
207 break;
208 case 'input_file':
209
210 break;
211 case 'custom_html':
212 $args['html_codes'] = $field['default'];
213 break;
214
215 case 'gdpr_agreement': // ??
216 $args['tnc_html'] = $field['config']['agreement'];
217 break;
218 case 'repeater_field':
219 $repeaterFields = ArrayHelper::get($field, 'fields', []);
220 $arr = [];
221 foreach ($repeaterFields as $serial => $repeaterField) {
222 $type = ArrayHelper::get($this->fieldTypes(), $repeaterField['type'], '');
223 $supportedRepeaterFields = ['input_text', 'select', 'input_number', 'email'];
224
225 if (in_array($type, $supportedRepeaterFields)) {
226 list($type, $args) = $this->formatFieldData($repeaterField);
227 $arr[] = $this->getFluentClassicField($type, $args);
228 }
229 }
230 if (empty($arr)) {
231 return '';
232 }
233 $args['fields'] = $arr;
234 return array('repeater_field', $args);
235 case 'submit':
236 $this->submitBtn = $this->getSubmitBttn(
237 [
238 'uniqElKey' => $field['key'],
239 'label' => $field['label'],
240 'class' => $field['element_class'],
241 ]
242 );
243 break;
244 }
245 return array($type, $args);
246 }
247
248 /**
249 * Get field type in fluentforms type
250 * @return array
251 */
252 public function fieldTypes()
253 {
254 $fieldTypes = [
255 'email' => 'email',
256 'textbox' => 'input_text',
257 'address' => 'input_text',
258 'city' => 'input_text',
259 'zip' => 'input_text',
260 'liststate' => 'select',
261 'firstname' => 'input_text',
262 'lastname' => 'input_text',
263 'listcountry' => 'select_country',
264 'textarea' => 'input_textarea',
265 'phone' => 'phone',
266 'select' => 'select',
267 'listselect' => 'select',
268 'listmultiselect' => 'multi_select',
269 'radio' => 'input_radio',
270 'listcheckbox' => 'input_checkbox',
271 'listimage' => 'input_radio',
272 'listradio' => 'input_radio',
273 'checkbox' => 'input_checkbox',
274 'date' => 'input_date',
275 'html' => 'custom_html',
276 'hr' => 'section_break',
277 'repeater' => 'repeater_field',
278 'starrating' => 'ratings',
279 'recaptcha' => 'reCaptcha',
280 'number' => 'input_number',
281 'hidden' => 'input_hidden',
282 'submit' => 'submit',
283 ];
284 return $fieldTypes;
285 }
286
287 /**
288 * Get formatted options for select,radio etc type fields
289 * @param $options
290 * @param bool $hasImage
291 * @return array (options list and selected option)
292 */
293 public function getOptions($options, $hasImage = false)
294 {
295 $formattedOptions = [];
296 $selectedOption = [];
297 foreach ($options as $key => $option) {
298 $arr = [
299 'label' => ArrayHelper::get($option, 'label', 'Item -' . $key),
300 'value' => ArrayHelper::get($option, 'value'),
301 'calc_value' => ArrayHelper::get($option, 'calc'),
302 'id' => ArrayHelper::get($option, 'order')
303 ];
304 if ($hasImage) {
305 $arr['image'] = ArrayHelper::get($option, 'image');
306 }
307 if (ArrayHelper::isTrue($option, 'selected')) {
308 $selectedOption[] = ArrayHelper::get($option, 'value', '');
309 }
310 $formattedOptions[] = $arr;
311 }
312
313 return ['options' => $formattedOptions, 'selectedOption' => $selectedOption];
314 }
315
316 /**
317 * Get Form Metas
318 * @param $form
319 * @return array
320 */
321 public function getFormMetas($form)
322 {
323 $this->addRecaptcha();
324
325 $formObject = new Form(wpFluentForm());
326 $defaults = $formObject->getFormsDefaultSettings();
327 $formSettings = $this->getFormSettings($form);
328
329 $formMeta = [];
330 $actions = Ninja_Forms()->form($this->getFormId($form))->get_actions();
331 if (is_array($actions)) {
332 foreach ($actions as $action) {
333 if ($action->get_type() != 'action') {
334 continue;
335 }
336 $actionData = $action->get_settings();
337
338 if ($actionData['type'] == 'email') {
339 $formMeta['notifications'] [] = $this->getNotificationData($actionData);
340 } elseif ($actionData['type'] == 'successmessage') {
341 $formMeta['formSettings']['confirmation'] = [
342 'messageToShow' => $this->dynamicShortcodeConverter($actionData['success_msg']),
343 'samePageFormBehavior' => ($formSettings['hide_complete']) ? 'hide_form' : 'reset_form',
344 'redirectTo' => 'samePage'
345 ];
346 } elseif ($actionData['type'] == 'save') {
347 $isAutoDelete = intval(ArrayHelper::get($actionData, 'set_subs_to_expire')) == 1;
348 if ($isAutoDelete) {
349 $formMeta['formSettings'] = [
350 'delete_after_x_days' => true,
351 'auto_delete_days' => $actionData['subs_expire_time'],
352 ];
353 }
354 } elseif ($actionData['type'] == 'redirect') {
355 $formMeta['formSettings']['confirmation'] = [
356 'messageToShow' => $this->dynamicShortcodeConverter($actionData['success_msg']),
357 'samePageFormBehavior' => isset($form['hide_form']) ? 'hide_form' : 'reset_form',
358 'redirectTo' => 'customUrl',
359 'customUrl' => $actionData['redirect_url'],
360 ];
361 }
362 }
363 }
364
365
366 $advancedValidation = [
367 'status' => false,
368 'type' => 'all',
369 'conditions' => [
370 [
371 'field' => '',
372 'operator' => '=',
373 'value' => ''
374 ]
375 ],
376 'error_message' => '',
377 'validation_type' => 'fail_on_condition_met'
378 ];
379
380 $defaults['restrictions']['requireLogin'] = [
381 'enabled' => ArrayHelper::isTrue($formSettings, 'logged_in', false),
382 'requireLoginMsg' => $formSettings['not_logged_in_msg']
383 ];
384
385 $defaults['restrictions']['limitNumberOfEntries'] = [
386 'enabled' => (intval($formSettings['sub_limit_number']) > 0),
387 'numberOfEntries' => $formSettings['sub_limit_number'],
388 'limitReachedMsg' => $formSettings['sub_limit_msg']
389 ];
390
391 $formMeta['formSettings']['restrictions'] = $defaults['restrictions'];
392 $formMeta['formSettings']['layout'] = $defaults['layout'];
393 $formMeta['advancedValidationSettings'] = $advancedValidation;
394 $formMeta['delete_entry_on_submission'] = 'no';
395 return $formMeta;
396 }
397
398 /**
399 * Update recaptcha key if already not has
400 */
401 protected function addRecaptcha()
402 {
403 $ffRecap = get_option('_fluentform_reCaptcha_details');
404 if ($ffRecap) {
405 return;
406 }
407 $recaptcha_site_key = Ninja_Forms()->get_settings();
408 $arr = '';
409 if ($recaptcha_site_key['recaptcha_site_key'] != '') {
410 $arr = [
411 'siteKey' => $recaptcha_site_key['recaptcha_site_key'],
412 'secretKey' => $recaptcha_site_key['recaptcha_secret_key'],
413 'api_version' => 'v2_visible'
414 ];
415 } elseif ($recaptcha_site_key['recaptcha_site_key_3'] != '') {
416 $arr = [
417 'siteKey' => $recaptcha_site_key['recaptcha_site_key_3'],
418 'secretKey' => $recaptcha_site_key['recaptcha_secret_key_3'],
419 'api_version' => 'v3_invisible',
420 ];
421 }
422 update_option('_fluentform_reCaptcha_details', $arr, false);
423 }
424
425 /**
426 * Get notification data for metas
427 * @param $actionData
428 * @return array
429 */
430 private function getNotificationData($actionData)
431 {
432
433 // Convert all shortcodes
434 $actionData['to'] = $this->dynamicShortcodeConverter($actionData['to']);
435 $actionData['reply_to'] = $this->dynamicShortcodeConverter($actionData['reply_to']);
436 $actionData['email_subject'] = $this->dynamicShortcodeConverter($actionData['email_subject']);
437 $actionData['email_message'] = $this->dynamicShortcodeConverter($actionData['email_message']);
438
439 $notification =
440 [
441 'sendTo' => [
442 'type' => 'email',
443 'email' => ($actionData['to'] == '{system:admin_email}') ? '{wp.admin_email}' : $actionData['to'],
444 'fromEmail' => $actionData['from_address'],
445 'field' => 'email',
446 'routing' => '',
447 ],
448 'enabled' => ArrayHelper::isTrue($actionData, 'active'),
449 'name' => $actionData['label'],
450 'subject' => $actionData['email_subject'],
451 'to' => ($actionData['to'] == '{system:admin_email}') ? '{wp.admin_email}' : $actionData['to'],
452 'replyTo' => ($actionData['to'] == '{system:admin_email}') ? '{wp.admin_email}' : $actionData['reply_to'],
453 'message' => $actionData['email_message'],
454 'fromName' => ArrayHelper::get($actionData, 'from_name'),
455 'fromAddress' => ArrayHelper::get($actionData, 'from_address'),
456 'bcc' => ArrayHelper::get($actionData, 'bcc'),
457 ];
458 return $notification;
459 }
460
461 /**
462 * Convert Ninja Forms merge Tags to Fluent forms dynamic shortcodes.
463 * @param $msg
464 * @return string
465 */
466 private function dynamicShortcodeConverter($msg)
467 {
468
469 $shortcodes = $this->dynamicShortcodes();
470
471 $msg = str_replace(array_keys($shortcodes), array_values($shortcodes), $msg);
472
473 return $msg;
474 }
475
476 /**
477 * Get shortcode in fluentforms format
478 * @return array
479 */
480 protected function dynamicShortcodes()
481 {
482 $dynamicShortcodes = [
483 // Input Options
484 'field:' => 'inputs.',
485 '{all_fields_table}' => '{all_data}',
486 '{fields_table}' => '{all_data}',
487 // General Smartcodes
488 '{wp:site_title}' => '{wp.site_title}',
489 '{wp:site_url}' => '{wp.site_url}',
490 '{wp:admin_email}' => '{wp.admin_email}',
491 '{other:user_ip}' => '{ip}',
492 '{other:date}' => '{date.d/m/Y}',
493 '{other:time}' => '',
494 '{wp:post_id}' => '{embed_post.ID}',
495 '{wp:post_title}' => '{embed_post.post_title}',
496 '{wp:post_url}' => '{embed_post.permalink}',
497 '{wp:post_author}' => '',
498 '{wp:post_author_email}' => '',
499 '{post_meta:YOUR_META_KEY}' => '{embed_post.meta.YOUR_META_KEY}',
500 '{wp:user_id}' => '{user.ID}',
501 '{wp:user_first_name}' => '{user.first_name}',
502 '{wp:user_last_name}' => '{user.last_name}',
503 '{wp:user_username}' => '{user.user_login}',
504 '{wp:user_display_name}' => '{user.display_name}',
505 '{wp:user_email}' => '{user.user_email}',
506 '{wp:user_url}' => '{wp.site_url}',
507 '{user_meta:YOUR_META_KEY}' => '',
508 // Entry Attributes
509 '{form:id}' => '',
510 '{form:title}' => '',
511 '{submission:sequence}' => '{submission.serial_number}',
512 '{submission:count}' => '{submission.serial_number}',
513 'querystring:' => 'get.'
514 ];
515
516 return $dynamicShortcodes;
517 }
518
519 /**
520 * Get form settings
521 * @param $form
522 * @return array $formSettings
523 */
524 protected function getFormSettings($form)
525 {
526 $formData = Ninja_Forms()->form($form['ID'])->get();
527 return $formData->get_settings();
528 }
529
530 /**
531 * @param $form
532 * @return mixed
533 */
534 protected function getFormId($form)
535 {
536 return intval($form['ID']);
537 }
538
539 /**
540 * @param $form
541 * @return mixed
542 */
543 protected function getFormName($form)
544 {
545 return $form['name'];
546 }
547
548 public function getEntries($formId)
549 {
550
551 $form = $this->getForm($formId);
552 if (empty($form)) {
553 return false;
554 }
555
556 $submissions = \Ninja_Forms()->form($formId)->get_subs();
557 if (!$submissions || !is_array($submissions)) {
558 return [];
559 }
560
561 $totalEntries = count($submissions);
562 $max_limit = apply_filters('fluentform/entry_migration_max_limit', static::DEFAULT_ENTRY_MIGRATION_MAX_LIMIT, $this->key, $totalEntries, $formId);
563 if ($totalEntries && $max_limit && $totalEntries > $max_limit) {
564 $submissions = array_slice($submissions, 0 , $max_limit);
565 }
566 $entries = [];
567 $fieldsMap = $this->getFieldKeyMaps($form);
568
569 foreach ($submissions as $submission) {
570 $values = $submission->get_field_values();
571 $values = ArrayHelper::only($values, $fieldsMap);
572 $values = $this->formatEntries($values);
573 if ($created_at = $submission->get_sub_date('Y-m-d H:i:s')) {
574 $values['created_at'] = $created_at;
575 }
576 if ($updated_at = $submission->get_mod_date('Y-m-d H:i:s')) {
577 $values['updated_at'] = $updated_at;
578 }
579 $entries[] = $values;
580 }
581 return array_reverse($entries);
582 }
583
584 public function getFieldKeyMaps($form)
585 {
586 $fields = ArrayHelper::get($form, 'fields');
587 $fieldsMap = [];
588 foreach ($fields as $field) {
589 $fieldsMap[] = $field['key'];
590 }
591 return $fieldsMap;
592 }
593
594 /**
595 * @param array $values
596 * @return array
597 */
598 public function formatEntries(array $values)
599 {
600 $formattedData = [];
601 foreach ($values as $key => $value) {
602 $key = str_replace('.', '_', $key);
603 $value = Helper::safeUnserialize($value);
604 $formattedData[$key] = $value;
605 if (is_array($value)) {
606 $value = $this->formatEntries($value);
607 }
608 }
609 return $formattedData;
610
611 }
612
613 }
614