| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Migrator\Classes; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\App\Modules\Form\Form; |
| 7 |
use FluentForm\App\Services\Migrator\Classes\BaseMigrator; |
| 8 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 9 |
|
| 10 |
class WpFormsMigrator extends BaseMigrator |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var bool |
| 14 |
*/ |
| 15 |
protected $hasStep = false; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
$this->key = 'wpforms'; |
| 20 |
$this->title = 'WPForms'; |
| 21 |
$this->shortcode = 'wp_form'; |
| 22 |
$this->hasStep = false; |
| 23 |
} |
| 24 |
|
| 25 |
protected function getForms() |
| 26 |
{ |
| 27 |
$forms = []; |
| 28 |
if (function_exists('wpforms')) { |
| 29 |
$formItems = wpforms()->form->get(''); |
| 30 |
foreach ($formItems as $form) { |
| 31 |
$formData = json_decode($form->post_content, true); |
| 32 |
$forms[] = [ |
| 33 |
'ID' => $form->ID, |
| 34 |
'name' => $form->post_title, |
| 35 |
'fields' => ArrayHelper::get($formData, 'fields'), |
| 36 |
'settings' => ArrayHelper::get($formData, 'settings'), |
| 37 |
]; |
| 38 |
} |
| 39 |
} |
| 40 |
return $forms; |
| 41 |
} |
| 42 |
|
| 43 |
public function getFields($form) |
| 44 |
{ |
| 45 |
$fluentFields = []; |
| 46 |
$fields = ArrayHelper::get($form, 'fields'); |
| 47 |
|
| 48 |
foreach ($fields as $field) { |
| 49 |
if ( |
| 50 |
"pagebreak" == ArrayHelper::get($field, 'type') && |
| 51 |
$position = ArrayHelper::get($field, 'position') |
| 52 |
) { |
| 53 |
if ("top" == $position || "bottom" == $position) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
} |
| 57 |
$fieldType = ArrayHelper::get($this->fieldTypeMap(), ArrayHelper::get($field, 'type')); |
| 58 |
$args = $this->formatFieldData($field, $fieldType); |
| 59 |
if ('select' == $fieldType && ArrayHelper::isTrue($field, 'multiple')) { |
| 60 |
$fieldType = 'multi_select'; |
| 61 |
} |
| 62 |
if ($fieldData = $this->getFluentClassicField($fieldType, $args)) { |
| 63 |
$fluentFields[$field['id']] = $fieldData; |
| 64 |
} else { |
| 65 |
$this->unSupportFields[] = ArrayHelper::get($field, 'label'); |
| 66 |
} |
| 67 |
} |
| 68 |
$submitBtn = $this->getSubmitBttn([ |
| 69 |
'uniqElKey' => 'button_' . time(), |
| 70 |
'label' => ArrayHelper::get($form, 'settings.submit_text'), |
| 71 |
'class' => ArrayHelper::get($form, 'settings.submit_class'), |
| 72 |
]); |
| 73 |
if (empty($fluentFields)) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
$returnData = [ |
| 78 |
'fields' => $fluentFields, |
| 79 |
'submitButton' => $submitBtn |
| 80 |
]; |
| 81 |
|
| 82 |
if ($this->hasStep && defined('FLUENTFORMPRO')) { |
| 83 |
$returnData['stepsWrapper'] = $this->getStepWrapper(); |
| 84 |
$this->hasStep = false; |
| 85 |
} |
| 86 |
return $returnData; |
| 87 |
} |
| 88 |
|
| 89 |
public function getSubmitBttn($args) |
| 90 |
{ |
| 91 |
return [ |
| 92 |
'uniqElKey' => $args['uniqElKey'], |
| 93 |
'element' => 'button', |
| 94 |
'attributes' => [ |
| 95 |
'type' => 'submit', |
| 96 |
'class' => $args['class'] |
| 97 |
], |
| 98 |
'settings' => [ |
| 99 |
'container_class' => '', |
| 100 |
'align' => 'left', |
| 101 |
'button_style' => 'default', |
| 102 |
'button_size' => 'md', |
| 103 |
'color' => '#ffffff', |
| 104 |
'background_color' => '#409EFF', |
| 105 |
'button_ui' => [ |
| 106 |
'type' => ArrayHelper::get($args, 'type', 'default'), |
| 107 |
'text' => $args['label'], |
| 108 |
'img_url' => ArrayHelper::get($args, 'img_url', '') |
| 109 |
], |
| 110 |
'normal_styles' => [], |
| 111 |
'hover_styles' => [], |
| 112 |
'current_state' => "normal_styles" |
| 113 |
], |
| 114 |
'editor_options' => [ |
| 115 |
'title' => 'Submit Button', |
| 116 |
], |
| 117 |
|
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
protected function getFormName($form) |
| 122 |
{ |
| 123 |
return $form['name']; |
| 124 |
} |
| 125 |
|
| 126 |
protected function getFormMetas($form) |
| 127 |
{ |
| 128 |
$formObject = new Form(wpFluentForm()); |
| 129 |
$defaults = $formObject->getFormsDefaultSettings(); |
| 130 |
$confirmationsFormatted = $this->getConfirmations($form, $defaults['confirmation']); |
| 131 |
$defaultConfirmation = array_pop($confirmationsFormatted); |
| 132 |
|
| 133 |
$notifications = $this->getNotifications($form); |
| 134 |
$metas = [ |
| 135 |
'formSettings' => [ |
| 136 |
'confirmation' => $defaultConfirmation, |
| 137 |
'restrictions' => $defaults['restrictions'], |
| 138 |
'layout' => $defaults['layout'], |
| 139 |
], |
| 140 |
'advancedValidationSettings' => $this->getAdvancedValidation(), |
| 141 |
'delete_entry_on_submission' => 'no', |
| 142 |
'notifications' => $notifications, |
| 143 |
'confirmations' => $confirmationsFormatted, |
| 144 |
]; |
| 145 |
if ($webhooks = $this->getWebhooks($form)) { |
| 146 |
$metas['webhooks'] = $webhooks; |
| 147 |
} |
| 148 |
return $metas; |
| 149 |
} |
| 150 |
|
| 151 |
protected function getFormId($form) |
| 152 |
{ |
| 153 |
return $form['ID']; |
| 154 |
} |
| 155 |
|
| 156 |
protected function getForm($id) |
| 157 |
{ |
| 158 |
if (function_exists('wpforms') && $form = wpforms()->form->get($id)) { |
| 159 |
$formData = json_decode($form->post_content, true); |
| 160 |
return [ |
| 161 |
'ID' => $form->ID, |
| 162 |
'name' => $form->post_title, |
| 163 |
'fields' => ArrayHelper::get($formData, 'fields'), |
| 164 |
'settings' => ArrayHelper::get($formData, 'settings'), |
| 165 |
]; |
| 166 |
} |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
public function getEntries($formId) |
| 171 |
{ |
| 172 |
if(!wpforms()->is_pro()){ |
| 173 |
wp_send_json([ |
| 174 |
'message' => __("Entries not available in WPForms Lite",'fluentform') |
| 175 |
], 200); |
| 176 |
} |
| 177 |
$form = $this->getForm($formId); |
| 178 |
if (empty($form)) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
$formFields = $this->getFields($form); |
| 182 |
if ($formFields) { |
| 183 |
$formFields = $formFields['fields']; |
| 184 |
} |
| 185 |
$args = [ |
| 186 |
'form_id' => $form['ID'], |
| 187 |
'order' => 'asc', |
| 188 |
]; |
| 189 |
$totalEntries = wpforms()->entry->get_entries($args, true);// 2nd parameter 'true' means return total entries count |
| 190 |
$args['number'] = apply_filters('fluentform/entry_migration_max_limit', static::DEFAULT_ENTRY_MIGRATION_MAX_LIMIT, $this->key, $totalEntries, $formId); |
| 191 |
$submissions = wpforms()->entry->get_entries($args); |
| 192 |
$entries = []; |
| 193 |
if (!$submissions || !is_array($submissions)) { |
| 194 |
return $entries; |
| 195 |
} |
| 196 |
foreach ($submissions as $submission) { |
| 197 |
$fields = \json_decode( $submission->fields , true); |
| 198 |
if (!$fields) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
$entry = []; |
| 202 |
foreach ($fields as $fieldId => $field) { |
| 203 |
if (!isset($formFields[$fieldId])) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
$formField = $formFields[$fieldId]; |
| 207 |
$name = ArrayHelper::get($formField, 'attributes.name'); |
| 208 |
if (!$name) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
$type = ArrayHelper::get($formField, 'element'); |
| 212 |
// format entry value by field name |
| 213 |
$finalValue = ArrayHelper::get($field, 'value'); |
| 214 |
if ("input_name" == $type) { |
| 215 |
$finalValue = $this->getSubmissionNameValue($formField['fields'], $field); |
| 216 |
} elseif ( |
| 217 |
"input_checkbox" == $type || |
| 218 |
( |
| 219 |
"select" == $type && |
| 220 |
ArrayHelper::isTrue($formField, 'attributes.multiple') |
| 221 |
) |
| 222 |
) { |
| 223 |
$finalValue = explode("\n", $finalValue); |
| 224 |
} elseif ("address" == $type) { |
| 225 |
$finalValue = [ |
| 226 |
"address_line_1" => ArrayHelper::get($field, 'address1', ''), |
| 227 |
"address_line_2" => ArrayHelper::get($field, 'address2', ''), |
| 228 |
"city" => ArrayHelper::get($field, 'city', ''), |
| 229 |
"state" => ArrayHelper::get($field, 'state', ''), |
| 230 |
"zip" => ArrayHelper::get($field, 'postal', ''), |
| 231 |
"country" => ArrayHelper::get($field, 'country', ''), |
| 232 |
]; |
| 233 |
} elseif ("input_file" == $type && $value = ArrayHelper::get($field, 'value')) { |
| 234 |
$finalValue = $this->migrateFilesAndGetUrls($value); |
| 235 |
} |
| 236 |
if (null == $finalValue) { |
| 237 |
$finalValue = ""; |
| 238 |
} |
| 239 |
$entry[$name] = $finalValue; |
| 240 |
} |
| 241 |
if ($submission->date) { |
| 242 |
$entry['created_at'] = $submission->date; |
| 243 |
} |
| 244 |
if ($submission->date_modified) { |
| 245 |
$entry['updated_at'] = $submission->date_modified; |
| 246 |
} |
| 247 |
if ($submission->starred) { |
| 248 |
$entry['is_favourite'] = $submission->starred; |
| 249 |
} |
| 250 |
if ($submission->viewed) { |
| 251 |
$entry['status'] = 'read'; |
| 252 |
} |
| 253 |
$entries[] = $entry; |
| 254 |
} |
| 255 |
return $entries; |
| 256 |
} |
| 257 |
|
| 258 |
protected function getSubmissionNameValue($nameFields, $submissionField) { |
| 259 |
$finalValue = []; |
| 260 |
foreach ($nameFields as $key => $field) { |
| 261 |
if ($name = ArrayHelper::get($field, 'attributes.name')) { |
| 262 |
$value = ""; |
| 263 |
if ("first_name" == $key) { |
| 264 |
$value = ArrayHelper::get($submissionField, 'first'); |
| 265 |
} elseif ("middle_name" == $key) { |
| 266 |
$value = ArrayHelper::get($submissionField, 'middle'); |
| 267 |
} elseif ("last_name" == $key) { |
| 268 |
$value = ArrayHelper::get($submissionField, 'last'); |
| 269 |
} |
| 270 |
$finalValue[$name] = $value; |
| 271 |
} |
| 272 |
} |
| 273 |
return $finalValue; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
public function getFormsFormatted() |
| 278 |
{ |
| 279 |
$forms = []; |
| 280 |
$items = $this->getForms(); |
| 281 |
foreach ($items as $item) { |
| 282 |
$forms[] = [ |
| 283 |
'name' => $item['name'], |
| 284 |
'id' => $item['ID'], |
| 285 |
'imported_ff_id' => $this->isAlreadyImported($item), |
| 286 |
]; |
| 287 |
} |
| 288 |
return $forms; |
| 289 |
} |
| 290 |
|
| 291 |
public function exist() |
| 292 |
{ |
| 293 |
return !!defined('WPFORMS_VERSION'); |
| 294 |
} |
| 295 |
|
| 296 |
protected function formatFieldData($field, $type) |
| 297 |
{ |
| 298 |
$args = [ |
| 299 |
'uniqElKey' => $field['id'] . '-' . time(), |
| 300 |
'index' => $field['id'], |
| 301 |
'required' => ArrayHelper::isTrue($field, 'required'), |
| 302 |
'label' => ArrayHelper::get($field, 'label', ''), |
| 303 |
'name' => ArrayHelper::get($field, 'type') . '_' . $field['id'], |
| 304 |
'placeholder' => ArrayHelper::get($field, 'placeholder', ''), |
| 305 |
'class' => '', |
| 306 |
'value' => ArrayHelper::get($field, 'default_value') ?: "", |
| 307 |
'help_message' => ArrayHelper::get($field, 'description', ''), |
| 308 |
'container_class' => ArrayHelper::get($field, 'css', ''), |
| 309 |
]; |
| 310 |
|
| 311 |
switch ($type) { |
| 312 |
case 'input_text': |
| 313 |
if (ArrayHelper::isTrue($field, 'limit_enabled')) { |
| 314 |
$max_length = ArrayHelper::get($field, 'limit_count', ''); |
| 315 |
$mode = ArrayHelper::get($field, 'limit_mode', ''); |
| 316 |
if ("words" == $mode && $max_length) { |
| 317 |
$max_length = (int)$max_length * 6; // average 6 characters is a word |
| 318 |
} |
| 319 |
$args['maxlength'] = $max_length; |
| 320 |
} |
| 321 |
break; |
| 322 |
case 'phone': |
| 323 |
$args['valid_phone_number'] = "1"; |
| 324 |
break; |
| 325 |
case 'input_name': |
| 326 |
$args['input_name_args'] = []; |
| 327 |
$fields = ArrayHelper::get($field, 'format'); |
| 328 |
if (!$fields) { |
| 329 |
break; |
| 330 |
} |
| 331 |
$fields = explode('-', $fields); |
| 332 |
$required = ArrayHelper::isTrue($field, 'required'); |
| 333 |
foreach ($fields as $subField) { |
| 334 |
if ($subField == 'simple') { |
| 335 |
$label = $args['label']; |
| 336 |
$subName = 'first_name'; |
| 337 |
$hideLabel = ArrayHelper::isTrue($field, 'label_hide'); |
| 338 |
} else { |
| 339 |
$subName = $subField . '_name'; |
| 340 |
$label = ucfirst($subField); |
| 341 |
$hideLabel = ArrayHelper::isTrue($field, 'sublabel_hide'); |
| 342 |
} |
| 343 |
$placeholder = ArrayHelper::get($field, $subField . "_placeholder" , ''); |
| 344 |
$default = ArrayHelper::get($field, $subField . "_default" , ''); |
| 345 |
$args['input_name_args'][$subName] = [ |
| 346 |
"visible" => true, |
| 347 |
"required" => $required, |
| 348 |
"name" => $subName, |
| 349 |
"default" => $default, |
| 350 |
]; |
| 351 |
if (!$hideLabel) { |
| 352 |
$args['input_name_args'][$subName]['label'] = $label; |
| 353 |
} |
| 354 |
if ($placeholder) { |
| 355 |
$args['input_name_args'][$subName]['placeholder'] = $placeholder; |
| 356 |
} |
| 357 |
} |
| 358 |
break; |
| 359 |
case 'select': |
| 360 |
case 'input_radio': |
| 361 |
case 'input_checkbox': |
| 362 |
list($options, $defaultVal) = $this->getOptions(ArrayHelper::get($field, 'choices', [])); |
| 363 |
$args['options'] = $options; |
| 364 |
$args['randomize_options'] = ArrayHelper::isTrue($field, 'random'); |
| 365 |
if ($type == 'select') { |
| 366 |
$isMulti = ArrayHelper::isTrue($field, 'multiple'); |
| 367 |
if ($isMulti) { |
| 368 |
$args['multiple'] = true; |
| 369 |
$args['value'] = $defaultVal; |
| 370 |
} else { |
| 371 |
$args['value'] = array_shift($defaultVal) ?: ""; |
| 372 |
} |
| 373 |
} elseif ($type == 'input_checkbox') { |
| 374 |
$args['value'] = $defaultVal; |
| 375 |
} elseif ($type == 'input_radio') { |
| 376 |
$args['value'] = array_shift($defaultVal) ?: ""; |
| 377 |
} |
| 378 |
break; |
| 379 |
case 'input_date': |
| 380 |
$format = ArrayHelper::get($field, 'format'); |
| 381 |
if ("date" == $format) { |
| 382 |
$format = ArrayHelper::get($field, 'date_format', 'd/m/Y'); |
| 383 |
} elseif ("time" == $format) { |
| 384 |
$format = ArrayHelper::get($field, 'time_format', 'H:i'); |
| 385 |
} else { |
| 386 |
$format = ArrayHelper::get($field, 'date_format', 'd/m/Y') . ' ' .ArrayHelper::get($field, 'time_format', 'H:i'); |
| 387 |
} |
| 388 |
$args['format'] = $format; |
| 389 |
break; |
| 390 |
case 'rangeslider': |
| 391 |
$args['step'] = $field['step']; |
| 392 |
$args['min'] = $field['min']; |
| 393 |
$args['max'] = $field['max']; |
| 394 |
break; |
| 395 |
case 'ratings': |
| 396 |
$number = ArrayHelper::get($field, 'scale', 5); |
| 397 |
$args['options'] = array_combine(range(1, $number), range(1, $number)); |
| 398 |
break; |
| 399 |
case 'input_file': |
| 400 |
$args['allowed_file_types'] = $this->getFileTypes($field, 'extensions'); |
| 401 |
$args['max_size_unit'] = 'MB'; |
| 402 |
$max_size = ArrayHelper::get($field, 'max_size') ?: 1; |
| 403 |
$args['max_file_size'] = ceil( $max_size * 1048576); // 1MB = 1048576 Bytes |
| 404 |
$args['max_file_count'] = ArrayHelper::get($field, 'max_file_number', 1); |
| 405 |
$args['upload_btn_text'] = ArrayHelper::get($field, 'label', 'File Upload'); |
| 406 |
break; |
| 407 |
case 'custom_html': |
| 408 |
$args['html_codes'] = ArrayHelper::get($field, 'code', ''); |
| 409 |
break; |
| 410 |
case 'form_step': |
| 411 |
$this->hasStep = true; |
| 412 |
break; |
| 413 |
case 'address': |
| 414 |
$args['address_args'] = $this->getAddressArgs($field, $args); |
| 415 |
break; |
| 416 |
case 'rich_text_input': |
| 417 |
$size = ArrayHelper::get($field, 'size'); |
| 418 |
if ('small' == $size) { |
| 419 |
$rows = 2; |
| 420 |
} elseif ('large' == $size) { |
| 421 |
$rows = 5; |
| 422 |
} else { |
| 423 |
$rows =3; |
| 424 |
} |
| 425 |
$args['rows'] = $rows; |
| 426 |
break; |
| 427 |
case 'section_break': |
| 428 |
$args['section_break_desc'] = ArrayHelper::get($field, 'description'); |
| 429 |
break; |
| 430 |
case 'input_number': |
| 431 |
$args['min'] = ''; |
| 432 |
$args['max'] = ''; |
| 433 |
break; |
| 434 |
default : |
| 435 |
break; |
| 436 |
} |
| 437 |
return $args; |
| 438 |
} |
| 439 |
|
| 440 |
private function fieldTypeMap() |
| 441 |
{ |
| 442 |
return [ |
| 443 |
'email' => 'email', |
| 444 |
'text' => 'input_text', |
| 445 |
'name' => 'input_name', |
| 446 |
'hidden' => 'input_hidden', |
| 447 |
'textarea' => 'input_textarea', |
| 448 |
'select' => 'select', |
| 449 |
'radio' => 'input_radio', |
| 450 |
'checkbox' => 'input_checkbox', |
| 451 |
'number' => 'input_number', |
| 452 |
'layout' => 'container', |
| 453 |
'date-time' => 'input_date', |
| 454 |
'address' => 'address', |
| 455 |
'password' => 'input_password', |
| 456 |
'html' => 'custom_html', |
| 457 |
'rating' => 'ratings', |
| 458 |
'divider' => 'section_break', |
| 459 |
'url' => 'input_url', |
| 460 |
'multi_select' => 'multi_select', |
| 461 |
'number-slider' => 'rangeslider', |
| 462 |
'richtext' => 'rich_text_input', |
| 463 |
'phone' => 'phone', |
| 464 |
'file-upload' => 'input_file', |
| 465 |
'pagebreak' => 'form_step', |
| 466 |
]; |
| 467 |
} |
| 468 |
|
| 469 |
private function getConfirmations($form, $defaultValues) |
| 470 |
{ |
| 471 |
$confirmations = ArrayHelper::get($form, 'settings.confirmations'); |
| 472 |
$confirmationsFormatted = []; |
| 473 |
if (!empty($confirmations)) { |
| 474 |
foreach ($confirmations as $confirmation) { |
| 475 |
$type = $confirmation['type']; |
| 476 |
if ($type == 'redirect') { |
| 477 |
$redirectTo = 'customUrl'; |
| 478 |
} else { |
| 479 |
if ($type == 'page') { |
| 480 |
$redirectTo = 'customPage'; |
| 481 |
} else { |
| 482 |
$redirectTo = 'samePage'; |
| 483 |
} |
| 484 |
} |
| 485 |
$confirmationsFormatted[] = wp_parse_args( |
| 486 |
[ |
| 487 |
'name' => ArrayHelper::get($confirmation, 'name'), |
| 488 |
'messageToShow' => $this->getResolveShortcode(ArrayHelper::get($confirmation, 'message'), $form), |
| 489 |
'samePageFormBehavior' => 'hide_form', |
| 490 |
'redirectTo' => $redirectTo, |
| 491 |
'customPage' => intval(ArrayHelper::get($confirmation, 'page')), |
| 492 |
'customUrl' => ArrayHelper::get($confirmation, 'redirect'), |
| 493 |
'active' => true, |
| 494 |
'conditionals' => $this->getConditionals($confirmation, $form) |
| 495 |
], $defaultValues |
| 496 |
); |
| 497 |
} |
| 498 |
} |
| 499 |
return $confirmationsFormatted; |
| 500 |
} |
| 501 |
|
| 502 |
private function getConditionals($notification, $form) |
| 503 |
{ |
| 504 |
$conditionals = ArrayHelper::get($notification, 'conditionals', []); |
| 505 |
$status = ArrayHelper::isTrue($notification, 'conditional_logic'); |
| 506 |
if ('stop' == ArrayHelper::get($notification, 'conditional_type')) { |
| 507 |
$status = false; |
| 508 |
} |
| 509 |
$type = 'all'; |
| 510 |
$conditions = []; |
| 511 |
if ($conditionals) { |
| 512 |
if (count($conditionals) > 1) { |
| 513 |
$type = 'any'; |
| 514 |
$conditionals = array_filter(array_column($conditionals, 0)); |
| 515 |
} else { |
| 516 |
$conditionals = ArrayHelper::get($conditionals, 0, []); |
| 517 |
} |
| 518 |
foreach ($conditionals as $condition) { |
| 519 |
$fieldId = ArrayHelper::get($condition, 'field'); |
| 520 |
list ($fieldName, $fieldType) = $this->getFormFieldName($fieldId, $form); |
| 521 |
if (!$fieldName) { |
| 522 |
continue; |
| 523 |
} |
| 524 |
if ($operator = $this->getResolveOperator(ArrayHelper::get($condition, 'operator', ''))) { |
| 525 |
$value = ArrayHelper::get($condition, 'value', ''); |
| 526 |
if ( |
| 527 |
in_array($fieldType, ['select', 'multi_select', 'input_radio', 'input_checkbox']) && |
| 528 |
$choices = ArrayHelper::get($form, "fields.$fieldId.choices") |
| 529 |
) { |
| 530 |
$choiceValue = ArrayHelper::get($choices, "$value.value", ''); |
| 531 |
if (!$choiceValue) { |
| 532 |
$choiceValue = ArrayHelper::get($choices, "$value.label", ''); |
| 533 |
} |
| 534 |
$value = $choiceValue; |
| 535 |
} |
| 536 |
$conditions[] = [ |
| 537 |
'field' => $fieldName, |
| 538 |
'operator' => $operator, |
| 539 |
'value' => $value |
| 540 |
]; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
return [ |
| 545 |
"status" => $status, |
| 546 |
"type" => $type, |
| 547 |
'conditions' => $conditions |
| 548 |
]; |
| 549 |
} |
| 550 |
|
| 551 |
private function getAdvancedValidation(): array |
| 552 |
{ |
| 553 |
return [ |
| 554 |
'status' => false, |
| 555 |
'type' => 'all', |
| 556 |
'conditions' => [ |
| 557 |
[ |
| 558 |
'field' => '', |
| 559 |
'operator' => '=', |
| 560 |
'value' => '' |
| 561 |
] |
| 562 |
], |
| 563 |
'error_message' => '', |
| 564 |
'validation_type' => 'fail_on_condition_met' |
| 565 |
]; |
| 566 |
} |
| 567 |
|
| 568 |
private function getNotifications($form) |
| 569 |
{ |
| 570 |
$notificationsFormatted = []; |
| 571 |
$enabled = ArrayHelper::isTrue($form, 'settings.notification_enable'); |
| 572 |
$notifications = ArrayHelper::get($form, 'settings.notifications'); |
| 573 |
|
| 574 |
foreach ($notifications as $notification) { |
| 575 |
$email = ArrayHelper::get($notification, 'email', ''); |
| 576 |
$sendTo = [ |
| 577 |
'type' => 'email', |
| 578 |
'email' => '{wp.admin_email}', |
| 579 |
'field' => '', |
| 580 |
'routing' => [], |
| 581 |
]; |
| 582 |
if ($this->isFormField($email)) { |
| 583 |
list($fieldName) = $this->getFormFieldName($email, $form); |
| 584 |
$sendTo['type'] = 'field'; |
| 585 |
$sendTo['field'] = $fieldName; |
| 586 |
$sendTo['email'] = ''; |
| 587 |
} else { |
| 588 |
if ($email) { |
| 589 |
$sendTo['email'] = $this->getResolveShortcode($email, $form); |
| 590 |
} |
| 591 |
} |
| 592 |
$message = $this->getResolveShortcode(ArrayHelper::get($notification, 'message', ''), $form); |
| 593 |
$replyTo = $this->getResolveShortcode(ArrayHelper::get($notification, 'replyto', ''), $form); |
| 594 |
$notificationsFormatted[] = [ |
| 595 |
'sendTo' => $sendTo, |
| 596 |
'enabled' => $enabled, |
| 597 |
'name' => ArrayHelper::get($notification, 'notification_name', 'Admin Notification'), |
| 598 |
'subject' => $this->getResolveShortcode(ArrayHelper::get($notification, 'subject', 'Notification'), $form), |
| 599 |
'to' => $sendTo['email'], |
| 600 |
'replyTo' => $replyTo ?: '{wp.admin_email}', |
| 601 |
'message' => str_replace("\n", "<br />", $message), |
| 602 |
'fromName' => $this->getResolveShortcode(ArrayHelper::get($notification, 'sender_name', ''), $form), |
| 603 |
'fromEmail' => $this->getResolveShortcode(ArrayHelper::get($notification, 'sender_address', ''), $form), |
| 604 |
'bcc' => '', |
| 605 |
'conditionals' => $this->getConditionals($notification, $form) |
| 606 |
]; |
| 607 |
} |
| 608 |
return $notificationsFormatted; |
| 609 |
} |
| 610 |
|
| 611 |
/** |
| 612 |
* Get webhooks feeds |
| 613 |
* |
| 614 |
* @param array $form |
| 615 |
* @return array |
| 616 |
*/ |
| 617 |
private function getWebhooks($form) |
| 618 |
{ |
| 619 |
$webhooksFeeds = []; |
| 620 |
foreach (ArrayHelper::get($form, 'settings.webhooks', []) as $webhook) { |
| 621 |
list($headers, $headersKeysStatus, $headersValuesStatus) = $this->getResolveMappingFields(ArrayHelper::get($webhook, 'headers', []), $form); |
| 622 |
|
| 623 |
$body = ArrayHelper::get($webhook, 'body', []); |
| 624 |
// ff webhook body parameter doesn't support custom type fields |
| 625 |
// remove custom type fields, wpforms add "custom_" prefix on key for custom type value |
| 626 |
$body = array_filter($body, function ($key) { |
| 627 |
return !(strpos($key, 'custom_') !== false); |
| 628 |
}, ARRAY_FILTER_USE_KEY); |
| 629 |
list($body) = $this->getResolveMappingFields($body, $form); |
| 630 |
|
| 631 |
$webhooksFeeds[] = [ |
| 632 |
'name' => ArrayHelper::get($webhook, 'name', ''), |
| 633 |
'request_url' => ArrayHelper::get($webhook, 'url', ''), |
| 634 |
'with_header' => count($headers) > 0 ? 'yup' : 'nop', |
| 635 |
'request_method' => strtoupper(ArrayHelper::get($webhook, 'method', 'GET')), |
| 636 |
'request_format' => strtoupper(ArrayHelper::get($webhook, 'format', 'FORM')), |
| 637 |
'request_body' => 'selected_fields', |
| 638 |
'custom_header_keys' => $headersKeysStatus, |
| 639 |
'custom_header_values' => $headersValuesStatus, |
| 640 |
'fields' => $body, |
| 641 |
'request_headers' => $headers, |
| 642 |
'conditionals' => $this->getConditionals($webhook, $form), |
| 643 |
'enabled' => ArrayHelper::isTrue($form, 'settings.webhooks_enable') |
| 644 |
]; |
| 645 |
} |
| 646 |
return $webhooksFeeds; |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* Get resolved mapping fields |
| 651 |
* |
| 652 |
* @param array $fields |
| 653 |
* @param array $form |
| 654 |
* @return array |
| 655 |
*/ |
| 656 |
private function getResolveMappingFields($fields, $form) |
| 657 |
{ |
| 658 |
$mapping = []; |
| 659 |
$mappingKeysStatus = []; |
| 660 |
$mappingValuesStatus = []; |
| 661 |
foreach ($fields as $key => $value) { |
| 662 |
// wpforms add "custom_" prefix on key for custom type value |
| 663 |
if ((strpos($key, 'custom_') !== false) || is_array($value)) { |
| 664 |
$key = str_replace('custom_', '', $key); |
| 665 |
// ff not support secure value, when value is secure decrypt it's by wpforms helper |
| 666 |
if (ArrayHelper::isTrue($value, 'secure') && method_exists('\WPForms\Helpers\Crypto', 'decrypt')) { |
| 667 |
$value = ArrayHelper::get($value, 'value', ''); |
| 668 |
if ($decryptValue = \WPForms\Helpers\Crypto::decrypt($value)) { |
| 669 |
$value = $decryptValue; |
| 670 |
} |
| 671 |
} else { |
| 672 |
$value = ArrayHelper::get($value, 'value', ''); |
| 673 |
} |
| 674 |
$mappingKeysStatus[] = true; |
| 675 |
$mappingValuesStatus[] = true; |
| 676 |
} else { |
| 677 |
list ($fieldName) = $this->getFormFieldName($value, $form); |
| 678 |
if (!$fieldName) { |
| 679 |
continue; |
| 680 |
} |
| 681 |
$value = "{inputs.$fieldName}"; |
| 682 |
$mappingKeysStatus[] = false; |
| 683 |
$mappingValuesStatus[] = false; |
| 684 |
} |
| 685 |
$mapping[] = [ |
| 686 |
'key' => $key, |
| 687 |
'value' => $value |
| 688 |
]; |
| 689 |
} |
| 690 |
return [$mapping, $mappingKeysStatus, $mappingValuesStatus]; |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* Get bool value depend on shortcode is form inputs or not |
| 695 |
* |
| 696 |
* @param string $string |
| 697 |
* @return boolean |
| 698 |
*/ |
| 699 |
private function isFormField($string) |
| 700 |
{ |
| 701 |
return (strpos($string, '{field_id=') !== false); |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* Get form field name in fluentforms format |
| 706 |
* |
| 707 |
* @param string $str |
| 708 |
* @param array $form |
| 709 |
* @return array |
| 710 |
*/ |
| 711 |
private function getFormFieldName($str, $form) |
| 712 |
{ |
| 713 |
preg_match('/\d+/', $str, $fieldId); |
| 714 |
$field = ArrayHelper::get($form, 'fields.' . ArrayHelper::get($fieldId, 0, '0')); |
| 715 |
$fieldType = ArrayHelper::get($this->fieldTypeMap(), ArrayHelper::get($field, 'type')); |
| 716 |
if (in_array(ArrayHelper::get($field, 'label'), $this->unSupportFields)) { |
| 717 |
return ['', $fieldType]; |
| 718 |
} |
| 719 |
$fieldName = ArrayHelper::get($this->formatFieldData($field, $fieldType), 'name', ''); |
| 720 |
return [$fieldName, $fieldType]; |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* Get shortcode in fluentforms format |
| 725 |
* @return array |
| 726 |
*/ |
| 727 |
private function dynamicShortcodes() |
| 728 |
{ |
| 729 |
return [ |
| 730 |
'{all_fields}' => '{all_data}', |
| 731 |
'{admin_email}' => '{wp.admin_email}', |
| 732 |
'{user_ip}' => '{ip}', |
| 733 |
'{date format="m/d/Y"}' => '{date.d/m/Y}', |
| 734 |
'{page_id}' => '{embed_post.ID}', |
| 735 |
'{page_title}' => '{embed_post.post_title}', |
| 736 |
'{page_url}' => '{embed_post.permalink}', |
| 737 |
'{user_id}' => '{user.ID}', |
| 738 |
'{user_first_name}' => '{user.first_name}', |
| 739 |
'{user_last_name}' => '{user.last_name}', |
| 740 |
'{user_display}' => '{user.display_name}', |
| 741 |
'{user_full_name}' => '{user.first_name} {user.last_name}', |
| 742 |
'{user_email}' => '{user.user_email}', |
| 743 |
'{entry_id}' => '{submission.id}', |
| 744 |
'{entry_date format="d/m/Y"}' => '{submission.created_at}', |
| 745 |
'{entry_details_url}' => '{submission.admin_view_url}', |
| 746 |
'{url_referer}' => '{http_referer}' |
| 747 |
]; |
| 748 |
} |
| 749 |
|
| 750 |
/** |
| 751 |
* Resolve shortcode in fluentforms format |
| 752 |
* |
| 753 |
* @param string $message |
| 754 |
* @param array $form |
| 755 |
* @return string |
| 756 |
*/ |
| 757 |
private function getResolveShortcode($message, $form) |
| 758 |
{ |
| 759 |
if (!$message) { |
| 760 |
return $message; |
| 761 |
} |
| 762 |
preg_match_all('/{(.*?)}/', $message, $matches); |
| 763 |
if (!$matches[0]) { |
| 764 |
return $message; |
| 765 |
} |
| 766 |
$shortcodes = $this->dynamicShortcodes(); |
| 767 |
foreach ($matches[0] as $match) { |
| 768 |
$replace = ''; |
| 769 |
if (isset($shortcodes[$match])) { |
| 770 |
$replace = $shortcodes[$match]; |
| 771 |
} elseif ($this->isFormField($match)) { |
| 772 |
list($fieldName) = $this->getFormFieldName($match, $form); |
| 773 |
if ($fieldName) { |
| 774 |
$replace = "{inputs.$fieldName}"; |
| 775 |
} |
| 776 |
} elseif (strpos($match, 'query_var') !== false) { |
| 777 |
preg_match('#key=["\'](\S+)["\']#', $match, $result); |
| 778 |
if ($key = ArrayHelper::get($result, 1)) { |
| 779 |
$replace = "{get.$key}"; |
| 780 |
} |
| 781 |
} |
| 782 |
$message = str_replace($match, $replace, $message); |
| 783 |
} |
| 784 |
return $message; |
| 785 |
} |
| 786 |
|
| 787 |
public function getOptions($options) |
| 788 |
{ |
| 789 |
$formattedOptions = []; |
| 790 |
$defaults = []; |
| 791 |
foreach ($options as $key => $option) { |
| 792 |
$formattedOption = [ |
| 793 |
'label' => ArrayHelper::get($option, 'label', 'Item -' . $key), |
| 794 |
'value' => !empty(ArrayHelper::get($option, 'value')) ? ArrayHelper::get($option, |
| 795 |
'value') : ArrayHelper::get($option, 'label', 'Item -' . $key), |
| 796 |
'image' => ArrayHelper::get($option, 'image'), |
| 797 |
'calc_value' => '', |
| 798 |
'id' => $key, |
| 799 |
]; |
| 800 |
if (ArrayHelper::isTrue($option, 'default')) { |
| 801 |
$defaults[] = $formattedOption['value']; |
| 802 |
} |
| 803 |
$formattedOptions[] = $formattedOption; |
| 804 |
|
| 805 |
} |
| 806 |
return [$formattedOptions, $defaults]; |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
* @return array |
| 811 |
*/ |
| 812 |
private function getStepWrapper() |
| 813 |
{ |
| 814 |
return [ |
| 815 |
'stepStart' => [ |
| 816 |
'element' => 'step_start', |
| 817 |
'attributes' => [ |
| 818 |
'id' => '', |
| 819 |
'class' => '', |
| 820 |
], |
| 821 |
'settings' => [ |
| 822 |
'progress_indicator' => 'progress-bar', |
| 823 |
'step_titles' => [], |
| 824 |
'disable_auto_focus' => 'no', |
| 825 |
'enable_auto_slider' => 'no', |
| 826 |
'enable_step_data_persistency' => 'no', |
| 827 |
'enable_step_page_resume' => 'no', |
| 828 |
], |
| 829 |
'editor_options' => [ |
| 830 |
'title' => 'Start Paging' |
| 831 |
], |
| 832 |
], |
| 833 |
'stepEnd' => [ |
| 834 |
'element' => 'step_end', |
| 835 |
'attributes' => [ |
| 836 |
'id' => '', |
| 837 |
'class' => '', |
| 838 |
], |
| 839 |
'settings' => [ |
| 840 |
'prev_btn' => [ |
| 841 |
'type' => 'default', |
| 842 |
'text' => 'Previous', |
| 843 |
'img_url' => '' |
| 844 |
] |
| 845 |
], |
| 846 |
'editor_options' => [ |
| 847 |
'title' => 'End Paging' |
| 848 |
], |
| 849 |
] |
| 850 |
|
| 851 |
]; |
| 852 |
} |
| 853 |
|
| 854 |
/** |
| 855 |
* @param array $field |
| 856 |
* @return array[] |
| 857 |
*/ |
| 858 |
private function getAddressArgs($field, $args) |
| 859 |
{ |
| 860 |
if ('us' == ArrayHelper::get($field, 'scheme')) { |
| 861 |
$field['country_default'] = 'US'; |
| 862 |
} |
| 863 |
$hideSubLabel = ArrayHelper::isTrue($field, 'sublabel_hide'); |
| 864 |
$name = ArrayHelper::get($args, 'name', 'address'); |
| 865 |
return [ |
| 866 |
'address_line_1' => [ |
| 867 |
'name' => $name . '_address_line_1', |
| 868 |
'default' => ArrayHelper::get($field, 'address1_default', ''), |
| 869 |
'placeholder' => ArrayHelper::get($field, 'address1_placeholder', ''), |
| 870 |
'label' => $hideSubLabel ? '' : 'Address Line 1', |
| 871 |
'visible' => true, |
| 872 |
], |
| 873 |
'address_line_2' => [ |
| 874 |
'name' => $name . '_address_line_2', |
| 875 |
'default' => ArrayHelper::get($field, 'address2_default', ''), |
| 876 |
'placeholder' => ArrayHelper::get($field, 'address2_placeholder', ''), |
| 877 |
'label' => $hideSubLabel ? '' : 'Address Line 2', |
| 878 |
'visible' => !ArrayHelper::isTrue($field, 'address2_hide'), |
| 879 |
], |
| 880 |
'city' => [ |
| 881 |
'name' => $name . '_city', |
| 882 |
'default' => ArrayHelper::get($field, 'city_default', ''), |
| 883 |
'placeholder' => ArrayHelper::get($field, 'city_placeholder', ''), |
| 884 |
'label' => $hideSubLabel ? '' : 'City', |
| 885 |
'visible' => true, |
| 886 |
], |
| 887 |
'state' => [ |
| 888 |
'name' => $name . '_state', |
| 889 |
'default' => ArrayHelper::get($field, 'state_default', ''), |
| 890 |
'placeholder' => ArrayHelper::get($field, 'state_placeholder', ''), |
| 891 |
'label' => $hideSubLabel ? '' : 'State', |
| 892 |
'visible' => true, |
| 893 |
], |
| 894 |
'zip' => [ |
| 895 |
'name' => $name . '_zip', |
| 896 |
'default' => ArrayHelper::get($field, 'postal_default', ''), |
| 897 |
'placeholder' => ArrayHelper::get($field, 'postal_placeholder', ''), |
| 898 |
'label' => $hideSubLabel ? '' : 'Zip', |
| 899 |
'visible' => !ArrayHelper::isTrue($field, 'postal_hide'), |
| 900 |
], |
| 901 |
'country' => [ |
| 902 |
'name' => $name . '_country', |
| 903 |
'default' => ArrayHelper::get($field, 'country_default', ''), |
| 904 |
'placeholder' => ArrayHelper::get($field, 'country_placeholder', ''), |
| 905 |
'label' => $hideSubLabel ? '' : 'Country', |
| 906 |
'visible' => !ArrayHelper::isTrue($field, 'country_hide'), |
| 907 |
], |
| 908 |
]; |
| 909 |
} |
| 910 |
|
| 911 |
} |
| 912 |
|