| @@ -1,164 +1,262 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Frontend\Ajax; |
| 4 | 4 | |
| 5 | -use WP_Error; | |
| 6 | -use BitCode\BitForm\Core\Util\HttpHelper; | |
| 7 | -use BitCode\BitForm\Core\Util\MailNotifier; | |
| 8 | 5 | use BitCode\BitForm\Admin\Form\AdminFormManager; |
| 9 | -use BitCode\BitForm\Core\Database\FormEntryModel; | |
| 6 | +use BitCode\BitForm\Admin\Form\Helpers; | |
| 10 | 7 | use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 11 | -use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper; | |
| 12 | 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 | 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; | |
| 14 | +use WP_Error; | |
| 17 | 15 | |
| 18 | 16 | final class FrontendAjax |
| 19 | 17 | { |
| 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']); | |
| 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); | |
| 28 | 44 | } |
| 45 | + } | |
| 29 | 46 | |
| 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 | - } | |
| 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); | |
| 40 | 58 | } |
| 59 | + } | |
| 41 | 60 | |
| 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); | |
| 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 | + } | |
| 50 | 149 | } else { |
| 51 | - wp_send_json_success($submitSatus); | |
| 150 | + wp_send_json_error(); | |
| 52 | 151 | } |
| 53 | - } | |
| 152 | + $trnasientData = get_transient("bitform_trigger_transient_{$entryID}"); | |
| 54 | 153 | |
| 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}"); | |
| 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(); | |
| 86 | 164 | |
| 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(); | |
| 165 | + $formEntry = $formEntryModel->get( | |
| 166 | + '*', | |
| 167 | + [ | |
| 168 | + 'form_id' => $formID, | |
| 169 | + 'id' => $entryID, | |
| 170 | + ] | |
| 171 | + ); | |
| 97 | 172 | |
| 98 | - $formEntry = $formEntryModel->get( | |
| 99 | - "*", | |
| 100 | - array( | |
| 101 | - 'form_id' => $formID, | |
| 102 | - 'id' => $entryID, | |
| 103 | - ) | |
| 104 | - ); | |
| 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 | + } | |
| 105 | 197 | |
| 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 | - ); | |
| 198 | + $workFlowRunHelper = new WorkFlow($formID); | |
| 199 | + $workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit( | |
| 200 | + 'create', | |
| 201 | + $submitted_fields, | |
| 202 | + $entries, | |
| 203 | + $entryID, | |
| 204 | + $logID | |
| 205 | + ); | |
| 138 | 206 | |
| 139 | - $triggerData = isset($workFlowreturnedOnSubmit['triggerData']) ? $workFlowreturnedOnSubmit['triggerData'] : null; | |
| 140 | - $triggerData['fields'] = $entries; | |
| 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); | |
| 141 | 230 | } |
| 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 | - } | |
| 231 | + } | |
| 232 | + wp_send_json_success(); | |
| 159 | 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 | + } | |
| 160 | 256 | } |
| 257 | + } | |
| 258 | + } | |
| 161 | 259 | |
| 162 | - wp_send_json_success(); | |
| 163 | - } | |
| 260 | + wp_send_json_success(); | |
| 261 | + } | |
| 164 | 262 | } |