| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Ajax; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 6 |
use BitCode\BitForm\Core\Form\FormHandler; |
| 7 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 8 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 9 |
use BitCode\BitForm\Core\Form\Validator\FormFieldValidator; |
| 10 |
|
| 11 |
class FrontendAjax |
| 12 |
{ |
| 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')); |
| 17 |
} |
| 18 |
|
| 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]); |
| 31 |
} |
| 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 |
} |
| 92 |
} |
| 93 |
wp_send_json_error( |
| 94 |
__( |
| 95 |
'Form does not exist', |
| 96 |
'bitform' |
| 97 |
), |
| 98 |
400 |
| 99 |
); |
| 100 |
} else { |
| 101 |
wp_send_json_error( |
| 102 |
__( |
| 103 |
'Token expired', |
| 104 |
'bitform' |
| 105 |
), |
| 106 |
401 |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|