Update the captcha setting [MAILPOET-2015]
This commit is contained in:
@@ -13,6 +13,7 @@ use MailPoet\Services\Bridge;
|
|||||||
use MailPoet\Settings\Hosts;
|
use MailPoet\Settings\Hosts;
|
||||||
use MailPoet\Settings\Pages;
|
use MailPoet\Settings\Pages;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
|
use MailPoet\Subscription\Captcha;
|
||||||
use MailPoet\Util\Installation;
|
use MailPoet\Util\Installation;
|
||||||
use MailPoet\Util\License\License;
|
use MailPoet\Util\License\License;
|
||||||
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
|
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
|
||||||
@@ -89,6 +90,7 @@ class Settings {
|
|||||||
'web' => Hosts::getWebHosts(),
|
'web' => Hosts::getWebHosts(),
|
||||||
'smtp' => Hosts::getSMTPHosts(),
|
'smtp' => Hosts::getSMTPHosts(),
|
||||||
],
|
],
|
||||||
|
'built_in_captcha_supported' => (new Captcha)->isSupported(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$data['is_new_user'] = $this->installation->isNewInstallation();
|
$data['is_new_user'] = $this->installation->isNewInstallation();
|
||||||
|
@@ -23,6 +23,7 @@ use MailPoet\Settings\Pages;
|
|||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Subscribers\NewSubscriberNotificationMailer;
|
use MailPoet\Subscribers\NewSubscriberNotificationMailer;
|
||||||
use MailPoet\Subscribers\Source;
|
use MailPoet\Subscribers\Source;
|
||||||
|
use MailPoet\Subscription\Captcha;
|
||||||
use MailPoet\Util\Helpers;
|
use MailPoet\Util\Helpers;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
@@ -217,13 +218,21 @@ class Populator {
|
|||||||
$this->settings->set('installed_at', date("Y-m-d H:i:s"));
|
$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');
|
$re_captcha = $this->settings->fetch('re_captcha');
|
||||||
if (empty($re_captcha)) {
|
if (empty($captcha)) {
|
||||||
$this->settings->set('re_captcha', [
|
$subscription_captcha = new Captcha;
|
||||||
'enabled' => false,
|
$captcha_type = Captcha::TYPE_DISABLED;
|
||||||
'site_token' => '',
|
if (!empty($re_captcha['enabled'])) {
|
||||||
'secret_token' => '',
|
$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'] : '',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
lib/Subscription/Captcha.php
Normal file
13
lib/Subscription/Captcha.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
@@ -8,6 +8,7 @@ use MailPoet\WP\Functions as WPFunctions;
|
|||||||
use Helper\WordPressHooks as WPHooksHelper;
|
use Helper\WordPressHooks as WPHooksHelper;
|
||||||
use MailPoet\API\JSON\Response as APIResponse;
|
use MailPoet\API\JSON\Response as APIResponse;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
|
use MailPoet\Subscription\Captcha;
|
||||||
|
|
||||||
class SetupTest extends \MailPoetTest {
|
class SetupTest extends \MailPoetTest {
|
||||||
function _before() {
|
function _before() {
|
||||||
@@ -29,6 +30,13 @@ class SetupTest extends \MailPoetTest {
|
|||||||
$signup_confirmation = $settings->fetch('signup_confirmation.enabled');
|
$signup_confirmation = $settings->fetch('signup_confirmation.enabled');
|
||||||
expect($signup_confirmation)->true();
|
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');
|
$woocommerce_optin_on_checkout = $settings->fetch('woocommerce.optin_on_checkout');
|
||||||
expect($woocommerce_optin_on_checkout['enabled'])->true();
|
expect($woocommerce_optin_on_checkout['enabled'])->true();
|
||||||
|
|
||||||
|
@@ -88,15 +88,15 @@
|
|||||||
errorFound = true;
|
errorFound = true;
|
||||||
}
|
}
|
||||||
// if reCAPTCHA is enabled but keys are emty, show error
|
// if reCAPTCHA is enabled but keys are emty, show error
|
||||||
var enabled = $('input[name="re_captcha[enabled]"]:checked').val(),
|
var enabled = $('input[name="captcha[type]"]:checked').val() == 'recaptcha',
|
||||||
site_key = $('input[name="re_captcha[site_token]"]').val().trim(),
|
site_key = $('input[name="captcha[recaptcha_site_token]"]').val().trim(),
|
||||||
secret_key = $('input[name="re_captcha[secret_token]"]').val().trim();
|
secret_key = $('input[name="captcha[recaptcha_secret_token]"]').val().trim();
|
||||||
if (enabled && (site_key == '' || secret_key == '')) {
|
if (enabled && (site_key == '' || secret_key == '')) {
|
||||||
$('#settings_re_captcha_tokens_error').show();
|
$('#settings_recaptcha_tokens_error').show();
|
||||||
window.location.href = '#advanced';
|
window.location.href = '#advanced';
|
||||||
errorFound = true;
|
errorFound = true;
|
||||||
} else {
|
} else {
|
||||||
$('#settings_re_captcha_tokens_error').hide();
|
$('#settings_recaptcha_tokens_error').hide();
|
||||||
}
|
}
|
||||||
// if new subscriber notification is enabled but sender is empty, show error
|
// if new subscriber notification is enabled but sender is empty, show error
|
||||||
var notifications_enabled = $('input[name="subscriber_email_notification[enabled]"]:checked').val(),
|
var notifications_enabled = $('input[name="subscriber_email_notification[enabled]"]:checked').val(),
|
||||||
@@ -202,15 +202,15 @@
|
|||||||
$('input[data-toggle]').each(toggleContent);
|
$('input[data-toggle]').each(toggleContent);
|
||||||
|
|
||||||
function toggleReCaptchaSettings() {
|
function toggleReCaptchaSettings() {
|
||||||
if ($('input[name="re_captcha[enabled]"]:checked').val()) {
|
if ($('input[name="captcha[type]"]:checked').val() == 'recaptcha') {
|
||||||
$('#settings_re_captcha_tokens').show();
|
$('#settings_recaptcha_tokens').show();
|
||||||
} else {
|
} 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();
|
toggleReCaptchaSettings();
|
||||||
$('#settings_re_captcha_tokens_error').hide();
|
$('#settings_recaptcha_tokens_error').hide();
|
||||||
|
|
||||||
$('#settings_subscriber_email_notification_error').hide();
|
$('#settings_subscriber_email_notification_error').hide();
|
||||||
$('#settings_stats_notifications_error').hide();
|
$('#settings_stats_notifications_error').hide();
|
||||||
|
@@ -268,14 +268,14 @@
|
|||||||
</p>
|
</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<!-- reCaptcha settings -->
|
<!-- captcha settings -->
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="row">
|
<th scope="row">
|
||||||
<label>
|
<label>
|
||||||
<%= __('Enable reCAPTCHA') %>
|
<%= __('Protect your forms against spam signups') %>
|
||||||
</label>
|
</label>
|
||||||
<p class="description">
|
<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
|
<a
|
||||||
href="https://www.google.com/recaptcha/admin"
|
href="https://www.google.com/recaptcha/admin"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
@@ -287,46 +287,66 @@
|
|||||||
<label>
|
<label>
|
||||||
<input
|
<input
|
||||||
type="radio"
|
type="radio"
|
||||||
name="re_captcha[enabled]"
|
name="captcha[type]"
|
||||||
value="1"
|
value="built-in"
|
||||||
<% if(settings.re_captcha.enabled) %>
|
<% if(not(built_in_captcha_supported)) %>
|
||||||
|
disabled="disabled"
|
||||||
|
<% endif %>
|
||||||
|
<% if(settings.captcha.type == 'built-in') %>
|
||||||
checked="checked"
|
checked="checked"
|
||||||
<% endif %>
|
<% endif %>
|
||||||
/><%= __('Yes') %>
|
/><%= __('Built-in captcha (default)') %>
|
||||||
|
<% if(not(built_in_captcha_supported)) %>
|
||||||
|
<%= __('(disabled because GD and FreeType extensions are missing)') %>
|
||||||
|
<% endif %>
|
||||||
</label>
|
</label>
|
||||||
|
<input type="hidden" name="subscription[pages][captcha]" value="<%= settings.subscription.pages.manage %>" />
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
<label>
|
<label>
|
||||||
<input
|
<input
|
||||||
type="radio"
|
type="radio"
|
||||||
name="re_captcha[enabled]"
|
name="captcha[type]"
|
||||||
value=""
|
value="recaptcha"
|
||||||
<% if not(settings.re_captcha.enabled) %>
|
<% if(settings.captcha.type == 'recaptcha') %>
|
||||||
checked="checked"
|
checked="checked"
|
||||||
<% endif %>
|
<% endif %>
|
||||||
/><%= __('No') %>
|
/><%= __('Google reCAPTCHA') %>
|
||||||
</label>
|
</label>
|
||||||
</p>
|
</p>
|
||||||
<div id="settings_re_captcha_tokens">
|
<div id="settings_recaptcha_tokens">
|
||||||
<p>
|
<p>
|
||||||
<input type="text"
|
<input type="text"
|
||||||
name="re_captcha[site_token]"
|
name="captcha[recaptcha_site_token]"
|
||||||
value="<%= settings.re_captcha.site_token %>"
|
value="<%= settings.captcha.recaptcha_site_token %>"
|
||||||
placeholder="<%= __('Your reCAPTCHA Site Key') %>"
|
placeholder="<%= __('Your reCAPTCHA Site Key') %>"
|
||||||
class="regular-text"
|
class="regular-text"
|
||||||
/>
|
/>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<input type="text"
|
<input type="text"
|
||||||
name="re_captcha[secret_token]"
|
name="captcha[recaptcha_secret_token]"
|
||||||
value="<%= settings.re_captcha.secret_token %>"
|
value="<%= settings.captcha.recaptcha_secret_token %>"
|
||||||
placeholder="<%= __('Your reCAPTCHA Secret Key') %>"
|
placeholder="<%= __('Your reCAPTCHA Secret Key') %>"
|
||||||
class="regular-text"
|
class="regular-text"
|
||||||
/>
|
/>
|
||||||
</p>
|
</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.') %>
|
<%= __('Please fill the reCAPTCHA keys.') %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<p>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="captcha[type]"
|
||||||
|
value=""
|
||||||
|
<% if not(settings.captcha.type) %>
|
||||||
|
checked="checked"
|
||||||
|
<% endif %>
|
||||||
|
/><%= __('Disable') %>
|
||||||
|
</label>
|
||||||
|
</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<!-- reinstall -->
|
<!-- reinstall -->
|
||||||
|
Reference in New Issue
Block a user