Convert throttling timeout from seconds to a human-readable string [MAILPOET-2015]
This commit is contained in:
@@ -206,7 +206,8 @@ class Subscribers extends APIEndpoint {
|
|||||||
$timeout = SubscriptionThrottling::throttle();
|
$timeout = SubscriptionThrottling::throttle();
|
||||||
|
|
||||||
if ($timeout > 0) {
|
if ($timeout > 0) {
|
||||||
throw new \Exception(sprintf(__('You need to wait %d seconds before subscribing again.', 'mailpoet'), $timeout));
|
$time_to_wait = SubscriptionThrottling::secondsToTimeString($timeout);
|
||||||
|
throw new \Exception(sprintf(__('You need to wait %s before subscribing again.', 'mailpoet'), $time_to_wait));
|
||||||
}
|
}
|
||||||
|
|
||||||
$subscriber = $this->subscriber_actions->subscribe($data, $segment_ids);
|
$subscriber = $this->subscriber_actions->subscribe($data, $segment_ids);
|
||||||
|
@@ -55,4 +55,17 @@ class Throttling {
|
|||||||
[$interval]
|
[$interval]
|
||||||
)->deleteMany();
|
)->deleteMany();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function secondsToTimeString($seconds) {
|
||||||
|
$wp = new WPFunctions;
|
||||||
|
$hrs = floor($seconds / 3600);
|
||||||
|
$min = floor($seconds % 3600 / 60);
|
||||||
|
$sec = $seconds % 3600 % 60;
|
||||||
|
$result = [
|
||||||
|
'hours' => $hrs ? sprintf($wp->__('%d hours', 'mailpoet'), $hrs) : '',
|
||||||
|
'minutes' => $min ? sprintf($wp->__('%d minutes', 'mailpoet'), $min) : '',
|
||||||
|
'seconds' => $sec ? sprintf($wp->__('%d seconds', 'mailpoet'), $sec) : '',
|
||||||
|
];
|
||||||
|
return join(' ', array_filter($result));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -608,7 +608,7 @@ class SubscribersTest extends \MailPoetTest {
|
|||||||
]);
|
]);
|
||||||
$this->fail('It should not be possible to subscribe a second time so soon');
|
$this->fail('It should not be possible to subscribe a second time so soon');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
expect($e->getMessage())->equals('You need to wait 60 seconds before subscribing again.');
|
expect($e->getMessage())->equals('You need to wait 1 minutes before subscribing again.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -635,7 +635,7 @@ class SubscribersTest extends \MailPoetTest {
|
|||||||
]);
|
]);
|
||||||
$this->fail('It should not be possible to resubscribe a second time so soon');
|
$this->fail('It should not be possible to resubscribe a second time so soon');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
expect($e->getMessage())->equals('You need to wait 60 seconds before subscribing again.');
|
expect($e->getMessage())->equals('You need to wait 1 minutes before subscribing again.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -55,6 +55,16 @@ class ThrottlingTest extends \MailPoetTest {
|
|||||||
expect(SubscriberIP::count())->equals(1);
|
expect(SubscriberIP::count())->equals(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItConvertsSecondsToTimeString() {
|
||||||
|
expect(Throttling::secondsToTimeString(122885))->equals('34 hours 8 minutes 5 seconds');
|
||||||
|
expect(Throttling::secondsToTimeString(3660))->equals('1 hours 1 minutes');
|
||||||
|
expect(Throttling::secondsToTimeString(3601))->equals('1 hours 1 seconds');
|
||||||
|
expect(Throttling::secondsToTimeString(3600))->equals('1 hours');
|
||||||
|
expect(Throttling::secondsToTimeString(61))->equals('1 minutes 1 seconds');
|
||||||
|
expect(Throttling::secondsToTimeString(60))->equals('1 minutes');
|
||||||
|
expect(Throttling::secondsToTimeString(59))->equals('59 seconds');
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
SubscriberIP::deleteMany();
|
SubscriberIP::deleteMany();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user