Encapsulate date formatting within the DateTime class [PREMIUM-4]

This commit is contained in:
Alexey Stoletniy
2017-05-09 08:54:12 +03:00
parent 90b2b46db4
commit 2eb98905b6
4 changed files with 18 additions and 7 deletions

View File

@ -1,7 +1,6 @@
<?php
namespace MailPoet\API\Endpoints\v1;
use Carbon\Carbon;
use MailPoet\API\Endpoint as APIEndpoint;
use MailPoet\API\Error as APIError;
use MailPoet\Services\Bridge;
@ -44,8 +43,7 @@ class Services extends APIEndpoint {
} elseif($state == Bridge::MAILPOET_KEY_EXPIRING) {
$success_message = sprintf(
__('Your MailPoet key expires on %s!', 'mailpoet'),
Carbon::createFromTimestamp(strtotime($result['data']['expire_at']))
->format($this->date_time->getDateFormat())
$this->date_time->formatDate(strtotime($result['data']['expire_at']))
);
}
@ -94,8 +92,7 @@ class Services extends APIEndpoint {
} elseif($state == Bridge::PREMIUM_KEY_EXPIRING) {
$success_message = sprintf(
__('Your license key expires on %s.', 'mailpoet'),
Carbon::createFromTimestamp(strtotime($result['data']['expire_at']))
->format($this->date_time->getDateFormat())
$this->date_time->formatDate(strtotime($result['data']['expire_at']))
);
}

View File

@ -36,7 +36,7 @@ class ServicesChecker {
) {
if($display_error_notice) {
$date_time = new DateTime();
$date = date($date_time->getDateFormat(), strtotime($mss_key['data']['expire_at']));
$date = $date_time->formatDate(strtotime($mss_key['data']['expire_at']));
$error = Helpers::replaceLinkTags(
__('Your newsletters are awesome! Don\'t forget to [link]upgrade your MailPoet email plan[/link] by %s to keep sending them to your subscribers.', 'mailpoet'),
'https://account.mailpoet.com?s=' . Subscriber::getTotalSubscribers()
@ -77,7 +77,7 @@ class ServicesChecker {
) {
if($display_error_notice) {
$date_time = new DateTime();
$date = date($date_time->getDateFormat(), strtotime($premium_key['data']['expire_at']));
$date = $date_time->formatDate(strtotime($premium_key['data']['expire_at']));
$error = Helpers::replaceLinkTags(
__('Your License Key is expiring! Don\'t forget to [link]renew your license[/link] by %s to keep enjoying automatic updates and Premium support.', 'mailpoet'),
'https://account.mailpoet.com'

View File

@ -38,6 +38,12 @@ class DateTime {
return date($format, $timestamp);
}
function formatDate($timestamp, $format=false) {
if (empty($format)) $format = $this->getDateFormat();
return date($format, $timestamp);
}
/**
* Generates a list of time strings within an interval,
* formatted and mapped from DEFAULT_TIME_FORMAT to WordPress time strings.

View File

@ -42,9 +42,17 @@ class DateTimeTest extends MailPoetTest {
function testFormatTime() {
$timestamp = 1234567;
$format = "H:i:s";
expect($this->date_time->formatTime($timestamp))->equals(date($this->date_time->getTimeFormat(), $timestamp));
expect($this->date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp));
}
function testFormatDate() {
$timestamp = 1234567;
$format = "Y-m-d";
expect($this->date_time->formatDate($timestamp))->equals(date($this->date_time->getDateFormat(), $timestamp));
expect($this->date_time->formatDate($timestamp, $format))->equals(date($format, $timestamp));
}
function testTimeInterval() {
$one_hour_interval = array_keys($this->date_time->getTimeInterval(
'00:00:00',