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 .= '
'; + $html .= '
+
+ +
'; } $html .= static::renderBlock($block) . PHP_EOL; } diff --git a/lib/Form/Widget.php b/lib/Form/Widget.php index 26df4bfe90..4649d06c14 100644 --- a/lib/Form/Widget.php +++ b/lib/Form/Widget.php @@ -14,8 +14,6 @@ use MailPoet\WP\Hooks; if(!defined('ABSPATH')) exit; class Widget extends \WP_Widget { - const RECAPTCHA_API_SCRIPT = ''; - private $renderer; function __construct() { @@ -51,6 +49,7 @@ class Widget extends \WP_Widget { wp_print_scripts('jquery'); wp_print_scripts('mailpoet_vendor'); wp_print_scripts('mailpoet_public'); + echo ''; $scripts = ob_get_contents(); ob_end_clean(); @@ -111,6 +110,14 @@ class Widget extends \WP_Widget { true ); + if(!empty(Setting::getValue('re_captcha')) && Setting::getValue('re_captcha.enabled')) { + wp_enqueue_script( + 'mailpoet_recaptcha', + 'https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit', + array('mailpoet_public') + ); + } + wp_localize_script('mailpoet_public', 'MailPoetForm', array( 'ajax_url' => admin_url('admin-ajax.php'), 'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false) @@ -274,12 +281,6 @@ EOL; if(!empty($body)) { $form_id = $this->id_base . '_' . $form['id']; - if(Setting::getValue('re_captcha.enabled')) { - if(empty($before_widget)) { - $before_widget = ''; - } - $before_widget .= self::RECAPTCHA_API_SCRIPT; - } $data = array( 'form_id' => $form_id, 'form_type' => $form_type,