| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Ajax; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Form\AdminFormManager; |
| 6 |
use BitCode\BitForm\Admin\Form\Helpers; |
| 7 |
use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 8 |
use BitCode\BitForm\Core\Database\FormEntryMetaModel; |
| 9 |
use BitCode\BitForm\Core\Database\FormEntryModel; |
| 10 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 11 |
use BitCode\BitForm\Core\Util\MailNotifier; |
| 12 |
use BitCode\BitForm\Core\WorkFlow\WorkFlow; |
| 13 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
final class FrontendAjax |
| 17 |
{ |
| 18 |
public function register() |
| 19 |
{ |
| 20 |
add_action('wp_ajax_nopriv_bitforms_submit_form', [$this, 'submit_form']); |
| 21 |
add_action('wp_ajax_bitforms_submit_form', [$this, 'submit_form']); |
| 22 |
add_action('wp_ajax_bitforms_entry_update', [$this, 'update_entry']); |
| 23 |
add_action('wp_ajax_nopriv_bitforms_entry_update', [$this, 'update_entry']); |
| 24 |
add_action('wp_ajax_bitforms_update_form_entry', [$this, 'update_entry']); |
| 25 |
add_action('wp_ajax_nopriv_bitforms_update_form_entry', [$this, 'update_entry']); |
| 26 |
add_action('wp_ajax_bitforms_before_submit_validate', [$this, 'beforeSubmittedValidate']); |
| 27 |
add_action('wp_ajax_nopriv_bitforms_before_submit_validate', [$this, 'beforeSubmittedValidate']); |
| 28 |
add_action('wp_ajax_nopriv_bitforms_trigger_workflow', [$this, 'triggerWorkFlow']); |
| 29 |
add_action('wp_ajax_bitforms_trigger_workflow', [$this, 'triggerWorkFlow']); |
| 30 |
add_action('wp_ajax_bitforms_onload_added_field', [$this, 'addHiddenField']); |
| 31 |
add_action('wp_ajax_nopriv_bitforms_onload_added_field', [$this, 'addHiddenField']); |
| 32 |
} |
| 33 |
|
| 34 |
public function beforeSubmittedValidate() |
| 35 |
{ |
| 36 |
$form_id = str_replace('bitforms_', '', $_POST['bitforms_id']); |
| 37 |
$FrontendFormManager = new FrontendFormManager($form_id); |
| 38 |
$FrontendFormManager->fieldNameReplaceOfPost(); |
| 39 |
$validateStatus = $FrontendFormManager->beforeSubmittedValidate(); |
| 40 |
if (is_wp_error($validateStatus)) { |
| 41 |
wp_send_json_error($validateStatus->get_error_message(), 400); |
| 42 |
} else { |
| 43 |
wp_send_json_success($validateStatus); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function submit_form() |
| 48 |
{ |
| 49 |
\ignore_user_abort(); |
| 50 |
$form_id = str_replace('bitforms_', '', $_POST['bitforms_id']); |
| 51 |
$FrontendFormManager = new FrontendFormManager($form_id); |
| 52 |
$submitSatus = $FrontendFormManager->handleSubmission(); |
| 53 |
if (is_wp_error($submitSatus)) { |
| 54 |
do_action('bitform_submit_error', $form_id, $submitSatus); |
| 55 |
wp_send_json_error($submitSatus->get_error_message(), 400); |
| 56 |
} else { |
| 57 |
wp_send_json_success($submitSatus); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function update_entry() |
| 62 |
{ |
| 63 |
\ignore_user_abort(); |
| 64 |
$form_id = str_replace('bitforms_', '', sanitize_text_field($_POST['bitforms_id'])); |
| 65 |
$entryId = sanitize_text_field($_REQUEST['entryID']); |
| 66 |
$entryToken = sanitize_text_field($_REQUEST['entryToken']); |
| 67 |
if (Helpers::validateEntryTokenAndUser($entryToken, $entryId)) { |
| 68 |
$FrontendFormManager = new FrontendFormManager($form_id); |
| 69 |
$updateStatus = $FrontendFormManager->handleUpdateEntry(); |
| 70 |
if (is_wp_error($updateStatus)) { |
| 71 |
do_action('bitform_update_entry', $form_id, $updateStatus); |
| 72 |
wp_send_json_error($updateStatus->get_error_message(), 400); |
| 73 |
} else { |
| 74 |
wp_send_json_success($updateStatus); |
| 75 |
} |
| 76 |
} else { |
| 77 |
wp_send_json_error('Entry Token is not Authorized', 401); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public function hiddenFields($formId) |
| 82 |
{ |
| 83 |
$tokens = Helpers::csrfEecrypted(); |
| 84 |
$fields = [ |
| 85 |
[ |
| 86 |
'name' => 'csrf', |
| 87 |
'value' => $tokens['csrf'], |
| 88 |
], |
| 89 |
[ |
| 90 |
'name' => 't_identity', |
| 91 |
'value' => $tokens['t_identity'], |
| 92 |
] |
| 93 |
]; |
| 94 |
$frontendFormManger = new FrontendFormManager($formId); |
| 95 |
if ($frontendFormManger->isHoneypotActive()) { |
| 96 |
$time = time(); |
| 97 |
$honeypodFldName = Helpers::honeypotEncryptedToken("_bitforms_{$formId}_{$time}_"); |
| 98 |
$fields[] = [ |
| 99 |
'name' => 'b_h_t', |
| 100 |
'value' => $honeypodFldName, |
| 101 |
]; |
| 102 |
} |
| 103 |
return $fields; |
| 104 |
} |
| 105 |
|
| 106 |
public function addHiddenField() |
| 107 |
{ |
| 108 |
ignore_user_abort(); |
| 109 |
$request = file_get_contents('php://input'); |
| 110 |
if ($request) { |
| 111 |
$data = is_string($request) ? \json_decode($request) : $request; |
| 112 |
if (!isset($data->formId)) { |
| 113 |
wp_send_json_error('Form Id not found', 400); |
| 114 |
} else { |
| 115 |
$fields = $this->hiddenFields($data->formId); |
| 116 |
wp_send_json_success(['hidden_fields'=>$fields]); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function triggerWorkFlow() |
| 122 |
{ |
| 123 |
ignore_user_abort(); |
| 124 |
$inputJSON = file_get_contents('php://input'); |
| 125 |
if ($inputJSON) { |
| 126 |
$request = is_string($inputJSON) ? \json_decode($inputJSON) : $inputJSON; |
| 127 |
$submitted_fields = []; |
| 128 |
if (isset($request->id, $request->cronNotOk)) { |
| 129 |
$formID = str_replace('bitforms_', '', $request->id); |
| 130 |
if (!wp_verify_nonce($request->token, $request->id) && is_user_logged_in()) { |
| 131 |
wp_send_json_error(); |
| 132 |
} |
| 133 |
$cronNotOk = $request->cronNotOk; |
| 134 |
$entryID = $cronNotOk[0]; |
| 135 |
$logID = $cronNotOk[1]; |
| 136 |
$entryLog = new FormEntryLogModel(); |
| 137 |
if (isset($cronNotOk[2]) && \is_int($cronNotOk[2])) { |
| 138 |
$queueudEntry = $entryLog->get( |
| 139 |
'response_obj', |
| 140 |
['id' => $cronNotOk[2]] |
| 141 |
); |
| 142 |
if ($queueudEntry) { |
| 143 |
if (!empty($queueudEntry[0]->response_obj) && \strpos($queueudEntry[0]->response_obj, 'processed') > 0) { |
| 144 |
wp_send_json_error(); |
| 145 |
} |
| 146 |
} else { |
| 147 |
wp_send_json_error(); |
| 148 |
} |
| 149 |
} else { |
| 150 |
wp_send_json_error(); |
| 151 |
} |
| 152 |
$trnasientData = get_transient("bitform_trigger_transient_{$entryID}"); |
| 153 |
|
| 154 |
if (!empty($trnasientData)) { |
| 155 |
delete_transient("bitform_trigger_transient_{$entryID}"); |
| 156 |
$triggerData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; |
| 157 |
} else { |
| 158 |
$formManager = new AdminFormManager($formID); |
| 159 |
if (!$formManager->isExist()) { |
| 160 |
return wp_send_json(new WP_Error('trigger_empty_form', __('provided form does not exists', 'bit-form'))); |
| 161 |
} |
| 162 |
$formEntryModel = new FormEntryModel(); |
| 163 |
$entryMeta = new FormEntryMetaModel(); |
| 164 |
|
| 165 |
$formEntry = $formEntryModel->get( |
| 166 |
'*', |
| 167 |
[ |
| 168 |
'form_id' => $formID, |
| 169 |
'id' => $entryID, |
| 170 |
] |
| 171 |
); |
| 172 |
|
| 173 |
if (!$formEntry) { |
| 174 |
return new WP_Error('trigger_empty_form', __('provided form entries does not exists', 'bit-form')); |
| 175 |
} |
| 176 |
$formEntryMeta = $entryMeta->get( |
| 177 |
[ |
| 178 |
'meta_key', |
| 179 |
'meta_value', |
| 180 |
], |
| 181 |
[ |
| 182 |
'bitforms_form_entry_id' => $entryID, |
| 183 |
] |
| 184 |
); |
| 185 |
$entries = []; |
| 186 |
foreach ($formEntryMeta as $key => $value) { |
| 187 |
$entries[$value->meta_key] = $value->meta_value; |
| 188 |
} |
| 189 |
$formContent = $formManager->getFormContent(); |
| 190 |
$submitted_fields = $formContent->fields; |
| 191 |
foreach ($submitted_fields as $key => $value) { |
| 192 |
if (isset($entries[$key])) { |
| 193 |
$submitted_fields->{$key}->val = $entries[$key]; |
| 194 |
$submitted_fields->{$key}->name = $key; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
$workFlowRunHelper = new WorkFlow($formID); |
| 199 |
$workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit( |
| 200 |
'create', |
| 201 |
$submitted_fields, |
| 202 |
$entries, |
| 203 |
$entryID, |
| 204 |
$logID |
| 205 |
); |
| 206 |
|
| 207 |
$triggerData = isset($workFlowreturnedOnSubmit['triggerData']) ? $workFlowreturnedOnSubmit['triggerData'] : null; |
| 208 |
$triggerData['fields'] = $entries; |
| 209 |
} |
| 210 |
if (!empty($triggerData)) { |
| 211 |
if (isset($triggerData['integrationRun']) && !$triggerData['integrationRun']) { |
| 212 |
$entryModel = new FormEntryModel(); |
| 213 |
$updatedStatus = $entryModel->update( |
| 214 |
[ |
| 215 |
'status' => 2, |
| 216 |
], |
| 217 |
[ |
| 218 |
'form_id' => $triggerData['formID'], |
| 219 |
'id' => $entryID, |
| 220 |
] |
| 221 |
); |
| 222 |
if (is_wp_error($updatedStatus)) { |
| 223 |
wp_send_json_error($updatedStatus->get_error_message(), 411); |
| 224 |
} else { |
| 225 |
if ($triggerData['dbl_opt_dflt_template']) { |
| 226 |
do_action('bf_double_optin_confirmation', $triggerData['dbl_opt_donf'], $triggerData); |
| 227 |
} elseif (isset($triggerData['dblOptin'])) { |
| 228 |
foreach ($triggerData['dblOptin'] as $value) { |
| 229 |
MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $entryID, true, $logID); |
| 230 |
} |
| 231 |
} |
| 232 |
wp_send_json_success(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if (isset($triggerData['mail'])) { |
| 237 |
$formManager = new AdminFormManager($formID); |
| 238 |
$formContent = $formManager->getFormContent(); |
| 239 |
$submitted_fields = $formContent->fields; |
| 240 |
$fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $triggerData['fields']); |
| 241 |
foreach ($triggerData['mail'] as $value) { |
| 242 |
MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $entryID); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
do_action('bitforms_exec_integrations', $triggerData['integrations'], $triggerData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']); |
| 247 |
if (isset($cronNotOk[2]) && \is_int($cronNotOk[2])) { |
| 248 |
$queueuEntry = $entryLog->update( |
| 249 |
[ |
| 250 |
'response_type' => 'success', |
| 251 |
'response_obj' => wp_json_encode(['status' => 'processed']), |
| 252 |
], |
| 253 |
['id' => $cronNotOk[2]] |
| 254 |
); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
wp_send_json_success(); |
| 261 |
} |
| 262 |
} |
| 263 |
|