PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Form / FormManager.php

FormManager.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Form/FormManager.php

857 lines 30.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Get set Form,fields
5 */
6
7 namespace BitCode\BitForm\Core\Form;
8
9 /**
10 * FrontendFormManager class
11 */
12
13 use BitCode\BitForm\Admin\Form\CustomFieldHandler;
14 use BitCode\BitForm\Core\Database\FormEntryLogModel;
15 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
16 use BitCode\BitForm\Core\Database\FormEntryModel;
17 use BitCode\BitForm\Core\Database\FormModel;
18 use BitCode\BitForm\Core\Form\Validator\FormFieldValidator;
19 use BitCode\BitForm\Core\Integration\IntegrationHandler;
20 use BitCode\BitForm\Core\Messages\SuccessMessageHandler;
21 use BitCode\BitForm\Core\Util\FieldValueHandler;
22 use BitCode\BitForm\Core\Util\FileHandler;
23 use BitCode\BitForm\Core\Util\FrontendHelpers;
24 use BitCode\BitForm\Core\Util\IpTool;
25 use BitCode\BitForm\Core\WorkFlow\WorkFlow;
26 use BitCode\BitForm\Core\WorkFlow\WorkFlowHandler;
27 use WP_Error;
28
29 class FormManager {
30 protected static $form;
31 protected $formModel;
32 protected $form_id;
33 private $_has_upload;
34 private $_field_label;
35 private $_fields;
36 private $_work_flows;
37 private $_conf_messages;
38 private $_atomic_class_map;
39
40 public function __construct($form_id) {
41 $this->form_id = $form_id;
42 $this->formModel = new FormModel();
43
44 static::$form = $this->formModel->get(
45 [
46 'id',
47 'form_content',
48 'form_name',
49 'created_at',
50 'views',
51 'entries',
52 'status',
53 'builder_helper_state',
54 'atomic_class_map',
55 'generated_script_page_ids',
56 ],
57 [
58 'id' => $form_id,
59 ]
60 );
61 if (!is_wp_error(static::$form)) {
62 $this->_atomic_class_map = json_decode(static::$form[0]->atomic_class_map);
63 FrontendHelpers::isPageBuilder();
64 global $bfMultipleFormsExists;
65 if ($bfMultipleFormsExists && isset($this->_atomic_class_map->atomic_class_map_with_form_id)) {
66 $this->_atomic_class_map = $this->_atomic_class_map->atomic_class_map_with_form_id;
67 } elseif (isset($this->_atomic_class_map->atomic_class_map)) {
68 $this->_atomic_class_map = $this->_atomic_class_map->atomic_class_map;
69 }
70 }
71 }
72
73 public function isExist() {
74 return (!static::$form || is_wp_error(static::$form)) ? false : true;
75 }
76
77 public function checkStatus() {
78 return '1' === static::$form[0]->status ? true : false;
79 }
80
81 public function getFieldsContent() {
82 return self::$form[0]->form_content;
83 }
84
85 public function getFont() {
86 $atomicClassMap = $this->_atomic_class_map;
87 $font = isset($atomicClassMap->font) ? $atomicClassMap->font : '';
88 return $font;
89 }
90
91 public function getStyle() {
92 $builerState = \json_decode(static::$form[0]->builder_helper_state);
93 $style = '';
94 $themeVars = $builerState->themeVars;
95 $themeColors = $builerState->themeColors;
96
97 if (!empty($themeVars)) {
98 $style .= ':root {';
99 foreach ($themeVars->lgLightThemeVars as $key => $value) {
100 $style .= "$key: $value; ";
101 }
102 $style .= '} ';
103 }
104 if (!empty($themeColors)) {
105 $style .= ' :root {';
106 foreach ($themeColors->lightThemeColors as $k => $v) {
107 $style .= "$k:$v; ";
108 }
109 $style .= '} ';
110 }
111
112 $field = $builerState->style->lgLightStyles->fields;
113 foreach ($field as $value) {
114 $classes = $value->classes;
115 foreach ($classes as $key => $value) {
116 $style .= "{$key} {";
117 foreach ($value as $k => $v) {
118 $style .= "$k:$v; ";
119 }
120 $style .= '} ';
121 }
122 }
123 return $style;
124 }
125
126 public function getCustomStyle() {
127 $customCssCodes = '';
128 $customCSSPath = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . "bitform-custom-{$this->form_id}.css";
129
130 if (file_exists($customCSSPath)) {
131 $file = fopen($customCSSPath, 'r');
132 $customCssCodes = fread($file, filesize($customCSSPath));
133 fclose($file);
134 }
135
136 return $customCssCodes;
137 }
138
139 public function getCustomJS() {
140 $customJSCodes = '';
141 $customJsPath = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-scripts' . DIRECTORY_SEPARATOR . "bitform-custom-{$this->form_id}.js";
142 if (file_exists($customJsPath)) {
143 $file = fopen($customJsPath, 'r');
144 $customJSCodes = fread($file, filesize($customJsPath));
145 fclose($file);
146 }
147
148 return $customJSCodes;
149 }
150
151 public function getFormContentWithValue($defaultValues) {
152 $form_content = \json_decode(static::$form[0]->form_content);
153 if (!is_array($defaultValues) || 0 === count($defaultValues)) {
154 return $form_content;
155 }
156 foreach ($form_content->fields as $fieldKey => $fieldDetails) {
157 // $field_name = empty($fieldDetails->lbl) ? null : \preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\!]/', '_', $fieldDetails->lbl);
158 $fieldName = $fieldDetails->fieldName;
159 $defaultValue = isset($defaultValues[$fieldName]) ? $defaultValues[$fieldName] : null;
160 $defaultValue = isset($defaultValues[$fieldKey]) ? $defaultValues[$fieldKey] : $defaultValue;
161 if ((isset($fieldDetails->mul) || 'check' === $fieldDetails->typ) && isset($defaultValue)) {
162 // if (is_array($defaultValue)) {
163 // $fieldDetails->val =
164 // wp_json_encode(
165 // array_map('sanitize_text_field', $defaultValue)
166 // );
167 // } else {
168 // $fieldDetails->val = sanitize_text_field($defaultValue);
169 // }
170 if ((isset($fieldDetails->mul) && true === $fieldDetails->mul) || is_array($defaultValue)) {
171 $fieldDetails->val = wp_json_encode(array_map('sanitize_text_field', $defaultValue));
172 }
173 } elseif (isset($defaultValue)) {
174 $fieldDetails->val = is_string($defaultValue) ?
175 sanitize_text_field($defaultValue) :
176 sanitize_text_field($defaultValue[count($defaultValue) - 1]);
177 }
178 }
179 return $form_content;
180 }
181
182 public function getFormContent() {
183 $formContent = json_decode(static::$form[0]->form_content);
184 $types = ['check', 'radio', 'select'];
185 $filter = false;
186 foreach ($formContent->fields as $field) {
187 if (in_array($field->typ, $types) && property_exists($field, 'customType')) {
188 $filter = true;
189 break; // reduce unnecessary loop
190 }
191 }
192 if (true === $filter) {
193 $updateFields = apply_filters('bitform_dynamic_field_filter', $formContent->fields);
194 $formContent->fields = $updateFields;
195 }
196 return $formContent;
197 }
198
199 public function getFormHelperStates() {
200 $formHelperStates = json_decode(static::$form[0]->builder_helper_state);
201 return $formHelperStates;
202 }
203
204 public function getAtomicClsMap() {
205 return $this->_atomic_class_map;
206 }
207
208 private function is_json($str) {
209 $json = json_decode($str);
210 return $json && $str !== $json;
211 }
212
213 public function getFormData($columnName = '') {
214 if (empty($columnName)) {
215 return null;
216 }
217
218 $form = static::$form[0];
219 if (!isset($form->{$columnName})) {
220 return null;
221 }
222
223 $data = $form->{$columnName};
224 if ($this->is_json($data)) {
225 return json_decode($data);
226 }
227
228 return $data;
229 }
230
231 public function getFormName() {
232 return static::$form[0]->form_name;
233 }
234
235 public function getFields() {
236 if (!is_null($this->_fields)) {
237 return $this->_fields;
238 }
239 $form_content = \json_decode(static::$form[0]->form_content);
240 $layout = $form_content->layout;
241 $fields = $form_content->fields;
242 $field_details = [];
243 foreach ($fields as $key => $field) {
244 if ('recaptcha' === $field->typ) {
245 continue;
246 }
247 // $field_name = empty($field->lbl) ? null : \preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $field->lbl);
248 $field_type = $field->typ;
249 $field_details[$key]['label'] = empty($field->lbl) ? null : $field->lbl;
250 $field_details[$key]['type'] = $field_type;
251 $field_details[$key]['key'] = $key;
252 $field_details[$key]['name'] = isset($field->fieldName) ? $field->fieldName : '';
253 if (isset($field->customType)) {
254 $field_details[$key]['customType'] = $field->customType;
255 }
256
257 if (isset($field->err)) {
258 if (isset($field->err->entryUnique)) {
259 $field_details[$key]['entryUnique'] = $field->err->entryUnique;
260 }
261 if (isset($field->err->userUnique)) {
262 $field_details[$key]['userUnique'] = $field->err->userUnique;
263 }
264 }
265
266 if (isset($field->mul)) {
267 $field_details[$key]['mul'] = $field->mul;
268 }
269 if ('file-up' === $field_type && isset($field->exts)) {
270 $field_details[$key]['valid']['type'] = $field->exts;
271 }
272 if ('file-up' === $field_type && isset($field->mxUp)) {
273 $field_details[$key]['valid']['upload_size'] = (int) $field->mxUp;
274 }
275 if (isset($field->valid) && !is_null($field->valid)) {
276 if (isset($field->valid->req)) {
277 $field_details[$key]['valid']['req'] = $field->valid->req;
278 }
279 if (isset($field->valid->reqMsg)) {
280 $field_details[$key]['valid']['reqMsg'] = $field->valid->reqMsg;
281 }
282 if (isset($field->valid->typMsg)) {
283 $field_details[$key]['valid']['typMsg'] = $field->valid->typMsg;
284 }
285 }
286 }
287 if ($this->isGCLIDEnabled()) {
288 $field_details['GCLID']['name'] = 'GCLID';
289 $field_details['GCLID']['adminLbl'] = 'GCLID';
290 $field_details['GCLID']['key'] = 'GCLID';
291 $field_details['GCLID']['type'] = 'hidden';
292 }
293 $this->_fields = $field_details;
294 return $field_details;
295 }
296
297 public function getFieldsKey() {
298 $form_content = \json_decode(static::$form[0]->form_content);
299 $fields = $form_content->fields;
300 $field_details = [];
301 foreach ($fields as $key => $field) {
302 if ('recaptcha' === $field->typ) {
303 continue;
304 }
305 // $field_name = empty($field->lbl) ? null : \preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $field->lbl);
306 $field_details[$key] = $key;
307 }
308 if ($this->isGCLIDEnabled()) {
309 $field_details['GCLID'] = 'GCLID';
310 }
311 return $field_details;
312 }
313
314 public function getFieldLabel($forQuery = false) {
315 if (!is_null($this->_field_label)) {
316 return $this->_field_label;
317 }
318 $form_content = \json_decode(static::$form[0]->form_content);
319 $fields = $form_content->fields;
320 $field_details = [];
321 $fieldCounter = 0;
322 foreach ($fields as $key => $field) {
323 if ('recaptcha' === $field->typ || 'html' === $field->typ || 'button' === $field->typ) {
324 continue;
325 }
326 $field_details[$fieldCounter]['name'] = empty($field->lbl) ? null : $field->lbl;
327 $field_details[$fieldCounter]['adminLbl'] = empty($field->adminLbl) ? $field_details[$fieldCounter]['name'] : $field->adminLbl;
328 $field_details[$fieldCounter]['key'] = $key;
329 $field_details[$fieldCounter]['type'] = $field->typ;
330 $fieldCounter += 1;
331 }
332 if ($this->isGCLIDEnabled()) {
333 $field_details[$fieldCounter]['name'] = 'GCLID';
334 $field_details[$fieldCounter]['adminLbl'] = 'GCLID';
335 $field_details[$fieldCounter]['key'] = 'GCLID';
336 $field_details[$fieldCounter]['type'] = 'hidden';
337 $fieldCounter += 1;
338 }
339 if (!$forQuery) {
340 $field_details = (array) $this->addEntryInfo($field_details, $fieldCounter);
341 }
342 $this->_field_label = $field_details;
343 return $field_details;
344 }
345
346 public function getUploadFields() {
347 if (!is_null($this->_has_upload)) {
348 return $this->_has_upload;
349 }
350 $upload_fields = [];
351 $form_field_details = $this->getFields();
352 foreach ($form_field_details as $field_name => $__field_detail) {
353 if (isset($__field_detail['type']) && ('file-up' === $__field_detail['type'] || 'advanced-file-up' === $__field_detail['type'])) {
354 $upload_fields[] = $field_name;
355 }
356 }
357 $this->_has_upload = $upload_fields;
358 return $upload_fields;
359 }
360
361 private function entryInsert($user_details) {
362 $formEntryModel = new FormEntryModel();
363 $entryId = $formEntryModel->insert(
364 [
365 'form_id' => $this->form_id,
366 'user_id' => $user_details['id'],
367 'user_ip' => $user_details['ip'],
368 'user_device' => $user_details['device'],
369 'referer' => $user_details['page'],
370 'status' => 1,
371 'created_at' => $user_details['time'],
372 ]
373 );
374 return $entryId;
375 }
376
377 private function submisionLog($user_details, $entry_id, $type) {
378 $formEntryLogModel = new FormEntryLogModel();
379 $logId = $formEntryLogModel->form_log_insert(
380 [
381 'user_id' => $user_details['id'],
382 'action_type' => $type,
383 'log_type' => 'entry',
384 'ip' => $user_details['ip'],
385 'form_entry_id' => $entry_id,
386 'content' => null,
387 'form_id' => $this->form_id,
388 'created_at' => $user_details['time'],
389 ]
390 );
391 return $logId;
392 }
393
394 private function isArrayAllKeyInt($InputArray) {
395 if (!is_array($InputArray)) {
396 return false;
397 }
398
399 if (count($InputArray) <= 0) {
400 return true;
401 }
402
403 return array_unique(array_map('is_int', array_keys($InputArray))) === [true];
404 }
405
406 private function formatSubmittedData($submitted_data) {
407 $form_content = $this->getFormContent();
408 $form_fields = $form_content->fields;
409 foreach ($submitted_data as $key => $value) {
410 if (!isset($form_fields->{$key})) {
411 continue;
412 }
413 $field_data = $form_fields->{$key};
414 $field_type = $field_data->typ;
415 if ('select' === $field_type && !empty($field_data->config->multipleSelect)) {
416 $valueArr = explode(BITFORMS_BF_SEPARATOR, $value);
417 $submitted_data[$key] = $valueArr;
418 }
419 }
420 return $submitted_data;
421 }
422
423 private function saveEntryMeta($submitted_data, $entry_id) {
424 $errorInEntryMetaInsert = false;
425 $entryMeta = new FormEntryMetaModel();
426 foreach ($submitted_data as $key => $value) {
427 $value = $submitted_data[$key];
428 if (is_string($value)) {
429 $value = wp_unslash($value);
430 } elseif ($this->isArrayAllKeyInt($value)) {
431 $value = wp_json_encode(array_values($value));
432 } else {
433 $value = wp_json_encode($value);
434 }
435 $status = $entryMeta->insert(
436 [
437 'bitforms_form_entry_id' => $entry_id,
438 'meta_key' => $key,
439 'meta_value' => $value,
440 ]
441 );
442 if (is_wp_error($status)) {
443 $errorInEntryMetaInsert = true;
444 break;
445 }
446 }
447 return $errorInEntryMetaInsert;
448 }
449
450 public function saveFormEntry($submitted_data) {
451 $submitted_data = $this->formatSubmittedData($submitted_data);
452 $form_content = \json_decode(static::$form[0]->form_content);
453 do_action('bitform_save_entry', $this, $submitted_data);
454 $key = null;
455 $ipTool = new IpTool();
456 $fileHandler = new FileHandler();
457 $form_fields = $this->getFields();
458 $file_fields = $this->getUploadFields();
459 foreach ($_FILES as $file_name => $file_details) {
460 if ($file_fields && in_array($file_name, $file_fields)) {
461 $validation = $fileHandler->validation($file_name, $file_details, $this->form_id);
462 if (!empty($validation['error_type']) && !empty($validation['message'])) {
463 return new WP_Error($validation['error_type'], __($validation['message'], 'bit-form'));
464 }
465 }
466 }
467 $user_details = $ipTool->getUserDetail();
468 $form_fields = $this->getFields();
469 $submitted_data = $this->passwordEncrypted($submitted_data, $form_fields);
470 global $wpdb;
471 $wpdb->query('START TRANSACTION');
472 $entry_id = $this->entryInsert($user_details);
473 $log_id = null;
474 if (is_wp_error($entry_id)) {
475 return new WP_Error('insert_error', __('Sorry, Error occurred in saving form entry', 'bit-form'));
476 }
477 if ($entry_id) {
478 $log_id = $this->submisionLog($user_details, $entry_id, 'create', $key);
479 if (is_wp_error($log_id)) {
480 $wpdb->query('ROLLBACK');
481 return new WP_Error('error_entry_log', __('Sorry, error occurred in logging form entry', 'bit-form'));
482 }
483 }
484 if ($entry_id) {
485 $submitted_fields = $this->getFormContentWithValue($submitted_data)->fields;
486 $workFlowRunHelper = new WorkFlow($this->form_id);
487 $workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit(
488 'create',
489 $submitted_fields,
490 $submitted_data,
491 $entry_id,
492 $log_id
493 );
494 if (!empty($workFlowreturnedOnSubmit['fields'])) {
495 $submitted_data = $workFlowreturnedOnSubmit['fields'];
496 }
497
498 $file_fields = $this->getUploadFields();
499 $formFields = $this->getFields();
500 $submitted_data = FileHandler::tempDirToUploadDir($submitted_data, $formFields, $this->form_id, $entry_id);
501 $fileHandler = new FileHandler();
502 foreach ($_FILES as $file_name => $file_details) {
503 if ($file_fields && in_array($file_name, $file_fields)) {
504 $filePath = $fileHandler->moveUploadedFiles($file_details, $this->form_id, $entry_id);
505 if (!empty($filePath)) {
506 $submitted_data[$file_name] = $filePath;
507 }
508 }
509 }
510
511 if (!isset($form_content->additional->enabled->submission)) {
512 $errorInEntryMetaInsert = $this->saveEntryMeta($submitted_data, $entry_id);
513 if ($errorInEntryMetaInsert) {
514 $wpdb->query('ROLLBACK');
515 return new WP_Error('insert_error', __('Sorry, Error occured in saving form entry data', 'bit-form'));
516 }
517 }
518 $wpdb->query('COMMIT');
519 $this->setSubmissionCount();
520 $workFlowreturnedOnSubmit['entry_id'] = $entry_id;
521 $workFlowreturnedOnSubmit['fields'] = $submitted_data;
522
523 return $workFlowreturnedOnSubmit;
524 }
525 }
526
527 public function passwordEncrypted($updatedValue, $form_fields) {
528 $integrationHandler = new IntegrationHandler($this->form_id);
529 $formIntegrations = $integrationHandler->getAllIntegration('wp_user_auth', 'wp_auth', 1);
530 if (!isset($formIntegrations->errors['result_empty'])) {
531 foreach ($form_fields as $field) {
532 if (array_key_exists($field['key'], $updatedValue) && 'password' === $field['type']) {
533 $updatedValue[$field['key']] = '**** (encrypted)';
534 }
535 }
536 }
537 return $updatedValue;
538 }
539
540 public function updateFormEntry($updatedValue, $formID, $entryID) {
541 $formEntryModel = new FormEntryModel();
542 $formEntryLogModel = new FormEntryLogModel();
543 $formOldData = $formEntryLogModel->get_form_value($entryID);
544 $key = null;
545 $entryMeta = new FormEntryMetaModel();
546 $ipTool = new IpTool();
547 $user_details = $ipTool->getUserDetail();
548 $form_fields = $this->getFields();
549 $updatedValue = $this->passwordEncrypted($updatedValue, $form_fields);
550 $field_map = [];
551 foreach ($formOldData as $index => $data) {
552 foreach ($form_fields as $field_key => $field) {
553 if ($data->meta_key === $field['key']) {
554 $field_map[$field_key] = $field['key'];
555 }
556 }
557 }
558 $formFieldValidator = new FormFieldValidator($form_fields, $updatedValue, $_FILES);
559 $validateField = $formFieldValidator->validate('edit', $formID);
560 if (!$validateField) {
561 $errorMessage = count($formFieldValidator->getMessage()) > 0 ?
562 $formFieldValidator->getMessage() : __('Internal error occured!!!', 'bit-form');
563 return new WP_Error('validation_error', $errorMessage);
564 }
565 $formEntry = $formEntryModel->update(
566 [
567 'user_id' => $user_details['id'],
568 'user_ip' => $user_details['ip'],
569 'user_device' => $user_details['device'],
570 // "status" => 0,
571 'updated_at' => $user_details['time'],
572 ],
573 [
574 'form_id' => $formID,
575 'id' => $entryID,
576 ]
577 );
578 $log_id = null;
579 if ($formEntry) {
580 $log_id = $this->submisionLog($user_details, $entryID, 'update');
581 }
582
583 if (is_wp_error($formEntry) || !$formEntry) {
584 return new WP_Error('empty_form', __('provided form entries does not exists', 'bit-form'));
585 }
586
587 $file_fields = $this->getUploadFields();
588 if (count($file_fields) > 0) {
589 $fileHandler = new FileHandler();
590 foreach ($file_fields as $file_name) {
591 if (isset($updatedValue[$file_name . '_old'])) {
592 $file_exists = $entryMeta->get(
593 'meta_value',
594 [
595 'bitforms_form_entry_id' => $entryID,
596 'meta_key' => $file_name,
597 ]
598 );
599 if (!is_wp_error($file_exists) && count($file_exists) > 0) {
600 $files_in_db = json_decode($file_exists[0]->meta_value);
601 $files_old = empty($updatedValue[$file_name . '_old']) ? [] : explode(',', $updatedValue[$file_name . '_old']);
602 $deleted_file = array_diff($files_in_db, $files_old);
603 if (count($deleted_file) > 0) {
604 $fileHandler->deleteFiles($formID, $entryID, $deleted_file);
605 }
606 $updatedValue[$file_name] = wp_json_encode($files_old);
607 }
608 }
609 if (!empty($_FILES[$file_name]['name'])) {
610 $meta_value = $fileHandler->moveUploadedFiles($_FILES[$file_name], $formID, $entryID);
611 if (!empty($meta_value)) {
612 if (isset($updatedValue[$file_name . '_old']) && !is_wp_error($file_exists) && count($file_exists) > 0) {
613 $meta_value = empty($files_old) ? $meta_value : array_merge($meta_value, $files_old);
614 $updatedValue[$file_name] = wp_json_encode($meta_value);
615 } else {
616 $data = [
617 'bitforms_form_entry_id' => $entryID,
618 'meta_key' => $file_name,
619 'meta_value' => wp_json_encode($meta_value),
620 ];
621 $newFileInsertStatus = $entryMeta->insert(
622 $data
623 );
624 $updatedValue[$file_name] = wp_json_encode($meta_value);
625 }
626 }
627 }
628 }
629 }
630
631 unset($updatedValue['_ajax_nonce'], $_REQUEST['g-recaptcha-response']);
632
633 $workFlowRunHelper = new WorkFlow($formID);
634 $workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit(
635 'edit',
636 $this->getFormContentWithValue($updatedValue)->fields,
637 $updatedValue,
638 $entryID,
639 $log_id
640 );
641 if (!empty($workFlowreturnedOnSubmit['fields'])) {
642 $updatedValue = $workFlowreturnedOnSubmit['fields'];
643 }
644
645 $toUpdateValues = [];
646 foreach ($form_fields as $field) {
647 if (isset($updatedValue[$field['key']])) {
648 $toUpdateValues[$field['key']] = $updatedValue[$field['key']];
649 }
650 }
651 $formEntryMetaUpdateStatus = $entryMeta->update(
652 $toUpdateValues,
653 [
654 'bitforms_form_entry_id' => $entryID,
655 ]
656 );
657 if (is_wp_error($formEntryMetaUpdateStatus) || isset($newFileInsertStatus) && is_wp_error($newFileInsertStatus)) {
658 return $formEntryMetaUpdateStatus;
659 }
660 $toUpdateValues = array_merge($formEntryMetaUpdateStatus, ['entry_id' => $entryID]);
661 if (empty($workFlowreturnedOnSubmit['message'])) {
662 $workFlowreturnedOnSubmit['message'] = __('Entry Updated Successfully', 'bit-form');
663 }
664 $customFieldHandler = new CustomFieldHandler();
665 $toUpdateValues = $customFieldHandler->updatedData($form_fields, $toUpdateValues);
666 $workFlowreturnedOnSubmit['updatedData'] = $toUpdateValues;
667 $counter = 0;
668 for ($i = 0; $i < count($formOldData); $i++) {
669 if (array_key_exists($formOldData[$i]->meta_key . '_old', $toUpdateValues)) {
670 unset($toUpdateValues[$formOldData[$i]->meta_key . '_old']);
671 }
672 if (in_array($formOldData[$i]->meta_key, $file_fields)) {
673 if (
674 empty($_FILES[$formOldData[$i]->meta_key]['name'])
675 || (is_array($_FILES[$formOldData[$i]->meta_key]['name'])
676 && 1 === count($_FILES[$formOldData[$i]->meta_key]['name'])
677 && empty($_FILES[$formOldData[$i]->meta_key]['name'][0]))
678 ) {
679 unset($toUpdateValues[$formOldData[$i]->meta_key]);
680 continue;
681 }
682 if (is_array($_FILES[$formOldData[$i]->meta_key]['name']) && !in_array($_FILES[$formOldData[$i]->meta_key]['name'], json_decode($formOldData[$i]->meta_value))) {
683 $key[$i] = '${' . $formOldData[$i]->meta_key . '} file was Updated To ' . json_encode($_FILES[$formOldData[$i]->meta_key]['name']);
684 } elseif (!is_array($_FILES[$formOldData[$i]->meta_key]['name']) && !in_array($_FILES[$formOldData[$i]->meta_key]['name'], json_decode($formOldData[$i]->meta_value))) {
685 $key[$i] = '${' . $formOldData[$i]->meta_key . '} file was Updated To ' . $_FILES[$formOldData[$i]->meta_key]['name'];
686 }
687 unset($toUpdateValues[$formOldData[$i]->meta_key]);
688 } elseif (isset($toUpdateValues[$formOldData[$i]->meta_key])) {
689 if (is_array($toUpdateValues[$formOldData[$i]->meta_key])) {
690 if (json_decode($formOldData[$i]->meta_value) !== $toUpdateValues[$formOldData[$i]->meta_key]) {
691 $key[$i] = '${' . $formOldData[$i]->meta_key . '} was Updated From ' . implode(',', json_decode($formOldData[$i]->meta_value)) . ' To ' . implode(',', $toUpdateValues[$formOldData[$i]->meta_key]);
692 }
693 } elseif (is_string($toUpdateValues[$formOldData[$i]->meta_key]) && !FieldValueHandler::isEmpty($toUpdateValues[$formOldData[$i]->meta_key])) {
694 if ($formOldData[$i]->meta_value !== $toUpdateValues[$formOldData[$i]->meta_key]) {
695 $key[$i] = '${' . $formOldData[$i]->meta_key . '} was Updated' . ($formOldData[$i]->meta_value ? ' From ' . $formOldData[$i]->meta_value : '') . ' To ' . $toUpdateValues[$formOldData[$i]->meta_key];
696 }
697 }
698 }
699 $counter++;
700 }
701
702 $newField = array_keys(array_diff_key($formEntryMetaUpdateStatus, $field_map));
703 for ($i = 0; $i < count($newField); $i++) {
704 if (is_array($toUpdateValues[$newField[$i]]) && !empty($toUpdateValues[$newField[$i]])) {
705 $key[$counter + $i] = '${' . $newField[$i] . '} Updated To ' . implode(',', $toUpdateValues[$newField[$i]]);
706 } elseif (is_string($newField[$i]) && !FieldValueHandler::isEmpty($toUpdateValues[$newField[$i]])) {
707 $key[$counter + $i] = '${' . $newField[$i] . '} Updated To ' . $toUpdateValues[$newField[$i]];
708 }
709 }
710 if (null !== $key) {
711 $logUpdate = implode('b::f', (array) $key);
712 $formEntryLogUpdate = $formEntryLogModel->logUpdate($logUpdate, $log_id);
713 }
714 return $workFlowreturnedOnSubmit;
715 }
716
717 public function setSubmissionCount($countStep = 1) {
718 $update_status = $this->formModel->update(
719 [
720 'entries' => intval(static::$form[0]->entries) + $countStep,
721 ],
722 [
723 'id' => $this->form_id,
724 ]
725 );
726 }
727
728 public function resetSubmissionCount($countStep) {
729 $update_status = $this->formModel->update(
730 [
731 'entries' => intval($countStep),
732 ],
733 [
734 'id' => $this->form_id,
735 ]
736 );
737 }
738
739 public function getCaptchaSettings() {
740 $formContents = $this->getFormContent();
741 $fieldStr = wp_json_encode($formContents->fields);
742 if (false !== strpos($fieldStr, '"typ":"recaptcha"')) {
743 return true;
744 }
745 }
746
747 public function getCaptchaV3Settings() {
748 $formContents = $this->getFormContent();
749 if (!empty($formContents->additional->enabled) && !empty($formContents->additional->enabled->recaptchav3)) {
750 return $formContents->additional->settings->recaptchav3;
751 }
752 return false;
753 }
754
755 public function getSuccessMessageMarkups() {
756 if (is_null($this->_work_flows)) {
757 $workFlowManager = new WorkFlowHandler($this->form_id);
758 $this->_work_flows = $workFlowManager->getAllworkFlow();
759 }
760
761 $ids = [];
762 foreach ($this->_work_flows as $msgItem) {
763 foreach ($msgItem['conditions'] as $condition) {
764 if (isset($condition->actions->success)) {
765 foreach ($condition->actions->success as $msg) {
766 if ('successMsg' === $msg->type && isset($msg->details->id)) {
767 $msgDetailsId = $msg->details->id;
768 $idObj = json_decode(stripslashes($msgDetailsId));
769 if (is_object($idObj) && !empty($idObj->id)) {
770 array_push($ids, $idObj->id);
771 }
772 }
773 }
774 }
775 if (isset($condition->actions->failure)) {
776 $idObj = json_decode(stripslashes($condition->actions->failure));
777 if (is_object($idObj) && !empty($idObj->id)) {
778 array_push($ids, $idObj->id);
779 }
780 }
781 }
782 }
783 $ids = array_unique($ids);
784 if (is_null($this->_conf_messages)) {
785 $successMsgHandler = new SuccessMessageHandler($this->form_id);
786 $this->_conf_messages = $successMsgHandler->getMessages($ids);
787 }
788
789 $messageMarkups = '';
790 if (is_wp_error($this->_conf_messages)) {
791 return $messageMarkups;
792 }
793
794 foreach ($this->_conf_messages as $key => $msgItem) {
795 $messageMarkups .= $this->messageMarkup($msgItem->id);
796 }
797
798 return $messageMarkups;
799 }
800
801 private function messageMarkup($msgId) {
802 return <<<SUCCESSMSG
803 <div role="dialog" aria-hidden="true" data-modal-backdrop="true" class="{$this->getAtomicCls("msg-container-{$msgId}")} deactive">
804 <div role="button" class="{$this->getAtomicCls("msg-background-{$msgId}")} msg-backdrop">
805 <div class="{$this->getAtomicCls("msg-content-{$msgId}")}">
806 <button class="{$this->getAtomicCls("close-{$msgId}")} msg-close" type="button">
807 <svg class="{$this->getAtomicCls("close-icn-{$msgId}")}" viewBox="0 0 30 30">
808 <line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" x1="4" y1="3.88" x2="26" y2="26.12"></line>
809 <line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" x1="26" y1="3.88" x2="4" y2="26.12"></line>
810 </svg>
811 </button>
812 <div class="msg-content"></div>
813 </div>
814 </div>
815 </div>
816 SUCCESSMSG;
817 }
818
819 public function getAtomicCls($element) {
820 $atomicClassMap = $this->getAtomicClsMap();
821 if (property_exists($atomicClassMap, ".$element")) {
822 $getAtomicCls = $atomicClassMap->{".$element"};
823 return implode(' ', $getAtomicCls) . " $element";
824 }
825 return $element;
826 }
827
828 public function isGCLIDEnabled() {
829 $formContents = $this->getFormContent();
830 if (isset($formContents->additional->enabled->captureGCLID) && $formContents->additional->enabled->captureGCLID) {
831 return true;
832 }
833 return false;
834 }
835
836 protected function addEntryInfo($field_details, $counter) {
837 $infos = [
838 '__user_id' => __('User'),
839 '__entry_status' => __('Status'),
840 //'__user_location' => __(''),
841 '__referer' => __('Refer URL'),
842 '__user_device' => __('Device'),
843 '__user_ip' => __('IP address'),
844 '__created_at' => __('Created Time'),
845 '__updated_at' => __('Modified Time'),
846 ];
847 foreach ($infos as $key => $value) {
848 $field_details[$counter]['name'] = $value;
849 $field_details[$counter]['key'] = $key;
850 $field_details[$counter]['type'] = 'sys';
851 $counter = $counter + 1;
852 }
853
854 return $field_details;
855 }
856 }
857