| | |
| | | <?php |
| | | namespace JVBase\blocks; |
| | | |
| | | use JVBase\managers\CacheManager; |
| | | use JVBase\base\Options; |
| | | use JVBase\base\Site; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\forms\TaxonomySelector; |
| | | use JVBase\meta\MetaManager; |
| | | use JVBase\meta\Form; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\meta\Render; |
| | | use WP_Block; |
| | | use WP_Query; |
| | | |
| | |
| | | |
| | | class MenuBlock |
| | | { |
| | | protected CacheManager $cache; |
| | | protected Cache $cache; |
| | | protected string $config; |
| | | protected string $type; |
| | | protected string $path = JVB_DIR . '/build/menu'; |
| | |
| | | protected string $detailsTitle; |
| | | protected array $details; |
| | | protected array|false $sections = false; |
| | | protected string $base; |
| | | |
| | | public function __construct() |
| | | { |
| | | $this->cache = CacheManager::for('menu', WEEK_IN_SECONDS)->connectTo('post', 'menu_item'); |
| | | $this->cache = Cache::for('menu', WEEK_IN_SECONDS)->connect('post'); |
| | | if (Site::hasIntegration('square')) { |
| | | $this->base = '_square_'; |
| | | } else if (Site::hasIntegration('helcim')) { |
| | | $this->base = '_helcim_'; |
| | | } |
| | | add_action('init', [ $this, 'registerBlock' ]); |
| | | } |
| | | |
| | | public function registerBlock() |
| | | public function registerBlock():void |
| | | { |
| | | register_block_type($this->path, [ |
| | | 'render_callback' => [ $this, 'render' ] |
| | |
| | | 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 = []; |
| | |
| | | if (empty($this->params)) { |
| | | return ''; |
| | | } |
| | | jvbInlineStyles('forms'); |
| | | $key = $this->cache->generateKey($this->params); |
| | | $cache = $this->cache->get($key); |
| | | if ($cache) { |
| | | return $cache; |
| | | } |
| | | |
| | | ob_start(); |
| | | $this->renderBlock(); |
| | | $content = ob_get_clean(); |
| | | $this->cache->set($key, $content); |
| | | return $content; |
| | | $this->cache->flush(); |
| | | return $this->cache->remember( |
| | | $key, |
| | | function() { |
| | | ob_start(); |
| | | $this->renderBlock(); |
| | | return ob_get_clean(); |
| | | } |
| | | ); |
| | | } |
| | | |
| | | protected function renderBlock():void |
| | |
| | | protected function getSections():array |
| | | { |
| | | if (!$this->sections) { |
| | | $options = new MetaManager(null, 'options'); |
| | | $sections = $options->getValue('menu_section_order'); |
| | | $sections = Options::get('menu_section_order'); |
| | | if (!is_array($sections)) { |
| | | $sections = []; |
| | | } |
| | |
| | | } |
| | | |
| | | protected function renderMenuItem(int $ID, string $slug, string $postType = 'menu_item') { |
| | | $meta = new MetaManager($ID, 'post'); |
| | | $meta = Meta::forPost($ID); |
| | | $fields = $meta->getAll(); |
| | | $values = $meta->getAll([ |
| | | 'post_title', |
| | | '_square_catalog_id', |
| | | 'price', |
| | | 'product_variations', |
| | | $this->base.'item_id', |
| | | $this->base.'price', |
| | | $this->base.'product_variations', |
| | | 'max_order', |
| | | 'min_order', |
| | | 'step' |
| | | ]); |
| | | $priceRange = jvbCurrency($values['price']); |
| | | $variations = $values['product_variations']; |
| | | $priceRange = jvbCurrency($values[$this->base.'price']); |
| | | $variations = $values[$this->base.'product_variations']; |
| | | |
| | | if(!empty($variations) && $variations[0]['name'] === '') { |
| | | if(!empty($variations) && $variations[0][$this->base.'name'] === '') { |
| | | $variations = []; |
| | | } |
| | | |
| | | if (!empty($variations)) { |
| | | $prices = []; |
| | | foreach ($variations as $row) { |
| | | if (isset($row['price']) && is_numeric($row['price'])) { |
| | | $prices[] = (float) $row['price']; |
| | | if (isset($row[$this->base.'price']) && is_numeric($row[$this->base.'price'])) { |
| | | $prices[] = (float) $row[$this->base.'price']; |
| | | } |
| | | } |
| | | if (!empty($prices)) { |
| | |
| | | } |
| | | } |
| | | |
| | | $max = $values['max_order']; |
| | | $min = $values['min_order']; |
| | | $step = $values['step']; |
| | | $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"> |
| | | <div class="menu-item<?= !empty($variations) ? ' variable' : '' ?>" data-section="<?=$slug?>" data-label="<?=$values['post_title']?>" data-id="<?=$ID?>"> |
| | | <div class="header row x-btw"> |
| | | <h3><?= $values['post_title']?></h3> |
| | | <p class="price"><?= $priceRange ?></p> |
| | | </div> |
| | | <div class="description"> |
| | | <?php $meta->render('render', 'post_excerpt')?> |
| | | <?= Render::renderFrom($meta, 'post_excerpt')?> |
| | | </div> |
| | | <div class="info row end"> |
| | | <div class="info col right"> |
| | | <?php |
| | | if (empty($variations)) { |
| | | $meta->render( |
| | | 'form', |
| | | echo Form::render( |
| | | $ID.'|cart_quantity', |
| | | [ |
| | | 'type' => 'number', |
| | | 'type' => 'quantity', |
| | | 'label' => 'Add to Order', |
| | | 'min' => $min, |
| | | 'max' => $max, |
| | | 'step' => $step, |
| | | 'data' => [ |
| | | 'price' => $values['price'], |
| | | 'price' => $values[$this->base.'price'], |
| | | 'id' => $ID, |
| | | 'square-catalog-id' => $values['_square_catalog_id'], |
| | | 'catalog-id' => $values[$this->base.'item_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', |
| | | echo sprintf( |
| | | '<div class="row right nowrap"> |
| | | <div> |
| | | <h4>%s — %s</h4> |
| | | %s%s%s |
| | | </div>', |
| | | $row[$this->base.'name'], |
| | | jvbCurrency($row[$this->base.'price']), |
| | | empty($row[$this->base.'description']) ? '' : wpautop($row[$this->base.'description']), |
| | | empty($row[$this->base.'ingredients']) ? '' : wpautop('<b>Ingredients:</b>'.$row[$this->base.'ingredients']), |
| | | empty($row[$this->base.'preparation_time_duration']) ? '' : wpautop('Ready in <b>'.$row[$this->base.'preparation_time_duration'].'</b>'), |
| | | ); |
| | | echo Form::render( |
| | | 'quantity-'.$index, |
| | | '', |
| | | [ |
| | | 'type' => 'number', |
| | | 'label' => $row['label'], |
| | | 'type' => 'quantity', |
| | | 'label' => $row[$this->base.'name'], |
| | | 'min' => $min, |
| | | 'max' => $max, |
| | | 'step' => $step, |
| | | 'data' => [ |
| | | 'price' => $row['price'], |
| | | 'id' => $ID, |
| | | 'square-catalog-id' => $row['_square_catalog_id'], |
| | | 'name' => $row['label'], |
| | | 'price' => $row[$this->base.'price'], |
| | | 'id' => $ID.'-'.$row[$this->base.'sku'], |
| | | 'catalog-id' => $row[$this->base.'item_id'], |
| | | 'name' => $row[$this->base.'name'], |
| | | 'add-to-cart' => '' |
| | | ], |
| | | 'add' => 'Add 1 '.$row['label'].' to cart.', |
| | | 'remove' => 'Remove 1 '.$row['label'].' from cart.', |
| | | ], |
| | | true); |
| | | 'add' => 'Add 1 '.$row[$this->base.'name'].' to cart.', |
| | | 'remove' => 'Remove 1 '.$row[$this->base.'name'].' from cart.', |
| | | ] |
| | | ); |
| | | echo '</div>'; |
| | | } |
| | | } |
| | | ?> |