<?php
|
namespace JVBase\blocks;
|
|
|
use DateTime;
|
use JVBase\meta\Meta;
|
use JVBase\utility\Image;
|
use WP_Block;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
class EventBlock {
|
public function __construct()
|
{
|
add_action('init', [$this, 'registerBlock']);
|
}
|
|
public function registerBlock():void
|
{
|
register_block_type(
|
JVB_DIR . '/build/event',
|
[
|
'render_callback' => [$this, 'render']
|
]
|
);
|
}
|
|
public function render(array $attributes, string $content, WP_Block $block):string
|
{
|
|
if (!is_singular(BASE.'event')) {
|
return '';
|
}
|
|
$meta = Meta::forPost(get_the_ID());
|
$values = $meta->getAll();
|
|
$out = '<section class="event-block">';
|
$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($values[$k]) && $values[$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>';
|
if (!empty($values['post_thumbnail'])) {
|
$out .= jvbFormatImage($values['post_thumbnail']);
|
}
|
$out .= '<header class="row x-btw">';
|
if (!empty($values['post_title'])) {
|
$out .= sprintf(
|
'<h1 id="%s">%s</h1>',
|
sanitize_title($values['post_title']),
|
$values['post_title']
|
);
|
}
|
$out .= $this->calendarDay($values['date_start']??'');
|
|
$out .= '</header>';
|
|
$out .= '<div class="meta">';
|
$out .= sprintf(
|
'<ul class="time term-list row left"><li class="title">%s</li>',
|
jvbIcon('clock')
|
);
|
$out .= $this->formatDay($values['date_start']??'', $values['time_start']??'', $values['time_end']??'');
|
foreach ($values['schedule']??[] as $date) {
|
$out .= $this->formatDay($date['date'], $date['time_start'], $date['time_end'], $date['notes']);
|
}
|
$out .= '</ul>';
|
|
$out .= $this->location($values['location']??[], $values['location_notes']??'');
|
|
$out .= $this->price($values['cost']??'', $values['ticket_url']??'');
|
$out .= $this->participants($values['max_participants']??'');
|
|
$get = ['city', 'event_category'];
|
foreach ($get as $g) {
|
if (!empty($values[$g])) {
|
$out .= jvbMetaTermList($values[$g], $g);
|
}
|
}
|
|
$out .= $this->rsvp($values['rsvp_required']??'', $values['rsvp_limit']??'', $values['rsvp_deadline']);
|
|
$out .= '</div>';
|
$out .= $this->gallery($values['gallery']??'');
|
$out .= $this->moreInfo($values['external_url']??'');
|
|
$out .= '</section>';
|
return $out;
|
}
|
public function renderGallery():void
|
{
|
jvbRenderGallery();
|
}
|
|
protected function calendarDay(string $day):string
|
{
|
$out = '';
|
if (!empty($day)) {
|
$date = new DateTime($day, wp_timezone());
|
$weekday = $date->format('D');
|
$num = $date->format('d');
|
$month = $date->format('M');
|
|
$out .= sprintf(
|
'<div class="col calendar">
|
<span class="weekday">%s</span>
|
<span class="month">%s</span>
|
<span class="day">%d</span>
|
</div>',
|
$weekday,
|
$month,
|
$num
|
);
|
}
|
return $out;
|
}
|
protected 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']
|
);
|
}
|
|
protected function formatDay(string $date, string $start, string $end, string $notes = ''):string
|
{
|
$out = '<li>';
|
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);
|
}
|
|
$out .= '</li>';
|
return $out;
|
}
|
|
protected function location(array $location, string $notes = ''):string
|
{
|
$out = '';
|
if (!empty($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'),
|
$this->renderLocationString($location),
|
jvbLocationLinks($location),
|
empty($notes) ? '' : '<li>'.$notes.'</li>',
|
);
|
}
|
return $out;
|
}
|
|
protected function price(string $price, string $url):string
|
{
|
$out = '';
|
if (!empty($price) || $price !== '0') {
|
$out = jvbCurrency($price);
|
}
|
if (!empty($url)) {
|
$out .= sprintf(
|
'<br><a href="%s">%sPurchase Tickets</a>',
|
esc_url($url),
|
jvbIcon('ticket')
|
);
|
}
|
return empty($out) ? $out : '<p>'.$out.'</p>';
|
}
|
|
protected function participants(string $num):string
|
{
|
$out = '';
|
if (!empty($num) || $num !== '0') {
|
$out .= sprintf(
|
'<p>Maximum %d participants</p>',
|
$num
|
);
|
}
|
return $out;
|
}
|
|
protected function moreInfo(string $url):string
|
{
|
$out = '';
|
if (!empty($url)) {
|
$out .= sprintf(
|
'<p>%s<a href="%s" target="_blank">Learn More</a></p>',
|
jvbIcon('link'),
|
esc_url($url)
|
);
|
}
|
return $out;
|
}
|
|
protected function rsvp(string $mustRSVP, string $limit, string $deadline):string
|
{
|
$out = '';
|
if (!empty($mustRSVP) && $mustRSVP === '1') {
|
$out = '<p class="rsvp">RSVP Required';
|
if (!empty($limit) && $limit !== '0') {
|
$out .= sprintf(
|
'<br>Maximum %d Seats',
|
$limit
|
);
|
}
|
if (!empty($deadline)) {
|
$out .= sprintf(
|
'<br>RSVP Deadline: <time datetime="%s">%s</time>',
|
date('c', $deadline),
|
date('F j, Y', $deadline)
|
);
|
}
|
$out .= '</p>';
|
}
|
return $out;
|
}
|
|
protected function gallery(string $gallery):string
|
{
|
$out = '';
|
if (!empty($gallery)) {
|
add_action('wp_footer', [$this, 'renderGallery']);
|
$out = '<div class="gallery row left">';
|
$gallery = array_map('absint', explode(',', $gallery));
|
foreach ($gallery as $id) {
|
$caption = wp_get_attachment_caption($id);
|
$out .= sprintf(
|
'<figure>%s<figcaption>%s</figcaption></figure>',
|
jvbFormatImage($id,'tiny','medium',true,'event'),
|
empty($caption) ? '' : $caption
|
);
|
}
|
$out .= '</div>';
|
}
|
return $out;
|
}
|
}
|