| @@ -1,110 +1,262 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Frontend\Ajax; |
| 4 | 4 | |
| 5 | -use BitCode\BitForm\Core\Util\HttpHelper; | |
| 6 | -use BitCode\BitForm\Core\Form\FormHandler; | |
| 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; | |
| 7 | 13 | use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 8 | -use BitCode\BitForm\Core\Integration\IntegrationHandler; | |
| 9 | -use BitCode\BitForm\Core\Form\Validator\FormFieldValidator; | |
| 14 | +use WP_Error; | |
| 10 | 15 | |
| 11 | -class FrontendAjax | |
| 16 | +final class FrontendAjax | |
| 12 | 17 | { |
| 13 | - public function register() | |
| 14 | - { | |
| 15 | - add_action('wp_ajax_nopriv_bitforms_submit_form', array($this, 'submit_form')); | |
| 16 | - add_action('wp_ajax_bitforms_submit_form', array($this, 'submit_form')); | |
| 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); | |
| 17 | 44 | } |
| 45 | + } | |
| 18 | 46 | |
| 19 | - public function submit_form() | |
| 20 | - { | |
| 21 | - \ignore_user_abort(); | |
| 22 | - $form_id = str_replace('bitforms_', null, $_REQUEST['bitforms_id']); | |
| 23 | - $FrontendFormManager = new FrontendFormManager($form_id); | |
| 24 | - if (wp_verify_nonce($_REQUEST['bitforms_token'], $_REQUEST['bitforms_id'])) { | |
| 25 | - unset($_REQUEST['_ajax_nonce']); | |
| 26 | - unset($_REQUEST['action']); | |
| 27 | - if ($FrontendFormManager->isExist()) { | |
| 28 | - $isRestricted = $FrontendFormManager->checkSubmissionRestriction(); | |
| 29 | - if ($isRestricted && !empty($isRestricted)) { | |
| 30 | - wp_send_json_error($isRestricted[0]); | |
| 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) || Helpers::validateFormEntryEditPermission($form_id, $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 or User 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); | |
| 31 | 230 | } |
| 32 | - $captchaSettings = $FrontendFormManager->getCaptchaSettings(); | |
| 33 | - if ($captchaSettings) { | |
| 34 | - $token = $_REQUEST['g-recaptcha-response']; | |
| 35 | - if (!isset($_REQUEST['g-recaptcha-response'])) { | |
| 36 | - wp_send_json_error( | |
| 37 | - __( | |
| 38 | - 'Please verify reCAPTCHA', | |
| 39 | - 'bitform' | |
| 40 | - ) | |
| 41 | - ); | |
| 42 | - } | |
| 43 | - $integrationHandler = new IntegrationHandler(0); | |
| 44 | - $allFormIntegrations = $integrationHandler->getAllIntegration('app', 'gReCaptcha'); | |
| 45 | - if (!is_wp_error($allFormIntegrations)) { | |
| 46 | - foreach ($allFormIntegrations as $integration) { | |
| 47 | - if (!is_null($integration->integration_type) && $integration->integration_type === 'gReCaptcha') { | |
| 48 | - $integrationDetails = json_decode($integration->integration_details); | |
| 49 | - $integrationDetails->id = $integration->id; | |
| 50 | - $reCAPTCHA = $integrationDetails; | |
| 51 | - } | |
| 52 | - } | |
| 53 | - } | |
| 54 | - if (!empty($reCAPTCHA->secretKey)) { | |
| 55 | - $gRecaptchaResponse = HttpHelper::post( | |
| 56 | - 'https://www.google.com/recaptcha/api/siteverify', | |
| 57 | - ['secret' => $reCAPTCHA->secretKey, 'response' => $token] | |
| 58 | - ); | |
| 59 | - $isgReCaptchaVerified = false; | |
| 60 | - if (!is_wp_error($gRecaptchaResponse)) { | |
| 61 | - $gRecaptchaResponse = $gRecaptchaResponse->success; | |
| 62 | - } | |
| 63 | - if (!$gRecaptchaResponse) { | |
| 64 | - wp_send_json_error( | |
| 65 | - __( | |
| 66 | - 'Please verify reCAPTCHA', | |
| 67 | - 'bitform' | |
| 68 | - ) | |
| 69 | - ); | |
| 70 | - } | |
| 71 | - } | |
| 72 | - } | |
| 73 | - unset($_REQUEST['g-recaptcha-response']); | |
| 74 | - $validatedFormData = $FrontendFormManager->validateFormSubmission($_REQUEST); | |
| 75 | - $form_fields = $FrontendFormManager->getFields(); | |
| 76 | - $formFieldValidator = new FormFieldValidator($form_fields, $validatedFormData, $_FILES); | |
| 77 | - $validateField = $formFieldValidator->validate('create', $form_id); | |
| 78 | - if ($validateField) { | |
| 79 | - $saveResponse = $FrontendFormManager->saveFormEntry($validatedFormData); | |
| 80 | - $FrontendFormManager->setSubmissionCount(); | |
| 81 | - $responseMsg = is_array($saveResponse) && !empty($saveResponse) ? $saveResponse : __('Form Submitted Successfully', 'bitform'); | |
| 82 | - wp_send_json_success($responseMsg); | |
| 83 | - } else { | |
| 84 | - // if (!$validateForm) { | |
| 85 | - // $errorMessages = __('Please submit form with valid fields', 'bitform'); | |
| 86 | - // } else { | |
| 87 | - $errorMessages = count($formFieldValidator->getMessage()) > 0 ? | |
| 88 | - $formFieldValidator->getMessage() : null; | |
| 89 | - // } | |
| 90 | - wp_send_json_error($errorMessages, 400); | |
| 91 | - } | |
| 231 | + } | |
| 232 | + wp_send_json_success(); | |
| 92 | 233 | } |
| 93 | - wp_send_json_error( | |
| 94 | - __( | |
| 95 | - 'Form does not exist', | |
| 96 | - 'bitform' | |
| 97 | - ), | |
| 98 | - 400 | |
| 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]] | |
| 99 | 254 | ); |
| 100 | - } else { | |
| 101 | - wp_send_json_error( | |
| 102 | - __( | |
| 103 | - 'Token expired', | |
| 104 | - 'bitform' | |
| 105 | - ), | |
| 106 | - 401 | |
| 107 | - ); | |
| 255 | + } | |
| 108 | 256 | } |
| 257 | + } | |
| 109 | 258 | } |
| 259 | + | |
| 260 | + wp_send_json_success(); | |
| 261 | + } | |
| 110 | 262 | } |