Fix session form data being reset if throttling kicks in [MAILPOET-2015]
This commit is contained in:
@ -23,12 +23,18 @@ jQuery(function ($) { // eslint-disable-line func-names
|
||||
}
|
||||
|
||||
function updateCaptcha(e) {
|
||||
var captcha = $('img.mailpoet_captcha');
|
||||
var captchaSrc = captcha.attr('src');
|
||||
var hashPos = captchaSrc.indexOf('#');
|
||||
var newSrc = hashPos > 0 ? captchaSrc.substring(0, hashPos) : captchaSrc;
|
||||
var captcha;
|
||||
var captchaSrc;
|
||||
var hashPos;
|
||||
var newSrc;
|
||||
captcha = $('img.mailpoet_captcha');
|
||||
if (!captcha.length) return false;
|
||||
captchaSrc = captcha.attr('src');
|
||||
hashPos = captchaSrc.indexOf('#');
|
||||
newSrc = hashPos > 0 ? captchaSrc.substring(0, hashPos) : captchaSrc;
|
||||
captcha.attr('src', newSrc + '#' + new Date().getTime());
|
||||
if (e) e.preventDefault();
|
||||
return true;
|
||||
}
|
||||
|
||||
$(function () { // eslint-disable-line func-names
|
||||
|
@ -162,7 +162,7 @@ class Subscribers extends APIEndpoint {
|
||||
$captcha_settings = $this->settings->get('captcha');
|
||||
|
||||
if (!empty($captcha_settings['type']) && $captcha_settings['type'] === Captcha::TYPE_BUILTIN) {
|
||||
if (empty($data['captcha'])) {
|
||||
if (!isset($data['captcha'])) {
|
||||
// Save form data to session
|
||||
$_SESSION[Captcha::SESSION_FORM_KEY] = array_merge($data, ['form_id' => $form_id]);
|
||||
} elseif (!empty($_SESSION[Captcha::SESSION_FORM_KEY])) {
|
||||
@ -207,7 +207,11 @@ class Subscribers extends APIEndpoint {
|
||||
|
||||
if ($timeout > 0) {
|
||||
$time_to_wait = SubscriptionThrottling::secondsToTimeString($timeout);
|
||||
throw new \Exception(sprintf(__('You need to wait %s before subscribing again.', 'mailpoet'), $time_to_wait));
|
||||
$meta = [];
|
||||
$meta['refresh_captcha'] = true;
|
||||
return $this->badRequest([
|
||||
APIError::BAD_REQUEST => sprintf(WPFunctions::get()->__('You need to wait %s before subscribing again.', 'mailpoet'), $time_to_wait),
|
||||
], $meta);
|
||||
}
|
||||
|
||||
$subscriber = $this->subscriber_actions->subscribe($data, $segment_ids);
|
||||
@ -216,6 +220,12 @@ class Subscribers extends APIEndpoint {
|
||||
if ($errors !== false) {
|
||||
return $this->badRequest($errors);
|
||||
} else {
|
||||
if (!empty($captcha_settings['type']) && $captcha_settings['type'] === Captcha::TYPE_BUILTIN) {
|
||||
// Captcha has been verified, invalidate the session vars
|
||||
$_SESSION[Captcha::SESSION_KEY] = null;
|
||||
$_SESSION[Captcha::SESSION_FORM_KEY] = null;
|
||||
}
|
||||
|
||||
$meta = [];
|
||||
|
||||
if ($form !== false) {
|
||||
@ -300,10 +310,6 @@ class Subscribers extends APIEndpoint {
|
||||
return $this->badRequest([
|
||||
APIError::BAD_REQUEST => WPFunctions::get()->__('The characters entered do not match with the previous captcha.', 'mailpoet'),
|
||||
], $meta);
|
||||
} else {
|
||||
// Captcha has been verified, invalidate the session vars
|
||||
$_SESSION[Captcha::SESSION_KEY] = null;
|
||||
$_SESSION[Captcha::SESSION_FORM_KEY] = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -600,16 +600,14 @@ class SubscribersTest extends \MailPoetTest {
|
||||
$this->obfuscatedSegments => [$this->segment_1->id, $this->segment_2->id],
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->endpoint->subscribe([
|
||||
$response = $this->endpoint->subscribe([
|
||||
$this->obfuscatedEmail => 'tata@mailpoet.com',
|
||||
'form_id' => $this->form->id,
|
||||
$this->obfuscatedSegments => [$this->segment_1->id, $this->segment_2->id],
|
||||
]);
|
||||
$this->fail('It should not be possible to subscribe a second time so soon');
|
||||
} catch (\Exception $e) {
|
||||
expect($e->getMessage())->equals('You need to wait 1 minutes before subscribing again.');
|
||||
}
|
||||
|
||||
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||
expect($response->errors[0]['message'])->equals('You need to wait 1 minutes before subscribing again.');
|
||||
}
|
||||
|
||||
function testItCannotMassResubscribe() {
|
||||
@ -627,16 +625,14 @@ class SubscribersTest extends \MailPoetTest {
|
||||
$subscriber->updated_at = Carbon::now();
|
||||
$subscriber->save();
|
||||
|
||||
try {
|
||||
$this->endpoint->subscribe([
|
||||
$response = $this->endpoint->subscribe([
|
||||
$this->obfuscatedEmail => $subscriber->email,
|
||||
'form_id' => $this->form->id,
|
||||
$this->obfuscatedSegments => [$this->segment_1->id, $this->segment_2->id],
|
||||
]);
|
||||
$this->fail('It should not be possible to resubscribe a second time so soon');
|
||||
} catch (\Exception $e) {
|
||||
expect($e->getMessage())->equals('You need to wait 1 minutes before subscribing again.');
|
||||
}
|
||||
|
||||
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||
expect($response->errors[0]['message'])->equals('You need to wait 1 minutes before subscribing again.');
|
||||
}
|
||||
|
||||
function testItSchedulesWelcomeEmailNotificationWhenSubscriberIsAdded() {
|
||||
|
Reference in New Issue
Block a user