<?php
|
namespace JVBase\blocks;
|
|
use JVBase\managers\Cache;
|
use JVBase\meta\Meta;
|
use JVBase\registrar\Registrar;
|
use WP_Block;
|
use WP_Query;
|
|
if (!defined('ABSPATH')) {
|
exit; // Exit if accessed directly
|
}
|
|
class SimpleCalendarBlock
|
{
|
protected Cache $cache;
|
protected string $config;
|
protected string $type;
|
protected string $path = JVB_DIR . '/build/simple-calendar';
|
protected string $image;
|
protected string $header;
|
protected array $params = ['all'];
|
protected string $headerExtra;
|
protected string $detailsTitle;
|
protected array $details;
|
protected array|false $sections = false;
|
protected string $base;
|
protected array $content;
|
protected array $taxonomies;
|
|
public function __construct()
|
{
|
$this->cache = Cache::for('menu', DAY_IN_SECONDS)->connect('post');
|
add_action('init', [ $this, 'registerBlock' ]);
|
}
|
|
public function registerBlock():void
|
{
|
register_block_type($this->path, [
|
'render_callback' => [ $this, 'render' ]
|
]);
|
}
|
|
protected function buildParams():void
|
{
|
$registered = Registrar::withFeature('is_calendar', 'post');
|
$registered = array_map('jvbCheckBase', $registered);
|
$types = [];
|
foreach ($registered as $type) {
|
$registrar = Registrar::getInstance($type);
|
$types[$type] = $registrar->getPlural();
|
}
|
$this->content = $types;
|
|
$taxonomies = [];
|
foreach ($registered as $type) {
|
$registrar = Registrar::getInstance($type);
|
$taxonomies = array_merge($taxonomies, $registrar->registrar->taxonomies);
|
}
|
$taxonomies = array_map('jvbCheckBase', $taxonomies);
|
$types = [];
|
foreach ($taxonomies as $type) {
|
$registrar = Registrar::getInstance($type);
|
$types[$type] = $registrar->getPlural();
|
}
|
$this->taxonomies = $types;
|
|
|
if (is_post_type_archive($registered)) {
|
$this->params = ['all'];
|
} elseif (is_tax($taxonomies)) {
|
$this->params = [];
|
} else {
|
$this->params = [];
|
}
|
}
|
|
public function render(array $attributes, string $content, WP_Block $block)
|
{
|
$this->buildParams();
|
if (empty($this->params)) {
|
return '';
|
}
|
|
$key = $this->cache->generateKey($this->params);
|
$this->cache->flush();
|
return $this->cache->remember(
|
$key,
|
function() {
|
ob_start();
|
$this->renderBlock();
|
return ob_get_clean();
|
}
|
);
|
}
|
|
protected function renderBlock():void
|
{
|
$this->renderHeader();
|
$this->renderCalendar();
|
$this->renderOnThisPage();
|
}
|
|
protected function renderHeader():void
|
{
|
$title = 'Events';
|
if(!in_array('all', $this->params)) {
|
$title = '<small>'.$title.'</small>';
|
}
|
|
?>
|
<header id="top">
|
<h1><?=$title?></h1>
|
<?php $this->buildNav(); ?>
|
</header>
|
<section id="intro">
|
<p>We bring southern-style barbeque eats with Northern flair to any event throughout Alberta.</p>
|
<p><b>Want to bring us to your event?</b> <a href="<?=get_home_url(null,'/book-your-event')?>">Let us know</a>, we'd love to come!</p>
|
</section>
|
<?php
|
}
|
protected function buildNav():void
|
{
|
$content = $taxonomies = '';
|
if (count($this->content) > 1) {
|
$out = [];
|
foreach ($this->content as $slug => $title) {
|
$out[] = sprintf(
|
'<li><a href="%s" title="See %s">%s</a></li>',
|
get_post_type_archive_link($slug),
|
$title,
|
$title
|
);
|
}
|
if (!empty($out)) {
|
$content = sprintf(
|
'<ul class="term-list content"><li class="label">Content:</li>%s</ul>',
|
implode('', $out)
|
);
|
}
|
}
|
|
if (!empty($this->taxonomies)) {
|
foreach ($this->taxonomies as $slug => $title) {
|
$check = get_terms([
|
'taxonomy' => $slug,
|
'hide_empty' => true,
|
'orderby' => 'name',
|
]);
|
$out = [];
|
if ($check && !is_wp_error($check)) {
|
foreach ($check as $term) {
|
$out[] = sprintf(
|
'<li><a href="%s" title="See %s">%s</a></li>',
|
get_term_link($term->term_id, $slug),
|
$term->name,
|
$term->name
|
);
|
}
|
}
|
if (!empty($out)) {
|
$taxonomies .= sprintf(
|
'<ul class="term-list %s"><li class="label">%s</li>%s</ul>',
|
$slug,
|
$title,
|
implode('', $out)
|
);
|
}
|
}
|
}
|
|
if (!empty($content) || !empty($taxonomies)) {
|
echo sprintf(
|
'<nav class="types">%s%s</nav>',
|
$content,
|
$taxonomies
|
);
|
}
|
}
|
|
protected function renderCalendar():void
|
{
|
?>
|
<details id="calendar" open>
|
<summary><h2>Upcoming Events <?= jvbIcon('calendar-blank')?></h2></summary>
|
<?= $this->getEvents() ?>
|
</details>
|
<details class="past">
|
<summary><h2>Past Events <?= jvbIcon('calendar-check')?></h2></summary>
|
<?= $this->getEvents(true) ?>
|
</details>
|
<?php
|
}
|
|
protected function getCurrentPostTypes():array
|
{
|
if (is_post_type_archive()) {
|
return [get_queried_object()->name];
|
}
|
if (is_singular()) {
|
return [get_queried_object()->post_type];
|
}
|
if (is_tax()) {
|
$tax = get_queried_object();
|
jvbDump($tax);
|
}
|
return [];
|
}
|
protected function getEvents(bool $past = false):string
|
{
|
|
$events = '';
|
if (is_singular()) {
|
return $this->formatSingular();
|
}
|
|
$page = $past
|
? $_GET['jpast']??1
|
: $_GET['jpage']??1;
|
|
$items = new WP_Query([
|
'post_type' =>$this->getCurrentPostTypes(),
|
'post_status' => 'publish',
|
'orderby' => 'meta_value',
|
'meta_key' => BASE.'date_start',
|
'posts_per_page'=> 30,
|
'fields' => 'ids',
|
'paged' => $page,
|
'meta_type' => 'DATETIME',
|
'meta_query' => [
|
[
|
'key' => BASE.'past',
|
'value' => '1',
|
'compare' => $past ? '=' : '!='
|
]
|
]
|
]);
|
|
$events = implode('', array_map([$this, 'formatItem'], $items->posts));
|
|
$events = empty($events) ? '<p class="empty">Nothing here yet</p>' : sprintf(
|
'<ul class="item-list">%s</ul>',
|
$events
|
);
|
$total = $items->max_num_pages;
|
if ($total > 1) {
|
$big = 999999999; // need an unlikely integer
|
$format = $past ? 'jpast' : 'jpage';
|
$events .= paginate_links([
|
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
|
'format' => '?'.$format.'=%#%',
|
'current' => $page,
|
'total' => $total
|
]);
|
}
|
|
return $events;
|
}
|
|
|
protected function formatItem(int $ID):string
|
{
|
$meta = Meta::forPost($ID);
|
$type = jvbNoBase(get_post_type($ID));
|
return sprintf(
|
'<li class="item %s">
|
%s
|
<h3>
|
<a href="%s" title="Learn More">%s</a>
|
<small><time datetime="%s">%s</time></small>
|
</h3>
|
%s
|
</li>',
|
$type,
|
empty($meta->get('post_thumbnail')) ? '' : jvbFormatImage($meta->get('post_thumbnail'), 'tiny', 'medium'),
|
get_the_permalink($ID),
|
$meta->get('post_title'),
|
date('c', $meta->get('date_start')),
|
date('F j, Y', $meta->get('date_start')),
|
empty($meta->get('post_excerpt')) ? '' : wpautop($meta->get('post_excerpt'))
|
);
|
}
|
|
protected function formatSingular():string
|
{
|
return '';
|
}
|
|
|
protected function renderOnThisPage():void
|
{
|
if (empty($this->details)) {
|
return;
|
}
|
echo jvbOnThisPage(array_keys($this->details));
|
}
|
}
|