| @@ -13,15 +13,14 @@ | ||
| 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; | |
| 18 | 17 | use BitCode\BitForm\Core\Database\ReportsModel; |
| 19 | 18 | use BitCode\BitForm\Core\Database\SuccessMessageModel; |
| 20 | 19 | use BitCode\BitForm\Core\Database\WorkFlowModel; |
| 20 | +use BitCode\BitForm\Core\Form\FormManager; | |
| 21 | 21 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 22 | 22 | use BitCode\BitForm\Core\Messages\EmailTemplateHandler; |
| 23 | -use BitCode\BitForm\Core\Messages\PdfTemplateHandler; | |
| 24 | 23 | use BitCode\BitForm\Core\Messages\SuccessMessageHandler; |
| 25 | 24 | use BitCode\BitForm\Core\Migration\MigrationHelper; |
| 26 | 25 | use BitCode\BitForm\Core\Util\FileHandler; |
| 27 | 26 | use BitCode\BitForm\Core\Util\FormDuplicateHelper; |
| @@ -87,9 +86,9 @@ | ||
| 87 | 86 | $sm_width = 400; |
| 88 | 87 | $lg_styles = ''; |
| 89 | 88 | $md_styles = ''; |
| 90 | 89 | $sm_styles = ''; |
| 91 | - for ($i = 0; $i < count($layout->lg); $i++) { | |
| 90 | + for ($i = 0; $i < count($layout->lg ?? []); $i++) { | |
| 92 | 91 | $fld = $layout->lg[$i]; |
| 93 | 92 | |
| 94 | 93 | if ($fld->w < 10) { |
| 95 | 94 | $lay_row_height = 43; |
| @@ -241,8 +240,12 @@ | ||
| 241 | 240 | 'empty_form', |
| 242 | 241 | __('Form Name Should Be Within 50 Characters', 'bit-form') |
| 243 | 242 | ); |
| 244 | 243 | } |
| 244 | + // Guard: never persist orphan fields (in fields but in no layout) — they | |
| 245 | + // render nothing yet their validation rules block submission. Covers old | |
| 246 | + // clients and hand-edited imports (importAForm funnels through here). | |
| 247 | + $this->pruneOrphanFields($fields, $nestedLayout, $layout, null, $post); | |
| 245 | 248 | $form_content = [ |
| 246 | 249 | 'fields' => $fields, |
| 247 | 250 | 'layout' => $layout, |
| 248 | 251 | 'nestedLayout' => $nestedLayout, |
| @@ -321,8 +324,11 @@ | ||
| 321 | 324 | $savedID = $successMessageHandler->saveMessage($messageDetail); |
| 322 | 325 | if ($savedID) { |
| 323 | 326 | $msgIdx = empty($messageDetail->id) ? $messageKey : wp_json_encode(['id' => $messageDetail->id]); |
| 324 | 327 | $integartionIDForWorkflow['successMsg'][$msgIdx] = $savedID; |
| 328 | + if (!empty($messageDetail->clRef)) { | |
| 329 | + $integartionIDForWorkflow['confirmationCLRef'][$messageDetail->clRef] = $savedID; | |
| 330 | + } | |
| 325 | 331 | $messageDetail->id = $savedID; |
| 326 | 332 | $style = $this->updateSuccessMessageClassName($style, $messageKey, $savedID); |
| 327 | 333 | $atomic_class_map = $this->updateSuccessMessageClassName($atomic_class_map, $messageKey, $savedID); |
| 328 | 334 | } |
| @@ -355,11 +361,17 @@ | ||
| 355 | 361 | $emailTemplateHandler |
| 356 | 362 | = new EmailTemplateHandler($formID, $user_details); |
| 357 | 363 | foreach ($mailTem as $templateKey => $templateDetail) { |
| 358 | 364 | $savedID = $emailTemplateHandler->saveTemplate($templateDetail); |
| 359 | - if ($savedID) { | |
| 365 | + if ($savedID && !is_wp_error($savedID)) { | |
| 360 | 366 | $tempIdx = empty($templateDetail->id) ? $templateKey : wp_json_encode(['id' => $templateDetail->id]); |
| 361 | 367 | $integartionIDForWorkflow['mailNotify'][$tempIdx] = $savedID; |
| 368 | + if (!empty($templateDetail->clRef)) { | |
| 369 | + $integartionIDForWorkflow['emailCLRef'][$templateDetail->clRef] = $savedID; | |
| 370 | + } | |
| 371 | + // return the new id (and drop the temp ref) so the frontend reconciles the template | |
| 372 | + $templateDetail->id = $savedID; | |
| 373 | + unset($templateDetail->clRef); | |
| 362 | 374 | } |
| 363 | 375 | } |
| 364 | 376 | } |
| 365 | 377 | // Email Template [end] */ |
| @@ -364,10 +376,10 @@ | ||
| 364 | 376 | } |
| 365 | 377 | // Email Template [end] */ |
| 366 | 378 | |
| 367 | 379 | // PDF Template [start] |
| 368 | - if (!empty($pdfTem)) { | |
| 369 | - $pdfTemplateHandler = new PdfTemplateHandler($formID); | |
| 380 | + if (!empty($pdfTem) && class_exists('\BitCode\BitFormPro\Core\Messages\PdfTemplateHandler')) { | |
| 381 | + $pdfTemplateHandler = new \BitCode\BitFormPro\Core\Messages\PdfTemplateHandler($formID); | |
| 370 | 382 | $workFlowString = ''; |
| 371 | 383 | if (!empty($workFlows)) { |
| 372 | 384 | $workFlowString = wp_json_encode($workFlows); |
| 373 | 385 | } |
| @@ -380,8 +392,17 @@ | ||
| 380 | 392 | $this->pdfFontDownload($pdfSetting->font->name); |
| 381 | 393 | } |
| 382 | 394 | if (!empty($workFlowString)) { |
| 383 | 395 | $workFlowString = str_replace('"pdfId":"{\"id\":\"' . $templateDetail->id . '\"}"', '"pdfId":"{\"id\":\"' . $savedID . '\"}"', $workFlowString); |
| 396 | + // Multi-PDF: remap ids inside "pdfIds":[...] segments only — email-template ids use | |
| 397 | + // the same {\"id\":N} token shape, so a global replace would corrupt them. | |
| 398 | + $workFlowString = preg_replace_callback( | |
| 399 | + '/"pdfIds":\[[^\]]*\]/', | |
| 400 | + function ($matches) use ($templateDetail, $savedID) { | |
| 401 | + return str_replace('{\"id\":\"' . $templateDetail->id . '\"}', '{\"id\":\"' . $savedID . '\"}', $matches[0]); | |
| 402 | + }, | |
| 403 | + $workFlowString | |
| 404 | + ); | |
| 384 | 405 | } |
| 385 | 406 | } |
| 386 | 407 | if (!empty($workFlowString) && !empty($workFlows)) { |
| 387 | 408 | $workFlows = json_decode($workFlowString); |
| @@ -393,8 +414,14 @@ | ||
| 393 | 414 | if (!empty($formSettings->confirmation->type)) { |
| 394 | 415 | $allIntegrations = $formSettings->confirmation->type; |
| 395 | 416 | foreach ($allIntegrations as $integrationType => $integrationGroup) { |
| 396 | 417 | foreach ($integrationGroup as $singleIntegrationKey => $singleIntegrationDetail) { |
| 418 | + $integrationStatus = isset($singleIntegrationDetail->status) ? (int) $singleIntegrationDetail->status : 1; | |
| 419 | + // Inline-CL temp ref for a brand-new redirect (reconciled to the real id for workflow rows). | |
| 420 | + $clRef = isset($singleIntegrationDetail->clRef) ? $singleIntegrationDetail->clRef : null; | |
| 421 | + if (is_object($singleIntegrationDetail)) { | |
| 422 | + unset($singleIntegrationDetail->status, $singleIntegrationDetail->clRef); | |
| 423 | + } | |
| 397 | 424 | if (empty($singleIntegrationDetail->details)) { |
| 398 | 425 | $integrationName = $singleIntegrationDetail->title; |
| 399 | 426 | unset($singleIntegrationDetail->title, $singleIntegrationDetail->id); |
| 400 | 427 | |
| @@ -404,11 +431,14 @@ | ||
| 404 | 431 | $integrationName = $singleIntegrationDetail->title; |
| 405 | 432 | $integrationDetails = !is_string($singleIntegrationDetail->details) ? |
| 406 | 433 | wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details; |
| 407 | 434 | } |
| 408 | - $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form'); | |
| 435 | + $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form', $integrationStatus); | |
| 409 | 436 | $integIdx = empty($singleIntegrationDetail->id) ? $savedID : wp_json_encode(['id' => $singleIntegrationDetail->id]); |
| 410 | 437 | $integartionIDForWorkflow[$integrationType][$integIdx] = $savedID; |
| 438 | + if (!empty($clRef) && 'redirectPage' === $integrationType) { | |
| 439 | + $integartionIDForWorkflow['redirectCLRef'][$clRef] = $savedID; | |
| 440 | + } | |
| 411 | 441 | } |
| 412 | 442 | } |
| 413 | 443 | } |
| 414 | 444 | if (isset($formSettings->formAbandonment)) { |
| @@ -426,8 +456,11 @@ | ||
| 426 | 456 | $integrationType = $singleIntegrationDetail->type; |
| 427 | 457 | unset($singleIntegrationDetail->type); |
| 428 | 458 | $singleIntegrationDetailID = empty($singleIntegrationDetail->id) ? null : $singleIntegrationDetail->id; |
| 429 | 459 | unset($singleIntegrationDetail->id); |
| 460 | + // Inline-CL temp ref for a brand-new integration (reconciled to the real id for workflow rows). | |
| 461 | + $clRef = isset($singleIntegrationDetail->__bf_cl_ref) ? $singleIntegrationDetail->__bf_cl_ref : null; | |
| 462 | + unset($singleIntegrationDetail->__bf_cl_ref); | |
| 430 | 463 | if (empty($singleIntegrationDetail->details)) { |
| 431 | 464 | $integrationDetails = !is_string($singleIntegrationDetail) ? |
| 432 | 465 | wp_json_encode($singleIntegrationDetail) : $singleIntegrationDetail; |
| 433 | 466 | } else { |
| @@ -436,8 +469,11 @@ | ||
| 436 | 469 | } |
| 437 | 470 | $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form'); |
| 438 | 471 | $integIdx = empty($singleIntegrationDetailID) ? $singleIntegrationKey : wp_json_encode(['id' => $singleIntegrationDetailID]); |
| 439 | 472 | $integartionIDForWorkflow['integ'][$integIdx] = $savedID; |
| 473 | + if (!empty($clRef) && !is_wp_error($savedID)) { | |
| 474 | + $integartionIDForWorkflow['integrationCLRef'][$clRef] = $savedID; | |
| 475 | + } | |
| 440 | 476 | } |
| 441 | 477 | } |
| 442 | 478 | //Integrations [end] |
| 443 | 479 | //wrokFlows [start] |
| @@ -488,8 +524,12 @@ | ||
| 488 | 524 | $updated_data['form_content']['is_default'] = 1; |
| 489 | 525 | } |
| 490 | 526 | $updated_data['message'] = __('Form Saved successfully', 'bit-form'); |
| 491 | 527 | |
| 528 | + // Form and dependent rows are persisted; multilingual providers register | |
| 529 | + // the form's translatable strings here. | |
| 530 | + do_action('bitform_form_saved', $save_status); | |
| 531 | + | |
| 492 | 532 | return $updated_data; |
| 493 | 533 | } |
| 494 | 534 | } |
| 495 | 535 | |
| @@ -546,8 +586,11 @@ | ||
| 546 | 586 | if (empty($fields) || empty($layout) || is_null($formID)) { |
| 547 | 587 | return new WP_Error('empty_form', 'Can not update empty form.'); |
| 548 | 588 | } |
| 549 | 589 | |
| 590 | + // Guard: never persist orphan fields (see createNewForm). Fail closed. | |
| 591 | + $this->pruneOrphanFields($fields, $nestedLayout, $layout, $formID, $post); | |
| 592 | + | |
| 550 | 593 | $form_content = [ |
| 551 | 594 | 'fields' => $fields, |
| 552 | 595 | 'layout' => $layout, |
| 553 | 596 | 'nestedLayout' => $nestedLayout, |
| @@ -588,8 +631,11 @@ | ||
| 588 | 631 | $savedID = $successMessageHandler->saveMessage($messageDetail); |
| 589 | 632 | if ($savedID) { |
| 590 | 633 | $newData['settingConfiramation'] = 1; |
| 591 | 634 | $integartionIDForWorkflow['successMsg'][$messageKey] = $savedID; |
| 635 | + if (!empty($messageDetail->clRef)) { | |
| 636 | + $integartionIDForWorkflow['confirmationCLRef'][$messageDetail->clRef] = $savedID; | |
| 637 | + } | |
| 592 | 638 | $messageDetail->id = $savedID; |
| 593 | 639 | $builder_helper_state->style = $this->updateSuccessMessageClassName($builder_helper_state->style, $messageKey, $savedID); |
| 594 | 640 | if (isset($atomic_class_map)) { |
| 595 | 641 | $atomic_class_map = $this->updateSuccessMessageClassName($atomic_class_map, $messageKey, $savedID); |
| @@ -614,18 +660,29 @@ | ||
| 614 | 660 | $newData['mailTemplate'] = 2; |
| 615 | 661 | } else { |
| 616 | 662 | $newData['mailTemplate'] = 1; |
| 617 | 663 | $integartionIDForWorkflow['mailTem'][$templateKey] = $savedID; |
| 664 | + if (!empty($templateDetail->clRef)) { | |
| 665 | + $integartionIDForWorkflow['emailCLRef'][$templateDetail->clRef] = $savedID; | |
| 666 | + } | |
| 667 | + // return the new id (and drop the temp ref) so the frontend reconciles the template | |
| 668 | + $templateDetail->id = $savedID; | |
| 669 | + unset($templateDetail->clRef); | |
| 618 | 670 | } |
| 619 | 671 | } else { |
| 620 | - $emailTemplateHandler->updateTemplate($templateDetail); | |
| 672 | + $updated = $emailTemplateHandler->updateTemplate($templateDetail); | |
| 673 | + if (is_wp_error($updated) && 'db_error' === $updated->get_error_code()) { | |
| 674 | + $newData['mailTemplate'] = 2; | |
| 675 | + } elseif (0 === $newData['mailTemplate']) { | |
| 676 | + $newData['mailTemplate'] = 1; | |
| 677 | + } | |
| 621 | 678 | } |
| 622 | 679 | } |
| 623 | 680 | } |
| 624 | 681 | // return $formSettings; |
| 625 | 682 | // Email Template [end] */ |
| 626 | - if (!empty($pdfTem)) { | |
| 627 | - $pdfTemplateHandler = new PdfTemplateHandler($formID); | |
| 683 | + if (!empty($pdfTem) && class_exists('\BitCode\BitFormPro\Core\Messages\PdfTemplateHandler')) { | |
| 684 | + $pdfTemplateHandler = new \BitCode\BitFormPro\Core\Messages\PdfTemplateHandler($formID); | |
| 628 | 685 | |
| 629 | 686 | foreach ($pdfTem as $templateKey => $templateDetail) { |
| 630 | 687 | if (isset($templateDetail->setting->font->name)) { |
| 631 | 688 | $this->pdfFontDownload($templateDetail->setting->font->name); |
| @@ -645,8 +702,15 @@ | ||
| 645 | 702 | if (!empty($formSettings->confirmation->type)) { |
| 646 | 703 | $allIntegrations = $formSettings->confirmation->type; |
| 647 | 704 | foreach ($allIntegrations as $integrationType => $integrationGroup) { |
| 648 | 705 | foreach ($integrationGroup as $singleIntegrationKey => $singleIntegrationDetail) { |
| 706 | + // Redirect (and webhook) enable/disable toggle persists to the integration status column. | |
| 707 | + $integrationStatus = isset($singleIntegrationDetail->status) ? (int) $singleIntegrationDetail->status : 1; | |
| 708 | + // Inline-CL temp ref for a brand-new redirect (reconciled to the real id for workflow rows). | |
| 709 | + $clRef = isset($singleIntegrationDetail->clRef) ? $singleIntegrationDetail->clRef : null; | |
| 710 | + if (is_object($singleIntegrationDetail)) { | |
| 711 | + unset($singleIntegrationDetail->status, $singleIntegrationDetail->clRef); | |
| 712 | + } | |
| 649 | 713 | if (empty($singleIntegrationDetail->details)) { |
| 650 | 714 | $integrationName = $singleIntegrationDetail->title; |
| 651 | 715 | unset($singleIntegrationDetail->title); |
| 652 | 716 | $singleIntegrationDetailID = empty($singleIntegrationDetail->id) ? null : $singleIntegrationDetail->id; |
| @@ -660,13 +724,16 @@ | ||
| 660 | 724 | $integrationDetails = !is_string($singleIntegrationDetail->details) ? |
| 661 | 725 | wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details; |
| 662 | 726 | } |
| 663 | 727 | if (empty($singleIntegrationDetailID)) { |
| 664 | - $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form'); | |
| 728 | + $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form', $integrationStatus); | |
| 665 | 729 | $newData['settingConfiramation'] = 1; |
| 666 | 730 | $integartionIDForWorkflow[$integrationType][$singleIntegrationKey] = $savedID; |
| 731 | + if (!empty($clRef) && 'redirectPage' === $integrationType) { | |
| 732 | + $integartionIDForWorkflow['redirectCLRef'][$clRef] = $savedID; | |
| 733 | + } | |
| 667 | 734 | } else { |
| 668 | - $integrationHandler->updateIntegration($singleIntegrationDetailID, $integrationName, $integrationType, $integrationDetails, 'form'); | |
| 735 | + $integrationHandler->updateIntegration($singleIntegrationDetailID, $integrationName, $integrationType, $integrationDetails, 'form', $integrationStatus); | |
| 669 | 736 | } |
| 670 | 737 | } |
| 671 | 738 | } |
| 672 | 739 | } |
| @@ -677,8 +744,11 @@ | ||
| 677 | 744 | $integrationType = $singleIntegrationDetail->type; |
| 678 | 745 | unset($singleIntegrationDetail->type); |
| 679 | 746 | $singleIntegrationDetailID = empty($singleIntegrationDetail->id) ? null : $singleIntegrationDetail->id; |
| 680 | 747 | unset($singleIntegrationDetail->id); |
| 748 | + // Inline-CL temp ref for a brand-new integration (reconciled to the real id for workflow rows). | |
| 749 | + $clRef = isset($singleIntegrationDetail->__bf_cl_ref) ? $singleIntegrationDetail->__bf_cl_ref : null; | |
| 750 | + unset($singleIntegrationDetail->__bf_cl_ref); | |
| 681 | 751 | if (empty($singleIntegrationDetail->details)) { |
| 682 | 752 | $integrationDetails = !is_string($singleIntegrationDetail) ? |
| 683 | 753 | wp_json_encode($singleIntegrationDetail) : $singleIntegrationDetail; |
| 684 | 754 | } else { |
| @@ -687,8 +757,11 @@ | ||
| 687 | 757 | } |
| 688 | 758 | if (empty($singleIntegrationDetailID)) { |
| 689 | 759 | $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form'); |
| 690 | 760 | $integartionIDForWorkflow[$integrationType][$singleIntegrationKey] = $savedID; |
| 761 | + if (!empty($clRef) && !is_wp_error($savedID)) { | |
| 762 | + $integartionIDForWorkflow['integrationCLRef'][$clRef] = $savedID; | |
| 763 | + } | |
| 691 | 764 | if (is_wp_error($savedID) && 'result_empty' !== $savedID->get_error_code()) { |
| 692 | 765 | $newData['integation'] = 2; |
| 693 | 766 | } else { |
| 694 | 767 | $newData['integation'] = 1; |
| @@ -728,8 +801,11 @@ | ||
| 728 | 801 | } |
| 729 | 802 | $form_content = array_merge($form_content, ['workFlowExist' => $workFlowExist]); |
| 730 | 803 | //wrokFlows [end] |
| 731 | 804 | //reports [start] */ |
| 805 | + // read unconditionally after the reports block; a save without currentReport | |
| 806 | + // must not trip an undefined-variable warning | |
| 807 | + $reportIsDefault = null; | |
| 732 | 808 | if (!empty($reports)) { |
| 733 | 809 | $reportsModel = new ReportsModel(); |
| 734 | 810 | $fieldNames = []; |
| 735 | 811 | foreach ($fields as $key => $field) { |
| @@ -747,9 +823,13 @@ | ||
| 747 | 823 | $fieldNames['__created_at'] = __('Created Time', 'bit-form'); |
| 748 | 824 | $fieldNames['__updated_at'] = __('Modified Time', 'bit-form'); |
| 749 | 825 | |
| 750 | 826 | $reportIsDefault = null; |
| 751 | - if (isset($form_content['report_id'])) { | |
| 827 | + // The client posts currentReport as {} whenever its report atom has not resolved yet. That | |
| 828 | + // is an empty stdClass, which empty() above treats as non-empty, so we reach here with | |
| 829 | + // nothing to save — and writing the validated result would replace the stored report's | |
| 830 | + // column order, hidden columns, page size and name with an empty list. Leave the row alone. | |
| 831 | + if (isset($form_content['report_id']) && ReportsModel::isValidatableReport($reports)) { | |
| 752 | 832 | $validDateReport = $reportsModel->validateReportFields($reports, $fieldNames); |
| 753 | 833 | if (isset($reports->isDefault)) { |
| 754 | 834 | $reportIsDefault = $reports->isDefault; |
| 755 | 835 | } else { |
| @@ -860,8 +940,12 @@ | ||
| 860 | 940 | } |
| 861 | 941 | if (2 === $newData['reports']) { |
| 862 | 942 | $errorIN .= empty($errorIN) ? 'reports' : ', reports'; |
| 863 | 943 | } |
| 944 | + // See createNewForm(). Fired before the partial-failure return: the rows | |
| 945 | + // that did save are already persisted, so their strings still need registering. | |
| 946 | + do_action('bitform_form_saved', $formId); | |
| 947 | + | |
| 864 | 948 | if (!empty($errorIN)) { |
| 865 | 949 | /* translators: %s: comma-separated list of areas where error occurred */ |
| 866 | 950 | $updated_data['message'] = sprintf(__('Error Occured in saving %s', 'bit-form'), $errorIN); |
| 867 | 951 | return new WP_Error('Form update Error.', $updated_data); |
| @@ -869,12 +953,59 @@ | ||
| 869 | 953 | if (null !== $reportIsDefault) { |
| 870 | 954 | $updated_data['form_content']['is_default'] = $reportIsDefault; |
| 871 | 955 | } |
| 872 | 956 | $updated_data['message'] = __('Form updated successfully.', 'bit-form'); |
| 957 | + | |
| 873 | 958 | return $updated_data; |
| 874 | 959 | } |
| 875 | 960 | } |
| 876 | 961 | |
| 962 | + /** | |
| 963 | + * Drop orphan fields (present in `fields` but in no layout) from form_content | |
| 964 | + * before persisting, and remove now-childless nestedLayout entries. | |
| 965 | + * | |
| 966 | + * Entry meta is NEVER touched here — only explicit user deletes clear meta | |
| 967 | + * (see setEmptyMetaValue). Fail closed: an unusable/partial layout, or a | |
| 968 | + * client that flagged a failed pre-save layout sync (->skipOrphanPrune), | |
| 969 | + * prunes nothing. Pruned keys are logged, never silently dropped. | |
| 970 | + * | |
| 971 | + * @param object|array $fields by ref; orphan keys removed | |
| 972 | + * @param object|null $nestedLayout by ref; stale container entries removed | |
| 973 | + * @param array|object $layout raw layout (single or multi-step) | |
| 974 | + * @param int|string|null $formID for logging ('new' when absent) | |
| 975 | + * @param object $post raw request; honors ->skipOrphanPrune | |
| 976 | + */ | |
| 977 | + private function pruneOrphanFields(&$fields, &$nestedLayout, $layout, $formID, $post) | |
| 978 | + { | |
| 979 | + $formLabel = $formID ? $formID : 'new'; | |
| 980 | + if (isset($post->skipOrphanPrune) && $post->skipOrphanPrune) { | |
| 981 | + Log::debug_log('Bit Form: orphan prune skipped (client reported failed layout sync). FormID=' . $formLabel); | |
| 982 | + return; | |
| 983 | + } | |
| 984 | + $orphanFldKeys = FormManager::computeOrphanFieldKeys($layout, $nestedLayout, $fields); | |
| 985 | + // null = unusable/partial layout (fail closed); [] = nothing to prune | |
| 986 | + if (!is_array($orphanFldKeys) || empty($orphanFldKeys)) { | |
| 987 | + return; | |
| 988 | + } | |
| 989 | + Log::debug_log('Bit Form: pruning orphan fields (absent from every layout). FormID=' . $formLabel . ' keys=' . implode(',', $orphanFldKeys)); | |
| 990 | + foreach ($orphanFldKeys as $orphanKey) { | |
| 991 | + if (is_object($fields)) { | |
| 992 | + unset($fields->{$orphanKey}); | |
| 993 | + } elseif (is_array($fields)) { | |
| 994 | + unset($fields[$orphanKey]); | |
| 995 | + } | |
| 996 | + } | |
| 997 | + // prune stale nestedLayout entries — containers that no longer exist in fields | |
| 998 | + if (is_object($nestedLayout)) { | |
| 999 | + foreach (array_keys(get_object_vars($nestedLayout)) as $nKey) { | |
| 1000 | + $parentExists = is_object($fields) ? isset($fields->{$nKey}) : isset($fields[$nKey]); | |
| 1001 | + if (!$parentExists) { | |
| 1002 | + unset($nestedLayout->{$nKey}); | |
| 1003 | + } | |
| 1004 | + } | |
| 1005 | + } | |
| 1006 | + } | |
| 1007 | + | |
| 877 | 1008 | public function setEmptyMetaValue($fieldkeys) |
| 878 | 1009 | { |
| 879 | 1010 | global $wpdb; |
| 880 | 1011 | // Placeholders and arguments built dynamically from $fieldkeys; count matches at runtime. |
| @@ -1002,9 +1133,9 @@ | ||
| 1002 | 1133 | 'nestedLayout' => !empty($form_content->nestedLayout) ? $form_content->nestedLayout : (object) [], |
| 1003 | 1134 | 'formInfo' => !empty($form_content->formInfo) ? $form_content->formInfo : (object) ['formName' => $formManager->getFormName()], |
| 1004 | 1135 | 'fields' => $form_content->fields, |
| 1005 | 1136 | 'form_name' => $formManager->getFormName(), |
| 1006 | - 'workFlowExist' => $form_content->workFlowExist, | |
| 1137 | + 'workFlowExist' => isset($form_content->workFlowExist) ? $form_content->workFlowExist : [], | |
| 1007 | 1138 | 'report_id' => isset($form_content->report_id) ? $form_content->report_id : null |
| 1008 | 1139 | ]; |
| 1009 | 1140 | $successMessageHandler |
| 1010 | 1141 | = new SuccessMessageHandler($formID); |
| @@ -1027,28 +1158,37 @@ | ||
| 1027 | 1158 | if (!is_wp_error($emailTemplates)) { |
| 1028 | 1159 | foreach ($emailTemplates as $emailTemplatekey => $emailTemplatevalue) { |
| 1029 | 1160 | $mailTem[] = |
| 1030 | 1161 | [ |
| 1031 | - 'id' => $emailTemplatevalue->id, | |
| 1032 | - 'title' => $emailTemplatevalue->title, | |
| 1033 | - 'sub' => $emailTemplatevalue->sub, | |
| 1034 | - 'body' => $emailTemplatevalue->body, | |
| 1162 | + 'id' => $emailTemplatevalue->id, | |
| 1163 | + 'title' => $emailTemplatevalue->title, | |
| 1164 | + 'sub' => $emailTemplatevalue->sub, | |
| 1165 | + 'body' => $emailTemplatevalue->body, | |
| 1166 | + 'status' => isset($emailTemplatevalue->status) ? (int) $emailTemplatevalue->status : 1, | |
| 1167 | + 'config' => EmailTemplateHandler::normalizeConfig($emailTemplatevalue->config ?? null), | |
| 1035 | 1168 | ]; |
| 1036 | 1169 | } |
| 1037 | 1170 | } |
| 1038 | 1171 | // get all pdf template |
| 1039 | - $pdfTemplateHandler = new PdfTemplateHandler($formID); | |
| 1040 | - $pdfTemplates = $pdfTemplateHandler->getAll(); | |
| 1172 | + if (class_exists('\BitCode\BitFormPro\Core\Messages\PdfTemplateHandler')) { | |
| 1173 | + $pdfTemplateHandler = new \BitCode\BitFormPro\Core\Messages\PdfTemplateHandler($formID); | |
| 1174 | + $pdfTemplates = $pdfTemplateHandler->getAll(); | |
| 1041 | 1175 | |
| 1042 | - if (!is_wp_error($pdfTemplates)) { | |
| 1043 | - foreach ($pdfTemplates as $value) { | |
| 1044 | - $pdfTem[] = | |
| 1045 | - [ | |
| 1046 | - 'id' => $value->id, | |
| 1047 | - 'title' => $value->title, | |
| 1048 | - 'setting' => json_decode($value->setting), | |
| 1049 | - 'body' => $value->body, | |
| 1050 | - ]; | |
| 1176 | + if (!is_wp_error($pdfTemplates)) { | |
| 1177 | + foreach ($pdfTemplates as $value) { | |
| 1178 | + $pdfTem[] = | |
| 1179 | + [ | |
| 1180 | + 'id' => $value->id, | |
| 1181 | + 'title' => $value->title, | |
| 1182 | + 'setting' => json_decode($value->setting), | |
| 1183 | + 'pdf_config' => !empty($value->pdf_config) ? json_decode($value->pdf_config) : (object) [ | |
| 1184 | + 'status' => 1, | |
| 1185 | + 'header' => '', | |
| 1186 | + 'footer' => '', | |
| 1187 | + ], | |
| 1188 | + 'body' => $value->body, | |
| 1189 | + ]; | |
| 1190 | + } | |
| 1051 | 1191 | } |
| 1052 | 1192 | } |
| 1053 | 1193 | |
| 1054 | 1194 | $integrationHandler = new IntegrationHandler($formID); |
| @@ -1056,10 +1196,11 @@ | ||
| 1056 | 1196 | if (!is_wp_error($formIntegrations)) { |
| 1057 | 1197 | foreach ($formIntegrations as $integrationkey => $integrationValue) { |
| 1058 | 1198 | if ('redirectPage' === $integrationValue->integration_type || 'webHooks' === $integrationValue->integration_type) { |
| 1059 | 1199 | $integrationData = [ |
| 1060 | - 'id' => $integrationValue->id, | |
| 1061 | - 'title' => $integrationValue->integration_name, | |
| 1200 | + 'id' => $integrationValue->id, | |
| 1201 | + 'title' => $integrationValue->integration_name, | |
| 1202 | + 'status' => isset($integrationValue->status) ? (int) $integrationValue->status : 1, | |
| 1062 | 1203 | ]; |
| 1063 | 1204 | if (!empty($integrationValue->integration_details)) { |
| 1064 | 1205 | $integration_details = (array) json_decode($integrationValue->integration_details); |
| 1065 | 1206 | $integrationData = array_merge($integration_details, $integrationData); |
| @@ -1191,8 +1332,9 @@ | ||
| 1191 | 1332 | 'style' => $helper_states->style, |
| 1192 | 1333 | 'themeVars' => $helper_states->themeVars, |
| 1193 | 1334 | 'themeColors' => $helper_states->themeColors, |
| 1194 | 1335 | 'builderSettings' => $helper_states->builderSettings, |
| 1336 | + 'customCode' => FrontEndScriptGenerator::getCustomCodes($formID), | |
| 1195 | 1337 | 'additional' => empty($form_content->additional) ? [ |
| 1196 | 1338 | 'enabled' => [ |
| 1197 | 1339 | 'blocked_ip' => false, |
| 1198 | 1340 | 'restrict_form' => false, |
| @@ -1289,9 +1431,9 @@ | ||
| 1289 | 1431 | $form_content_arr = [ |
| 1290 | 1432 | 'layout' => $form_content->layout, |
| 1291 | 1433 | 'fields' => $form_content->fields, |
| 1292 | 1434 | 'form_name' => $formManager->getFormName(), |
| 1293 | - 'workFlowExist' => $form_content->workFlowExist, | |
| 1435 | + 'workFlowExist' => isset($form_content->workFlowExist) ? $form_content->workFlowExist : [], | |
| 1294 | 1436 | 'report_id' => isset($form_content->report_id) ? $form_content->report_id : null |
| 1295 | 1437 | ]; |
| 1296 | 1438 | $successMessageHandler |
| 1297 | 1439 | = new SuccessMessageHandler($formID); |
| @@ -1299,11 +1441,12 @@ | ||
| 1299 | 1441 | if (!is_wp_error($successMessages)) { |
| 1300 | 1442 | foreach ($successMessages as $sucessMessagekey => $sucessMessagevalue) { |
| 1301 | 1443 | $allConfirmation['type']['successMsg'][$sucessMessagekey] = |
| 1302 | 1444 | [ |
| 1303 | - 'id' => $sucessMessagevalue->id, | |
| 1304 | - 'title' => $sucessMessagevalue->message_title, | |
| 1305 | - 'msg' => $sucessMessagevalue->message_content, | |
| 1445 | + 'id' => $sucessMessagevalue->id, | |
| 1446 | + 'title' => $sucessMessagevalue->message_title, | |
| 1447 | + 'msg' => $sucessMessagevalue->message_content, | |
| 1448 | + 'config' => !empty($sucessMessagevalue->message_config) ? json_decode($sucessMessagevalue->message_config) : (object) [], | |
| 1306 | 1449 | ]; |
| 1307 | 1450 | } |
| 1308 | 1451 | } |
| 1309 | 1452 | $emailTemplateHandler |
| @@ -1312,12 +1455,14 @@ | ||
| 1312 | 1455 | if (!is_wp_error($emailTemplates)) { |
| 1313 | 1456 | foreach ($emailTemplates as $emailTemplatekey => $emailTemplatevalue) { |
| 1314 | 1457 | $mailTem[] = |
| 1315 | 1458 | [ |
| 1316 | - 'id' => $emailTemplatevalue->id, | |
| 1317 | - 'title' => $emailTemplatevalue->title, | |
| 1318 | - 'sub' => $emailTemplatevalue->sub, | |
| 1319 | - 'body' => $emailTemplatevalue->body, | |
| 1459 | + 'id' => $emailTemplatevalue->id, | |
| 1460 | + 'title' => $emailTemplatevalue->title, | |
| 1461 | + 'sub' => $emailTemplatevalue->sub, | |
| 1462 | + 'body' => $emailTemplatevalue->body, | |
| 1463 | + 'status' => isset($emailTemplatevalue->status) ? (int) $emailTemplatevalue->status : 1, | |
| 1464 | + 'config' => EmailTemplateHandler::normalizeConfig($emailTemplatevalue->config ?? null), | |
| 1320 | 1465 | ]; |
| 1321 | 1466 | } |
| 1322 | 1467 | } |
| 1323 | 1468 | $integrationHandler = new IntegrationHandler($formID); |
| @@ -1325,10 +1470,11 @@ | ||
| 1325 | 1470 | if (!is_wp_error($formIntegrations)) { |
| 1326 | 1471 | foreach ($formIntegrations as $integrationkey => $integrationValue) { |
| 1327 | 1472 | if ('redirectPage' === $integrationValue->integration_type || 'webHooks' === $integrationValue->integration_type) { |
| 1328 | 1473 | $integrationData = [ |
| 1329 | - 'id' => $integrationValue->id, | |
| 1330 | - 'title' => $integrationValue->integration_name, | |
| 1474 | + 'id' => $integrationValue->id, | |
| 1475 | + 'title' => $integrationValue->integration_name, | |
| 1476 | + 'status' => isset($integrationValue->status) ? (int) $integrationValue->status : 1, | |
| 1331 | 1477 | ]; |
| 1332 | 1478 | if (!empty($integrationValue->integration_details)) { |
| 1333 | 1479 | // var_dump(json_decode($integrationValue->integration_details)); |
| 1334 | 1480 | $integration_details = (array) json_decode($integrationValue->integration_details); |
| @@ -1513,10 +1659,12 @@ | ||
| 1513 | 1659 | |
| 1514 | 1660 | $emailTemplateModel = new EmailTemplateModel(); |
| 1515 | 1661 | $deleteTemplateStatus = $emailTemplateModel->delete(['form_id' => $formID]); |
| 1516 | 1662 | |
| 1517 | - $pdfTemplateModel = new PdfTemplateModel(); | |
| 1518 | - $deletePdfTemplateStatus = $pdfTemplateModel->delete(['form_id' => $formID]); | |
| 1663 | + if (class_exists('\BitCode\BitFormPro\Core\Database\PdfTemplateModel')) { | |
| 1664 | + $pdfTemplateModel = new \BitCode\BitFormPro\Core\Database\PdfTemplateModel(); | |
| 1665 | + $deletePdfTemplateStatus = $pdfTemplateModel->delete(['form_id' => $formID]); | |
| 1666 | + } | |
| 1519 | 1667 | |
| 1520 | 1668 | $integrationModel = new IntegrationModel(); |
| 1521 | 1669 | $deleteIntegrationStatus = $integrationModel->delete(['form_id' => $formID]); |
| 1522 | 1670 | |
| @@ -1606,10 +1754,12 @@ | ||
| 1606 | 1754 | |
| 1607 | 1755 | $emailTemplateModel = new EmailTemplateModel(); |
| 1608 | 1756 | $deleteTemplateStatus = $emailTemplateModel->bulkDelete(['form_id' => $formID]); |
| 1609 | 1757 | |
| 1610 | - $pdfTemplateModel = new PdfTemplateModel(); | |
| 1611 | - $deletePdfTemplateStatus = $pdfTemplateModel->bulkDelete(['form_id' => $formID]); | |
| 1758 | + if (class_exists('\BitCode\BitFormPro\Core\Database\PdfTemplateModel')) { | |
| 1759 | + $pdfTemplateModel = new \BitCode\BitFormPro\Core\Database\PdfTemplateModel(); | |
| 1760 | + $deletePdfTemplateStatus = $pdfTemplateModel->bulkDelete(['form_id' => $formID]); | |
| 1761 | + } | |
| 1612 | 1762 | |
| 1613 | 1763 | $integrationModel = new IntegrationModel(); |
| 1614 | 1764 | $deleteIntegrationStatus = $integrationModel->bulkDelete(['form_id' => $formID]); |
| 1615 | 1765 | |
| @@ -1890,8 +2040,65 @@ | ||
| 1890 | 2040 | $response['fieldDetails'] = $labels; |
| 1891 | 2041 | return $response; |
| 1892 | 2042 | } |
| 1893 | 2043 | |
| 2044 | + /** | |
| 2045 | + * Sanitize a request-supplied entry query condition before it reaches the Model | |
| 2046 | + * layer. Only known entry-table columns are allowed as condition keys, operators | |
| 2047 | + * are restricted to a safe allow-list ['form_id' => $id] when nothing valid remains. | |
| 2048 | + * | |
| 2049 | + * @param mixed $queryCondition | |
| 2050 | + * @param int $id | |
| 2051 | + * @return array | |
| 2052 | + */ | |
| 2053 | + private function sanitizeEntryQueryCondition($queryCondition, $id) | |
| 2054 | + { | |
| 2055 | + $allowedColumns = [ | |
| 2056 | + 'id', 'form_id', 'status', 'created_at', 'updated_at', | |
| 2057 | + 'user_id', 'user_ip', 'user_device', 'user_location', 'referer', | |
| 2058 | + ]; | |
| 2059 | + $allowedOperators = ['=', '!=', '<>', '<', '>', '<=', '>=', 'LIKE', 'IN']; | |
| 2060 | + | |
| 2061 | + if (!is_array($queryCondition)) { | |
| 2062 | + return ['form_id' => $id]; | |
| 2063 | + } | |
| 2064 | + | |
| 2065 | + $safe = []; | |
| 2066 | + foreach ($queryCondition as $key => $value) { | |
| 2067 | + if (!in_array($key, $allowedColumns, true)) { | |
| 2068 | + continue; | |
| 2069 | + } | |
| 2070 | + | |
| 2071 | + if (is_array($value)) { | |
| 2072 | + // Never honour a request-supplied raw SQL fragment. | |
| 2073 | + unset($value['raw']); | |
| 2074 | + | |
| 2075 | + if (array_key_exists('operator', $value) || array_key_exists('value', $value)) { | |
| 2076 | + $operator = isset($value['operator']) && is_string($value['operator']) | |
| 2077 | + ? strtoupper(trim($value['operator'])) | |
| 2078 | + : '='; | |
| 2079 | + if (!in_array($operator, $allowedOperators, true)) { | |
| 2080 | + $operator = '='; | |
| 2081 | + } | |
| 2082 | + $condValue = isset($value['value']) ? $value['value'] : ''; | |
| 2083 | + if (is_scalar($condValue)) { | |
| 2084 | + $safe[$key] = ['operator' => $operator, 'value' => $condValue]; | |
| 2085 | + } | |
| 2086 | + } else { | |
| 2087 | + // IN-style list: keep scalar members only (bound by the Model). | |
| 2088 | + $list = array_values(array_filter($value, 'is_scalar')); | |
| 2089 | + if (!empty($list)) { | |
| 2090 | + $safe[$key] = $list; | |
| 2091 | + } | |
| 2092 | + } | |
| 2093 | + } elseif (is_scalar($value)) { | |
| 2094 | + $safe[$key] = $value; | |
| 2095 | + } | |
| 2096 | + } | |
| 2097 | + | |
| 2098 | + return empty($safe) ? ['form_id' => $id] : $safe; | |
| 2099 | + } | |
| 2100 | + | |
| 1894 | 2101 | public function getFormEntry($Request, $post) |
| 1895 | 2102 | { |
| 1896 | 2103 | if (!empty($Request['id'])) { |
| 1897 | 2104 | $id = absint(wp_unslash($Request['id'])); |
| @@ -1911,8 +2118,9 @@ | ||
| 1911 | 2118 | $pageSize = isset($post->pageSize) ? |
| 1912 | 2119 | wp_unslash($post->pageSize) : 10; |
| 1913 | 2120 | $queryCondition = isset($post->queryCondition) ? wp_unslash($post->queryCondition) : ['form_id' => $id]; |
| 1914 | 2121 | } |
| 2122 | + $queryCondition = $this->sanitizeEntryQueryCondition($queryCondition, $id); | |
| 1915 | 2123 | if (is_null($id)) { |
| 1916 | 2124 | return new WP_Error('empty_form', __('Form id is empty.', 'bit-form')); |
| 1917 | 2125 | } |
| 1918 | 2126 | $formManager = new AdminFormManager($id); |