diff --git a/source/changelog.txt b/source/changelog.txt index 3fa8413..d2b6674 100644 --- a/source/changelog.txt +++ b/source/changelog.txt @@ -6,6 +6,7 @@ - Add donation link to plugin on plugins page #### Changed - Use `wp_remote_get()` instead of cURL functions for downloading the images +- Use date and time formats from general site settings #### Deprecated #### Removed #### Fixed diff --git a/source/connector-mobilizon.php b/source/connector-mobilizon.php index 75878a0..e9b8074 100644 --- a/source/connector-mobilizon.php +++ b/source/connector-mobilizon.php @@ -16,7 +16,8 @@ require_once __DIR__ . '/includes/Constants.php'; require_once __DIR__ . '/includes/Api.php'; require_once __DIR__ . '/includes/EventsCache.php'; require_once __DIR__ . '/includes/Settings.php'; -require_once __DIR__ . '/includes/DateTimeWrapper.php'; +require_once __DIR__ . '/includes/SiteSettings.php'; +require_once __DIR__ . '/includes/LocalDateTime.php'; require_once __DIR__ . '/includes/Formatter.php'; require_once __DIR__ . '/includes/GraphQlClient.php'; require_once __DIR__ . '/includes/EventsListBlock.php'; diff --git a/source/includes/DateTimeWrapper.php b/source/includes/DateTimeWrapper.php deleted file mode 100644 index 9c5a578..0000000 --- a/source/includes/DateTimeWrapper.php +++ /dev/null @@ -1,34 +0,0 @@ -dateTime = new \DateTime($text); - $this->locale = $locale; - $this->timeZone = new \DateTimeZone($timeZone); - } - - public function get24Time(): string { - $formatter = \IntlDateFormatter::create($this->locale, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT, $this->timeZone); - return $formatter->format($this->dateTime); - } - - public function getShortDate(): string { - $formatter = \IntlDateFormatter::create($this->locale, \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, $this->timeZone); - return $formatter->format($this->dateTime); - } - - public function getTimeZoneName(): string { - return $this->timeZone->getName(); - } -} diff --git a/source/includes/EventsListBlock.php b/source/includes/EventsListBlock.php index b376fa4..f91084c 100644 --- a/source/includes/EventsListBlock.php +++ b/source/includes/EventsListBlock.php @@ -51,9 +51,9 @@ class EventsListBlock { $events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); } - $locale = get_locale(); - $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); - $timeZone = wp_timezone_string(); + $dateFormat = SiteSettings::getDateFormat(); + $timeFormat = SiteSettings::getTimeFormat(); + $timeZone = SiteSettings::getTimeZone(); require dirname(__DIR__) . '/view/events-list.php'; } catch (GeneralException $e) { diff --git a/source/includes/EventsListShortcut.php b/source/includes/EventsListShortcut.php index 742e132..9a26a77 100644 --- a/source/includes/EventsListShortcut.php +++ b/source/includes/EventsListShortcut.php @@ -34,9 +34,9 @@ class EventsListShortcut { $events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); } - $locale = get_locale(); - $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); - $timeZone = wp_timezone_string(); + $dateFormat = SiteSettings::getDateFormat(); + $timeFormat = SiteSettings::getTimeFormat(); + $timeZone = SiteSettings::getTimeZone(); require dirname(__DIR__) . '/view/events-list.php'; } catch (GeneralException $e) { diff --git a/source/includes/EventsListWidget.php b/source/includes/EventsListWidget.php index bfe0a62..e4fbd88 100644 --- a/source/includes/EventsListWidget.php +++ b/source/includes/EventsListWidget.php @@ -34,9 +34,9 @@ class EventsListWidget extends \WP_Widget { $events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); } - $locale = get_locale(); - $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); - $timeZone = wp_timezone_string(); + $dateFormat = SiteSettings::getDateFormat(); + $timeFormat = SiteSettings::getTimeFormat(); + $timeZone = SiteSettings::getTimeZone(); require dirname(__DIR__) . '/view/events-list.php'; } catch (GeneralException $e) { diff --git a/source/includes/Formatter.php b/source/includes/Formatter.php deleted file mode 100644 index 8386e7d..0000000 --- a/source/includes/Formatter.php +++ /dev/null @@ -1,42 +0,0 @@ -getShortDate(); - $dateText .= ' ' . $startDateTime->get24Time(); - if (!$end && $isShortOffsetNameShown) { - $dateText .= ' (' . $startDateTime->getTimeZoneName() . ')'; - } - if ($end) { - $endDateTime = new DateTimeWrapper($end, $locale, $timeZone); - if ($startDateTime->getShortDate() != $endDateTime->getShortDate()) { - $dateText .= ' - '; - $dateText .= $endDateTime->getShortDate() . ' '; - } else { - $dateText .= ' - '; - } - $dateText .= $endDateTime->get24Time(); - if ($isShortOffsetNameShown) { - $dateText .= ' (' . $endDateTime->getTimeZoneName() . ')'; - } - } - return $dateText; - } - - public static function format_location(string $description, ?string $locality): string { - $location = ''; - if ($description && trim($description)) { - $location .= trim($description); - } - if ($location && $locality) { - $location .= ', '; - } - if ($locality) { - $location .= $locality; - } - return $location; - } -} diff --git a/source/includes/LineFormatter.php b/source/includes/LineFormatter.php new file mode 100644 index 0000000..09c4ef0 --- /dev/null +++ b/source/includes/LineFormatter.php @@ -0,0 +1,36 @@ +dateTime = $date->setTimezone($timeZone); + } + + public function getValue() { + return $this->dateTime; + } +} diff --git a/source/includes/LocalDateTimeFormatter.php b/source/includes/LocalDateTimeFormatter.php new file mode 100644 index 0000000..78e8ae1 --- /dev/null +++ b/source/includes/LocalDateTimeFormatter.php @@ -0,0 +1,10 @@ +getValue()->getTimestamp(); + return date_i18n($format, $timestamp); + } +} diff --git a/source/includes/Settings.php b/source/includes/Settings.php index de33123..6a2d1d9 100644 --- a/source/includes/Settings.php +++ b/source/includes/Settings.php @@ -46,7 +46,7 @@ class Settings { 'label_for' => self::$SETTING_FIELD_NAME_URL ) ); - add_settings_field( + add_settings_field( // TODO remove self::$SETTING_FIELD_NAME_IS_SHORT_OFFSET_NAME_SHOWN, esc_html__('Display named offset', 'connector-mobilizon'), 'MobilizonConnector\Settings::output_field_is_short_offset_name_shown', diff --git a/source/includes/SiteSettings.php b/source/includes/SiteSettings.php new file mode 100644 index 0000000..1b91059 --- /dev/null +++ b/source/includes/SiteSettings.php @@ -0,0 +1,21 @@ +
- +
- + diff --git a/tests/DateTimeWrapperTest.php b/tests/DateTimeWrapperTest.php deleted file mode 100644 index 7f173a2..0000000 --- a/tests/DateTimeWrapperTest.php +++ /dev/null @@ -1,47 +0,0 @@ -assertSame('16:45', $d->get24Time()); - } - - public function testCanGetShortDateForUsualDate(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z'); - $this->assertSame('24/12/2020', $d->getShortDate()); - } - - public function testCanGetShortDateForUsualDateWithLocaleWithUnderscore(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z'); - $this->assertSame('24/12/2020', $d->getShortDate(), 'en_GB'); - } - - public function testCanGetShortDateForUsualDateWithTimezoneString(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', 'Europe/Rome'); - $this->assertSame('24/12/2020', $d->getShortDate()); - } - - public function testCanGetShortDateForUsualDateWithNamedOffset(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', 'UTC'); - $this->assertSame('24/12/2020', $d->getShortDate()); - } - - public function testCanGetShortDateForUsualDateWithOffset(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', '+02:00'); - $this->assertSame('24/12/2020', $d->getShortDate()); - } - - public function testCanGetShortDateForUsualDateWithEmptyTimezone(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', ''); - $this->assertSame('24/12/2020', $d->getShortDate()); - } - - public function testCanGetShortOffsetNameForUsualTime(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z'); - $this->assertSame('UTC', $d->getTimeZoneName()); - } -} diff --git a/tests/FormatterTest.php b/tests/FormatterTest.php deleted file mode 100644 index 1516080..0000000 --- a/tests/FormatterTest.php +++ /dev/null @@ -1,56 +0,0 @@ -assertSame('15/04/2021 10:30 - 15:30', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false)); - } - - public function testCanDateFormatOneDateWithOffset(): void { - $this->assertSame('15/04/2021 10:30 - 15:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', true)); - } - - public function testCanDateFormatOneDateWithTimeZoneOffset(): void { - $this->assertSame('15/04/2021 11:30 - 16:30', Formatter::format_date('en-GB', '+01:00', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false)); - } - - public function testCanDateFormatTwoDates(): void { - $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', false)); - } - - public function testCanDateFormatTwoDatesWithOffset(): void { - $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', true)); - } - - public function testCanDateFormatWhenSecondDateIsNull(): void { - $this->assertSame('15/04/2021 10:30', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', null, false)); - } - - public function testCanDateFormatWhenSecondDateIsNullWithOffset(): void { - $this->assertSame('15/04/2021 10:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', null, true)); - } - - public function testCanLocationFormatBothParameters(): void { - $this->assertSame('a, b', Formatter::format_location('a', 'b')); - } - - public function testLocationFormatDescriptionOnly(): void { - $this->assertSame('a', Formatter::format_location('a', '')); - } - - public function testLocationFormatDescriptionOnlyWithNull(): void { - $this->assertSame('a', Formatter::format_location('a', null)); - } - - public function testLocationFormatDescriptionWithSpaceOnly(): void { - $this->assertSame('', Formatter::format_location(' ', '')); - } - - public function testLocationFormatLocalityOnly(): void { - $this->assertSame('a', Formatter::format_location('', 'a')); - } -} diff --git a/tests/LineFormatterTest.php b/tests/LineFormatterTest.php new file mode 100644 index 0000000..db73597 --- /dev/null +++ b/tests/LineFormatterTest.php @@ -0,0 +1,56 @@ +assertSame('15/04/2021 10:30 - 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false)); + } + + public function testCanDateFormatOneDateWithOffset(): void { + $this->assertSame('15/04/2021 10:30 - 15:30 (UTC)', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', true)); + } + + public function testCanDateFormatOneDateWithTimeZoneOffset(): void { + $this->assertSame('15/04/2021 11:30 - 16:30', LineFormatter::format_date_time(new \DateTimeZone('+01:00'), '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false)); + } + + public function testCanDateFormatTwoDates(): void { + $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', false)); + } + + public function testCanDateFormatTwoDatesWithOffset(): void { + $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30 (UTC)', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', true)); + } + + public function testCanDateFormatWhenSecondDateIsNull(): void { + $this->assertSame('15/04/2021 10:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', null, false)); + } + + public function testCanDateFormatWhenSecondDateIsNullWithOffset(): void { + $this->assertSame('15/04/2021 10:30 (UTC)', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', null, true)); + } + + public function testCanLocationFormatBothParameters(): void { + $this->assertSame('a, b', LineFormatter::format_location('a', 'b')); + } + + public function testLocationFormatDescriptionOnly(): void { + $this->assertSame('a', LineFormatter::format_location('a', '')); + } + + public function testLocationFormatDescriptionOnlyWithNull(): void { + $this->assertSame('a', LineFormatter::format_location('a', null)); + } + + public function testLocationFormatDescriptionWithSpaceOnly(): void { + $this->assertSame('', LineFormatter::format_location(' ', '')); + } + + public function testLocationFormatLocalityOnly(): void { + $this->assertSame('a', LineFormatter::format_location('', 'a')); + } +} diff --git a/tests/LocalDateTimeFormatterTest.php b/tests/LocalDateTimeFormatterTest.php new file mode 100644 index 0000000..aaaf94c --- /dev/null +++ b/tests/LocalDateTimeFormatterTest.php @@ -0,0 +1,17 @@ +assertSame('15 April 2021', LocalDateTimeFormatter::format(new LocalDateTime('2021-04-15T10:30:00Z', new DateTimeZone("utc")), 'j F Y')); + } + + public function testCanFormatTime(): void { + $this->assertSame('10:30', LocalDateTimeFormatter::format(new LocalDateTime('2021-04-15T10:30:00Z', new DateTimeZone("utc")), 'H:i')); + } +} diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php new file mode 100644 index 0000000..19e15aa --- /dev/null +++ b/tests/LocalDateTimeTest.php @@ -0,0 +1,12 @@ +assertEquals(new DateTimeImmutable('2020-12-24T16:45:00Z'), $d->getValue()); + } +}