<?php
|
namespace JVBase\blocks;
|
|
use JVBase\managers\CacheManager;
|
use JVBase\forms\TaxonomySelector;
|
use JVBase\meta\MetaManager;
|
use WP_Block;
|
use WP_Query;
|
|
if (!defined('ABSPATH')) {
|
exit; // Exit if accessed directly
|
}
|
|
class MenuBlock
|
{
|
protected CacheManager $cache;
|
protected string $config;
|
protected string $type;
|
protected string $path = JVB_DIR . '/build/menu';
|
protected string $image;
|
protected string $header;
|
protected array $params = ['all'];
|
protected string $headerExtra;
|
protected string $detailsTitle;
|
protected array $details;
|
protected array|false $sections = false;
|
|
public function __construct()
|
{
|
$this->cache = new CacheManager('menu', WEEK_IN_SECONDS);
|
add_action('init', [ $this, 'registerBlock' ]);
|
}
|
|
public function registerBlock()
|
{
|
register_block_type($this->path, [
|
'render_callback' => [ $this, 'render' ]
|
]);
|
}
|
|
protected function buildParams():void
|
{
|
if (is_post_type_archive(BASE.'menu_item')) {
|
$this->params = ['all'];
|
} elseif (is_tax(BASE.'section')) {
|
jvbDump(get_queried_object());
|
$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);
|
$cache = $this->cache->get($key);
|
$cache = false;
|
if ($cache) {
|
return $cache;
|
}
|
|
ob_start();
|
$this->renderBlock();
|
$content = ob_get_clean();
|
$this->cache->set($key, $content);
|
return $content;
|
}
|
|
protected function renderBlock():void
|
{
|
$this->renderHeader();
|
$this->renderMenu();
|
$this->renderOnThisPage();
|
}
|
|
protected function renderHeader():void
|
{
|
$title = 'Our Menu';
|
if(!in_array('all', $this->params)) {
|
$title = '<small>'.$title.'</small>';
|
}
|
|
?>
|
<header id="top">
|
<h1><?=$title?></h1>
|
<nav class="menu"><ul>
|
<?php
|
$sections = $this->getSections();
|
foreach ($sections as $section) {
|
if (!jvbTermHasPosts($section['id'], BASE.'section')) {
|
continue;
|
}
|
?>
|
<li><a href="#<?=sanitize_title($section['name'])?>" title="Jump to <?=$section['name']?>"><?=$section['name']?></a></li>
|
<?php
|
}
|
?>
|
</ul></nav>
|
</header>
|
<?php
|
}
|
|
protected function getSections():array
|
{
|
if (!$this->sections) {
|
$options = new MetaManager(null, 'options');
|
$sections = $options->getValue('menu_section_order');
|
if (!is_array($sections)) {
|
$sections = [];
|
}
|
$this->sections = $sections;
|
}
|
|
return $this->sections;
|
}
|
|
protected function renderMenu():void
|
{
|
$sections = $this->getSections();
|
foreach ($sections as $section) {
|
$items = new WP_Query([
|
'post_type' => BASE.'menu_item',
|
'order' => 'ASC',
|
'posts_per_page' => -1,
|
'tax_query' => [
|
[
|
'taxonomy' => BASE.'section',
|
'terms' => (int) $section['id'],
|
]
|
],
|
'fields' => 'ids'
|
]);
|
if ($items->have_posts()) {
|
$slug = sanitize_title($section['name']);
|
?>
|
<section id="<?= $slug?>" class="menu-section">
|
<h2><?=$section['name']?></h2>
|
<hr>
|
<?= (array_key_exists('description', $section) && $section['description'] !== '') ? $section['description'] : ''?>
|
<div class="menu-items">
|
<?php
|
foreach ($items->posts as $ID) {
|
$this->renderMenuItem($ID, $slug);
|
}
|
?>
|
</div>
|
</section>
|
<?php
|
}
|
wp_reset_postdata();
|
}
|
}
|
|
protected function renderMenuItem(int $ID, string $slug, string $postType = 'menu_item') {
|
$meta = new MetaManager($ID, 'post');
|
$values = $meta->getAll([
|
'post_title',
|
'_square_catalog_id',
|
'price',
|
'product_variations',
|
'max_order',
|
'min_order',
|
'step'
|
]);
|
$priceRange = jvbCurrency($values['price']);
|
$variations = $values['product_variations'];
|
|
if(!empty($variations) && $variations[0]['name'] === '') {
|
$variations = [];
|
}
|
|
if (!empty($variations)) {
|
$prices = [];
|
foreach ($variations as $row) {
|
if (isset($row['price']) && is_numeric($row['price'])) {
|
$prices[] = (float) $row['price'];
|
}
|
}
|
if (!empty($prices)) {
|
$priceRange = jvbCurrency(min($prices)).' - '.jvbCurrency(max($prices));
|
}
|
}
|
|
$max = $values['max_order'];
|
$min = $values['min_order'];
|
$step = $values['step'];
|
$max = ($max === '') ? 50 : $max;
|
$min = ($min === '') ? 0 : $min;
|
$step = ($step === '') ? 1 : $step;
|
|
|
?>
|
<div class="menu-item<?= !empty($variations) ? ' variable' : '' ?>" data-section="<?=$slug?>">
|
<div class="header row btw">
|
<h3><?= $values['post_title']?></h3>
|
<p class="price"><?= $priceRange ?></p>
|
</div>
|
<div class="description">
|
<?php $meta->render('render', 'post_excerpt')?>
|
</div>
|
<div class="info row end">
|
<?php
|
if (empty($variations)) {
|
$meta->render(
|
'form',
|
$ID.'|cart_quantity',
|
[
|
'type' => 'number',
|
'label' => 'Add to Order',
|
'min' => $min,
|
'max' => $max,
|
'step' => $step,
|
'data' => [
|
'price' => $values['price'],
|
'id' => $ID,
|
'square-catalog-id' => $values['_square_catalog_id'],
|
'name' => $values['post_title'],
|
'step' => $step,
|
'add-to-cart' => ''
|
],
|
'add' => 'Add 1 '.$values['post_title'].' to cart',
|
'remove' => 'Remove 1 '.$values['post_title'].' from cart',
|
],
|
true);
|
} else {
|
foreach ($variations as $index =>$row) {
|
jvbDump($index, 'index');
|
jvbDump($row, 'row');
|
$meta->render(
|
'form',
|
'quantity-'.$index,
|
[
|
'type' => 'number',
|
'label' => $row['label'],
|
'min' => $min,
|
'max' => $max,
|
'step' => $step,
|
'data' => [
|
'price' => $row['price'],
|
'id' => $ID,
|
'square-catalog-id' => $row['_square_catalog_id'],
|
'name' => $row['label'],
|
'add-to-cart' => ''
|
],
|
'add' => 'Add 1 '.$row['label'].' to cart.',
|
'remove' => 'Remove 1 '.$row['label'].' from cart.',
|
],
|
true);
|
}
|
}
|
?>
|
</div>
|
|
|
</div>
|
<?php
|
}
|
|
|
protected function renderOnThisPage():void
|
{
|
if (empty($this->details)) {
|
return;
|
}
|
echo jvbOnThisPage(array_keys($this->details));
|
}
|
}
|