mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
Compare commits
9 Commits
main
...
use-site-d
Author | SHA1 | Date | |
---|---|---|---|
91cf9bcaae | |||
108e677abe | |||
1c3ea8462e | |||
4f9785a1a8 | |||
a6e720679c | |||
be8fd96ee9 | |||
9662e1f333 | |||
77485b3010 | |||
6af81bf53d |
@ -6,8 +6,10 @@
|
||||
- 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
|
||||
- Removed option "Display named offset"
|
||||
#### Fixed
|
||||
- Show group not found error message in block
|
||||
#### Security
|
||||
|
@ -16,8 +16,10 @@ 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/Formatter.php';
|
||||
require_once __DIR__ . '/includes/SiteSettings.php';
|
||||
require_once __DIR__ . '/includes/LocalDateTime.php';
|
||||
require_once __DIR__ . '/includes/LocalDateTimeFormatter.php';
|
||||
require_once __DIR__ . '/includes/LineFormatter.php';
|
||||
require_once __DIR__ . '/includes/GraphQlClient.php';
|
||||
require_once __DIR__ . '/includes/EventsListBlock.php';
|
||||
require_once __DIR__ . '/includes/EventsListShortcut.php';
|
||||
@ -72,13 +74,11 @@ final class Mobilizon_Connector {
|
||||
|
||||
public function enable_activation() {
|
||||
MobilizonConnector\Settings::setDefaultOptions();
|
||||
MobilizonConnector\Settings::removeObsoleteOptionsIfNeeded();
|
||||
}
|
||||
|
||||
private function load_settings_globally_before_script($scriptName) {
|
||||
$settings = array(
|
||||
'isShortOffsetNameShown' => MobilizonConnector\Settings::isShortOffsetNameShown(),
|
||||
'locale' => str_replace('_', '-', get_locale()),
|
||||
'timeZone' => wp_timezone_string(),
|
||||
'url' => MobilizonConnector\Settings::getUrl()
|
||||
);
|
||||
wp_add_inline_script($scriptName, 'var MOBILIZON_CONNECTOR = ' . json_encode($settings), 'before');
|
||||
|
@ -1,87 +0,0 @@
|
||||
import test from 'ava'
|
||||
import DateTimeWrapper from './date-time-wrapper.js'
|
||||
|
||||
test('#getShortDate usual date', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with timezone string', (t) => {
|
||||
const d = new DateTimeWrapper({
|
||||
text: '2020-12-24T16:45:00Z',
|
||||
timeZone: 'Europe/Rome',
|
||||
})
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with fixed offset', (t) => {
|
||||
const d = new DateTimeWrapper({
|
||||
text: '2020-12-24T16:45:00Z',
|
||||
timeZone: 'UTC+02:00',
|
||||
})
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with fixed offset without UTC prefix', (t) => {
|
||||
const d = new DateTimeWrapper({
|
||||
text: '2020-12-24T16:45:00Z',
|
||||
timeZone: '+02:00',
|
||||
})
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with empty time zone', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: '' })
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#get24Time usual time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.get24Time(), '16:45')
|
||||
})
|
||||
|
||||
test('#equalsDate same date, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2020-12-24T17:46:01Z' })
|
||||
t.true(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different date, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2021-11-25T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different day, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2020-12-25T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different month, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2020-11-24T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different year, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2021-12-24T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#getCurrentDatetimeAsString correct format', (t) => {
|
||||
const d = DateTimeWrapper.getCurrentDatetimeAsString()
|
||||
t.is(d[4], '-')
|
||||
t.is(d[7], '-')
|
||||
t.is(d[10], 'T')
|
||||
t.is(d[13], ':')
|
||||
t.is(d[16], ':')
|
||||
t.is(d[19], '.')
|
||||
t.is(d[d.length - 3], ':')
|
||||
})
|
||||
|
||||
test('#getShortOffsetName usual time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.getShortOffsetName(), 'UTC')
|
||||
})
|
@ -1,42 +0,0 @@
|
||||
import { DateTime } from 'luxon'
|
||||
|
||||
export default class DateTimeWrapper {
|
||||
constructor({ locale = 'en-GB', text, timeZone = 'utc' } = {}) {
|
||||
if (!timeZone) {
|
||||
timeZone = 'utc'
|
||||
}
|
||||
if (
|
||||
timeZone.includes(':') &&
|
||||
timeZone.substring(0, 3).toUpperCase() !== 'UTC'
|
||||
) {
|
||||
timeZone = 'UTC' + timeZone
|
||||
}
|
||||
this.dateTime = DateTime.fromISO(text, { locale, zone: timeZone })
|
||||
}
|
||||
|
||||
getShortDate() {
|
||||
return this.dateTime.toLocaleString(DateTime.DATE_SHORT)
|
||||
}
|
||||
|
||||
getShortOffsetName() {
|
||||
return this.dateTime.offsetNameShort
|
||||
}
|
||||
|
||||
get24Time() {
|
||||
return this.dateTime.toLocaleString(DateTime.TIME_24_SIMPLE)
|
||||
}
|
||||
|
||||
equalsDate(other) {
|
||||
return (
|
||||
this.dateTime &&
|
||||
other.dateTime &&
|
||||
this.dateTime.day === other.dateTime.day &&
|
||||
this.dateTime.month === other.dateTime.month &&
|
||||
this.dateTime.year === other.dateTime.year
|
||||
)
|
||||
}
|
||||
|
||||
static getCurrentDatetimeAsString() {
|
||||
return DateTime.now().toString()
|
||||
}
|
||||
}
|
@ -51,6 +51,10 @@ test('#displayEvents one event', (t) => {
|
||||
description: 'c',
|
||||
locality: 'd',
|
||||
},
|
||||
startDateFormatted: '15/04/2021',
|
||||
startTimeFormatted: '10:30',
|
||||
endDateFormatted: '15/04/2021',
|
||||
endTimeFormatted: '15:30',
|
||||
},
|
||||
]
|
||||
const container = t.context.container
|
||||
|
@ -9,11 +9,6 @@ export function clearEventsList(container) {
|
||||
export function displayEvents({ events, document, container, maxEventsCount }) {
|
||||
hideLoadingIndicator(container)
|
||||
|
||||
const isShortOffsetNameShown =
|
||||
window.MOBILIZON_CONNECTOR.isShortOffsetNameShown
|
||||
const locale = window.MOBILIZON_CONNECTOR.locale
|
||||
const timeZone = window.MOBILIZON_CONNECTOR.timeZone
|
||||
|
||||
const eventsCount = Math.min(maxEventsCount, events.length)
|
||||
const list = container.querySelector('ul')
|
||||
for (let i = 0; i < eventsCount; i++) {
|
||||
@ -43,11 +38,10 @@ export function displayEvents({ events, document, container, maxEventsCount }) {
|
||||
li.appendChild(br)
|
||||
|
||||
const date = Formatter.formatDate({
|
||||
locale,
|
||||
start: events[i].beginsOn,
|
||||
end: events[i].endsOn,
|
||||
timeZone,
|
||||
isShortOffsetNameShown,
|
||||
startDateFormatted: events[i].startDateFormatted,
|
||||
startTimeFormatted: events[i].startTimeFormatted,
|
||||
endDateFormatted: events[i].endDateFormatted,
|
||||
endTimeFormatted: events[i].endTimeFormatted,
|
||||
})
|
||||
const textnode = document.createTextNode(date)
|
||||
li.appendChild(textnode)
|
||||
|
@ -15,55 +15,34 @@ test('#escapeHTML', (t) => {
|
||||
|
||||
test('#formatDate one date', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-15T15:30:00Z',
|
||||
startDateFormatted: '15/04/2021',
|
||||
startTimeFormatted: '10:30',
|
||||
endDateFormatted: '15/04/2021',
|
||||
endTimeFormatted: '15:30',
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 15:30')
|
||||
})
|
||||
|
||||
test('#formatDate one date with short offset name', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-15T15:30:00Z',
|
||||
isShortOffsetNameShown: true,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 15:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatDate two dates', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-16T15:30:00Z',
|
||||
startDateFormatted: '15/04/2021',
|
||||
startTimeFormatted: '10:30',
|
||||
endDateFormatted: '16/04/2021',
|
||||
endTimeFormatted: '15:30',
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 16/04/2021 15:30')
|
||||
})
|
||||
|
||||
test('#formatDate two dates with short offset name', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-16T15:30:00Z',
|
||||
isShortOffsetNameShown: true,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 16/04/2021 15:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatDate second date is null', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: null,
|
||||
startDateFormatted: '15/04/2021',
|
||||
startTimeFormatted: '10:30',
|
||||
endDateFormatted: undefined,
|
||||
endTimeFormatted: undefined,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30')
|
||||
})
|
||||
|
||||
test('#formatDate second date is null with short offset name', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: null,
|
||||
isShortOffsetNameShown: true,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatLocation both parameters', (t) => {
|
||||
const location = Formatter.formatLocation({ description: 'a', locality: 'b' })
|
||||
t.is(location, 'a, b')
|
||||
|
@ -1,5 +1,3 @@
|
||||
import DateTimeWrapper from './date-time-wrapper.js'
|
||||
|
||||
export default class Formatter {
|
||||
static escapeHTML(input) {
|
||||
const div = document.createElement('div')
|
||||
@ -7,30 +5,22 @@ export default class Formatter {
|
||||
return div.innerHTML
|
||||
}
|
||||
|
||||
static formatDate({ locale, timeZone, start, end, isShortOffsetNameShown }) {
|
||||
const startDateTime = new DateTimeWrapper({
|
||||
locale,
|
||||
text: start,
|
||||
timeZone,
|
||||
})
|
||||
let dateText = startDateTime.getShortDate()
|
||||
dateText += ' ' + startDateTime.get24Time()
|
||||
if (!end && isShortOffsetNameShown) {
|
||||
dateText += ' (' + startDateTime.getShortOffsetName() + ')'
|
||||
}
|
||||
|
||||
if (end) {
|
||||
const endDateTime = new DateTimeWrapper({ locale, text: end, timeZone })
|
||||
if (!startDateTime.equalsDate(endDateTime)) {
|
||||
static formatDate({
|
||||
startDateFormatted,
|
||||
startTimeFormatted,
|
||||
endDateFormatted,
|
||||
endTimeFormatted,
|
||||
}) {
|
||||
let dateText = startDateFormatted
|
||||
dateText += ' ' + startTimeFormatted
|
||||
if (endDateFormatted) {
|
||||
if (startDateFormatted !== endDateFormatted) {
|
||||
dateText += ' - '
|
||||
dateText += endDateTime.getShortDate() + ' '
|
||||
dateText += endDateFormatted + ' '
|
||||
} else {
|
||||
dateText += ' - '
|
||||
}
|
||||
dateText += endDateTime.get24Time()
|
||||
if (isShortOffsetNameShown) {
|
||||
dateText += ' (' + endDateTime.getShortOffsetName() + ')'
|
||||
}
|
||||
dateText += endTimeFormatted
|
||||
}
|
||||
return dateText
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ class Api {
|
||||
} else {
|
||||
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
|
||||
}
|
||||
$events = array_map([self::class, 'addDateAndTimeFormats'], $events);
|
||||
return $events;
|
||||
} catch (GeneralException $e) {
|
||||
return new \WP_Error('events_not_loading', 'The events could not be loaded!', array('status' => 500));
|
||||
@ -50,4 +51,21 @@ class Api {
|
||||
return new \WP_Error('group_not_found', sprintf('The group "%s" could not be found!', $groupName), array('status' => 404));
|
||||
}
|
||||
}
|
||||
|
||||
public static function addDateAndTimeFormats($event) {
|
||||
$dateFormat = SiteSettings::getDateFormat();
|
||||
$timeFormat = SiteSettings::getTimeFormat();
|
||||
$timeZone = SiteSettings::getTimeZone();
|
||||
|
||||
$startDateTime = new LocalDateTime($event['beginsOn'], $timeZone);
|
||||
$event['startDateFormatted'] = LocalDateTimeFormatter::format($startDateTime, $dateFormat);
|
||||
$event['startTimeFormatted'] = LocalDateTimeFormatter::format($startDateTime, $timeFormat);
|
||||
|
||||
if ($event['endsOn']) {
|
||||
$endDateTime = new LocalDateTime($event['endsOn'], $timeZone);
|
||||
$event['endDateFormatted'] = LocalDateTimeFormatter::format($endDateTime, $dateFormat);
|
||||
$event['endTimeFormatted'] = LocalDateTimeFormatter::format($endDateTime, $timeFormat);
|
||||
}
|
||||
return $event;
|
||||
}
|
||||
}
|
||||
|
@ -4,3 +4,4 @@ namespace MobilizonConnector;
|
||||
const DEFAULT_EVENTS_COUNT = 5;
|
||||
const NAME = '<wordpress-name>';
|
||||
const NICE_NAME = '<wordpress-nice-name>';
|
||||
const PLUGIN_VERSION = '<wordpress-version>';
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
41
source/includes/LineFormatter.php
Normal file
41
source/includes/LineFormatter.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?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);
|
||||
$startDate = LocalDateTimeFormatter::format($startDateTime, $dateFormat);
|
||||
$startTime = LocalDateTimeFormatter::format($startDateTime, $timeFormat);
|
||||
|
||||
$dateText = $startDate . ' ' . $startTime;
|
||||
if ($end) {
|
||||
$endDateTime = new LocalDateTime($end, $timeZone);
|
||||
$endDate = LocalDateTimeFormatter::format($endDateTime, $dateFormat);
|
||||
$endTime = LocalDateTimeFormatter::format($endDateTime, $timeFormat);
|
||||
|
||||
if ($startDate != $endDate) {
|
||||
$dateText .= ' - ';
|
||||
$dateText .= $endDate . ' ';
|
||||
} else {
|
||||
$dateText .= ' - ';
|
||||
}
|
||||
$dateText .= $endTime;
|
||||
}
|
||||
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 $dateTimeString, \DateTimeZone $timeZone) {
|
||||
$date = new \DateTimeImmutable($dateTimeString);
|
||||
$this->dateTime = $date->setTimezone($timeZone);
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
return $this->dateTime;
|
||||
}
|
||||
}
|
12
source/includes/LocalDateTimeFormatter.php
Normal file
12
source/includes/LocalDateTimeFormatter.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class LocalDateTimeFormatter
|
||||
{
|
||||
public static function format(LocalDateTime $dateTime, string $format) {
|
||||
$timestamp = $dateTime->getValue()->getTimestamp();
|
||||
$offset = $dateTime->getValue()->getOffset();
|
||||
$timestampWithOffset = $timestamp + $offset;
|
||||
return date_i18n($format, $timestampWithOffset);
|
||||
}
|
||||
}
|
@ -4,12 +4,11 @@ namespace MobilizonConnector;
|
||||
class Settings {
|
||||
|
||||
private static $DEFAULT_OPTION_URL = 'https://mobilizon.fr';
|
||||
private static $DEFAULT_IS_SHORT_OFFSET_NAME_SHOWN = false;
|
||||
private static $PAGE_NAME = 'wordpress_mobilizon';
|
||||
private static $OPTIONS_GROUP_NAME = 'wordpress_mobilizon';
|
||||
private static $OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN = 'wordpress_mobilizon_is_short_offset_name_shown';
|
||||
private static $OPTION_NAME_PLUGIN_VERSION = 'wordpress_mobilizon_plugin_version';
|
||||
private static $OPTION_NAME_URL = 'wordpress_mobilizon_url';
|
||||
private static $SETTING_FIELD_NAME_IS_SHORT_OFFSET_NAME_SHOWN = 'wordpress_mobilizon_field_is_short_offset_name_shown';
|
||||
private static $SETTING_FIELD_NAME_URL = 'wordpress_mobilizon_field_url';
|
||||
private static $SETTINGS_SECTION_NAME = 'wordpress_mobilizon_section_general';
|
||||
|
||||
@ -19,10 +18,6 @@ class Settings {
|
||||
}
|
||||
|
||||
public static function init_settings() {
|
||||
register_setting(
|
||||
self::$OPTIONS_GROUP_NAME,
|
||||
self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN
|
||||
);
|
||||
register_setting(
|
||||
self::$OPTIONS_GROUP_NAME,
|
||||
self::$OPTION_NAME_URL,
|
||||
@ -46,21 +41,6 @@ class Settings {
|
||||
'label_for' => self::$SETTING_FIELD_NAME_URL
|
||||
)
|
||||
);
|
||||
add_settings_field(
|
||||
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',
|
||||
self::$PAGE_NAME,
|
||||
self::$SETTINGS_SECTION_NAME,
|
||||
array(
|
||||
'label_for' => self::$SETTING_FIELD_NAME_IS_SHORT_OFFSET_NAME_SHOWN
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function output_field_is_short_offset_name_shown($args) {
|
||||
$isShortOffsetNameShown = self::isShortOffsetNameShown();
|
||||
require dirname(__DIR__) . '/view/settings/is-short-offset-name-shown-field.php';
|
||||
}
|
||||
|
||||
public static function output_field_url($args) {
|
||||
@ -101,22 +81,26 @@ class Settings {
|
||||
require dirname(__DIR__) . '/view/settings/page.php';
|
||||
}
|
||||
|
||||
public static function isShortOffsetNameShown() {
|
||||
return get_option(self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN);
|
||||
}
|
||||
|
||||
public static function getUrl() {
|
||||
return get_option(self::$OPTION_NAME_URL);
|
||||
}
|
||||
|
||||
public static function setDefaultOptions() {
|
||||
add_option(self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN, self::$DEFAULT_IS_SHORT_OFFSET_NAME_SHOWN);
|
||||
add_option(self::$OPTION_NAME_URL, self::$DEFAULT_OPTION_URL);
|
||||
}
|
||||
|
||||
public static function deleteAllOptions() {
|
||||
delete_option(self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN);
|
||||
delete_option(self::$OPTION_NAME_URL);
|
||||
}
|
||||
|
||||
public static function removeObsoleteOptionsIfNeeded() {
|
||||
$storedPluginVersion = get_option(self::$OPTION_NAME_PLUGIN_VERSION);
|
||||
if ($storedPluginVersion !== PLUGIN_VERSION) {
|
||||
if (version_compare($storedPluginVersion, '1.5.0', '<') ) {
|
||||
delete_option(self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN);
|
||||
}
|
||||
update_option(self::$OPTION_NAME_PLUGIN_VERSION, PLUGIN_VERSION);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
25
source/includes/SiteSettings.php
Normal file
25
source/includes/SiteSettings.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?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();
|
||||
}
|
||||
|
||||
public static function getTimeZoneString() {
|
||||
return wp_timezone_string();
|
||||
}
|
||||
|
||||
}
|
@ -21,7 +21,6 @@ Features
|
||||
- Configure number of events to show per block, per widget and per shortcut
|
||||
- Optionally filter events by a specific public group per block, per widget and per shortcut
|
||||
- Set the URL of the Mobilizon instance in the settings
|
||||
- Toggle adding named offset in brackets after the time in the settings
|
||||
|
||||
This plugin requests the events via Mobilizon's GraphQL API.
|
||||
|
||||
|
@ -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,15 +0,0 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<input id="<?php echo esc_attr($args['label_for']); ?>"
|
||||
name="<?php echo esc_attr(self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN); ?>"
|
||||
type="checkbox"
|
||||
<?php echo $isShortOffsetNameShown == true ? 'checked' : ''; ?>>
|
||||
<p class="description">
|
||||
<?php esc_html_e('The time zone of this WordPress installation is used. Whether the current offset should be displayed in brackets after the time, e.g. 10:00 (UTC)', 'connector-mobilizon'); ?>
|
||||
</p>
|
@ -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'));
|
||||
}
|
||||
}
|
58
tests/LineFormatterTest.php
Normal file
58
tests/LineFormatterTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\LineFormatter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
function date_i18n(string $format, int $timestamp) {
|
||||
// Mock WordPress function.
|
||||
if ($format == 'H:i') {
|
||||
if ($timestamp == 1618482600) {
|
||||
return '10:30';
|
||||
} else if ($timestamp == 1618500600 || $timestamp == 1618587000) {
|
||||
return '15:30';
|
||||
}
|
||||
} else if ($format == 'd/m/Y') {
|
||||
if ($timestamp == 1618482600 || $timestamp == 1618500600) {
|
||||
return '15/04/2021';
|
||||
} else if ($timestamp == 1618587000) {
|
||||
return '16/04/2021';
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
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'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z'));
|
||||
}
|
||||
|
||||
public function testCanDateFormatTwoDates(): void {
|
||||
$this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z'));
|
||||
}
|
||||
|
||||
public function testCanDateFormatWhenSecondDateIsNull(): void {
|
||||
$this->assertSame('15/04/2021 10:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', null));
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
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