fixing minor issues and adding unit test
This commit is contained in:
@ -72,6 +72,10 @@ function ( // eslint-disable-line func-names
|
||||
form.trigger('reset');
|
||||
// reset validation
|
||||
parsley.reset();
|
||||
// reset captcha
|
||||
if (window.grecaptcha) {
|
||||
window.grecaptcha.reset();
|
||||
}
|
||||
|
||||
// resize iframe
|
||||
if (
|
||||
|
@ -59,8 +59,8 @@ class API {
|
||||
|
||||
$ignoreToken = (
|
||||
Setting::getValue('re_captcha.enabled') &&
|
||||
$this->_request_endpoint == 'subscribers' &&
|
||||
$this->_request_method == 'subscribe'
|
||||
$this->_request_endpoint === 'subscribers' &&
|
||||
$this->_request_method === 'subscribe'
|
||||
);
|
||||
|
||||
if(!$ignoreToken && $this->checkToken() === false) {
|
||||
|
@ -90,13 +90,13 @@ class Subscribers extends APIEndpoint {
|
||||
));
|
||||
}
|
||||
|
||||
if($recaptcha['enabled'] && !isset($data['recaptcha'])) {
|
||||
if(!empty($recaptcha['enabled']) && $recaptcha['enabled'] && !isset($data['recaptcha'])) {
|
||||
return $this->badRequest(array(
|
||||
APIError::BAD_REQUEST => __('Please check the captcha.', 'mailpoet')
|
||||
));
|
||||
}
|
||||
|
||||
if($recaptcha['enabled']) {
|
||||
if(!empty($recaptcha['enabled']) && $recaptcha['enabled']) {
|
||||
$res = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array(
|
||||
'body' => array(
|
||||
'secret' => $recaptcha['secret_token'],
|
||||
@ -108,8 +108,8 @@ class Subscribers extends APIEndpoint {
|
||||
APIError::BAD_REQUEST => __('Error while validating the captcha.', 'mailpoet')
|
||||
));
|
||||
}
|
||||
$res = json_decode($res['body']);
|
||||
if(!$res->success) {
|
||||
$res = json_decode(wp_remote_retrieve_body($res));
|
||||
if(empty($res) || !$res->success) {
|
||||
return $this->badRequest(array(
|
||||
APIError::BAD_REQUEST => __('Error while validating the captcha.', 'mailpoet')
|
||||
));
|
||||
|
@ -41,23 +41,19 @@ class Renderer {
|
||||
}
|
||||
|
||||
static function renderBlocks($blocks = array(), $honeypot_enabled = true) {
|
||||
$html = array();
|
||||
// add honeypot for spambots
|
||||
$html[] = ($honeypot_enabled) ?
|
||||
$html = ($honeypot_enabled) ?
|
||||
'<label class="mailpoet_hp_email_label">' . __('Please leave this field empty', 'mailpoet') . '<input type="email" name="data[email]"></label>' :
|
||||
'';
|
||||
foreach($blocks as $key => $block) {
|
||||
$html[] = static::renderBlock($block) . PHP_EOL;
|
||||
}
|
||||
|
||||
if(Setting::getValue('re_captcha.enabled')) {
|
||||
$submit = array_pop($html);
|
||||
$site_key = Setting::getValue('re_captcha.site_token');
|
||||
$html[] = '<div class="g-recaptcha" data-size="compact" data-sitekey="'. $site_key .'"></div>';
|
||||
$html[] = $submit;
|
||||
if($block['type'] == 'submit' && Setting::getValue('re_captcha.enabled')) {
|
||||
$site_key = Setting::getValue('re_captcha.site_token');
|
||||
$html .= '<div class="g-recaptcha" data-size="compact" data-sitekey="'. $site_key .'"></div>';
|
||||
}
|
||||
$html .= static::renderBlock($block) . PHP_EOL;
|
||||
}
|
||||
|
||||
return implode('', $html);
|
||||
return $html;
|
||||
}
|
||||
|
||||
static function renderBlock($block = array()) {
|
||||
|
@ -51,9 +51,6 @@ class Widget extends \WP_Widget {
|
||||
wp_print_scripts('jquery');
|
||||
wp_print_scripts('mailpoet_vendor');
|
||||
wp_print_scripts('mailpoet_public');
|
||||
if(Setting::getValue('re_captcha.enabled')) {
|
||||
echo self::RECAPTCHA_API_SCRIPT;
|
||||
}
|
||||
$scripts = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
@ -278,6 +275,9 @@ 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(
|
||||
|
@ -461,6 +461,19 @@ class SubscribersTest extends \MailPoetTest {
|
||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||
}
|
||||
|
||||
function testItCannotSubscribeWithoutCaptchaWhenEnabled() {
|
||||
Setting::setValue('re_captcha', array('enabled' => true));
|
||||
$router = new Subscribers();
|
||||
$response = $router->subscribe(array(
|
||||
$this->obfuscatedEmail => 'toto@mailpoet.com',
|
||||
'form_id' => $this->form->id,
|
||||
$this->obfuscatedSegments => array($this->segment_1->id, $this->segment_2->id)
|
||||
));
|
||||
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||
expect($response->errors[0]['message'])->equals('Please check the captcha.');
|
||||
Setting::setValue('re_captcha', array());
|
||||
}
|
||||
|
||||
function testItCanSubscribeWithoutSegmentsIfTheyAreSelectedByAdmin() {
|
||||
$form = $this->form->asArray();
|
||||
$form['settings']['segments_selected_by'] = 'admin';
|
||||
|
@ -70,6 +70,7 @@
|
||||
secret_key = $('input[name="re_captcha[secret_token]"]').val().trim();
|
||||
if (enabled && (site_key == '' || secret_key == '')) {
|
||||
$('#settings_re_captcha_tokens_error').show();
|
||||
window.location.href = '#advanced';
|
||||
return false;
|
||||
}
|
||||
// if we're setting up a sending method, try to activate it
|
||||
|
@ -207,18 +207,20 @@
|
||||
<input type="text"
|
||||
name="re_captcha[site_token]"
|
||||
value="<%= settings.re_captcha.site_token %>"
|
||||
placeholder="Your reCAPTCHA Site Key"
|
||||
placeholder="<%= __('Your reCAPTCHA Site Key') %>"
|
||||
class="regular-text"
|
||||
/>
|
||||
</p>
|
||||
<p>
|
||||
<input type="text"
|
||||
name="re_captcha[secret_token]"
|
||||
value="<%= settings.re_captcha.secret_token %>"
|
||||
placeholder="Your reCAPTCHA Secret Key"
|
||||
placeholder="<%= __('Your reCAPTCHA Secret Key') %>"
|
||||
class="regular-text"
|
||||
/>
|
||||
</p>
|
||||
<div id="settings_re_captcha_tokens_error" class="mailpoet_error_item mailpoet_error">
|
||||
Please fill the reCAPTCHA keys.
|
||||
<%= __('Please fill the reCAPTCHA keys.') %>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
|
Reference in New Issue
Block a user