Rename variables to make the ReCaptcha code easier to understand

This commit renames two variables to make it easier to understand the
code responsible for adding a ReCapcha to a subscription form:

formData.recaptcha -> formData.recaptchaWidgetId
formData.data.recaptcha -> formData.data.recaptchaResponseToken

Before this change it was harder to understand the difference between
formData.recaptcha and formData.data.recaptcha since they used the same
name.

[MAILPOET-4145]
This commit is contained in:
Rodrigo Primo
2022-04-26 18:22:05 -03:00
committed by Veljko V
parent 909e59ca1f
commit ba47f0b854
3 changed files with 14 additions and 13 deletions

View File

@ -106,8 +106,8 @@ jQuery(($) => {
} }
}) })
.done((response) => { .done((response) => {
if (window.grecaptcha && formData.recaptcha) { if (window.grecaptcha && formData.recaptchaWidgetId) {
window.grecaptcha.reset(formData.recaptcha); window.grecaptcha.reset(formData.recaptchaWidgetId);
} }
return response; return response;
}) })
@ -129,8 +129,8 @@ jQuery(($) => {
// reset validation // reset validation
parsley.reset(); parsley.reset();
// reset captcha // reset captcha
if (window.grecaptcha && formData.recaptcha) { if (window.grecaptcha && formData.recaptchaWidgetId) {
window.grecaptcha.reset(formData.recaptcha); window.grecaptcha.reset(formData.recaptchaWidgetId);
} }
// resize iframe // resize iframe
@ -171,9 +171,11 @@ jQuery(($) => {
const params = { sitekey, size }; const params = { sitekey, size };
if (size === 'invisible') { if (size === 'invisible') {
params.callback = function invisibleReCaptchaCallback(token) { params.callback = function invisibleReCaptchaCallback(
recaptchaResponseToken,
) {
const formData = form.mailpoetSerializeObject() || {}; const formData = form.mailpoetSerializeObject() || {};
formData.data.recaptcha = token; formData.data.recaptchaResponseToken = recaptchaResponseToken;
submitSubscribeForm(form, formData, form.parsley()); submitSubscribeForm(form, formData, form.parsley());
}; };
@ -398,7 +400,7 @@ jQuery(($) => {
const formData = form.mailpoetSerializeObject() || {}; const formData = form.mailpoetSerializeObject() || {};
const size = form.find('.mailpoet_recaptcha').attr('data-size'); const size = form.find('.mailpoet_recaptcha').attr('data-size');
if (window.grecaptcha && formData.recaptcha) { if (window.grecaptcha && formData.recaptchaWidgetId) {
// The API for the invisible and checkbox ReCaptchas is slightly different. For the // The API for the invisible and checkbox ReCaptchas is slightly different. For the
// former, we need to call execute() and then the ReCaptcha API calls the callback set // former, we need to call execute() and then the ReCaptcha API calls the callback set
// inside renderCaptcha() with a token if the captcha was solved successfully. The // inside renderCaptcha() with a token if the captcha was solved successfully. The
@ -408,9 +410,8 @@ jQuery(($) => {
if (size === 'invisible') { if (size === 'invisible') {
window.grecaptcha.execute(); window.grecaptcha.execute();
} else { } else {
formData.data.recaptcha = window.grecaptcha.getResponse( formData.data.recaptchaResponseToken =
formData.recaptcha, window.grecaptcha.getResponse(formData.recaptchaWidgetId);
);
} }
} }

View File

@ -113,7 +113,7 @@ class Renderer {
</div> </div>
</div> </div>
</noscript> </noscript>
<input class="mailpoet_recaptcha_field" type="hidden" name="recaptcha"> <input class="mailpoet_recaptcha_field" type="hidden" name="recaptchaWidgetId">
</div>'; </div>';
return $html; return $html;

View File

@ -216,7 +216,7 @@ class SubscriberSubscribeController {
} }
} }
if (Captcha::isReCaptcha($captchaSettings['type']) && empty($data['recaptcha'])) { if (Captcha::isReCaptcha($captchaSettings['type']) && empty($data['recaptchaResponseToken'])) {
return ['error' => __('Please check the CAPTCHA.', 'mailpoet')]; return ['error' => __('Please check the CAPTCHA.', 'mailpoet')];
} }
@ -227,7 +227,7 @@ class SubscriberSubscribeController {
$secretToken = $captchaSettings['recaptcha_secret_token']; $secretToken = $captchaSettings['recaptcha_secret_token'];
} }
$response = empty($data['recaptcha']) ? $data['recaptcha-no-js'] : $data['recaptcha']; $response = empty($data['recaptchaResponseToken']) ? $data['recaptcha-no-js'] : $data['recaptchaResponseToken'];
$response = $this->wp->wpRemotePost('https://www.google.com/recaptcha/api/siteverify', [ $response = $this->wp->wpRemotePost('https://www.google.com/recaptcha/api/siteverify', [
'body' => [ 'body' => [
'secret' => $secretToken, 'secret' => $secretToken,