1
0
mirror of https://github.com/dwaxweiler/connector-mobilizon synced 2025-06-05 21:59:25 +02:00

9 Commits

Author SHA1 Message Date
91cf9bcaae remove space 2025-05-28 10:36:55 +02:00
108e677abe let frontend dates and times be formatted by backend before sending 2025-05-28 10:31:08 +02:00
1c3ea8462e remove option from features 2025-05-27 10:06:36 +02:00
4f9785a1a8 remove unneeded option 2025-05-26 16:56:40 +02:00
a6e720679c fix offset 2025-05-26 16:21:02 +02:00
be8fd96ee9 Revert "use timezone of event"
This reverts commit 9662e1f333.
2025-05-26 10:20:11 +02:00
9662e1f333 use timezone of event 2025-05-26 09:58:54 +02:00
77485b3010 fix require, add mock 2025-05-26 09:49:19 +02:00
6af81bf53d wip 2025-05-25 22:48:55 +02:00
27 changed files with 243 additions and 432 deletions

View File

@ -6,8 +6,10 @@
- Add donation link to plugin on plugins page - Add donation link to plugin on plugins page
#### Changed #### Changed
- Use `wp_remote_get()` instead of cURL functions for downloading the images - Use `wp_remote_get()` instead of cURL functions for downloading the images
- Use date and time formats from general site settings
#### Deprecated #### Deprecated
#### Removed #### Removed
- Removed option "Display named offset"
#### Fixed #### Fixed
- Show group not found error message in block - Show group not found error message in block
#### Security #### Security

View File

@ -16,8 +16,10 @@ require_once __DIR__ . '/includes/Constants.php';
require_once __DIR__ . '/includes/Api.php'; require_once __DIR__ . '/includes/Api.php';
require_once __DIR__ . '/includes/EventsCache.php'; require_once __DIR__ . '/includes/EventsCache.php';
require_once __DIR__ . '/includes/Settings.php'; require_once __DIR__ . '/includes/Settings.php';
require_once __DIR__ . '/includes/DateTimeWrapper.php'; require_once __DIR__ . '/includes/SiteSettings.php';
require_once __DIR__ . '/includes/Formatter.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/GraphQlClient.php';
require_once __DIR__ . '/includes/EventsListBlock.php'; require_once __DIR__ . '/includes/EventsListBlock.php';
require_once __DIR__ . '/includes/EventsListShortcut.php'; require_once __DIR__ . '/includes/EventsListShortcut.php';
@ -72,13 +74,11 @@ final class Mobilizon_Connector {
public function enable_activation() { public function enable_activation() {
MobilizonConnector\Settings::setDefaultOptions(); MobilizonConnector\Settings::setDefaultOptions();
MobilizonConnector\Settings::removeObsoleteOptionsIfNeeded();
} }
private function load_settings_globally_before_script($scriptName) { private function load_settings_globally_before_script($scriptName) {
$settings = array( $settings = array(
'isShortOffsetNameShown' => MobilizonConnector\Settings::isShortOffsetNameShown(),
'locale' => str_replace('_', '-', get_locale()),
'timeZone' => wp_timezone_string(),
'url' => MobilizonConnector\Settings::getUrl() 'url' => MobilizonConnector\Settings::getUrl()
); );
wp_add_inline_script($scriptName, 'var MOBILIZON_CONNECTOR = ' . json_encode($settings), 'before'); wp_add_inline_script($scriptName, 'var MOBILIZON_CONNECTOR = ' . json_encode($settings), 'before');

View File

@ -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')
})

View File

@ -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()
}
}

View File

@ -51,6 +51,10 @@ test('#displayEvents one event', (t) => {
description: 'c', description: 'c',
locality: 'd', locality: 'd',
}, },
startDateFormatted: '15/04/2021',
startTimeFormatted: '10:30',
endDateFormatted: '15/04/2021',
endTimeFormatted: '15:30',
}, },
] ]
const container = t.context.container const container = t.context.container

View File

