1
0
mirror of https://github.com/dwaxweiler/connector-mobilizon synced 2025-06-05 21:59:25 +02:00
This commit is contained in:
Daniel Waxweiler
2025-05-25 22:48:55 +02:00
parent b8c918db67
commit 6af81bf53d
18 changed files with 182 additions and 192 deletions

View File

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

View File

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

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);
}
$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) {

View File

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

View File

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

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

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

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

View File

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

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

View File

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