<?php
|
namespace JVBase\blocks;
|
|
|
use DateTime;
|
use JVBase\helpers\Events;
|
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 .= Events::tags($values);
|
|
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 .= Events::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 .= sprintf(
|
'<li>%s</li>',
|
Events::formatDay($values['date_start']??'', $values['time_start']??'', $values['time_end']??'')
|
);
|
foreach ($values['schedule']??[] as $date) {
|
$out .= sprintf(
|
'<li>%s</li>',
|
Events::formatDay($date['date'], $date['time_start'], $date['time_end'], $date['notes'])
|
);
|
}
|
$out .= '</ul>';
|
|
$out .= Events::location($values);
|
|
$out .= $this->price($values['cost']??'', $values['ticket_url']??'');
|
$out .= $this->participants($values['max_participants']??'');
|
|
$out .= Events::terms($values);
|
|
$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 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;
|
}
|
}
|