@ -9,11 +9,6 @@ export function clearEventsList(container) {
export function displayEvents({ events, document, container, maxEventsCount }) { export function displayEvents({ events, document, container, maxEventsCount }) {
hideLoadingIndicator(container) 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 eventsCount = Math.min(maxEventsCount, events.length)
const list = container.querySelector('ul') const list = container.querySelector('ul')
for (let i = 0; i < eventsCount; i++) { for (let i = 0; i < eventsCount; i++) {
@ -43,11 +38,10 @@ export function displayEvents({ events, document, container, maxEventsCount }) {
li.appendChild(br) li.appendChild(br)
const date = Formatter.formatDate({ const date = Formatter.formatDate({
locale, startDateFormatted: events[i].startDateFormatted,
start: events[i].beginsOn, startTimeFormatted: events[i].startTimeFormatted,
end: events[i].endsOn, endDateFormatted: events[i].endDateFormatted,
timeZone, endTimeFormatted: events[i].endTimeFormatted,
isShortOffsetNameShown,
}) })
const textnode = document.createTextNode(date) const textnode = document.createTextNode(date)
li.appendChild(textnode) li.appendChild(textnode)

View File

@ -15,55 +15,34 @@ test('#escapeHTML', (t) => {
test('#formatDate one date', (t) => { test('#formatDate one date', (t) => {
const date = Formatter.formatDate({ const date = Formatter.formatDate({
start: '2021-04-15T10:30:00Z', startDateFormatted: '15/04/2021',
end: '2021-04-15T15:30:00Z', startTimeFormatted: '10:30',
endDateFormatted: '15/04/2021',
endTimeFormatted: '15:30',
}) })
t.is(date, '15/04/2021 10:30 - 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) => { test('#formatDate two dates', (t) => {
const date = Formatter.formatDate({ const date = Formatter.formatDate({
start: '2021-04-15T10:30:00Z', startDateFormatted: '15/04/2021',
end: '2021-04-16T15:30:00Z', startTimeFormatted: '10:30',
endDateFormatted: '16/04/2021',
endTimeFormatted: '15:30',
}) })
t.is(date, '15/04/2021 10:30 - 16/04/2021 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) => { test('#formatDate second date is null', (t) => {
const date = Formatter.formatDate({ const date = Formatter.formatDate({
start: '2021-04-15T10:30:00Z', startDateFormatted: '15/04/2021',
end: null, startTimeFormatted: '10:30',
endDateFormatted: undefined,
endTimeFormatted: undefined,
}) })
t.is(date, '15/04/2021 10:30') 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) => { test('#formatLocation both parameters', (t) => {
const location = Formatter.formatLocation({ description: 'a', locality: 'b' }) const location = Formatter.formatLocation({ description: 'a', locality: 'b' })
t.is(location, 'a, b') t.is(location, 'a, b')

View File

@ -1,5 +1,3 @@
import DateTimeWrapper from './date-time-wrapper.js'
export default class Formatter { export default class Formatter {
static escapeHTML(input) { static escapeHTML(input) {
const div = document.createElement('div') const div = document.createElement('div')
@ -7,30 +5,22 @@ export default class Formatter {
return div.innerHTML return div.innerHTML
} }
static formatDate({ locale, timeZone, start, end, isShortOffsetNameShown }) { static formatDate({
const startDateTime = new DateTimeWrapper({ startDateFormatted,
locale, startTimeFormatted,
text: start, endDateFormatted,
timeZone, endTimeFormatted,
}) }) {
let dateText = startDateTime.getShortDate() let dateText = startDateFormatted
dateText += ' ' + startDateTime.get24Time() dateText += ' ' + startTimeFormatted
if (!end && isShortOffsetNameShown) { if (endDateFormatted) {
dateText += ' (' + startDateTime.getShortOffsetName() + ')' if (startDateFormatted !== endDateFormatted) {
}
if (end) {
const endDateTime = new DateTimeWrapper({ locale, text: end, timeZone })
if (!startDateTime.equalsDate(endDateTime)) {
dateText += ' - ' dateText += ' - '
dateText += endDateTime.getShortDate() + ' ' dateText += endDateFormatted + ' '
} else { } else {
dateText += ' - ' dateText += ' - '
} }
dateText += endDateTime.get24Time() dateText += endTimeFormatted
if (isShortOffsetNameShown) {
dateText += ' (' + endDateTime.getShortOffsetName() + ')'
}
} }
return dateText return dateText
} }

View File

@ -43,6 +43,7 @@ class Api {
} else { } else {
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); $events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
} }
$events = array_map([self::class, 'addDateAndTimeFormats'], $events);
return $events; return $events;
} catch (GeneralException $e) { } catch (GeneralException $e) {
return new \WP_Error('events_not_loading', 'The events could not be loaded!', array('status' => 500)); 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)); 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;
}
} }

View File

@ -4,3 +4,4 @@ namespace MobilizonConnector;
const DEFAULT_EVENTS_COUNT = 5; const DEFAULT_EVENTS_COUNT = 5;
const NAME = '<wordpress-name>'; const NAME = '<wordpress-name>';
const NICE_NAME = '<wordpress-nice-name>'; const NICE_NAME = '<wordpress-nice-name>';
const PLUGIN_VERSION = '<wordpress-version>';

View File

@ -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();
}
}

View File

@ -51,9 +51,9 @@ class EventsListBlock {
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); $events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
} }
$locale = get_locale(); $dateFormat = SiteSettings::getDateFormat();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown(); $timeFormat = SiteSettings::getTimeFormat();
$timeZone = wp_timezone_string(); $timeZone = SiteSettings::getTimeZone();
require dirname(__DIR__) . '/view/events-list.php'; require dirname(__DIR__) . '/view/events-list.php';
} catch (GeneralException $e) { } catch (GeneralException $e) {

View File

@ -34,9 +34,9 @@ class EventsListShortcut {
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); $events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
} }
$locale = get_locale(); $dateFormat = SiteSettings::getDateFormat();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown(); $timeFormat = SiteSettings::getTimeFormat();
$timeZone = wp_timezone_string(); $timeZone = SiteSettings::getTimeZone();
require dirname(__DIR__) . '/view/events-list.php'; require dirname(__DIR__) . '/view/events-list.php';
} catch (GeneralException $e) { } catch (GeneralException $e) {

View File

@ -34,9 +34,9 @@ class EventsListWidget extends \WP_Widget {
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); $events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
} }
$locale = get_locale(); $dateFormat = SiteSettings::getDateFormat();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown(); $timeFormat = SiteSettings::getTimeFormat();
$timeZone = wp_timezone_string(); $timeZone = SiteSettings::getTimeZone();
require dirname(__DIR__) . '/view/events-list.php'; require dirname(__DIR__) . '/view/events-list.php';
} catch (GeneralException $e) { } catch (GeneralException $e) {

View File

@ -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;
}
}

View 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;
}
}

