PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.4
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
← All changes | includes/Admin/Form/AdminFormHandler.php +1095 -377 2.02.21.4 View file →
@@ -13,32 +13,40 @@
13 13 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
14 14 use BitCode\BitForm\Core\Database\FormEntryModel;
15 15 use BitCode\BitForm\Core\Database\FormModel;
16 16 use BitCode\BitForm\Core\Database\IntegrationModel;
17 +use BitCode\BitForm\Core\Database\PdfTemplateModel;
17 18 use BitCode\BitForm\Core\Database\ReportsModel;
18 19 use BitCode\BitForm\Core\Database\SuccessMessageModel;
19 20 use BitCode\BitForm\Core\Database\WorkFlowModel;
20 21 use BitCode\BitForm\Core\Integration\IntegrationHandler;
21 22 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
23 +use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
22 24 use BitCode\BitForm\Core\Messages\SuccessMessageHandler;
25 +use BitCode\BitForm\Core\Migration\MigrationHelper;
23 26 use BitCode\BitForm\Core\Util\FileHandler;
24 27 use BitCode\BitForm\Core\Util\FormDuplicateHelper;
25 28 use BitCode\BitForm\Core\Util\HttpHelper;
26 29 use BitCode\BitForm\Core\Util\IpTool;
30 +use BitCode\BitForm\Core\Util\Log;
31 +use BitCode\BitForm\Core\Util\Utilities;
32 +use BitCode\BitForm\Core\WorkFlow\WorkFlow;
27 33 use BitCode\BitForm\Core\WorkFlow\WorkFlowHandler;
28 -use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;
29 34 use WP_Error;
30 35
31 -class AdminFormHandler {
36 +class AdminFormHandler
37 +{
32 38 private static $formModel;
33 39 private static $ipTool;
34 40
35 - public function __construct() {
41 + public function __construct()
42 + {
36 43 static::$formModel = new FormModel();
37 44 static::$ipTool = new IpTool();
38 45 }
39 46
40 - public function getTemplate($Request, $post) {
47 + public function getTemplate($Request, $post)
48 + {
41 49 $templateName = null;
42 50 $newFormId = null;
43 51 if (isset($post->template)) {
44 52 $templateName = $post->template;
@@ -49,9 +57,9 @@
49 57 $form_content_raw = $templateProvider->getTemplate($templateName, $newFormId);
50 58 if (!$form_content_raw) {
51 59 return new WP_Error('template_empty', __('Template not found, by this name: ', 'bit-form') . $templateName);
52 60 } else {
53 - return \json_encode(
61 + return wp_json_encode(
54 62 [
55 63 'id' => 0,
56 64 'form_content' => $form_content_raw,
57 65 ]
@@ -58,9 +66,10 @@
58 66 );
59 67 }
60 68 }
61 69
62 - public function saveStyleSheet($styles, $sheetName) {
70 + public function saveStyleSheet($styles, $sheetName)
71 + {
63 72 $this->createFormStylesDirIfNotExists();
64 73
65 74 if (
66 75 file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')
@@ -73,9 +82,10 @@
73 82 }
74 83 return false;
75 84 }
76 85
77 - public function saveLayoutStyleSheet($layout, $sheetName, $lay_row_height = 43) {
86 + public function saveLayoutStyleSheet($layout, $sheetName, $lay_row_height = 43)
87 + {
78 88 $md_width = 600;
79 89 $sm_width = 400;
80 90 $lg_styles = '';
81 91 $md_styles = '';
@@ -155,9 +165,10 @@
155 165 }
156 166 return false;
157 167 }
158 168
159 - public function createFormStylesDirIfNotExists() {
169 + public function createFormStylesDirIfNotExists()
170 + {
160 171 if (!file_exists(BITFORMS_UPLOAD_DIR)) {
161 172 wp_mkdir_p(BITFORMS_UPLOAD_DIR);
162 173 }
163 174 if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')) {
@@ -164,9 +175,18 @@
164 175 wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles');
165 176 }
166 177 }
167 178
168 - public function createNewForm($Request, $post) {
179 + private function pdfFontDownload($fontName)
180 + {
181 + if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) {
182 + \BitCode\BitFormPro\Admin\AppSetting\Pdf::getInstance()->fontsDownload($fontName);
183 + }
184 + return false;
185 + }
186 +
187 + public function createNewForm($Request, $post)
188 + {
169 189 // wp_send_json_success($post);
170 190 // exit;
171 191 if (!isset($post->form_id) || '' === $post->form_id) {
172 192 return new WP_Error('empty_form', __('Error Occurred, Please Reload', 'bit-form'));
@@ -192,15 +212,20 @@
192 212 }
193 213
194 214 if (isset($post->layoutChanged) && $post->layoutChanged) {
195 215 $sheetName = 'bitform-layout-' . $post->form_id . '.css';
196 - $rowHeight = (int)$post->rowHeight;
216 + $rowHeight = (int) $post->rowHeight;
197 217 $this->saveLayoutStyleSheet($post->layout, $sheetName, $rowHeight);
198 218 }
199 219
200 220 $fields = wp_unslash($post->fields);
221 + $fields = Helpers::replaceFieldsDefaultErrorMsg($fields);
222 + // $fields = $post->fields;
201 223 $layout = wp_unslash($post->layout);
224 + $nestedLayout = isset($post->nestedLayouts) ? wp_unslash($post->nestedLayouts) : (object) [];
225 + $formInfo = wp_unslash($post->formInfo);
202 226 $form_name = wp_unslash($post->form_name);
227 + $form_name = strlen($form_name) > 50 ? substr($form_name, 0, 50) : $form_name;
203 228 $formSettings = (object) wp_unslash($post->formSettings);
204 229 $atomic_class_map = isset($post->atomicClassMap) ? wp_unslash($post->atomicClassMap) : [];
205 230 $breakpointSize = wp_unslash($post->breakpointSize);
206 231 $style = $post->style;
@@ -212,9 +237,11 @@
212 237 $reports = !empty($post->reports) ? $post->reports : (object) [];
213 238 $builderSettings = (object) isset($post->builderSettings) ? wp_unslash($post->builderSettings) : [];
214 239
215 240 $mailTem = $formSettings->mailTem;
241 + $pdfTem = $formSettings->pdfTem;
216 242 $integrations = $formSettings->integrations;
243 + $formPermissions = $formSettings->formPermissions;
217 244
218 245 $user_details = static::$ipTool->getUserDetail();
219 246 if (empty($fields) || empty($layout)) {
220 247 return new WP_Error(
@@ -228,10 +255,12 @@
228 255 __('Form Name Should Be Within 50 Characters', 'bit-form')
229 256 );
230 257 }
231 258 $form_content = [
232 - 'fields' => $fields,
233 - 'layout' => $layout,
259 + 'fields' => $fields,
260 + 'layout' => $layout,
261 + 'nestedLayout' => $nestedLayout,
262 + 'formInfo' => $formInfo,
234 263 ];
235 264
236 265 /* if (!empty($formSettings->submitBtn)) {
237 266 $form_content = array_merge($form_content, array('buttons' => $formSettings->submitBtn));
@@ -239,20 +268,26 @@
239 268 if (!empty($formSettings->theme)) {
240 269 $form_content = array_merge($form_content, ['theme' => $formSettings->theme]);
241 270 }
242 271
272 + if (!empty($formSettings->formPermissions)) {
273 + $form_content = array_merge($form_content, ['formPermissions' => $formSettings->formPermissions]);
274 + }
275 +
243 276 if (!empty($additional)) {
244 277 $form_content = array_merge($form_content, ['additional' => $additional]);
245 278 }
246 279 if (!empty($workFlows)) {
247 - $workFlowsStr = is_string($workFlows) ? $workFlows : json_encode($workFlows);
280 + $workflows = is_string($workFlows) ? json_decode($workFlows) : $workFlows;
248 281 $workFlowExist = [];
249 - if (\strpos($workFlowsStr, 'onload')) {
250 - $workFlowExist['onload'] = true;
282 + foreach ($workflows as $index => $workFlow) {
283 + if (in_array($workFlow->action_type, ['always', 'onload'])) {
284 + $workFlowExist['onload'] = true;
285 + }
286 + if ('oninput' === $workFlow->action_type || ('always' === $workFlow->action_type && 'cond' === $workFlow->action_behaviour)) {
287 + $workFlowExist['oninput'] = true;
288 + }
251 289 }
252 - if (\strpos($workFlowsStr, 'oninput')) {
253 - $workFlowExist['oninput'] = true;
254 - }
255 290 $form_content = array_merge($form_content, ['workFlowExist' => $workFlowExist]);
256 291 }
257 292 $form_content = \wp_json_encode($form_content);
258 293
@@ -286,9 +321,9 @@
286 321 return $save_status;
287 322 } elseif (!$save_status) {
288 323 return false;
289 324 } else {
290 - wp_mkdir_p(BITFORMS_UPLOAD_DIR . "/$save_status");
325 + FileHandler::createIndexFile(BITFORMS_UPLOAD_DIR . "/$save_status");
291 326 $formID = $save_status;
292 327 $integartionIDForWorkflow = [];
293 328 // Confiramtion Message [start]
294 329 if (!empty($formSettings->confirmation->type->successMsg)) {
@@ -296,9 +331,9 @@
296 331 $successMessageHandler = new SuccessMessageHandler($formID, $user_details);
297 332 foreach ($successMessages as $messageKey => $messageDetail) {
298 333 $savedID = $successMessageHandler->saveMessage($messageDetail);
299 334 if ($savedID) {
300 - $msgIdx = empty($messageDetail->id) ? $messageKey : \json_encode(['id' => $messageDetail->id]);
335 + $msgIdx = empty($messageDetail->id) ? $messageKey : wp_json_encode(['id' => $messageDetail->id]);
301 336 $integartionIDForWorkflow['successMsg'][$msgIdx] = $savedID;
302 337 $messageDetail->id = $savedID;
303 338 $style = $this->updateSuccessMessageClassName($style, $messageKey, $savedID);
304 339 $atomic_class_map = $this->updateSuccessMessageClassName($atomic_class_map, $messageKey, $savedID);
@@ -311,11 +346,13 @@
311 346 'themeColors' => $themeColors,
312 347 'builderSettings' => $builderSettings
313 348 ];
314 349 $update_status = static::$formModel->update(
315 - ['builder_helper_state' => \wp_json_encode($builder_helper_state),
316 - 'atomic_class_map' => $atomic_class_map, ],
317 350 [
351 + 'builder_helper_state' => \wp_json_encode($builder_helper_state),
352 + 'atomic_class_map' => $atomic_class_map,
353 + ],
354 + [
318 355 'id' => $formID,
319 356 ]
320 357 );
321 358 if (is_wp_error($update_status)) {
@@ -331,14 +368,39 @@
331 368 = new EmailTemplateHandler($formID, $user_details);
332 369 foreach ($mailTem as $templateKey => $templateDetail) {
333 370 $savedID = $emailTemplateHandler->saveTemplate($templateDetail);
334 371 if ($savedID) {
335 - $tempIdx = empty($templateDetail->id) ? $templateKey : \json_encode(['id' => $templateDetail->id]);
372 + $tempIdx = empty($templateDetail->id) ? $templateKey : wp_json_encode(['id' => $templateDetail->id]);
336 373 $integartionIDForWorkflow['mailNotify'][$tempIdx] = $savedID;
337 374 }
338 375 }
339 376 }
340 377 // Email Template [end] */
378 +
379 + // PDF Template [start]
380 + if (!empty($pdfTem)) {
381 + $pdfTemplateHandler = new PdfTemplateHandler($formID);
382 + $workFlowString = '';
383 + if (!empty($workFlows)) {
384 + $workFlowString = wp_json_encode($workFlows);
385 + }
386 + foreach ($pdfTem as $templateDetail) {
387 + $savedID = $pdfTemplateHandler->save($templateDetail);
388 +
389 + $pdfSetting = $templateDetail->setting;
390 +
391 + if (isset($pdfSetting->font->name) && !empty($pdfSetting->font->name)) {
392 + $this->pdfFontDownload($pdfSetting->font->name);
393 + }
394 + if (!empty($workFlowString)) {
395 + $workFlowString = str_replace('"pdfId":"{\"id\":\"' . $templateDetail->id . '\"}"', '"pdfId":"{\"id\":\"' . $savedID . '\"}"', $workFlowString);
396 + }
397 + }
398 + if (!empty($workFlowString) && !empty($workFlows)) {
399 + $workFlows = json_decode($workFlowString);
400 + }
401 + }
402 + // PDF Template [end] */
341 403 //Integration [start] */
342 404 $integrationHandler = new IntegrationHandler($formID, $user_details);
343 405 if (!empty($formSettings->confirmation->type)) {
344 406 $allIntegrations = $formSettings->confirmation->type;
@@ -355,13 +417,21 @@
355 417 $integrationDetails = !is_string($singleIntegrationDetail->details) ?
356 418 wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details;
357 419 }
358 420 $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form');
359 - $integIdx = empty($singleIntegrationDetail->id) ? $savedID : \json_encode(['id' => $singleIntegrationDetail->id]);
421 + $integIdx = empty($singleIntegrationDetail->id) ? $savedID : wp_json_encode(['id' => $singleIntegrationDetail->id]);
360 422 $integartionIDForWorkflow[$integrationType][$integIdx] = $savedID;
361 423 }
362 424 }
363 425 }
426 + if (isset($formSettings->formAbandonment)) {
427 + $formAbandonment = $formSettings->formAbandonment;
428 +
429 + $integrationName = 'formAbandonment';
430 + $integrationType = 'formAbandonment';
431 + $abandonmentIntegDetails = wp_json_encode($formAbandonment);
432 + $integrationHandler->saveIntegration($integrationName, $integrationType, $abandonmentIntegDetails, 'formAbandonment');
433 + }
364 434 if (!empty($integrations)) {
365 435 foreach ($integrations as $singleIntegrationKey => $singleIntegrationDetail) {
366 436 $integrationName = $singleIntegrationDetail->name;
367 437 unset($singleIntegrationDetail->name);
@@ -376,9 +446,9 @@
376 446 $integrationDetails = !is_string($singleIntegrationDetail->details) ?
377 447 wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details;
378 448 }
379 449 $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form');
380 - $integIdx = empty($singleIntegrationDetailID) ? $singleIntegrationKey : \json_encode(['id' => $singleIntegrationDetailID]);
450 + $integIdx = empty($singleIntegrationDetailID) ? $singleIntegrationKey : wp_json_encode(['id' => $singleIntegrationDetailID]);
381 451 $integartionIDForWorkflow['integ'][$integIdx] = $savedID;
382 452 }
383 453 }
384 454 //Integrations [end]
@@ -399,10 +469,10 @@
399 469 // }
400 470 }
401 471 //wrokFlows [end]
402 472 $details = [
403 - 'report_name' => 'All Entries',
404 - 'hiddenColumns' => [],
473 + 'report_name' => __('All Entries', 'bit-form'),
474 + 'hiddenColumns' => ['__user_id', '__user_ip', '__referer', '__user_device', '__created_at', '__updated_at'],
405 475 'pageSize' => 10,
406 476 'sortBy' => [],
407 477 'filters' => [],
408 478 'globalFilter' => '',
@@ -434,20 +504,11 @@
434 504 return $updated_data;
435 505 }
436 506 }
437 507
438 - public function updateForm($Request, $post) {
439 - // wp_send_json_success($post);
440 - if ($post->formStyle) {
441 - $sheetName = 'bitform-' . $post->id . '.css';
442 - $this->saveStyleSheet($post->formStyle, $sheetName);
443 - }
444 -
445 - if ($post->layoutChanged) {
446 - $sheetName = 'bitform-layout-' . $post->id . '.css';
447 - $rowHeight = (int)$post->rowHeight;
448 - $this->saveLayoutStyleSheet($post->layout, $sheetName, $rowHeight);
449 - }
508 + public function updateForm($Request, $post)
509 + {
510 + // return ['checked dta'=>$post];
450 511 $formId = $post->id;
451 512 // get the builder_helper_state from the form
452 513 $builder_helper_state = self::$formModel->get(['builder_helper_state'], ['id' => $formId]);
453 514 $builder_helper_state = (!is_wp_error($builder_helper_state) && 1 === count($builder_helper_state) && isset($builder_helper_state[0]->builder_helper_state)) ? $builder_helper_state[0]->builder_helper_state : '{}';
@@ -470,14 +531,17 @@
470 531 if (property_exists($post, 'builderSettings')) {
471 532 $builder_helper_state->builderSettings = $post->builderSettings;
472 533 }
473 534 if (property_exists($post, 'atomicClassMap')) {
474 - $atomic_class_map = \wp_json_encode((object)$post->atomicClassMap);
535 + $atomic_class_map = \wp_json_encode((object) $post->atomicClassMap);
475 536 }
476 537
477 538 if (property_exists($post, 'fields') && null !== $post->fields && property_exists($post, 'layout') && $post->layout) {
478 539 $fields = wp_unslash($post->fields);
540 + $fields = Helpers::replaceFieldsDefaultErrorMsg($fields);
479 541 $layout = wp_unslash($post->layout);
542 + $nestedLayout = wp_unslash($post->nestedLayouts);
543 + $formInfo = wp_unslash($post->formInfo);
480 544 $formID = wp_unslash($post->id);
481 545 $form_name = wp_unslash($post->form_name);
482 546 $formSettings = wp_unslash($post->formSettings);
483 547 $workFlows = wp_unslash($post->workFlows);
@@ -485,11 +549,13 @@
485 549 $reports = wp_unslash($post->currentReport);
486 550 }
487 551
488 552 $mailTem = $formSettings->mailTem;
553 + $pdfTem = isset($formSettings->pdfTem) ? $formSettings->pdfTem : [];
489 554 $integrations = $formSettings->integrations;
490 555
491 556 $user_details = static::$ipTool->getUserDetail();
557 +
492 558 if (empty($fields) || empty($layout) || is_null($formID)) {
493 559 return new WP_Error('empty_form', 'Can not update empty form.');
494 560 }
495 561
@@ -500,12 +566,16 @@
500 566 __('You are allowed to add maximum 2 workflows ', 'bit-form')
501 567 );
502 568 }
503 569 }
570 +
504 571 $form_content = [
505 - 'fields' => $fields,
506 - 'layout' => $layout,
572 + 'fields' => $fields,
573 + 'layout' => $layout,
574 + 'nestedLayout' => $nestedLayout,
575 + 'formInfo' => $formInfo,
507 576 ];
577 +
508 578 if (isset($post->report_id)) {
509 579 $form_content['report_id'] = wp_unslash($post->report_id);
510 580 }
511 581
@@ -514,8 +584,12 @@
514 584 } */
515 585 if (!empty($formSettings->theme)) {
516 586 $form_content = array_merge($form_content, ['theme' => $formSettings->theme]);
517 587 }
588 +
589 + if (!empty($formSettings->formPermissions)) {
590 + $form_content = array_merge($form_content, ['formPermissions' => $formSettings->formPermissions]);
591 + }
518 592 if (!empty($additional)) {
519 593 $form_content = array_merge($form_content, ['additional' => $additional]);
520 594 }
521 595 $integartionIDForWorkflow = [];
@@ -523,8 +597,9 @@
523 597 $newData['integation'] = 0;
524 598 $newData['mailTemplate'] = 0;
525 599 $newData['workflow'] = 0;
526 600 $newData['reports'] = 0;
601 + $newData['pdfTemplate'] = 0;
527 602 // Confiramtion Message [start]
528 603 if (!empty($formSettings->confirmation->type->successMsg)) {
529 604 $successMessages = $formSettings->confirmation->type->successMsg;
530 605 $successMessageHandler
@@ -547,9 +622,9 @@
547 622 }
548 623 unset($formSettings->confirmation->type->successMsg);
549 624 }
550 625 // return $formSettings;
551 - // Confiramtion Message [end] */
626 + // Confirmation Message [end] */
552 627 // Email Template [start]
553 628 if (!empty($mailTem)) {
554 629 $emailTemplateHandler
555 630 = new EmailTemplateHandler($formID, $user_details);
@@ -568,8 +643,25 @@
568 643 }
569 644 }
570 645 // return $formSettings;
571 646 // Email Template [end] */
647 + if (!empty($pdfTem)) {
648 + $pdfTemplateHandler = new PdfTemplateHandler($formID);
649 +
650 + foreach ($pdfTem as $templateKey => $templateDetail) {
651 + if (isset($templateDetail->setting->font->name)) {
652 + $this->pdfFontDownload($templateDetail->setting->font->name);
653 + }
654 +
655 + $savedID = $pdfTemplateHandler->saveOrUpdate($templateDetail);
656 + if (is_wp_error($savedID) && 'result_empty' === $savedID->get_error_code()) {
657 + $newData['pdfTemplate'] = 2;
658 + } else {
659 + $newData['pdfTemplate'] = 1;
660 + $integartionIDForWorkflow['pdfTem'][$templateKey] = $savedID;
661 + }
662 + }
663 + }
572 664 //Integration [start] */
573 665 $integrationHandler = new IntegrationHandler($formID, $user_details);
574 666 if (!empty($formSettings->confirmation->type)) {
575 667 $allIntegrations = $formSettings->confirmation->type;
@@ -632,16 +724,18 @@
632 724 $workFlowExist = [];
633 725 if (!empty($workFlows)) {
634 726 $workFlowHandler = new WorkFlowHandler($formID, $user_details);
635 727 // foreach ($workFlows as $workFlowIndex => $workFlow) {
728 + $workflows = is_string($workFlows) ? json_decode($workFlows) : $workFlows;
636 729
637 - foreach ($workFlows as $index => $workFlow) {
638 - if ('onload' === $workFlow->action_type) {
730 + foreach ($workflows as $index => $workFlow) {
731 + if (in_array($workFlow->action_type, ['always', 'onload'])) {
639 732 $workFlowExist['onload'] = true;
640 733 }
641 - if ('oninput' === $workFlow->action_type) {
734 + if ('oninput' === $workFlow->action_type || ('always' === $workFlow->action_type && 'cond' === $workFlow->action_behaviour)) {
642 735 $workFlowExist['oninput'] = true;
643 736 }
737 + // $integartionIDForWorkflow = apply_filters('bitform_filter_integration_id', $workFlow, $formID, $index);
644 738 if (empty($workFlow->id)) {
645 739 $savedID = $workFlowHandler->saveworkFlow($workFlow, $integartionIDForWorkflow, $index);
646 740 if (is_wp_error($savedID) && 'result_empty' !== $savedID->get_error_code()) {
647 741 $newData['workflow'] = 2;
@@ -648,9 +742,9 @@
648 742 } else {
649 743 $newData['workflow'] = 1;
650 744 }
651 745 } else {
652 - $newData = $workFlowHandler->updateworkFlow($workFlow->id, $workFlow, $integartionIDForWorkflow, $index, $newData);
746 + $newData = $workFlowHandler->updateworkFlow($workFlow->id, $workFlow, $integartionIDForWorkflow, $index, $newData);
653 747 }
654 748 }
655 749 }
656 750 $form_content = array_merge($form_content, ['workFlowExist' => $workFlowExist]);
@@ -701,8 +795,26 @@
701 795 );
702 796 }
703 797 }
704 798 //reports [end]
799 + //form abandonment [start]
800 + if (!empty($formSettings->formAbandonment)) {
801 + $formAbandonment = $formSettings->formAbandonment;
802 + // search for existing form abandonment in integration table
803 + $abandonmentInteg = $integrationHandler->getAllIntegration('formAbandonment', 'formAbandonment');
804 + if (!is_wp_error($abandonmentInteg) && !empty($abandonmentInteg)) {
805 + $abandonmentInteg = $abandonmentInteg[0];
806 + }
807 + if (isset($abandonmentInteg->id)) {
808 + $abandonmentIntegID = $abandonmentInteg->id;
809 + $abandonmentIntegDetails = wp_json_encode($formAbandonment);
810 + $integrationHandler->updateIntegration($abandonmentIntegID, 'formAbandonment', 'formAbandonment', $abandonmentIntegDetails, 'formAbandonment');
811 + } else {
812 + $abandonmentIntegDetails = wp_json_encode($formAbandonment);
813 + $integrationHandler->saveIntegration('formAbandonment', 'formAbandonment', $abandonmentIntegDetails, 'formAbandonment');
814 + }
815 + }
816 + //form abandonment [end]
705 817 $form_content = wp_json_encode($form_content);
706 818 $updateData = [
707 819 'form_name' => $form_name,
708 820 'form_content' => $form_content,
@@ -707,12 +819,11 @@
707 819 'form_name' => $form_name,
708 820 'form_content' => $form_content,
709 821 'user_id' => $user_details['id'],
710 822 'user_ip' => $user_details['ip'],
711 - 'builder_helper_state' => \wp_json_encode((object)$builder_helper_state),
823 + 'builder_helper_state' => \wp_json_encode((object) $builder_helper_state),
712 824 'generated_script_page_ids' => \wp_json_encode((object) []),
713 825 'user_device' => $user_details['device'],
714 - 'status' => 1,
715 826 'updated_at' => $user_details['time'],
716 827 ];
717 828 if (isset($atomic_class_map)) {
718 829 $updateData['atomic_class_map'] = $atomic_class_map;
@@ -741,9 +852,10 @@
741 852 unset($post->deletedFldKey);
742 853 }
743 854
744 855 $updated_data = $this->getAForm(['id' => $formID], null);
745 - if (0 === $newData['settingConfiramation'] && 0 === $newData['mailTemplate'] && 0 === $newData['integation']) {
856 +
857 + if (0 === $newData['settingConfiramation'] && 0 === $newData['mailTemplate'] && 0 === $newData['integation'] && 0 === $newData['pdfTemplate']) {
746 858 unset($updated_data['formSettings']);
747 859 }
748 860 if (0 === $newData['integation']) {
749 861 unset($updated_data['integrations']);
@@ -755,8 +867,13 @@
755 867 unset($updated_data['mailTem']);
756 868 } elseif (2 === $newData['mailTemplate']) {
757 869 $errorIN .= empty($errorIN) ? 'mailTemplate' : ', mailTemplate';
758 870 }
871 + if (0 === $newData['pdfTemplate']) {
872 + unset($updated_data['pdfTem']);
873 + } elseif (2 === $newData['pdfTemplate']) {
874 + $errorIN .= empty($errorIN) ? 'pdfTemplate' : ', pdfTemplate';
875 + }
759 876 if (0 === $newData['workflow']) {
760 877 unset($updated_data['workFlows']);
761 878 } elseif (2 === $newData['workflow']) {
762 879 $errorIN .= empty($errorIN) ? 'workflow' : ', workflow';
@@ -770,14 +887,15 @@
770 887 }
771 888 if (null !== $reportIsDefault) {
772 889 $updated_data['form_content']['is_default'] = $reportIsDefault;
773 890 }
774 - $updated_data['message'] = __('Form updated successfully.', 'bitform');
891 + $updated_data['message'] = __('Form updated successfully.', 'bit-form');
775 892 return $updated_data;
776 893 }
777 894 }
778 895
779 - public function setEmptyMetaValue($fieldkeys) {
896 + public function setEmptyMetaValue($fieldkeys)
897 + {
780 898 $sqlEscaped = array_map(function ($fld) {
781 899 return "'" . esc_sql($fld) . "'";
782 900 }, $fieldkeys);
783 901 $deletedFldKey = implode(',', $sqlEscaped);
@@ -786,95 +904,99 @@
786 904 $sql = $wpdb->prepare("UPDATE $tablename SET meta_value = %s WHERE meta_key IN ( " . $deletedFldKey . ')', '');
787 905 $wpdb->query($sql);
788 906 }
789 907
790 - public function changeFormStatus($Request, $post) {
791 - if (isset($Request['status']) && $Request['id']) {
792 - $status = wp_unslash($Request['status']);
793 - $id = wp_unslash($Request['id']);
794 - } else {
795 - $status = wp_unslash($post->status);
796 - $id = wp_unslash($post->id);
797 - }
798 - $user_details = static::$ipTool->getUserDetail();
799 - // return $post->fields;
800 - $status = 'true' === $status || true === $status ? true : false;
801 - if (!is_bool($status) || is_null($id)) {
802 - // echo $status;
803 - return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
804 - }
805 - $update_status = static::$formModel->update(
806 - [
807 - 'user_id' => $user_details['id'],
808 - 'user_ip' => $user_details['ip'],
809 - 'user_device' => $user_details['device'],
810 - 'status' => $status ? 1 : 0,
811 - 'updated_at' => $user_details['time'],
812 - ],
813 - [
814 - 'id' => $id,
815 - ]
816 - );
817 - if (is_wp_error($update_status)) {
818 - return $update_status;
819 - } elseif (!$update_status) {
820 - return false;
821 - } else {
822 - return __('Form status changed successfully', 'bit-form');
823 - }
908 + public function changeFormStatus($Request, $post)
909 + {
910 + if (isset($Request['status']) && $Request['id']) {
911 + $status = wp_unslash($Request['status']);
912 + $id = wp_unslash($Request['id']);
913 + } else {
914 + $status = wp_unslash($post->status);
915 + $id = wp_unslash($post->id);
824 916 }
917 + $user_details = static::$ipTool->getUserDetail();
918 + // return $post->fields;
919 + $status = 'true' === $status || true === $status ? true : false;
920 + if (!is_bool($status) || is_null($id)) {
921 + // echo $status;
922 + return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
923 + }
924 + $update_status = static::$formModel->update(
925 + [
926 + 'user_id' => $user_details['id'],
927 + 'user_ip' => $user_details['ip'],
928 + 'user_device' => $user_details['device'],
929 + 'status' => $status ? 1 : 0,
930 + 'updated_at' => $user_details['time'],
931 + ],
932 + [
933 + 'id' => $id,
934 + ]
935 + );
936 + if (is_wp_error($update_status)) {
937 + return $update_status;
938 + } elseif (!$update_status) {
939 + return false;
940 + } else {
941 + return __('Form status changed successfully', 'bit-form');
942 + }
943 + }
825 944
826 - public function changeBulkFormStatus($Request, $post) {
827 - if (isset($Request['status']) && $Request['formID']) {
828 - $status = wp_unslash($Request['status']);
829 - $formID = wp_unslash($Request['formID']);
830 - } else {
831 - $status = wp_unslash($post->status);
832 - $formID = wp_unslash($post->formID);
833 - }
834 - $user_details = static::$ipTool->getUserDetail();
835 - // return $post->fields;
836 - $status = 'true' === $status || true === $status ? true : false;
837 - if (!is_bool($status) || is_null($formID) || !is_array($formID)) {
838 - // echo $status;
839 - return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
840 - }
841 - $update_status = static::$formModel->bulkUpdate(
842 - [
843 - 'user_id' => $user_details['id'],
844 - 'user_ip' => $user_details['ip'],
845 - 'user_device' => $user_details['device'],
846 - 'status' => $status ? 1 : 0,
847 - 'updated_at' => $user_details['time'],
848 - ],
849 - [
850 - 'id' => $formID,
851 - ]
852 - );
853 - if (is_wp_error($update_status)) {
854 - return $update_status;
855 - } elseif (!$update_status) {
856 - return false;
857 - } else {
858 - return __('Form status changed successfully', 'bit-form');
859 - }
945 + public function changeBulkFormStatus($Request, $post)
946 + {
947 + if (isset($Request['status']) && $Request['formID']) {
948 + $status = wp_unslash($Request['status']);
949 + $formID = wp_unslash($Request['formID']);
950 + } else {
951 + $status = wp_unslash($post->status);
952 + $formID = wp_unslash($post->formID);
860 953 }
954 + $user_details = static::$ipTool->getUserDetail();
955 + // return $post->fields;
956 + $status = 'true' === $status || true === $status ? true : false;
957 + if (!is_bool($status) || is_null($formID) || !is_array($formID)) {
958 + // echo $status;
959 + return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
960 + }
961 + $update_status = static::$formModel->bulkUpdate(
962 + [
963 + 'user_id' => $user_details['id'],
964 + 'user_ip' => $user_details['ip'],
965 + 'user_device' => $user_details['device'],
966 + 'status' => $status ? 1 : 0,
967 + 'updated_at' => $user_details['time'],
968 + ],
969 + [
970 + 'id' => $formID,
971 + ]
972 + );
973 + if (is_wp_error($update_status)) {
974 + return $update_status;
975 + } elseif (!$update_status) {
976 + return false;
977 + } else {
978 + return __('Form status changed successfully', 'bit-form');
979 + }
980 + }
861 981
862 - public function getAllForm() {
863 - global $wpdb;
982 + public function getAllForm()
983 + {
984 + global $wpdb;
864 985
865 - $allForms = $wpdb->get_results("SELECT forms.id,forms.entries as fm_entries,forms.form_name,forms.status,forms.views,forms.created_at,COUNT(entries.id) as entries FROM `{$wpdb->prefix}bitforms_form` as forms LEFT JOIN `{$wpdb->prefix}bitforms_form_entries` as entries ON forms.id = entries.form_id GROUP BY forms.id");
986 + $allForms = $wpdb->get_results("SELECT forms.id,forms.entries as fm_entries,forms.form_name,forms.status,forms.views,forms.created_at,COUNT(entries.id) as entries FROM `{$wpdb->prefix}bitforms_form` as forms LEFT JOIN `{$wpdb->prefix}bitforms_form_entries` as entries ON forms.id = entries.form_id GROUP BY forms.id");
866 987
867 - if (is_wp_error($allForms)) {
868 - return $allForms;
869 - } elseif (!$allForms) {
870 - return [];
871 - } else {
872 - return $allForms;
873 - }
988 + if (is_wp_error($allForms)) {
989 + return $allForms;
990 + } elseif (!$allForms) {
991 + return [];
992 + } else {
993 + return $allForms;
874 994 }
995 + }
875 996
876 - public function getAForm($Request, $post) {
997 + public function getAForm($Request, $post)
998 + {
877 999 if (isset($Request['id'])) {
878 1000 $formID = wp_unslash($Request['id']);
879 1001 } else {
880 1002 $formID = wp_unslash($post->id);
@@ -891,8 +1013,10 @@
891 1013 $helper_states = $formManager->getFormHelperStates();
892 1014 if ($formManager->isExist()) {
893 1015 $form_content_arr = [
894 1016 'layout' => $form_content->layout,
1017 + 'nestedLayout' => !empty($form_content->nestedLayout) ? $form_content->nestedLayout : (object) [],
1018 + 'formInfo' => !empty($form_content->formInfo) ? $form_content->formInfo : (object) ['formName' => $formManager->getFormName()],
895 1019 'fields' => $form_content->fields,
896 1020 'form_name' => $formManager->getFormName(),
897 1021 'workFlowExist' => $form_content->workFlowExist,
898 1022 'report_id' => isset($form_content->report_id) ? $form_content->report_id : null
@@ -925,8 +1049,24 @@
925 1049 'body' => $emailTemplatevalue->body,
926 1050 ];
927 1051 }
928 1052 }
1053 + // get all pdf template
1054 + $pdfTemplateHandler = new PdfTemplateHandler($formID);
1055 + $pdfTemplates = $pdfTemplateHandler->getAll();
1056 +
1057 + if (!is_wp_error($pdfTemplates)) {
1058 + foreach ($pdfTemplates as $value) {
1059 + $pdfTem[] =
1060 + [
1061 + 'id' => $value->id,
1062 + 'title' => $value->title,
1063 + 'setting' => json_decode($value->setting),
1064 + 'body' => $value->body,
1065 + ];
1066 + }
1067 + }
1068 +
929 1069 $integrationHandler = new IntegrationHandler($formID);
930 1070 $formIntegrations = $integrationHandler->getAllIntegration('form');
931 1071 if (!is_wp_error($formIntegrations)) {
932 1072 foreach ($formIntegrations as $integrationkey => $integrationValue) {
@@ -954,17 +1094,32 @@
954 1094 ];
955 1095 $integrations[] = array_merge(
956 1096 $integrationData,
957 1097 is_string($integrationValue->integration_details) ?
958 - (array) json_decode($integrationValue->integration_details) :
959 - $integrationValue->integration_details
1098 + (array) json_decode($integrationValue->integration_details) :
1099 + $integrationValue->integration_details
960 1100 );
961 1101 }
962 1102 }
963 1103 }
1104 +
1105 + // get form abandonment integration if exist
1106 + $formAbandonmentInteg = $integrationHandler->getAllIntegration('formAbandonment', 'formAbandonment');
1107 + if (!is_wp_error($formAbandonmentInteg) && !empty($formAbandonmentInteg)) {
1108 + $formAbandonmentInteg = $formAbandonmentInteg[0];
1109 + if (!empty($formAbandonmentInteg->integration_details)) {
1110 + $formAbandonment = json_decode($formAbandonmentInteg->integration_details);
1111 + } else {
1112 + $formAbandonment = (object) [];
1113 + }
1114 + } else {
1115 + $formAbandonment = (object) [];
1116 + }
1117 +
964 1118 $settingsContent = [
965 - 'formName' => $formManager->getFormName(),
966 - 'theme' => isset($form_content->theme) ? $form_content->theme : 'default',
1119 + 'formName' => $formManager->getFormName(),
1120 + 'theme' => isset($form_content->theme) ? $form_content->theme : 'default',
1121 + 'formPermissions' => isset($form_content->formPermissions) ? $form_content->formPermissions : (object) [],
967 1122 // 'submitBtn' => $form_content->buttons,
968 1123 ];
969 1124
970 1125 if (!empty($allConfirmation)) {
@@ -991,8 +1146,9 @@
991 1146 'integrations' => []
992 1147 ]
993 1148 );
994 1149 }
1150 +
995 1151 if (!empty($mailTem)) {
996 1152 $settingsContent = array_merge(
997 1153 $settingsContent,
998 1154 [
@@ -1006,8 +1162,40 @@
1006 1162 'mailTem' => []
1007 1163 ]
1008 1164 );
1009 1165 }
1166 +
1167 + if (!empty($pdfTem)) {
1168 + $settingsContent = array_merge(
1169 + $settingsContent,
1170 + [
1171 + 'pdfTem' => $pdfTem,
1172 + ]
1173 + );
1174 + } else {
1175 + $settingsContent = array_merge(
1176 + $settingsContent,
1177 + [
1178 + 'pdfTem' => [],
1179 + ]
1180 + );
1181 + }
1182 +
1183 + if (!empty($formAbandonment)) {
1184 + $settingsContent = array_merge(
1185 + $settingsContent,
1186 + [
1187 + 'formAbandonment' => $formAbandonment,
1188 + ]
1189 + );
1190 + } else {
1191 + $settingsContent = array_merge(
1192 + $settingsContent,
1193 + [
1194 + 'formAbandonment' => (object) [],
1195 + ]
1196 + );
1197 + }
1010 1198 $formSettings = [
1011 1199 'formSettings' => $settingsContent,
1012 1200 ];
1013 1201 $data = [
@@ -1022,8 +1210,9 @@
1022 1210 'additional' => empty($form_content->additional) ? [
1023 1211 'enabled' => [
1024 1212 'blocked_ip' => false,
1025 1213 'restrict_form' => false,
1214 + 'onePerIp' => false,
1026 1215 ],
1027 1216 'settings' => [
1028 1217 'restrict_form' => [
1029 1218 'day' => [],
@@ -1038,9 +1227,8 @@
1038 1227 ],
1039 1228 'entry_limit' => null,
1040 1229 'blocked_ip' => [],
1041 1230 ],
1042 - 'onePerIp' => false,
1043 1231 ] : $form_content->additional,
1044 1232 'created_at' => $formManager->getFormMetaData()['created_at'],
1045 1233 'views' => $formManager->getFormMetaData()['views'],
1046 1234 'entries' => $formManager->getFormMetaData()['entries'],
@@ -1081,9 +1269,9 @@
1081 1269 } else {
1082 1270 $returnedReportData[$reportKey]->details = [];
1083 1271 }
1084 1272 if ('1' === (string) $reportData->isDefault) {
1085 - $returnedReportData[$reportKey]->details->report_name = 'All Entries';
1273 + $returnedReportData[$reportKey]->details->report_name = __('All Entries', 'bit-form');
1086 1274 }
1087 1275 }
1088 1276 $reports = ['reports' => $returnedReportData];
1089 1277 $data = array_merge($data, $reports);
@@ -1095,17 +1283,240 @@
1095 1283 return $data;
1096 1284 }
1097 1285 }
1098 1286
1099 - public function deleteAForm($Request, $post) {
1287 + public function getAFormV1($Request, $post)
1288 + {
1100 1289 if (isset($Request['id'])) {
1101 1290 $formID = wp_unslash($Request['id']);
1102 1291 } else {
1103 1292 $formID = wp_unslash($post->id);
1104 1293 }
1294 + // return $post->fields;
1105 1295 if (is_null($formID)) {
1106 1296 return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1107 1297 }
1298 + $formManager = new AdminFormManager($formID);
1299 + if (!$formManager->isExist()) {
1300 + return new WP_Error('not_exists', __('Form is not exists.', 'bit-form'));
1301 + }
1302 + $form_content = $formManager->getFormContent();
1303 + if ($formManager->isExist()) {
1304 + $form_content_arr = [
1305 + 'layout' => $form_content->layout,
1306 + 'fields' => $form_content->fields,
1307 + 'form_name' => $formManager->getFormName(),
1308 + 'workFlowExist' => $form_content->workFlowExist,
1309 + 'report_id' => isset($form_content->report_id) ? $form_content->report_id : null
1310 + ];
1311 + $successMessageHandler
1312 + = new SuccessMessageHandler($formID);
1313 + $successMessages = $successMessageHandler->getAllMessage();
1314 + if (!is_wp_error($successMessages)) {
1315 + foreach ($successMessages as $sucessMessagekey => $sucessMessagevalue) {
1316 + $allConfirmation['type']['successMsg'][$sucessMessagekey] =
1317 + [
1318 + 'id' => $sucessMessagevalue->id,
1319 + 'title' => $sucessMessagevalue->message_title,
1320 + 'msg' => $sucessMessagevalue->message_content,
1321 + ];
1322 + }
1323 + }
1324 + $emailTemplateHandler
1325 + = new EmailTemplateHandler($formID);
1326 + $emailTemplates = $emailTemplateHandler->getAllTemplate();
1327 + if (!is_wp_error($emailTemplates)) {
1328 + foreach ($emailTemplates as $emailTemplatekey => $emailTemplatevalue) {
1329 + $mailTem[] =
1330 + [
1331 + 'id' => $emailTemplatevalue->id,
1332 + 'title' => $emailTemplatevalue->title,
1333 + 'sub' => $emailTemplatevalue->sub,
1334 + 'body' => $emailTemplatevalue->body,
1335 + ];
1336 + }
1337 + }
1338 + $integrationHandler = new IntegrationHandler($formID);
1339 + $formIntegrations = $integrationHandler->getAllIntegration('form');
1340 + if (!is_wp_error($formIntegrations)) {
1341 + foreach ($formIntegrations as $integrationkey => $integrationValue) {
1342 + if ('redirectPage' === $integrationValue->integration_type || 'webHooks' === $integrationValue->integration_type) {
1343 + $integrationData = [
1344 + 'id' => $integrationValue->id,
1345 + 'title' => $integrationValue->integration_name,
1346 + ];
1347 + if (!empty($integrationValue->integration_details)) {
1348 + // var_dump(json_decode($integrationValue->integration_details));
1349 + $integration_details = (array) json_decode($integrationValue->integration_details);
1350 + $integrationData = array_merge($integration_details, $integrationData);
1351 + } else {
1352 + $integration_details = [
1353 + 'url' => '',
1354 + ];
1355 + $integrationData = array_merge($integrationData, $integration_details);
1356 + }
1357 + $allConfirmation['type'][$integrationValue->integration_type][]
1358 + = $integrationData;
1359 + } else {
1360 + $integrationData = [
1361 + 'id' => $integrationValue->id,
1362 + 'name' => $integrationValue->integration_name,
1363 + 'type' => $integrationValue->integration_type,
1364 + ];
1365 + $integrations[] = array_merge(
1366 + $integrationData,
1367 + is_string($integrationValue->integration_details) ?
1368 + (array) json_decode($integrationValue->integration_details) :
1369 + $integrationValue->integration_details
1370 + );
1371 + }
1372 + }
1373 + }
1374 + $settingsContent = [
1375 + 'formName' => $formManager->getFormName(),
1376 + 'theme' => $form_content->theme,
1377 + // 'submitBtn' => $form_content->buttons,
1378 + ];
1379 +
1380 + if (!empty($allConfirmation)) {
1381 + $confirmation = [
1382 + 'confirmation' => $allConfirmation,
1383 + ];
1384 + } else {
1385 + $confirmation = [
1386 + 'confirmation' => ['type' => []],
1387 + ];
1388 + }
1389 + $settingsContent = array_merge($settingsContent, $confirmation);
1390 + if (!empty($integrations)) {
1391 + $settingsContent = array_merge(
1392 + $settingsContent,
1393 + [
1394 + 'integrations' => $integrations,
1395 + ]
1396 + );
1397 + } else {
1398 + $settingsContent = array_merge(
1399 + $settingsContent,
1400 + [
1401 + 'integrations' => []
1402 + ]
1403 + );
1404 + }
1405 + if (!empty($mailTem)) {
1406 + $settingsContent = array_merge(
1407 + $settingsContent,
1408 + [
1409 + 'mailTem' => $mailTem,
1410 + ]
1411 + );
1412 + } else {
1413 + $settingsContent = array_merge(
1414 + $settingsContent,
1415 + [
1416 + 'mailTem' => []
1417 + ]
1418 + );
1419 + }
1420 + $formSettings = [
1421 + 'formSettings' => $settingsContent,
1422 + ];
1423 + $data = [
1424 + 'id' => $formID,
1425 + 'form_name' => $formManager->getFormName(),
1426 + 'form_content' => $form_content_arr,
1427 + 'additional' => empty($form_content->additional) ? [
1428 + 'enabled' => [
1429 + 'blocked_ip' => false,
1430 + 'restrict_form' => false,
1431 + 'onePerIp' => false,
1432 + ],
1433 + 'settings' => [
1434 + 'restrict_form' => [
1435 + 'day' => [],
1436 + 'date' => [
1437 + 'from' => null,
1438 + 'to' => null,
1439 + ],
1440 + 'time' => [
1441 + 'from' => null,
1442 + 'to' => null,
1443 + ],
1444 + ],
1445 + 'entry_limit' => null,
1446 + 'blocked_ip' => [],
1447 + ],
1448 + ] : $form_content->additional,
1449 + 'created_at' => $formManager->getFormMetaData()['created_at'],
1450 + 'views' => $formManager->getFormMetaData()['views'],
1451 + 'entries' => $formManager->getFormMetaData()['entries'],
1452 + 'status' => $formManager->getFormMetaData()['status'],
1453 + ];
1454 + $data = array_merge($data, $formSettings);
1455 + $workFlowHandler = new WorkFlowHandler($formID);
1456 + $workFlows = ['workFlows' => $workFlowHandler->getAllworkFlowV1()];
1457 + $data = array_merge($data, $workFlows);
1458 + $reportsModel = new ReportsModel();
1459 + $returnedReportData = $reportsModel->get(
1460 + [
1461 + 'id',
1462 + 'details',
1463 + 'isDefault',
1464 + 'type',
1465 + ],
1466 + [
1467 + 'category' => 'form',
1468 + 'context' => $formID,
1469 + ]
1470 + );
1471 +
1472 + if (!is_wp_error($returnedReportData)) {
1473 + $fieldNames = [];
1474 + foreach ($formManager->getFieldLabel() as $key => $field) {
1475 + $fieldNames[$field['key']] = isset($field->lbl) ? $field->lbl : '';
1476 + }
1477 + if ($formManager->isGCLIDEnabled()) {
1478 + $fieldNames['GCLID'] = 'GCLID';
1479 + }
1480 + foreach ($returnedReportData as $reportKey => $reportData) {
1481 + $reportDetails = $reportsModel->validateReportFields($reportData, $fieldNames);
1482 + if (!empty($reportDetails) && is_string($reportDetails)) {
1483 + $returnedReportData[$reportKey]->details = json_decode($reportDetails);
1484 + } elseif (!empty($reportDetails)) {
1485 + $returnedReportData[$reportKey]->details = $reportDetails;
1486 + } else {
1487 + $returnedReportData[$reportKey]->details = [];
1488 + }
1489 + if ('1' === (string) $reportData->isDefault) {
1490 + $returnedReportData[$reportKey]->details->report_name = __('All Entries', 'bit-form');
1491 + }
1492 + }
1493 + $reports = ['reports' => $returnedReportData];
1494 + $data = array_merge($data, $reports);
1495 + }
1496 +
1497 + $formFields = $formManager->getFieldLabel();
1498 + $data = array_merge($data, ['Labels' => $formFields]);
1499 +
1500 + return $data;
1501 + }
1502 + }
1503 +
1504 + public function deleteAForm($Request, $post)
1505 + {
1506 + if (isset($Request['id'])) {
1507 + $formID = wp_unslash($Request['id']);
1508 + } else {
1509 + $formID = wp_unslash($post->id);
1510 + }
1511 + if (is_null($formID)) {
1512 + return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1513 + }
1514 +
1515 + if (!filter_var($formID, FILTER_VALIDATE_INT)) {
1516 + return new WP_Error('invalid_form', __('Form id is invalid.', 'bit-form'));
1517 + }
1518 +
1108 1519 $delete_status = static::$formModel->delete(
1109 1520 [
1110 1521 'id' => $formID,
1111 1522 ]
@@ -1110,16 +1521,17 @@
1110 1521 'id' => $formID,
1111 1522 ]
1112 1523 );
1113 1524
1114 - $successMessageModel
1115 - = new SuccessMessageModel();
1525 + $successMessageModel = new SuccessMessageModel();
1116 1526 $deleteMsgStatus = $successMessageModel->delete(['form_id' => $formID]);
1117 1527
1118 - $emailTemplateModel
1119 - = new EmailTemplateModel();
1528 + $emailTemplateModel = new EmailTemplateModel();
1120 1529 $deleteTemplateStatus = $emailTemplateModel->delete(['form_id' => $formID]);
1121 1530
1531 + $pdfTemplateModel = new PdfTemplateModel();
1532 + $deletePdfTemplateStatus = $pdfTemplateModel->delete(['form_id' => $formID]);
1533 +
1122 1534 $integrationModel = new IntegrationModel();
1123 1535 $deleteIntegrationStatus = $integrationModel->delete(['form_id' => $formID]);
1124 1536
1125 1537 $workFlowModel = new WorkFlowModel();
@@ -1131,22 +1543,11 @@
1131 1543 $formEntryModel = new FormEntryModel();
1132 1544 $returnedEntries = $formEntryModel->get('id', ['form_id' => $formID]);
1133 1545 $entries = [];
1134 1546
1135 - if (!is_wp_error($returnedEntries)) {
1136 - foreach ($returnedEntries as $entryKey => $entryInfo) {
1137 - $entries[] = $entryInfo->id;
1138 - }
1139 - global $wpdb;
1140 - $prefix = $wpdb->prefix;
1141 - if (count($entries) > 0) {
1142 - $deleteEntriesStatus = $formEntryModel->bulkDelete(
1143 - [
1144 - "`{$prefix}bitforms_form_entries`.`id`" => $entries,
1145 - "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
1146 - ]
1147 - );
1148 - }
1547 + // checked namespace exist
1548 + if (class_exists('\BitCode\BitFormPro\Admin\BfTable\Table')) {
1549 + \BitCode\BitFormPro\Admin\BfTable\Table::deleteTableByFormId($formID);
1149 1550 }
1150 1551
1151 1552 $fileHandler = new FileHandler();
1152 1553 if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
@@ -1154,35 +1555,54 @@
1154 1555 }
1155 1556 if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
1156 1557 $fileHandler->rmrf(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID);
1157 1558 }
1158 - if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-' . $formID . '.css')) {
1159 - unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-' . $formID . '.css');
1160 - }
1161 - if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-layout-' . $formID . '.css')) {
1162 - unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-layout-' . $formID . '.css');
1163 - }
1559 + $formStylePath = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles';
1560 +
1561 + $formCssPath = $formStylePath . DIRECTORY_SEPARATOR . 'bitform-' . $formID . '.css';
1562 + FileHandler::deleteIsFileExists($formCssPath);
1563 +
1564 + $layoutCssPath = $formStylePath . DIRECTORY_SEPARATOR . 'bitform-layout-' . $formID . '.css';
1565 + FileHandler::deleteIsFileExists($layoutCssPath);
1566 +
1567 + $customCssPath = $formStylePath . DIRECTORY_SEPARATOR . 'bitform-custom-' . $formID . '.css';
1568 + FileHandler::deleteIsFileExists($customCssPath);
1569 +
1570 + $customJSPath = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-scripts' . DIRECTORY_SEPARATOR . 'bitform-custom-' . $formID . '.js';
1571 + FileHandler::deleteIsFileExists($customJSPath);
1572 +
1573 + $conversationCssPath = $formStylePath . DIRECTORY_SEPARATOR . 'bitform-conversational-' . $formID . '.css';
1574 + FileHandler::deleteIsFileExists($conversationCssPath);
1575 +
1164 1576 return is_wp_error($delete_status) ? $delete_status : __('Form deleted successfully.', 'bit-form');
1165 1577 }
1166 1578
1167 - public function deleteBlukForm($Request, $post) {
1579 + public function deleteBlukForm($Request, $post)
1580 + {
1168 1581 if (isset($Request['formID'])) {
1169 1582 $formID = wp_unslash($Request['formID']);
1170 1583 } else {
1171 1584 $formID = wp_unslash($post->formID);
1172 1585 }
1586 +
1173 1587 if (is_null($formID) || !is_array($formID)) {
1174 1588 return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1175 1589 }
1590 +
1591 + if (!Helpers::checkIsIntArr($formID)) {
1592 + return new WP_Error('invalid_form', __('Form id is invalid', 'bit-form'));
1593 + }
1594 +
1176 1595 $cssPath = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR;
1177 1596 foreach ($formID as $id) {
1178 - unlink($cssPath . 'bitform-' . $id . '.css');
1179 - if (file_exists($cssPath . 'bitform-layout-' . $id . '.css')) {
1180 - unlink($cssPath . 'bitform-layout-' . $id . '.css');
1181 - }
1182 - if (file_exists($cssPath . 'bitform-' . $id . '-formid' . '.css')) {
1183 - unlink($cssPath . 'bitform-' . $id . '-formid' . '.css');
1184 - }
1597 + $formCssPath = $cssPath . 'bitform-' . $id . '.css';
1598 + FileHandler::deleteIsFileExists($formCssPath);
1599 +
1600 + $layoutPath = $cssPath . 'bitform-layout-' . $id . '.css';
1601 + FileHandler::deleteIsFileExists($layoutPath);
1602 +
1603 + $bitformCustomPath = $cssPath . 'bitform-' . $id . '-formid' . '.css';
1604 + FileHandler::deleteIsFileExists($bitformCustomPath);
1185 1605 }
1186 1606 $delete_status = static::$formModel->bulkDelete(
1187 1607 [
1188 1608 'id' => $formID,
@@ -1187,16 +1607,23 @@
1187 1607 [
1188 1608 'id' => $formID,
1189 1609 ]
1190 1610 );
1611 +
1612 + if (is_wp_error($delete_status)) {
1613 + wp_send_json_error($delete_status->get_error_message(), 400);
1614 + }
1615 +
1191 1616 $successMessageModel
1192 1617 = new SuccessMessageModel();
1193 1618 $deleteMsgStatus = $successMessageModel->bulkDelete(['form_id' => $formID]);
1194 1619
1195 - $emailTemplateModel
1196 - = new EmailTemplateModel();
1620 + $emailTemplateModel = new EmailTemplateModel();
1197 1621 $deleteTemplateStatus = $emailTemplateModel->bulkDelete(['form_id' => $formID]);
1198 1622
1623 + $pdfTemplateModel = new PdfTemplateModel();
1624 + $deletePdfTemplateStatus = $pdfTemplateModel->bulkDelete(['form_id' => $formID]);
1625 +
1199 1626 $integrationModel = new IntegrationModel();
1200 1627 $deleteIntegrationStatus = $integrationModel->bulkDelete(['form_id' => $formID]);
1201 1628
1202 1629 $workFlowModel = new WorkFlowModel();
@@ -1206,11 +1633,18 @@
1206 1633 $deleteReportStatus = $reportsModel->bulkDelete(['context' => $formID]);
1207 1634
1208 1635 $formEntryModel = new FormEntryModel();
1209 1636 $returnedEntries = $formEntryModel->get('id', ['form_id' => $formID]);
1637 +
1638 + if (class_exists('\BitCode\BitFormPro\Admin\BfTable\Table')) {
1639 + \BitCode\BitFormPro\Admin\BfTable\Table::deleteTableByBulkFormId($formID);
1640 + }
1641 +
1210 1642 $entries = [];
1211 1643 foreach ($returnedEntries as $entryKey => $entryInfo) {
1212 - $entries[] = $entryInfo->id;
1644 + if (!empty($entryInfo->id)) {
1645 + $entries[] = $entryInfo->id;
1646 + }
1213 1647 }
1214 1648 global $wpdb;
1215 1649 $prefix = $wpdb->prefix;
1216 1650 if (count($entries) > 0) {
@@ -1221,22 +1655,24 @@
1221 1655 ]
1222 1656 );
1223 1657 }
1224 1658
1659 + // delete entry log and details
1660 + $sql = 'DELETE em, e FROM wp_bitforms_form_log_details AS em JOIN wp_bitforms_form_entry_log AS e ON em.log_id = e.id WHERE form_id = %d';
1661 + $wpdb->prepare($sql, $formID);
1662 +
1225 1663 $fileHandler = new FileHandler();
1226 1664 foreach ($formID as $fId) {
1227 1665 if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $fId)) {
1228 1666 $fileHandler->rmrf(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $fId);
1229 1667 }
1230 -
1231 - // unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-' . $fId . '.css');
1232 - // unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-layout-' . $fId . '.css');
1233 1668 }
1234 1669
1235 1670 return is_wp_error($delete_status) ? $delete_status : __('Forms deleted successfully.', 'bit-form');
1236 1671 }
1237 1672
1238 - public function genarateNewLayoutNField($layout, $fields, $oldId, $newId) {
1673 + public function genarateNewLayoutNField($layout, $fields, $oldId, $newId)
1674 + {
1239 1675 $newField = (object) [];
1240 1676 foreach ($layout->lg as $ind => $itm) {
1241 1677 $fld_tmp = $fields->{$layout->lg[$ind]->i};
1242 1678 $layout->lg[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->lg[$ind]->i);
@@ -1246,9 +1682,10 @@
1246 1682 }
1247 1683 return ['layout' => $layout, 'fields' => $newField];
1248 1684 }
1249 1685
1250 - public function duplicateAForm($Request, $post) {
1686 + public function duplicateAForm($Request, $post)
1687 + {
1251 1688 $oldFormId = intval($post->id);
1252 1689 $newFormId = intval($post->newFormId);
1253 1690 if ($oldFormId < 1) {
1254 1691 return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
@@ -1290,19 +1727,22 @@
1290 1727 }
1291 1728 return $duplicateResponse;
1292 1729 }
1293 1730
1294 - public function importAForm($post) {
1731 + public function importAForm($post)
1732 + {
1295 1733 $oldFormId = !empty($post->formDetail->form_id) ? $post->formDetail->form_id : null;
1296 1734 $newFormId = intval($post->newFormId);
1297 1735 if (!$oldFormId || empty($post->formDetail->fields) || empty($post->formDetail->layout)) {
1298 1736 return new WP_Error('invalid_form', __('Please import a valid json.', 'bit-form'));
1299 1737 }
1300 - $importtedForm = (array)$post->formDetail;
1738 + $importtedForm = (array) $post->formDetail;
1301 1739 $importtedForm['form_content']['fields'] = $importtedForm['fields'];
1302 1740 $importtedForm['form_content']['layout'] = $importtedForm['layout'];
1303 - unset($importtedForm['fields'], $importtedForm['layout']);
1304 - $formData = FormDuplicateHelper::createReplica((array)$importtedForm, $oldFormId, $newFormId);
1741 + $importtedForm['form_content']['nestedLayout'] = $importtedForm['nestedLayouts'];
1742 + $importtedForm['form_content']['formInfo'] = $importtedForm['formInfo'];
1743 + unset($importtedForm['fields'], $importtedForm['layout'], $importtedForm['nestedLayout'], $importtedForm['formInfo']);
1744 + $formData = FormDuplicateHelper::createReplica((array) $importtedForm, $oldFormId, $newFormId);
1305 1745
1306 1746 if (!empty($importtedForm['rowHeight'])) {
1307 1747 $formData->rowHeight = $importtedForm['rowHeight'];
1308 1748 }
@@ -1312,10 +1752,29 @@
1312 1752 $formData->layoutChanged = '-';
1313 1753 }
1314 1754 $importResponse = $this->createNewForm(null, $formData);
1315 1755
1756 + $customCode = [];
1757 + // add custom js
1758 + if (!empty($formData->customCode->customJs)) {
1759 + $customJsPath = 'form-scripts';
1760 + $fileName = 'bitform-custom-' . $newFormId . '.js';
1761 + $customCode['JavaScript'] = $formData->customCode->customJs;
1762 + Helpers::saveFile($customJsPath, $fileName, $formData->customCode->customJs, 'w');
1763 + }
1764 + // add custom css
1765 + if (!empty($formData->customCode->customCss)) {
1766 + $customCssPath = 'form-styles';
1767 + $fileName = 'bitform-custom-' . $newFormId . '.css';
1768 + $customCode['CSS'] = $formData->customCode->customCss;
1769 + Helpers::saveFile($customCssPath, $fileName, $formData->customCode->customCss, 'w');
1770 + }
1771 + if (!is_wp_error($importResponse)) {
1772 + $importResponse['customCode'] = $customCode;
1773 + }
1316 1774 if (!is_wp_error($importResponse) || (is_wp_error($importResponse) && 'result_empty' === $importResponse->get_error_code())) {
1317 1775 $responseData = $this->getAForm(null, (object) ['id' => $newFormId]);
1776 + $responseData['customCode'] = $customCode;
1318 1777 $responseData['message'] = __('Form imported successfully.', 'bit-form');
1319 1778 return $responseData;
1320 1779 }
1321 1780 return $importResponse;
@@ -1342,9 +1801,9 @@
1342 1801 // $formData->formStyle = str_replace("-$oldFormId", "-$newFormId", $styleStr);
1343 1802 // $formData->layoutChanged = '-';
1344 1803 // $formData->rowHeight = FormDuplicateHelper::calcRowHeight($styleStr, $oldFormId);
1345 1804
1346 - // $formJson = json_encode($formData);
1805 + // $formJson = wp_json_encode($formData);
1347 1806 // header('Content-Type: application/force-download');
1348 1807 // header('Content-Type: application/octet-stream');
1349 1808 // header('Content-Type: application/download');
1350 1809 // header('Content-Disposition: attachment; filename="bitform_export_' . $oldFormId . '.json"');
@@ -1358,9 +1817,10 @@
1358 1817 // echo $formJson;
1359 1818 // die();
1360 1819 // }
1361 1820
1362 - public function exportAForm($Request, $post) {
1821 + public function exportAForm($Request, $post)
1822 + {
1363 1823 $oldFormId = intval($post->id);
1364 1824 if ($oldFormId < 1) {
1365 1825 return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1366 1826 }
@@ -1369,9 +1829,9 @@
1369 1829 return new WP_Error('empty_form', __('Form does not exists.', 'bit-form'));
1370 1830 }
1371 1831
1372 1832 $newFormId = $oldFormId;
1373 - $duplicatedForm = $this->getAForm($Request, (object)['id' => $oldFormId]);
1833 + $duplicatedForm = $this->getAForm($Request, (object) ['id' => $oldFormId]);
1374 1834 $formData = FormDuplicateHelper::createReplica($duplicatedForm, $oldFormId, $newFormId);
1375 1835 // for custom css
1376 1836 $customCssPath = 'form-styles/bitform-custom-' . $newFormId . '.css';
1377 1837 $cssPath = Helpers::generatePathDirOrFile($customCssPath);
@@ -1389,9 +1849,10 @@
1389 1849 }
1390 1850 return $formData;
1391 1851 }
1392 1852
1393 - public function getFormEntryLabelAndCount($Request, $post) {
1853 + public function getFormEntryLabelAndCount($Request, $post)
1854 + {
1394 1855 if (isset($Request['id'])) {
1395 1856 $id = wp_unslash($Request['id']);
1396 1857 } else {
1397 1858 $id = wp_unslash($post->id);
@@ -1446,9 +1907,10 @@
1446 1907 $response['fieldDetails'] = $labels;
1447 1908 return $response;
1448 1909 }
1449 1910
1450 - public function getFormEntry($Request, $post) {
1911 + public function getFormEntry($Request, $post)
1912 + {
1451 1913 if (isset($Request['id'])) {
1452 1914 $id = wp_unslash($Request['id']);
1453 1915 $offset = isset($Request['offset']) ?
1454 1916 wp_unslash($Request['offset']) : 0;
@@ -1453,8 +1915,9 @@
1453 1915 $offset = isset($Request['offset']) ?
1454 1916 wp_unslash($Request['offset']) : 0;
1455 1917 $pageSize = isset($Request['pageSize']) ?
1456 1918 wp_unslash($Request['pageSize']) : 10;
1919 + $queryCondition = isset($Request['queryCondition']) ? wp_unslash($Request['queryCondition']) : ['form_id' => $id];
1457 1920 } else {
1458 1921 $id = wp_unslash($post->id);
1459 1922 $conditions = isset($post->conditions) ? wp_unslash($post->conditions) : [];
1460 1923 $dateBetweenFilter = isset($post->entriesFilterByDate) ? wp_unslash($post->entriesFilterByDate) : [];
@@ -1463,8 +1926,9 @@
1463 1926 $filters = isset($post->filters) ? wp_unslash($post->filters) : null;
1464 1927 $globalFilter = isset($post->globalFilter) ? wp_unslash($post->globalFilter) : null;
1465 1928 $pageSize = isset($post->pageSize) ?
1466 1929 wp_unslash($post->pageSize) : 10;
1930 + $queryCondition = isset($post->queryCondition) ? wp_unslash($post->queryCondition) : ['form_id' => $id];
1467 1931 }
1468 1932 if (is_null($id)) {
1469 1933 return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1470 1934 }
@@ -1471,9 +1935,90 @@
1471 1935 $formManager = new AdminFormManager($id);
1472 1936 if (!$formManager->isExist()) {
1473 1937 return new WP_Error('empty_form', __('Form does not exists', 'bit-form'));
1474 1938 }
1939 + $formEntry = new FormEntryModel();
1940 + $entries = $formEntry->get(
1941 + 'id',
1942 + $queryCondition,
1943 + null,
1944 + null,
1945 + 'created_at',
1946 + 'DESC'
1947 + );
1948 + if (is_wp_error($entries)) {
1949 + if ('result_empty' === $entries->get_error_code()) {
1950 + return [
1951 + 'count' => 0,
1952 + 'entries' => [],
1953 + ];
1954 + }
1955 + return $entries;
1956 + }
1957 + $filter['field'] = $filters;
1958 + $filter['global'] = $globalFilter;
1475 1959
1960 + $formFields = $formManager->getFieldLabel(true);
1961 + $fieldDetails = $formManager->getFields();
1962 +
1963 + $entryMeta = new FormEntryMetaModel();
1964 + $formEntries = $entryMeta->getEntryMeta($formFields, $entries, $pageSize, $offset, $filter, $sortBy, $conditions, $dateBetweenFilter);
1965 + $customFieldHandler = new CustomFieldHandler();
1966 + $formEntries = $customFieldHandler->updatedEntries($formEntries, $fieldDetails);
1967 + return $formEntries;
1968 + }
1969 +
1970 + public function getSingleEntry($formId, $entryId)
1971 + {
1972 + $formManager = new AdminFormManager($formId);
1973 + if (!$formManager->isExist()) {
1974 + return new WP_Error('empty_form', __('Form does not exists', 'bit-form'));
1975 + }
1976 +
1977 + $formFields = $formManager->getFieldLabel(true);
1978 + $fieldDetails = $formManager->getFields();
1979 + $entryMeta = new FormEntryMetaModel();
1980 + // $formEntry = new FormEntryModel();
1981 +
1982 + $entries = $entryMeta->getSingleEntryMeta($formFields, $entryId);
1983 +
1984 + if (!is_wp_error($entries)) {
1985 + return $entries[0];
1986 + }
1987 + return new WP_Error('entry_not_fonud', __('Entry Not Found!', 'bit-form'));
1988 +
1989 + // $customFieldHandler = new CustomFieldHandler();
1990 + // $formEntries = $customFieldHandler->updatedEntries($formEntries, $fieldDetails);
1991 + // return $formEntries;
1992 + }
1993 +
1994 + public function getEntriesForReport($Request, $post)
1995 + {
1996 + if (isset($Request['id'])) {
1997 + $id = wp_unslash($Request['id']);
1998 + $offset = isset($Request['offset']) ?
1999 + wp_unslash($Request['offset']) : null;
2000 + $pageSize = isset($Request['pageSize']) ?
2001 + wp_unslash($Request['pageSize']) : null;
2002 + } else {
2003 + $id = wp_unslash($post->id);
2004 + $conditions = isset($post->conditions) ? wp_unslash($post->conditions) : [];
2005 + $dateBetweenFilter = isset($post->entriesFilterByDate) ? wp_unslash($post->entriesFilterByDate) : [];
2006 + $offset = isset($post->offset) ? wp_unslash($post->offset) : null;
2007 + $sortBy = isset($post->sortBy) ? wp_unslash($post->sortBy) : null;
2008 + $filters = isset($post->filters) ? wp_unslash($post->filters) : null;
2009 + $globalFilter = isset($post->globalFilter) ? wp_unslash($post->globalFilter) : null;
2010 + $pageSize = isset($post->pageSize) ?
2011 + wp_unslash($post->pageSize) : null;
2012 + }
2013 + if (is_null($id)) {
2014 + return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
2015 + }
2016 + $formManager = new AdminFormManager($id);
2017 + if (!$formManager->isExist()) {
2018 + return new WP_Error('empty_form', __('Form does not exists', 'bit-form'));
2019 + }
2020 +
1476 2021 $formEntry = new FormEntryModel();
1477 2022 $entries = $formEntry->get(
1478 2023 'id',
1479 2024 [
@@ -1502,185 +2047,210 @@
1502 2047 $entryMeta = new FormEntryMetaModel();
1503 2048 $formEntries = $entryMeta->getEntryMeta($formFields, $entries, $pageSize, $offset, $filter, $sortBy, $conditions, $dateBetweenFilter);
1504 2049 $customFieldHandler = new CustomFieldHandler();
1505 2050 $formEntries = $customFieldHandler->updatedEntries($formEntries, $fieldDetails);
2051 + $formEntries['entries'] = Helpers::filterNullEntries($formEntries['entries']);
1506 2052 return $formEntries;
1507 2053 }
1508 2054
1509 - public function getExportEntry($post) {
1510 - $formId = wp_unslash($post->formId);
1511 - $fileFormate = isset($post->fileFormate) ? wp_unslash($post->fileFormate) : null;
1512 - $limit = isset($post->limit) ? wp_unslash($post->limit) : null;
1513 - $sortBy = isset($post->sort) ? wp_unslash($post->sort) : 'ASC';
1514 - $sortByField = isset($post->sortField) ? wp_unslash($post->sortField) : 'bitforms_form_entry_id';
1515 - $formFields = json_decode($post->selectedField);
1516 - if (is_null($formId)) {
1517 - return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
2055 + public function getExportEntry($post)
2056 + {
2057 + $formId = wp_unslash($post->formId);
2058 + $fileFormate = isset($post->fileFormate) ? wp_unslash($post->fileFormate) : null;
2059 + $limit = isset($post->limit) ? wp_unslash($post->limit) : null;
2060 + $entryCondition = isset($post->filter) ? wp_unslash($post->filter) : null;
2061 + $sortBy = isset($post->sort) ? wp_unslash($post->sort) : 'ASC';
2062 + $sortByField = isset($post->sortField) ? wp_unslash($post->sortField) : 'bitforms_form_entry_id';
2063 + $formFields = json_decode($post->selectedField);
2064 + if (is_null($formId)) {
2065 + return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
2066 + }
2067 + $formManager = new AdminFormManager($formId);
2068 + $fieldLabels = $formManager->getFieldLabel(true);
2069 + if (!$formManager->isExist()) {
2070 + return new WP_Error('empty_form', __('Form does not exists', 'bit-form'));
2071 + }
2072 +
2073 + $formEntry = new FormEntryModel();
2074 + $entries = $formEntry->get(
2075 + 'id',
2076 + [
2077 + 'form_id' => $formId,
2078 + ],
2079 + null,
2080 + null,
2081 + 'created_at',
2082 + 'DESC'
2083 + );
2084 + if (is_wp_error($entries)) {
2085 + if ('result_empty' === $entries->get_error_code()) {
2086 + return [
2087 + 'count' => 0,
2088 + 'entries' => [],
2089 + ];
1518 2090 }
1519 - $formManager = new AdminFormManager($formId);
1520 - $fieldLabels = $formManager->getFieldLabel(true);
1521 - if (!$formManager->isExist()) {
1522 - return new WP_Error('empty_form', __('Form does not exists', 'bit-form'));
1523 - }
2091 + return $entries;
2092 + }
2093 + $entryMeta = new FormEntryMetaModel();
2094 + $filter = [];
2095 + $formEntries = $entryMeta->getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit, $sortBy, $sortByField, $offset, $entryCondition);
2096 + return $formEntries;
2097 + }
1524 2098
1525 - $formEntry = new FormEntryModel();
1526 - $entries = $formEntry->get(
1527 - 'id',
1528 - [
1529 - 'form_id' => $formId,
1530 - ],
1531 - null,
1532 - null,
1533 - 'created_at',
1534 - 'DESC'
1535 - );
1536 - if (is_wp_error($entries)) {
1537 - if ('result_empty' === $entries->get_error_code()) {
1538 - return [
1539 - 'count' => 0,
1540 - 'entries' => [],
1541 - ];
1542 - }
1543 - return $entries;
1544 - }
1545 - $entryMeta = new FormEntryMetaModel();
1546 - $filter = [];
1547 - $formEntries = $entryMeta->getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit, $sortBy, $sortByField);
1548 - return $formEntries;
2099 + public function deleteBlukFormEntries($Request, $post)
2100 + {
2101 + if (isset($Request['formID'])) {
2102 + $formID = wp_unslash($Request['formID']);
2103 + $entries = wp_unslash($Request['entries']);
2104 + } else {
2105 + $formID = wp_unslash($post->formID);
2106 + $entries = wp_unslash($post->entries);
1549 2107 }
2108 + if (is_null($formID) || !is_array($entries) || 0 === count($entries)) {
2109 + return new WP_Error('empty_form', __('Invalid Form ID or Entries ID.', 'bit-form'));
2110 + }
1550 2111
1551 - public function deleteBlukFormEntries($Request, $post) {
1552 - if (isset($Request['formID'])) {
1553 - $formID = wp_unslash($Request['formID']);
1554 - $entries = wp_unslash($Request['entries']);
2112 + if (!filter_var($formID, FILTER_VALIDATE_INT)) {
2113 + return new WP_Error('invalid_form', __('Form id is invalid.', 'bit-form'));
2114 + }
2115 +
2116 + if (!Helpers::checkIsIntArr($entries)) {
2117 + return new WP_Error('invalid_entries', __('Entries id is invalid', 'bit-form'));
2118 + }
2119 +
2120 + $formManager = new AdminFormManager($formID);
2121 + if (!$formManager->isExist()) {
2122 + return new WP_Error('empty_form', __('Form does not exist.', 'bit-form'));
2123 + }
2124 + $workFlowRunHelper = new WorkFlow($formID);
2125 +
2126 + $workFlowreturnedOnDelete = $workFlowRunHelper->executeOnDelete(
2127 + $formManager,
2128 + $formID,
2129 + $entries
2130 + );
2131 +
2132 + if (isset($workFlowreturnedOnDelete['entries'])) {
2133 + if (0 === count($workFlowreturnedOnDelete['entries'])) {
2134 + return ['message' => __('Entry Deletetion prevented by workflow', 'bit-form')];
2135 + } elseif (count($workFlowreturnedOnDelete['entries']) === count($entries)) {
2136 + $message = __('Entry Deleted successfully', 'bit-form');
1555 2137 } else {
1556 - $formID = wp_unslash($post->formID);
1557 - $entries = wp_unslash($post->entries);
2138 + $result['prevented'] = array_diff($entries, $workFlowreturnedOnDelete['entries']);
2139 + $entries = $workFlowreturnedOnDelete['entries'];
2140 + $message = __('Entry Deleted successfully, Some prevented by workflow', 'bit-form');
1558 2141 }
1559 - if (is_null($formID) || !is_array($entries) || 0 === count($entries)) {
1560 - return new WP_Error('empty_form', __('Invalid Form ID or Entries ID.', 'bit-form'));
1561 - }
1562 - $formManager = new AdminFormManager($formID);
1563 - if (!$formManager->isExist()) {
1564 - return new WP_Error('empty_form', __('Form does not exist.', 'bit-form'));
1565 - }
1566 - $workFlowRunHelper = new WorkFlowRunHelper($formID);
1567 - $workFlowreturnedOnDelete = $workFlowRunHelper->executeOnDelete(
1568 - $formManager,
1569 - $formID,
1570 - $entries
1571 - );
1572 - if (isset($workFlowreturnedOnDelete['entries'])) {
1573 - if (0 === count($workFlowreturnedOnDelete['entries'])) {
1574 - return ['message' => __('Entry Deletetion prevented by workflow', 'bit-form')];
1575 - } elseif (count($workFlowreturnedOnDelete['entries']) === count($entries)) {
1576 - $message = __('Entry Deleted successfully', 'bit-form');
1577 - } else {
1578 - $result['prevented'] = array_diff($entries, $workFlowreturnedOnDelete['entries']);
1579 - $entries = $workFlowreturnedOnDelete['entries'];
1580 - $message = __('Entry Deleted successfully, Some prevented by workflow', 'bit-form');
2142 + } else {
2143 + $message = __('Entry Deleted successfully', 'bit-form');
2144 + }
2145 + global $wpdb;
2146 + $prefix = $wpdb->prefix;
2147 + $formEntryModel = new FormEntryModel();
2148 + $delete_status = $formEntryModel->bulkDelete(
2149 + [
2150 + "`{$prefix}bitforms_form_entries`.`id`" => $entries,
2151 + "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
2152 + ]
2153 + );
2154 + if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
2155 + $fileHandler = new FileHandler();
2156 + foreach ($entries as $enrtyKey => $entryID) {
2157 + $fileEntries = $fileHandler->getEntriesFileUploadDir($formID, $entryID);
2158 + if (file_exists($fileEntries)) {
2159 + $fileHandler->rmrf($fileEntries);
1581 2160 }
1582 - } else {
1583 - $message = __('Entry Deleted successfully', 'bit-form');
1584 2161 }
1585 - global $wpdb;
1586 - $prefix = $wpdb->prefix;
1587 - $formEntryModel = new FormEntryModel();
1588 - $delete_status = $formEntryModel->bulkDelete(
2162 + }
2163 + $count = $formEntryModel->count(
2164 + [
2165 + 'form_id' => $formID,
2166 + ]
2167 + );
2168 + $formManager->resetSubmissionCount(intval($count[0]->count));
2169 + if (is_wp_error($delete_status)) {
2170 + return new WP_Error('entry_not_exists', __('Form entry deletion failed.', 'bit-form'));
2171 + }
2172 + $result['message'] = $message;
2173 + return $result;
2174 + }
2175 +
2176 + public function duplicateFormEntry($Request, $post)
2177 + {
2178 + if (isset($Request['formID'])) {
2179 + $formID = wp_unslash($Request['formID']);
2180 + $entries = wp_unslash($Request['entries']);
2181 + } else {
2182 + $formID = wp_unslash($post->formID);
2183 + $entries = wp_unslash($post->entries);
2184 + }
2185 + if (is_null($formID) || !is_array($entries) || empty($entries)) {
2186 + return new WP_Error('empty_form', __('Form id or entries id is invalid', 'bit-form'));
2187 + }
2188 +
2189 + if (!filter_var($formID, FILTER_VALIDATE_INT)) {
2190 + return new WP_Error('invalid_form', __('Form id is invalid', 'bit-form'));
2191 + }
2192 +
2193 + if (!Helpers::checkIsIntArr($entries)) {
2194 + return new WP_Error('invalid_entries', __('Entries id is invalid', 'bit-form'));
2195 + }
2196 +
2197 + $formManager = new AdminFormManager($formID);
2198 + if (!$formManager->isExist()) {
2199 + return new WP_Error('empty_form', __('Form does not exist', 'bit-form'));
2200 + }
2201 + $formEntryModel = new FormEntryModel();
2202 + $entryMeta = new FormEntryMetaModel();
2203 + $user_details = static::$ipTool->getUserDetail();
2204 + $total_entries = count($entries);
2205 + $duplicate_count = 0;
2206 + $test = '';
2207 + $fileHandler = new FileHandler();
2208 + $result = [];
2209 + foreach ($entries as $entryIndex => $entryID) {
2210 + $duplicatedEntryId = $formEntryModel->insert(
1589 2211 [
1590 - "`{$prefix}bitforms_form_entries`.`id`" => $entries,
1591 - "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
2212 + 'form_id' => $formID,
2213 + 'user_id' => $user_details['id'],
2214 + 'user_ip' => $user_details['ip'],
2215 + 'user_device' => $user_details['device'],
2216 + 'referer' => 'duplicate of #' . $entryID,
2217 + 'status' => 1,
2218 + 'created_at' => $user_details['time'],
1592 2219 ]
1593 2220 );
1594 - if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
1595 - $fileHandler = new FileHandler();
1596 - foreach ($entries as $enrtyKey => $entryID) {
1597 - $fileEntries = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID;
1598 - if (file_exists($fileEntries)) {
1599 - $fileHandler->rmrf($fileEntries);
1600 - }
1601 - }
1602 - }
1603 - $count = $formEntryModel->count(
1604 - [
1605 - 'form_id' => $formID,
1606 - ]
1607 - );
1608 - $formManager->resetSubmissionCount(intval($count[0]->count));
1609 - if (is_wp_error($delete_status)) {
1610 - return new WP_Error('entry_not_exists', __('Form entry deletion failed.', 'bit-form'));
1611 - }
1612 - $result['message'] = $message;
1613 - return $result;
1614 - }
1615 -
1616 - public function duplicateFormEntry($Request, $post) {
1617 - if (isset($Request['formID'])) {
1618 - $formID = wp_unslash($Request['formID']);
1619 - $entries = wp_unslash($Request['entries']);
1620 - } else {
1621 - $formID = wp_unslash($post->formID);
1622 - $entries = wp_unslash($post->entries);
1623 - }
1624 - if (is_null($formID) || !is_array($entries) || empty($entries)) {
1625 - return new WP_Error('empty_form', __('Form id or entries id is invalid', 'bit-form'));
1626 - }
1627 -
1628 - $formManager = new AdminFormManager($formID);
1629 - if (!$formManager->isExist()) {
1630 - return new WP_Error('empty_form', __('Form does not exist', 'bit-form'));
1631 - }
1632 - $formEntryModel = new FormEntryModel();
1633 - $entryMeta = new FormEntryMetaModel();
1634 - $user_details = static::$ipTool->getUserDetail();
1635 - $total_entries = count($entries);
1636 - $duplicate_count = 0;
1637 - $test = '';
1638 - $fileHandler = new FileHandler();
1639 - $result = [];
1640 - foreach ($entries as $entryIndex => $entryID) {
1641 - $duplicatedEntryId = $formEntryModel->insert(
2221 + if ($duplicatedEntryId) {
2222 + $duplicate_status = $entryMeta->duplicateEntryMeta(
1642 2223 [
1643 - 'form_id' => $formID,
1644 - 'user_id' => $user_details['id'],
1645 - 'user_ip' => $user_details['ip'],
1646 - 'user_device' => $user_details['device'],
1647 - 'referer' => 'duplicate of #' . $entryID,
1648 - 'status' => 1,
1649 - 'created_at' => $user_details['time'],
2224 + 'duplicateID' => $duplicatedEntryId,
2225 + 'entryID' => $entryID,
1650 2226 ]
1651 2227 );
1652 - if ($duplicatedEntryId) {
1653 - $duplicate_status = $entryMeta->duplicateEntryMeta(
1654 - [
1655 - 'duplicateID' => $duplicatedEntryId,
1656 - 'entryID' => $entryID,
1657 - ]
1658 - );
1659 - if ($duplicate_status) {
1660 - $result['details'][$entryID] = $duplicatedEntryId;
1661 - $duplicate_count = $duplicate_count + 1;
1662 - if (file_exists(BITFORMS_UPLOAD_DIR . "/$formID/$entryID")) {
1663 - $fileHandler->cpyr(
1664 - BITFORMS_UPLOAD_DIR . "/$formID/$entryID",
1665 - BITFORMS_UPLOAD_DIR . "/$formID/$duplicatedEntryId"
1666 - );
1667 - }
2228 + if ($duplicate_status) {
2229 + $result['details'][$entryID] = $duplicatedEntryId;
2230 + $duplicate_count = $duplicate_count + 1;
2231 + if (file_exists(FileHandler::getEntriesFileUploadDir($formID, $entryID))) {
2232 + $fileHandler->cpyr(
2233 + FileHandler::getEntriesFileUploadDir($formID, $entryID),
2234 + FileHandler::getEntriesFileUploadDir($formID, $duplicatedEntryId)
2235 + );
1668 2236 }
1669 2237 }
1670 2238 }
2239 + }
1671 2240
1672 - $count = $formEntryModel->count(
1673 - [
1674 - 'form_id' => $formID,
1675 - ]
1676 - );
1677 - $formManager->resetSubmissionCount(intval($count[0]->count));
1678 - $result['message'] = 1 === count($entries) ? __('Entry Duplicated successfully', 'bit-form') : __('Entries Duplicated successfully', 'bit-form');
1679 - return ($total_entries === $duplicate_count) ? $result : false;
1680 - }
2241 + $count = $formEntryModel->count(
2242 + [
2243 + 'form_id' => $formID,
2244 + ]
2245 + );
2246 + $formManager->resetSubmissionCount(intval($count[0]->count));
2247 + $result['message'] = 1 === count($entries) ? __('Entry Duplicated successfully', 'bit-form') : __('Entries Duplicated successfully', 'bit-form');
2248 + return ($total_entries === $duplicate_count) ? $result : false;
2249 + }
1681 2250
1682 - public function editFormEntry($Request, $post) {
2251 + public function editFormEntry($Request, $post)
2252 + {
1683 2253 if (isset($Request['formID'])) {
1684 2254 $formID = wp_unslash($Request['formID']);
1685 2255 $entryID = wp_unslash($Request['entryID']);
1686 2256 } else {
@@ -1732,9 +2302,9 @@
1732 2302 $form_fields->{$key}->val = $entries[$key];
1733 2303 $form_fields->{$key}->name = $key;
1734 2304 }
1735 2305 }
1736 - $workFlowRunHelper = new WorkFlowRunHelper($formID);
2306 + $workFlowRunHelper = new WorkFlow($formID);
1737 2307 $workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad(
1738 2308 'edit',
1739 2309 $form_fields
1740 2310 );
@@ -1755,16 +2325,18 @@
1755 2325 ];
1756 2326 return $formData;
1757 2327 }
1758 2328
1759 - public function updateFormEntry($Request, $post) {
2329 + public function updateFormEntry($Request, $post)
2330 + {
1760 2331 if (isset($Request['formID'], $Request['entryID'])) {
1761 2332 $formID = wp_unslash($Request['formID']);
1762 2333 $entryID = wp_unslash($Request['entryID']);
1763 2334 unset($Request['action'], $Request['formID'], $Request['entryID']);
1764 -
1765 - $updatedValue = wp_unslash($post);
1766 2335 }
2336 + $formManager = new AdminFormManager($formID);
2337 + $formManager->fieldNameReplaceOfPost();
2338 + $updatedValue = wp_unslash($_POST);
1767 2339
1768 2340 if (is_null($updatedValue) || !is_array($updatedValue)) {
1769 2341 return new WP_Error('empty_data', __('Failed to update, Data is empty.', 'bit-form'));
1770 2342 }
@@ -1772,9 +2344,8 @@
1772 2344 if (is_null($formID) || is_null($entryID)) {
1773 2345 return new WP_Error('empty_data', __('Invalid Form ID or entries ID.', 'bit-form'));
1774 2346 }
1775 2347
1776 - $formManager = new AdminFormManager($formID);
1777 2348 if (!$formManager->isExist()) {
1778 2349 return new WP_Error('empty_data', __('Form does not exist.', 'bit-form'));
1779 2350 }
1780 2351 return $formManager->updateFormEntry($updatedValue, $formID, $entryID);
@@ -1779,9 +2350,10 @@
1779 2350 }
1780 2351 return $formManager->updateFormEntry($updatedValue, $formID, $entryID);
1781 2352 }
1782 2353
1783 - public function getLogHistory($Request, $post) {
2354 + public function getLogHistory($Request, $post)
2355 + {
1784 2356 if (isset($Request['formID'])) {
1785 2357 $formID = wp_unslash($Request['formID']);
1786 2358 $entryID = wp_unslash($Request['entryID']);
1787 2359 } else {
@@ -1802,9 +2374,10 @@
1802 2374 $log_history = $formLogModel->geLogHistory($formID, $entryID);
1803 2375 return $log_history;
1804 2376 }
1805 2377
1806 - public function importDataStore($Request, $post) {
2378 + public function importDataStore($Request, $post)
2379 + {
1807 2380 if (isset($Request['formID'])) {
1808 2381 $formID = wp_unslash($Request['formID']);
1809 2382 } else {
1810 2383 $formID = wp_unslash($post->formID);
@@ -1813,19 +2386,25 @@
1813 2386 return new WP_Error('empty_form', __('Invalid Form id or entries id.', 'bit-form'));
1814 2387 }
1815 2388 }
1816 2389
1817 - public function getAllWPPages($Request, $post) {
2390 + public function getAllWPPages($Request, $post)
2391 + {
1818 2392 $pages = get_pages(['post_status' => 'publish', 'sort_column' => 'post_date', 'sort_order' => 'desc']);
1819 2393 $allPages = [];
1820 2394 foreach ($pages as $pageKey => $pageDetails) {
1821 - $allPages[$pageKey]['title'] = $pageDetails->post_title;
1822 - $allPages[$pageKey]['url'] = get_page_link($pageDetails->ID);
2395 + // $allPages[$pageKey]['title'] = $pageDetails->post_title;
2396 + // $allPages[$pageKey]['url'] = get_page_link($pageDetails->ID);
2397 + $allPages[] = (object) [
2398 + 'title' => $pageDetails->post_title,
2399 + 'url' => get_page_link($pageDetails->ID)
2400 + ];
1823 2401 }
1824 2402 return $allPages;
1825 2403 }
1826 2404
1827 - public function deleteAIntegration($Request, $post) {
2405 + public function deleteAIntegration($Request, $post)
2406 + {
1828 2407 if (isset($Request['formID']) && $Request['id']) {
1829 2408 $formID = json_decode(wp_unslash($Request['formID']));
1830 2409 $id = wp_unslash($Request['id']);
1831 2410 } else {
@@ -1846,9 +2425,10 @@
1846 2425 'message' => __('Integration deleted', 'bit-form'),
1847 2426 ];
1848 2427 }
1849 2428
1850 - public function deleteSuccessMessage($Request, $post) {
2429 + public function deleteSuccessMessage($Request, $post)
2430 + {
1851 2431 if (isset($Request['formID']) && $Request['id']) {
1852 2432 $formID = json_decode(wp_unslash($Request['formID']));
1853 2433 $id = wp_unslash($Request['id']);
1854 2434 } else {
@@ -1868,9 +2448,10 @@
1868 2448 'message' => __('Message deleted', 'bit-form'),
1869 2449 ];
1870 2450 }
1871 2451
1872 - public function deleteAWorkflow($Request, $post) {
2452 + public function deleteAWorkflow($Request, $post)
2453 + {
1873 2454 if (isset($Request['formID']) && $Request['id']) {
1874 2455 $formID = json_decode(wp_unslash($Request['formID']));
1875 2456 $id = wp_unslash($Request['id']);
1876 2457 } else {
@@ -1889,9 +2470,10 @@
1889 2470 'message' => __('workflow deleted', 'bit-form'),
1890 2471 ];
1891 2472 }
1892 2473
1893 - public function deleteAMailTemplate($Request, $post) {
2474 + public function deleteAMailTemplate($Request, $post)
2475 + {
1894 2476 if (isset($Request['formID']) && $Request['id']) {
1895 2477 $formID = json_decode(wp_unslash($Request['formID']));
1896 2478 $id = wp_unslash($Request['id']);
1897 2479 } else {
@@ -1910,9 +2492,10 @@
1910 2492 'message' => __('Email Template deleted', 'bit-form'),
1911 2493 ];
1912 2494 }
1913 2495
1914 - public function duplicateAMailTemplate($Request, $post) {
2496 + public function duplicateAMailTemplate($Request, $post)
2497 + {
1915 2498 if (isset($Request['formID']) && $Request['id']) {
1916 2499 $formID = json_decode(wp_unslash($Request['formID']));
1917 2500 $id = wp_unslash($Request['id']);
1918 2501 } else {
@@ -1921,8 +2504,17 @@
1921 2504 }
1922 2505 if (empty($formID) || empty($id)) {
1923 2506 return new WP_Error('empty_form', 'Invalid Form ID or Email Template ID.');
1924 2507 }
2508 +
2509 + if (false === filter_var($id, FILTER_VALIDATE_INT)) {
2510 + return new WP_Error('empty_form', 'Invalid Email Template ID.');
2511 + }
2512 +
2513 + if (false === filter_var($formID, FILTER_VALIDATE_INT)) {
2514 + return new WP_Error('empty_form', 'Invalid Form ID.');
2515 + }
2516 +
1925 2517 $emailTemplateHandler = new EmailTemplateHandler($formID);
1926 2518 $duplicate_status = $emailTemplateHandler->duplicateTemplate($id);
1927 2519 if (is_wp_error($duplicate_status)) {
1928 2520 return $duplicate_status;
@@ -1931,9 +2523,10 @@
1931 2523 'message' => __('Email Template duplicated', 'bit-form'),
1932 2524 ];
1933 2525 }
1934 2526
1935 - public function setAllFormsReport($Request, $post) {
2527 + public function setAllFormsReport($Request, $post)
2528 + {
1936 2529 $reports = empty($post->reports) ? null : wp_unslash($post->reports);
1937 2530 if (empty($reports)) {
1938 2531 return new WP_Error('empty_report_prefs', 'Report data is Empty, nothing to save or update.');
1939 2532 }
@@ -2009,9 +2602,10 @@
2009 2602 'reports' => empty($reports) ? [] : $reports,
2010 2603 ];
2011 2604 }
2012 2605
2013 - public function savegReCaptcha($Request, $post) {
2606 + public function savegReCaptcha($Request, $post)
2607 + {
2014 2608 $reCaptcha = $post->reCaptcha;
2015 2609
2016 2610 if (is_null($reCaptcha)) {
2017 2611 return new WP_Error('empty_gcaptchdetails', __('g-ReCAPTCHA details is empty', 'bit-form'));
@@ -2023,11 +2617,16 @@
2023 2617 } else {
2024 2618 $integrationID = $reCaptcha->id;
2025 2619 unset($reCaptcha->id);
2026 2620 }
2027 - $reCaptcha = json_encode($reCaptcha);
2028 - $integrationName = 'google reCaptcha';
2029 - $integrationType = 'v2' === $post->version ? 'gReCaptcha' : 'gReCaptchaV3';
2621 + $reCaptcha = wp_json_encode($reCaptcha);
2622 +
2623 + // $integrationName = 'google reCaptcha';
2624 + $integrationName = $post->integrationName;
2625 +
2626 + // $integrationType = 'v2' === $post->version ? 'gReCaptcha' : 'gReCaptchaV3';
2627 + $integrationType = $post->integrationType;
2628 +
2030 2629 $integrationDetails = $reCaptcha;
2031 2630 $user_details = static::$ipTool->getUserDetail();
2032 2631 $integrationHandler = new IntegrationHandler(0, $user_details);
2033 2632 $response = [];
@@ -2033,12 +2632,12 @@
2033 2632 $response = [];
2034 2633 if (empty($integrationID)) {
2035 2634 $captchaSaveStatus = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'app');
2036 2635 $response['id'] = $captchaSaveStatus;
2037 - $response['message'] = __('g-reCAPTCHA saved successfully', 'bit-form');
2636 + $response['message'] = __('reCAPTCHA saved successfully', 'bit-form');
2038 2637 } else {
2039 2638 $captchaSaveStatus = $integrationHandler->updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, 'app');
2040 - $response['message'] = __('g-reCAPTCHA updated successfully', 'bit-form');
2639 + $response['message'] = __('reCAPTCHA updated successfully', 'bit-form');
2041 2640 }
2042 2641
2043 2642 if (is_wp_error($captchaSaveStatus)) {
2044 2643 return $captchaSaveStatus;
@@ -2045,9 +2644,10 @@
2045 2644 }
2046 2645 return $response;
2047 2646 }
2048 2647
2049 - public function savePaymentSetting($Request, $post) {
2648 + public function savePaymentSetting($Request, $post)
2649 + {
2050 2650 if (isset($post->paySetting)) {
2051 2651 $paySetting = $post->paySetting;
2052 2652 }
2053 2653 if (is_null($paySetting)) {
@@ -2055,21 +2655,37 @@
2055 2655 }
2056 2656 if (is_string($paySetting)) {
2057 2657 $paySetting = json_decode($paySetting);
2058 2658 }
2059 - if ('PayPal' === $paySetting->type) {
2060 - $canSave = $this->addWebhookToPaypal($paySetting);
2061 - } elseif ('Razorpay' === $paySetting->type) {
2062 - $canSave = true;
2659 +
2660 + $canSave = null;
2661 + switch ($paySetting->type) {
2662 + case 'PayPal':
2663 + $canSave = $this->addWebhookToPaypal($paySetting);
2664 + break;
2665 + case 'Razorpay':
2666 + $canSave = true;
2667 + break;
2668 + case 'Stripe':
2669 + $canSave = $this->addWebhookToStripe($paySetting);
2670 + break;
2671 + case 'Mollie':
2672 + $canSave = true;
2673 + break;
2674 + default:
2675 + $canSave = null;
2063 2676 }
2064 2677 if (is_null($canSave)) {
2065 - return ['message' => __('Please re-check your Client ID & Secret', 'bit-form')];
2678 + return new WP_Error('stripe', __('Please re-check your Client ID & Secret', 'bit-form'));
2066 2679 }
2067 - $integrationID = $paySetting->id;
2068 - unset($paySetting->id);
2680 + $integrationID = null;
2681 + if (isset($paySetting->id)) {
2682 + $integrationID = $paySetting->id;
2683 + unset($paySetting->id);
2684 + }
2069 2685 $integrationName = $paySetting->name;
2070 2686 $integrationType = 'payments';
2071 - $paySetting = json_encode($paySetting);
2687 + $paySetting = wp_json_encode($paySetting);
2072 2688 $integrationDetails = $paySetting;
2073 2689 $user_details = static::$ipTool->getUserDetail();
2074 2690 $integrationHandler = new IntegrationHandler(0, $user_details);
2075 2691 $response = [];
@@ -2087,9 +2703,10 @@
2087 2703 }
2088 2704 return $response;
2089 2705 }
2090 2706
2091 - private function addWebhookToPaypal($paySetting) {
2707 + private function addWebhookToPaypal($paySetting)
2708 + {
2092 2709 $transaction_mode = $paySetting->mode;
2093 2710 $clientId = $paySetting->clientID;
2094 2711 $clientSecret = $paySetting->clientSecret;
2095 2712 $base_url = 'sandbox' === $transaction_mode ? 'https://api-m.sandbox.paypal.com' : 'https://api.paypal.com';
@@ -2115,9 +2732,51 @@
2115 2732 }
2116 2733 return true;
2117 2734 }
2118 2735
2119 - public function builerHelperState($formId) {
2736 + private function addWebhookToStripe($paySetting)
2737 + {
2738 + $clientSecret = $paySetting->clientSecret;
2739 +
2740 + $create_webhook_url = 'https://api.stripe.com/v1/webhook_endpoints';
2741 + $headers = [
2742 + 'Content-Type' => 'application/x-www-form-urlencoded',
2743 + 'Authorization' => "Bearer $clientSecret",
2744 + ];
2745 +
2746 + $webhook_url = get_rest_url() . 'bitform/v1/payments/stripe';
2747 +
2748 + $apiResponse = HttpHelper::get($create_webhook_url, '', $headers);
2749 +
2750 + if (isset($apiResponse->error)) {
2751 + return null;
2752 + }
2753 + $webhookList = $apiResponse->data;
2754 + $webhook_already_exist = false;
2755 +
2756 + foreach ($webhookList as $webhook) {
2757 + if ($webhook->url === $webhook_url) {
2758 + $webhook_already_exist = true;
2759 + break;
2760 + }
2761 + }
2762 +
2763 + $data = [
2764 + 'enabled_events[]' => 'payment_intent.succeeded',
2765 + 'url' => $webhook_url,
2766 + ];
2767 +
2768 + if (!$webhook_already_exist) {
2769 + $webhook_response = HttpHelper::post($create_webhook_url, $data, $headers);
2770 + if (is_wp_error($webhook_response) || (isset($webhook_response->error) && $webhook_response->error)) {
2771 + return null;
2772 + }
2773 + }
2774 + return true;
2775 + }
2776 +
2777 + public function builerHelperState($formId)
2778 + {
2120 2779 $data = static::$formModel->get(
2121 2780 ['id', 'builder_helper_state'],
2122 2781 ['id' => $formId]
2123 2782 );
@@ -2132,9 +2791,10 @@
2132 2791
2133 2792 return $data;
2134 2793 }
2135 2794
2136 - public function updateGeneratedScriptPageIds() {
2795 + public function updateGeneratedScriptPageIds()
2796 + {
2137 2797 $updateData = [
2138 2798 'generated_script_page_ids' => \wp_json_encode((object) []),
2139 2799 ];
2140 2800 $update_status = static::$formModel->update(
@@ -2151,9 +2811,67 @@
2151 2811 return true;
2152 2812 }
2153 2813 }
2154 2814
2155 - public function updateSuccessMessageClassName($str, $msgIdx, $msgId) {
2815 + public function replaceAllFormsErrorMessagesByGlobalMessages()
2816 + {
2817 + try {
2818 + $formModel = new FormModel();
2819 + $allForms = $formModel->get(['id', 'form_content']);
2820 +
2821 + foreach ($allForms as $form) {
2822 + // Decode form_content JSON to stdClass
2823 + $formContent = \json_decode($form->form_content);
2824 + $fields = $formContent->fields ?? null;
2825 + // If decode fails, skip to next
2826 + if (!$fields) {
2827 + Log::debug_log("Form ID {$form->id}: invalid JSON in form_content");
2828 + continue;
2829 + }
2830 +
2831 + // Replace default error messages using your helper
2832 + $updatedFields = Helpers::replaceFieldsDefaultErrorMsg($fields);
2833 +
2834 + $formContent->fields = $updatedFields;
2835 + // Encode back to JSON
2836 + $updatedContent = \json_encode($formContent);
2837 +
2838 + if (false === $updatedContent) {
2839 + Log::debug_log("Form ID {$form->id}: JSON encoding failed");
2840 + continue;
2841 + }
2842 + // Update form content in DB
2843 + $formModel->update(
2844 + ['form_content' => $updatedContent], // data to update
2845 + ['id' => $form->id] // condition for update
2846 + );
2847 + }
2848 + } catch (\Exception $e) {
2849 + Log::debug_log('Error in updateAllFormsErrorMessagesWithGlobalMessages', $e->getMessage());
2850 + }
2851 + }
2852 +
2853 + public function updateSuccessMessageClassName($str, $msgIdx, $msgId)
2854 + {
2156 2855 $TEMP_CONF_ID = '_tmp_' . $msgIdx . '_conf_id';
2157 2856 return str_replace($TEMP_CONF_ID, $msgId, $str);
2857 + }
2858 +
2859 + public function startMigrationProcess()
2860 + {
2861 + Utilities::duplicateDbTable('form', 'form_v1');
2862 + Utilities::duplicateDbTable('workflows', 'workflows_v1');
2863 + Utilities::duplicateDbTable('success_messages', 'success_messages_v1');
2864 + MigrationHelper::duplicateV1StyleFiles();
2865 + $all_forms = $this->getAllForm();
2866 + $v1FormContents = [];
2867 + $defaultStyleClass = MigrationHelper::getBitformDefaultStyleClass();
2868 + foreach ($all_forms as $form) {
2869 + $formContents = $this->getAFormV1(['id' => $form->id], ['id' => $form->id]);
2870 + $v1FormContents[] = $formContents;
2871 + FrontEndScriptGenerator::saveCssFile("{$form->id}", $defaultStyleClass);
2872 + FrontEndScriptGenerator::saveCssFile("{$form->id}-formid", $defaultStyleClass);
2873 + }
2874 + set_transient('bitforms_v1_form_contents', $v1FormContents);
2875 + update_option('bitforms_migrating_to_v2', true);
2158 2876 }
2159 2877 }