Update the captcha setting [MAILPOET-2015]

This commit is contained in:
wxa
2019-07-03 12:59:06 +03:00
committed by M. Shull
parent e022d5a9de
commit f8ea514887
6 changed files with 86 additions and 34 deletions

View File

@@ -13,6 +13,7 @@ use MailPoet\Services\Bridge;
use MailPoet\Settings\Hosts;
use MailPoet\Settings\Pages;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscription\Captcha;
use MailPoet\Util\Installation;
use MailPoet\Util\License\License;
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
@@ -89,6 +90,7 @@ class Settings {
'web' => Hosts::getWebHosts(),
'smtp' => Hosts::getSMTPHosts(),
],
'built_in_captcha_supported' => (new Captcha)->isSupported(),
];
$data['is_new_user'] = $this->installation->isNewInstallation();

View File

@@ -23,6 +23,7 @@ use MailPoet\Settings\Pages;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\NewSubscriberNotificationMailer;
use MailPoet\Subscribers\Source;
use MailPoet\Subscription\Captcha;
use MailPoet\Util\Helpers;
use MailPoet\WP\Functions as WPFunctions;
@@ -217,13 +218,21 @@ class Populator {
$this->settings->set('installed_at', date("Y-m-d H:i:s"));
}
// set reCaptcha settings
// set captcha settings
$captcha = $this->settings->fetch('captcha');
$re_captcha = $this->settings->fetch('re_captcha');
if (empty($re_captcha)) {
$this->settings->set('re_captcha', [
'enabled' => false,
'site_token' => '',
'secret_token' => '',
if (empty($captcha)) {
$subscription_captcha = new Captcha;
$captcha_type = Captcha::TYPE_DISABLED;
if (!empty($re_captcha['enabled'])) {
$captcha_type = Captcha::TYPE_RECAPTCHA;
} elseif ($subscription_captcha->isSupported()) {
$captcha_type = Captcha::TYPE_BUILTIN;
}
$this->settings->set('captcha', [
'type' => $captcha_type,
'recaptcha_site_token' => !empty($re_captcha['site_token']) ? $re_captcha['site_token'] : '',
'recaptcha_secret_token' => !empty($re_captcha['secret_token']) ? $re_captcha['secret_token'] : '',
]);
}

View File

@@ -0,0 +1,13 @@
<?php
namespace MailPoet\Subscription;
class Captcha {
const TYPE_BUILTIN = 'built-in';
const TYPE_RECAPTCHA = 'recaptcha';
const TYPE_DISABLED = null;
function isSupported() {
return extension_loaded('gd') && function_exists('imagettftext');
}
}

View File

@@ -8,6 +8,7 @@ use MailPoet\WP\Functions as WPFunctions;
use Helper\WordPressHooks as WPHooksHelper;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscription\Captcha;
class SetupTest extends \MailPoetTest {
function _before() {
@@ -29,6 +30,13 @@ class SetupTest extends \MailPoetTest {
$signup_confirmation = $settings->fetch('signup_confirmation.enabled');
expect($signup_confirmation)->true();
$captcha = $settings->fetch('captcha');
$subscription_captcha = new Captcha;
$captcha_type = $subscription_captcha->isSupported() ? Captcha::TYPE_BUILTIN : Captcha::TYPE_DISABLED;
expect($captcha['type'])->equals($captcha_type);
expect($captcha['recaptcha_site_token'])->equals('');
expect($captcha['recaptcha_secret_token'])->equals('');
$woocommerce_optin_on_checkout = $settings->fetch('woocommerce.optin_on_checkout');
expect($woocommerce_optin_on_checkout['enabled'])->true();

View File

@@ -88,15 +88,15 @@
errorFound = true;
}
// if reCAPTCHA is enabled but keys are emty, show error
var enabled = $('input[name="re_captcha[enabled]"]:checked').val(),
site_key = $('input[name="re_captcha[site_token]"]').val().trim(),
secret_key = $('input[name="re_captcha[secret_token]"]').val().trim();
var enabled = $('input[name="captcha[type]"]:checked').val() == 'recaptcha',
site_key = $('input[name="captcha[recaptcha_site_token]"]').val().trim(),
secret_key = $('input[name="captcha[recaptcha_secret_token]"]').val().trim();
if (enabled && (site_key == '' || secret_key == '')) {
$('#settings_re_captcha_tokens_error').show();
$('#settings_recaptcha_tokens_error').show();
window.location.href = '#advanced';
errorFound = true;
} else {
$('#settings_re_captcha_tokens_error').hide();
$('#settings_recaptcha_tokens_error').hide();
}
// if new subscriber notification is enabled but sender is empty, show error
var notifications_enabled = $('input[name="subscriber_email_notification[enabled]"]:checked').val(),
@@ -202,15 +202,15 @@
$('input[data-toggle]').each(toggleContent);
function toggleReCaptchaSettings() {
if ($('input[name="re_captcha[enabled]"]:checked').val()) {
$('#settings_re_captcha_tokens').show();
if ($('input[name="captcha[type]"]:checked').val() == 'recaptcha') {
$('#settings_recaptcha_tokens').show();
} else {
$('#settings_re_captcha_tokens').hide();
$('#settings_recaptcha_tokens').hide();
}
}
$('input[name="re_captcha[enabled]"]').on('click', toggleReCaptchaSettings);
$('input[name="captcha[type]"]').on('click', toggleReCaptchaSettings);
toggleReCaptchaSettings();
$('#settings_re_captcha_tokens_error').hide();
$('#settings_recaptcha_tokens_error').hide();
$('#settings_subscriber_email_notification_error').hide();
$('#settings_stats_notifications_error').hide();

View File

@@ -268,14 +268,14 @@
</p>
</td>
</tr>
<!-- reCaptcha settings -->
<!-- captcha settings -->
<tr>
<th scope="row">
<label>
<%= __('Enable reCAPTCHA') %>
<%= __('Protect your forms against spam signups') %>
</label>
<p class="description">
<%= __('Use reCAPTCHA to protect MailPoet subscription forms.') %>
<%= __('Built-in captcha protects your subscription forms after a second signup attempt by a bot. Alternatively, use reCAPTCHA by Google.') %>
<a
href="https://www.google.com/recaptcha/admin"
target="_blank"
@@ -287,46 +287,66 @@
<label>
<input
type="radio"
name="re_captcha[enabled]"
value="1"
<% if(settings.re_captcha.enabled) %>
name="captcha[type]"
value="built-in"
<% if(not(built_in_captcha_supported)) %>
disabled="disabled"
<% endif %>
<% if(settings.captcha.type == 'built-in') %>
checked="checked"
<% endif %>
/><%= __('Yes') %>
/><%= __('Built-in captcha (default)') %>
<% if(not(built_in_captcha_supported)) %>
<%= __('(disabled because GD and FreeType extensions are missing)') %>
<% endif %>
</label>
&nbsp;
<input type="hidden" name="subscription[pages][captcha]" value="<%= settings.subscription.pages.manage %>" />
</p>
<p>
<label>
<input
type="radio"
name="re_captcha[enabled]"
value=""
<% if not(settings.re_captcha.enabled) %>
name="captcha[type]"
value="recaptcha"
<% if(settings.captcha.type == 'recaptcha') %>
checked="checked"
<% endif %>
/><%= __('No') %>
/><%= __('Google reCAPTCHA') %>
</label>
</p>
<div id="settings_re_captcha_tokens">
<div id="settings_recaptcha_tokens">
<p>
<input type="text"
name="re_captcha[site_token]"
value="<%= settings.re_captcha.site_token %>"
name="captcha[recaptcha_site_token]"
value="<%= settings.captcha.recaptcha_site_token %>"
placeholder="<%= __('Your reCAPTCHA Site Key') %>"
class="regular-text"
/>
</p>
<p>
<input type="text"
name="re_captcha[secret_token]"
value="<%= settings.re_captcha.secret_token %>"
name="captcha[recaptcha_secret_token]"
value="<%= settings.captcha.recaptcha_secret_token %>"
placeholder="<%= __('Your reCAPTCHA Secret Key') %>"
class="regular-text"
/>
</p>
<div id="settings_re_captcha_tokens_error" class="mailpoet_error_item mailpoet_error">
<div id="settings_recaptcha_tokens_error" class="mailpoet_error_item mailpoet_error">
<%= __('Please fill the reCAPTCHA keys.') %>
</div>
</div>
<p>
<label>
<input
type="radio"
name="captcha[type]"
value=""
<% if not(settings.captcha.type) %>
checked="checked"
<% endif %>
/><%= __('Disable') %>
</label>
</p>
</td>
</tr>
<!-- reinstall -->