View 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;
}
}

View 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);
}
}

View File

@ -4,12 +4,11 @@ namespace MobilizonConnector;
class Settings { class Settings {
private static $DEFAULT_OPTION_URL = 'https://mobilizon.fr'; 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 $PAGE_NAME = 'wordpress_mobilizon';
private static $OPTIONS_GROUP_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_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 $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 $SETTING_FIELD_NAME_URL = 'wordpress_mobilizon_field_url';
private static $SETTINGS_SECTION_NAME = 'wordpress_mobilizon_section_general'; private static $SETTINGS_SECTION_NAME = 'wordpress_mobilizon_section_general';
@ -19,10 +18,6 @@ class Settings {
} }
public static function init_settings() { public static function init_settings() {
register_setting(
self::$OPTIONS_GROUP_NAME,
self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN
);
register_setting( register_setting(
self::$OPTIONS_GROUP_NAME, self::$OPTIONS_GROUP_NAME,
self::$OPTION_NAME_URL, self::$OPTION_NAME_URL,
@ -46,21 +41,6 @@ class Settings {
'label_for' => self::$SETTING_FIELD_NAME_URL '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) { public static function output_field_url($args) {
@ -101,22 +81,26 @@ class Settings {
require dirname(__DIR__) . '/view/settings/page.php'; 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() { public static function getUrl() {
return get_option(self::$OPTION_NAME_URL); return get_option(self::$OPTION_NAME_URL);
} }
public static function setDefaultOptions() { 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); add_option(self::$OPTION_NAME_URL, self::$DEFAULT_OPTION_URL);
} }
public static function deleteAllOptions() { public static function deleteAllOptions() {
delete_option(self::$OPTION_NAME_IS_SHORT_OFFSET_NAME_SHOWN);
delete_option(self::$OPTION_NAME_URL); 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);
}
}
} }

View 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();
}
}

View File

@ -21,7 +21,6 @@ Features
- Configure number of events to show per block, per widget and per shortcut - 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 - 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 - 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. This plugin requests the events via Mobilizon's GraphQL API.

View File

@ -15,10 +15,10 @@ if (!defined('ABSPATH')) {
<?php } ?> <?php } ?>
<a href="<?php echo esc_attr($event['url']); ?>"><?php echo esc_html($event['title']); ?></a> <a href="<?php echo esc_attr($event['url']); ?>"><?php echo esc_html($event['title']); ?></a>
<br> <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'])) { ?> <?php if (isset($event['physicalAddress'])) { ?>
<br> <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 } ?> <?php } ?>
</li> </li>
<?php } ?> <?php } ?>

View File

@ -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>

View File

@ -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());
}
}

View File

@ -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'));
}
}

View 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'));
}
}

View 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());
}
}