diff --git a/assets/js/src/public.js b/assets/js/src/public.js
index b246e92bfd..b64b76f745 100644
--- a/assets/js/src/public.js
+++ b/assets/js/src/public.js
@@ -8,6 +8,16 @@ function ( // eslint-disable-line func-names
jQuery
) {
jQuery(function ($) { // eslint-disable-line func-names
+ window.reCaptchaCallback = function () {
+ $('.mailpoet_recaptcha').each(function() {
+ var sitekey = $(this).attr('data-sitekey');
+ var container = $(this).find('> .mailpoet_recaptcha_container').get(0);
+ var field = $(this).find('> .mailpoet_recaptcha_field');
+ var widget_id = window.grecaptcha.render(container, {sitekey: sitekey, size: 'compact'});
+ field.val(widget_id);
+ });
+ };
+
function isSameDomain(url) {
var link = document.createElement('a');
link.href = url;
@@ -37,8 +47,8 @@ function ( // eslint-disable-line func-names
return true;
}
- if (formData['g-recaptcha-response']) {
- formData.data.recaptcha = formData['g-recaptcha-response'];
+ if (window.grecaptcha && formData.recaptcha) {
+ formData.data.recaptcha = window.grecaptcha.getResponse(formData.recaptcha);
}
// ajax request
@@ -55,7 +65,14 @@ function ( // eslint-disable-line func-names
return error.message;
}).join('
')
).show();
+<<<<<<< HEAD
}).done(function (response) { // eslint-disable-line func-names
+=======
+ if (window.grecaptcha) {
+ window.grecaptcha.reset(formData.recaptcha);
+ }
+ }).done(function (response) {
+>>>>>>> handling multiple instances of reCaptcha
// successfully subscribed
if (
response.meta !== undefined
@@ -74,7 +91,7 @@ function ( // eslint-disable-line func-names
parsley.reset();
// reset captcha
if (window.grecaptcha) {
- window.grecaptcha.reset();
+ window.grecaptcha.reset(formData.recaptcha);
}
// resize iframe
diff --git a/lib/API/JSON/v1/Subscribers.php b/lib/API/JSON/v1/Subscribers.php
index 233ae87a18..11a6c5ef83 100644
--- a/lib/API/JSON/v1/Subscribers.php
+++ b/lib/API/JSON/v1/Subscribers.php
@@ -90,13 +90,13 @@ class Subscribers extends APIEndpoint {
));
}
- if(!empty($recaptcha['enabled']) && $recaptcha['enabled'] && !isset($data['recaptcha'])) {
+ if(!empty($recaptcha['enabled']) && !isset($data['recaptcha'])) {
return $this->badRequest(array(
APIError::BAD_REQUEST => __('Please check the captcha.', 'mailpoet')
));
}
- if(!empty($recaptcha['enabled']) && $recaptcha['enabled']) {
+ if(!empty($recaptcha['enabled'])) {
$res = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array(
'body' => array(
'secret' => $recaptcha['secret_token'],
@@ -109,7 +109,7 @@ class Subscribers extends APIEndpoint {
));
}
$res = json_decode(wp_remote_retrieve_body($res));
- if(empty($res) || !$res->success) {
+ if(empty($res->success)) {
return $this->badRequest(array(
APIError::BAD_REQUEST => __('Error while validating the captcha.', 'mailpoet')
));
diff --git a/lib/Form/Renderer.php b/lib/Form/Renderer.php
index f8aeac9d59..a9f4025812 100644
--- a/lib/Form/Renderer.php
+++ b/lib/Form/Renderer.php
@@ -48,7 +48,10 @@ class Renderer {
foreach($blocks as $key => $block) {
if($block['type'] == 'submit' && Setting::getValue('re_captcha.enabled')) {
$site_key = Setting::getValue('re_captcha.site_token');
- $html .= '