| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Ajax; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 7 |
use BitCode\BitForm\Core\Util\MailNotifier; |
| 8 |
use BitCode\BitForm\Admin\Form\AdminFormManager; |
| 9 |
use BitCode\BitForm\Core\Database\FormEntryModel; |
| 10 |
use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 11 |
use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper; |
| 12 |
use BitCode\BitForm\Core\Database\FormEntryMetaModel; |
| 13 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 14 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 15 |
use BitCode\BitForm\Core\Form\Validator\FormFieldValidator; |
| 16 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 17 |
|
| 18 |
final class FrontendAjax |
| 19 |
{ |
| 20 |
public function register() |
| 21 |
{ |
| 22 |
add_action('wp_ajax_nopriv_bitforms_submit_form', [$this, 'submit_form']); |
| 23 |
add_action('wp_ajax_bitforms_submit_form', [$this, 'submit_form']); |
| 24 |
add_action('wp_ajax_bitforms_before_submit_validate', [$this, 'beforeSubmittedValidate']); |
| 25 |
add_action('wp_ajax_nopriv_bitforms_before_submit_validate', [$this, 'beforeSubmittedValidate']); |
| 26 |
add_action('wp_ajax_nopriv_bitforms_trigger_workflow', [$this, 'triggerWorkFlow']); |
| 27 |
add_action('wp_ajax_bitforms_trigger_workflow', [$this, 'triggerWorkFlow']); |
| 28 |
} |
| 29 |
|
| 30 |
public function beforeSubmittedValidate() |
| 31 |
{ |
| 32 |
$form_id = str_replace('bitforms_', null, $_POST['bitforms_id']); |
| 33 |
$FrontendFormManager = new FrontendFormManager($form_id); |
| 34 |
$validateStatus = $FrontendFormManager->beforeSubmittedValidate(); |
| 35 |
if (is_wp_error($validateStatus)) { |
| 36 |
wp_send_json_error($validateStatus->get_error_message(), 400); |
| 37 |
} else { |
| 38 |
wp_send_json_success($validateStatus); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public function submit_form() |
| 43 |
{ |
| 44 |
\ignore_user_abort(); |
| 45 |
$form_id = str_replace('bitforms_', null, $_POST['bitforms_id']); |
| 46 |
$FrontendFormManager = new FrontendFormManager($form_id); |
| 47 |
$submitSatus = $FrontendFormManager->handleSubmission(); |
| 48 |
if (is_wp_error($submitSatus)) { |
| 49 |
wp_send_json_error($submitSatus->get_error_message(), 400); |
| 50 |
} else { |
| 51 |
wp_send_json_success($submitSatus); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function triggerWorkFlow() |
| 56 |
{ |
| 57 |
ignore_user_abort(); |
| 58 |
$inputJSON = file_get_contents('php://input'); |
| 59 |
if ($inputJSON) { |
| 60 |
$request = is_string($inputJSON) ? \json_decode($inputJSON) : $inputJSON; |
| 61 |
if (isset($request->id) && isset($request->cronNotOk)) { |
| 62 |
$formID = str_replace('bitforms_', null, $request->id); |
| 63 |
if (!wp_verify_nonce($request->token, $request->id) && is_user_logged_in()) { |
| 64 |
wp_send_json_error(); |
| 65 |
} |
| 66 |
$cronNotOk = $request->cronNotOk; |
| 67 |
$entryID = $cronNotOk[0]; |
| 68 |
$logID = $cronNotOk[1]; |
| 69 |
$entryLog = new FormEntryLogModel(); |
| 70 |
if (isset($cronNotOk[2]) && \is_int($cronNotOk[2])) { |
| 71 |
$queueudEntry = $entryLog->get( |
| 72 |
"response_obj", |
| 73 |
["id" => $cronNotOk[2]] |
| 74 |
); |
| 75 |
if ($queueudEntry) { |
| 76 |
if (!empty($queueudEntry[0]->response_obj) && \strpos($queueudEntry[0]->response_obj, "processed") > 0) { |
| 77 |
wp_send_json_error(); |
| 78 |
} |
| 79 |
} else { |
| 80 |
wp_send_json_error(); |
| 81 |
} |
| 82 |
} else { |
| 83 |
wp_send_json_error(); |
| 84 |
} |
| 85 |
$trnasientData = get_transient("bitform_trigger_transient_{$entryID}"); |
| 86 |
|
| 87 |
if (!empty($trnasientData)) { |
| 88 |
delete_transient("bitform_trigger_transient_{$entryID}"); |
| 89 |
$triggerData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; |
| 90 |
} else { |
| 91 |
$formManager = new AdminFormManager($formID); |
| 92 |
if (!$formManager->isExist()) { |
| 93 |
return wp_send_json(new WP_Error('trigger_empty_form', __('provided form does not exists', 'bitform'))); |
| 94 |
} |
| 95 |
$formEntryModel = new FormEntryModel(); |
| 96 |
$entryMeta = new FormEntryMetaModel(); |
| 97 |
|
| 98 |
$formEntry = $formEntryModel->get( |
| 99 |
"*", |
| 100 |
array( |
| 101 |
'form_id' => $formID, |
| 102 |
'id' => $entryID, |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
if (!$formEntry) { |
| 107 |
return new WP_Error('trigger_empty_form', __('provided form entries does not exists', 'bitform')); |
| 108 |
} |
| 109 |
$formEntryMeta = $entryMeta->get( |
| 110 |
array( |
| 111 |
'meta_key', |
| 112 |
'meta_value', |
| 113 |
), |
| 114 |
array( |
| 115 |
'bitforms_form_entry_id' => $entryID, |
| 116 |
) |
| 117 |
); |
| 118 |
$entries = array(); |
| 119 |
foreach ($formEntryMeta as $key => $value) { |
| 120 |
$entries[$value->meta_key] = $value->meta_value; |
| 121 |
} |
| 122 |
$formContent = $formManager->getFormContent(); |
| 123 |
$submitted_fields = $formContent->fields; |
| 124 |
foreach ($submitted_fields as $key => $value) { |
| 125 |
if (isset($entries[$key])) { |
| 126 |
$submitted_fields->{$key}->val = $entries[$key]; |
| 127 |
$submitted_fields->{$key}->name = $key; |
| 128 |
} |
| 129 |
} |
| 130 |
$workFlowRunHelper = new WorkFlowRunHelper($formID); |
| 131 |
$workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit( |
| 132 |
'create', |
| 133 |
$submitted_fields, |
| 134 |
$entries, |
| 135 |
$entryID, |
| 136 |
$logID |
| 137 |
); |
| 138 |
|
| 139 |
$triggerData = isset($workFlowreturnedOnSubmit['triggerData']) ? $workFlowreturnedOnSubmit['triggerData'] : null; |
| 140 |
$triggerData['fields'] = $entries; |
| 141 |
} |
| 142 |
if (!empty($triggerData)) { |
| 143 |
if (isset($triggerData['mail'])) { |
| 144 |
foreach ($triggerData['mail'] as $value) { |
| 145 |
MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $entryID); |
| 146 |
} |
| 147 |
} |
| 148 |
do_action("bitforms_exec_integrations", $triggerData['integrations'], $triggerData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']); |
| 149 |
if (isset($cronNotOk[2]) && \is_int($cronNotOk[2])) { |
| 150 |
$queueuEntry = $entryLog->update( |
| 151 |
array( |
| 152 |
"response_type" => "success", |
| 153 |
"response_obj" => json_encode(['status' => 'processed']), |
| 154 |
), |
| 155 |
["id" => $cronNotOk[2]] |
| 156 |
); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
wp_send_json_success(); |
| 163 |
} |
| 164 |
} |
| 165 |
|