mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
wip
This commit is contained in:
@ -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
|
||||
|
@ -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';
|
||||
|
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class DateTimeWrapper {
|
||||
private $dateTime;
|
||||
private $locale;
|
||||
private $timeZone;
|
||||
|
||||
public function __construct(string $text, string $locale = 'en-GB', string $timeZone = 'utc') {
|
||||
if (!$locale) {
|
||||
$locale = 'en-GB';
|
||||
}
|
||||
if (!$timeZone) {
|
||||
$timeZone = 'utc';
|
||||
}
|
||||
$this->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();
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -1,42 +0,0 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class Formatter
|
||||
{
|
||||
public static function format_date(string $locale, string $timeZone, string $start, ?string $end, bool $isShortOffsetNameShown): string {
|
||||
$startDateTime = new DateTimeWrapper($start, $locale, $timeZone);
|
||||
$dateText = $startDateTime->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;
|
||||
}
|
||||
}
|
36
source/includes/LineFormatter.php
Normal file
36
source/includes/LineFormatter.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class LineFormatter
|
||||
{
|
||||
public static function format_date_time(\DateTimeZone $timeZone, string $dateFormat, string $timeFormat, string $start, ?string $end): string {
|
||||
$startDateTime = new LocalDateTime($start, $timeZone);
|
||||
$dateText = LocalDateTimeFormatter::format($startDateTime, $dateFormat);
|
||||
$dateText .= ' ' . LocalDateTimeFormatter::format($startDateTime, $timeFormat);
|
||||
if ($end) {
|
||||
$endDateTime = new LocalDateTime($end, $timeZone);
|
||||
if (LocalDateTimeFormatter::format($startDateTime, $dateFormat) != LocalDateTimeFormatter::format($endDateTime, $dateFormat)) {
|
||||
$dateText .= ' - ';
|
||||
$dateText .= LocalDateTimeFormatter::format($endDateTime, $dateFormat) . ' ';
|
||||
} else {
|
||||
$dateText .= ' - ';
|
||||
}
|
||||
$dateText .= LocalDateTimeFormatter::format($endDateTime, $timeFormat);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
15
source/includes/LocalDateTime.php
Normal file
15
source/includes/LocalDateTime.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class LocalDateTime {
|
||||
private $dateTime;
|
||||
|
||||
public function __construct(string $text, \DateTimeZone $timeZone) {
|
||||
$date = new \DateTimeImmutable($text);
|
||||
$this->dateTime = $date->setTimezone($timeZone);
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
return $this->dateTime;
|
||||
}
|
||||
}
|
10
source/includes/LocalDateTimeFormatter.php
Normal file
10
source/includes/LocalDateTimeFormatter.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class LocalDateTimeFormatter
|
||||
{
|
||||
public static function format(LocalDateTime $dateTime, string $format) {
|
||||
$timestamp = $dateTime->getValue()->getTimestamp();
|
||||
return date_i18n($format, $timestamp);
|
||||
}
|
||||
}
|
@ -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',
|
||||
|
21
source/includes/SiteSettings.php
Normal file
21
source/includes/SiteSettings.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
class SiteSettings {
|
||||
|
||||
private static $OPTION_NAME_DATE_FORMAT = 'date_format';
|
||||
private static $OPTION_NAME_TIME_FORMAT = 'time_format';
|
||||
|
||||
public static function getDateFormat() {
|
||||
return get_option(self::$OPTION_NAME_DATE_FORMAT);
|
||||
}
|
||||
|
||||
public static function getTimeFormat() {
|
||||
return get_option(self::$OPTION_NAME_TIME_FORMAT);
|
||||
}
|
||||
|
||||
public static function getTimeZone() {
|
||||
return wp_timezone();
|
||||
}
|
||||
|
||||
}
|
@ -15,10 +15,10 @@ if (!defined('ABSPATH')) {
|
||||
<?php } ?>
|
||||
<a href="<?php echo esc_attr($event['url']); ?>"><?php echo esc_html($event['title']); ?></a>
|
||||
<br>
|
||||
<?php echo esc_html(Formatter::format_date($locale, $timeZone, $event['beginsOn'], $event['endsOn'], $isShortOffsetNameShown)); ?>
|
||||
<?php echo esc_html(LineFormatter::format_date_time($timeZone, $dateFormat, $timeFormat, $event['beginsOn'], $event['endsOn'])); ?>
|
||||
<?php if (isset($event['physicalAddress'])) { ?>
|
||||
<br>
|
||||
<?php echo esc_html(Formatter::format_location($event['physicalAddress']['description'], $event['physicalAddress']['locality'])) ?>
|
||||
<?php echo esc_html(LineFormatter::format_location($event['physicalAddress']['description'], $event['physicalAddress']['locality'])) ?>
|
||||
<?php } ?>
|
||||
</li>
|
||||
<?php } ?>
|
||||
|
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\DateTimeWrapper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class DateTimeWrapperTest extends TestCase {
|
||||
public function testCanGet24TimeForUsualTime(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z');
|
||||
$this->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());
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\Formatter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FormatterTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testCanDateFormatOneDate(): void {
|
||||
$this->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'));
|
||||
}
|
||||
}
|
56
tests/LineFormatterTest.php
Normal file
56
tests/LineFormatterTest.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\LineFormatter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class LineFormatterTest extends TestCase
|
||||
{
|
||||
public function testCanDateFormatOneDate(): void {
|
||||
$this->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'));
|
||||
}
|
||||
}
|
17
tests/LocalDateTimeFormatterTest.php
Normal file
17
tests/LocalDateTimeFormatterTest.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\LocalDateTime;
|
||||
use MobilizonConnector\LocalDateTimeFormatter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class LocalDateTimeFormatterTest extends TestCase
|
||||
{
|
||||
public function testCanFormatDate(): void {
|
||||
$this->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'));
|
||||
}
|
||||
}
|
12
tests/LocalDateTimeTest.php
Normal file
12
tests/LocalDateTimeTest.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\LocalDateTime;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class LocalDateTimeTest extends TestCase {
|
||||
public function testCanGet24TimeForUsualTime(): void {
|
||||
$d = new LocalDateTime('2020-12-24T16:45:00Z', new DateTimeZone("utc"));
|
||||
$this->assertEquals(new DateTimeImmutable('2020-12-24T16:45:00Z'), $d->getValue());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user