<?php
|
|
namespace JVBase\helpers;
|
use DateTime;
|
use Exception;
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
exit;
|
}
|
|
/**
|
* Helper class that renders fields based on the presets from MakeCalendarType.php
|
*/
|
class Events {
|
public static function tags(array $fields):string
|
{
|
$out = '<div class="event tags row right">';
|
$get = ['is_free' => 'Free', 'rsvp_required' => 'Must RSVP', 'public' => 'Private Event'];
|
foreach ($get as $k => $g) {
|
$check = !empty($fields[$k]) && $fields[$k] === '1';
|
if ($k === 'public') {
|
$check = !$check;
|
}
|
$icon = match ($k) {
|
'is_free' => jvbIcon('check-circle'),
|
'rsvp_required' => jvbIcon('envelope'),
|
'public' => jvbIcon('lock-key'),
|
};
|
if ($check) {
|
$out .= sprintf(
|
'<div class="tag">%s%s</div>',
|
$icon,
|
$g
|
);
|
}
|
}
|
$out .= '</div>';
|
return $out;
|
}
|
|
/**
|
* @param string $date
|
*
|
* @return string
|
*/
|
public static function calendarDay(string $date):string
|
{
|
|
if (empty($date)) {
|
return '';
|
}
|
try {
|
$date = new DateTime($date, wp_timezone());
|
$weekday = $date->format('D');
|
$num = $date->format('d');
|
$month = $date->format('M');
|
|
return sprintf(
|
'<div class="col calendar">
|
<span class="weekday">%s</span>
|
<span class="month">%s</span>
|
<span class="day">%d</span>
|
</div>',
|
$weekday,
|
$month,
|
$num
|
);
|
} catch (Exception $e){
|
return '';
|
}
|
}
|
|
public static function formatDay(string $date, string $start, string $end, string $notes = ''):string
|
{
|
$out = '';
|
if (!empty($date)) {
|
$out .= sprintf(
|
'<time datetime="%s">%s</time><br>',
|
date('c', strtotime($date)),
|
date('F j, Y', strtotime($date))
|
);
|
}
|
$doit = false;
|
|
if (!empty($start)) {
|
$doit = true;
|
$d = strtotime($date.' '.$start);
|
$out .= sprintf(
|
'<time datetime="%s">%s</time>',
|
date('c', $d),
|
date('g:ia', $d)
|
);
|
}
|
if (!empty($end)) {
|
if ($doit) {
|
$out .= ' - ';
|
}
|
$d = strtotime($date.' '.$end);
|
$out .= sprintf(
|
'<time datetime="%s">%s</time>',
|
date('c', $d),
|
date('g:ia', $d)
|
);
|
|
}
|
if (!empty($notes)) {
|
$out .= wpautop($notes);
|
}
|
|
return $out;
|
}
|
|
public static function getMonthName(int $num):string
|
{
|
return match($num) {
|
1 => 'January',
|
2 => 'February',
|
3 => 'March',
|
4 => 'April',
|
5 => 'May',
|
6 => 'June',
|
7 => 'July',
|
8 => 'August',
|
9 => 'September',
|
10 => 'October',
|
11 => 'November',
|
12 => 'December',
|
};
|
}
|
|
public static function location(array $fields):string
|
{
|
$out = '';
|
if (!array_key_exists('location', $fields)) {
|
return '';
|
}
|
if (!empty($fields['location']['address']??'')) {
|
$out .= sprintf(
|
'<ul class="location term-list row left">
|
<li class="title">%sLocation</li>
|
<li>%s</li>
|
<li>%s</li>
|
%s
|
</ul>',
|
jvbIcon('map-pin'),
|
self::renderLocationString($fields['location']),
|
jvbLocationLinks($fields['location']),
|
empty($location['location_notes']??'') ? '' : '<li>'.$fields['location_notes'].'</li>',
|
);
|
}
|
return $out;
|
}
|
public static function renderLocationString(array $location):string
|
{
|
if (!array_key_exists('address', $location) ||
|
!array_key_exists('street', $location) ||
|
!array_key_exists('city', $location) ||
|
!array_key_exists('province', $location) ||
|
!array_key_exists('country', $location)) {
|
return '';
|
}
|
|
return sprintf(
|
'<p class="location">%s%s<br>%s, %s %s</p>',
|
$location['address'],
|
$location['street'],
|
$location['city'],
|
$location['province'],
|
$location['country'] === 'CA' ? '' : $location['country']
|
);
|
}
|
public static function terms(array $fields, bool $showTaxonomy = true):string
|
{
|
$out = '';
|
$get = ['city', 'event_category'];
|
foreach ($get as $g) {
|
if (!empty($fields[$g])) {
|
$out .= jvbMetaTermList($fields[$g], $g, true, $showTaxonomy);
|
}
|
}
|
return $out;
|
}
|
}
|