Jake Vanderwerf
2026-08-29 f859e64a57c624ff2daa2b21355f65a49fd4807d
inc/blocks/MenuBlock.php
@@ -5,7 +5,9 @@
use JVBase\base\Site;
use JVBase\managers\Cache;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
use WP_Block;
use WP_Post;
use WP_Query;
if (!defined('ABSPATH')) {
@@ -30,14 +32,28 @@
    public function __construct()
    {
        $this->cache = Cache::for('menu', 'post', WEEK_IN_SECONDS);
      $this->cache->flush();
      if (Site::hasIntegration('square')) {
         $this->base = '_square_';
      } else if (Site::hasIntegration('helcim')) {
         $this->base = '_helcim_';
      }
        add_action('init', [ $this, 'registerBlock' ]);
      add_action('save_post', [$this, 'flushCache'], 10, 2);
    }
   public function flushCache(int $postID, WP_Post $post):void
   {
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
      if (wp_is_post_revision($postID) || wp_is_post_autosave($postID)) return;
      $postType = $post->post_type;
      if ($postType !== 'product' && !in_array($postType, Registrar::getFoodMenuTypes())) {
         return;
      }
      error_log('Flushing food menu block cache');
      $this->cache->flush();
   }
    public function registerBlock():void
    {
        register_block_type($this->path, [
@@ -66,7 +82,6 @@
         return '';
      }
      jvbInlineStyles('forms');
      $this->cache->flush();
      return $this->cache->remember(
         $this->params,
         function() {
@@ -84,6 +99,23 @@
        $this->renderOnThisPage();
    }
   protected function termHasPosts(int $termID):bool
   {
      $query = new WP_Query([
         'post_type' => 'product',
         'post_status'  => 'publish',
         'posts_per_page'  => 1,
         'fields' => 'ids',
         'tax_query' => [
            [
               'taxonomy' => 'product_cat',
               'terms'  => $termID
            ]
         ]
      ]);
      return $query->have_posts();
   }
    protected function renderHeader():void
    {
      $title = 'Our Menu';
@@ -99,7 +131,7 @@
            $sections = $this->getSections();
            foreach ($sections as $section) {
//             if (!jvbTermHasPosts($section['id'], BASE.'section')) {
               if (!jvbTermHasPosts($section['id'], 'product_cat')) {
               if (!$this->termHasPosts($section['id'])) {
                  continue;
               }
               ?>
@@ -179,23 +211,27 @@
      $fields = $meta->getAll();
//    jvbDump($fields, 'Fields');
      $excerpt = get_the_excerpt($ID);
      $isVariable = !empty($fields['variations']??[]);
      $variations = $isVariable ? array_map('floatval', array_column($fields['variations'], 'price')) : [];
      $priceRange = $isVariable ?
         (min($variations).' - '.max($variations)) :
         $fields['_price']??'';
         (jvbCurrency(min($variations)).' - '.jvbCurrency(max($variations))) :
         jvbCurrency($fields['_price']??'');
//    if (!$isVariable) {
//       jvbDump($fields, 'Fields');
//    }
      $out = sprintf(
   '<div class="menu-item%s" data-section="%s" data-label="%s" data-id="%s">
   '<div id="%s" class="menu-item%s" data-section="%s" data-label="%s" data-id="%s">
            <div class="header row x-btw">
               <h3>%s</h3>
               <p class="price">$%s</p>
               <p class="price">%s</p>
            </div>
            <div class="description">%s</div>
            <div class="info col right">',
         sanitize_title($fields['post_title']),
         $isVariable ? ' variable' : '',
         $slug,
         $fields['post_title'],