| | |
| | | use JVBase\base\Options; |
| | | use JVBase\base\Site; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\forms\TaxonomySelector; |
| | | use JVBase\meta\Form; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\meta\Render; |
| | | use JVBase\registrar\Registrar; |
| | | use WP_Block; |
| | | use WP_Post; |
| | | use WP_Query; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | |
| | | $this->base = '_helcim_'; |
| | | } |
| | | add_action('init', [ $this, 'registerBlock' ]); |
| | | add_action('save_post', [$this, 'flushCache'], 10, 2); |
| | | add_action('wp_footer', [$this, 'renderGallery']); |
| | | } |
| | | |
| | | 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, [ |
| | |
| | | return ''; |
| | | } |
| | | jvbInlineStyles('forms'); |
| | | $this->cache->flush(); |
| | | return $this->cache->remember( |
| | | $this->params, |
| | | function() { |
| | |
| | | $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'; |
| | |
| | | $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; |
| | | } |
| | | ?> |
| | |
| | | protected function getSections():array |
| | | { |
| | | if (!$this->sections) { |
| | | $sections = Options::get('menu_section_order'); |
| | | if (!is_array($sections)) { |
| | | $sections = []; |
| | | } |
| | | $this->sections = $sections; |
| | | $this->sections = aneb_menu_sections(); |
| | | } |
| | | |
| | | return $this->sections; |
| | |
| | | |
| | | $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']??''; |
| | | |
| | | $priceRange = jvbCurrency($fields['price']); |
| | | if ($isVariable) { |
| | | $prices = array_map('floatval', array_column($fields['variations'], 'price')); |
| | | $min = min($prices); |
| | | $max = max($prices); |
| | | $priceRange = $min===$max ? jvbCurrency($min) : jvbCurrency($min) .' - '.jvbCurrency($max); |
| | | |
| | | } |
| | | |
| | | // if (!$isVariable) { |
| | | // jvbDump($fields, 'Fields'); |
| | | // } |
| | | // jvbDump(get_post_thumbnail_id($ID), 'thumbnail'); |
| | | // $product = wc_get_product($ID); |
| | | // jvbDump($product->get_image_id(), 'product image'); |
| | | // jvbDump($product->get_gallery_image_ids(), 'Gallery images'); |
| | | if ($fields['post_title'] === 'Breakfast Poutine') { |
| | | // jvbDump($ID); |
| | | // jvbDump($fields); |
| | | } |
| | | |
| | | $gallery = ''; |
| | | if (!empty($fields['gallery_image_ids'])) { |
| | | $gallery = implode(array_map(function($id) use ($fields){ |
| | | $id = absint($id); |
| | | $caption = wp_get_attachment_caption($id); |
| | | return sprintf( |
| | | '<figure>%s<figcaption>%s</figcaption></figure>', |
| | | jvbFormatImage($id,'tiny','medium',true,'menu'), |
| | | empty($caption) ? $fields['post_title'] : $caption |
| | | ); |
| | | }, $fields['gallery_image_ids'])); |
| | | } |
| | | |
| | | $caption = ''; |
| | | if (!empty($fields['image_id'])) { |
| | | $caption = wp_get_attachment_caption((int)$fields['image_id']); |
| | | $caption = empty($caption) ? $fields['post_title'] : $caption; |
| | | } |
| | | |
| | | |
| | | $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> |
| | | <figure>%s<figcaption>%s</figcaption></figure> |
| | | <div class="text row x-btw"> |
| | | <h3>%s</h3> |
| | | <p class="price">%s</p> |
| | | </div> |
| | | </div> |
| | | <div class="gallery row%s">%s</div> |
| | | <div class="description">%s</div> |
| | | <div class="info col right">', |
| | | sanitize_title($fields['post_title']), |
| | | $isVariable ? ' variable' : '', |
| | | $slug, |
| | | $fields['post_title'], |
| | | $ID, |
| | | !empty($fields['image_id']) ? jvbFormatImage((int)$fields['image_id'],'tiny','medium',true,'menu') : '', |
| | | $caption, |
| | | $fields['post_title'], |
| | | $priceRange, |
| | | empty($gallery) ? '' : ' has', |
| | | $gallery, |
| | | $excerpt |
| | | ); |
| | | |
| | |
| | | <div> |
| | | <h4>%s — %s</h4> |
| | | %s |
| | | </div>', //Note: we had 3 %s%s%s before the last </div> last time. This was to add optional ingredients or preparation time duration, not default in WooCommerce |
| | | </div><a href="?add-to-cart=%d&variation_id=%d" |
| | | data-quantity="1" |
| | | data-product_id="%d" |
| | | data-variation_id="%d" |
| | | class="btn chip add_to_cart_button ajax_add_to_cart add-to-cart"> |
| | | %s |
| | | <span>Add to Cart</span> |
| | | </a>', //Note: we had 3 %s%s%s before the last </div> last time. This was to add optional ingredients or preparation time duration, not default in WooCommerce |
| | | $row['title'], |
| | | jvbCurrency($row['price']), |
| | | empty($row['description']) ? '' : wpautop($row['description']), |
| | | $ID, |
| | | $row['id'], |
| | | $ID, |
| | | $row['id'], |
| | | jvbIcon('plus-square') |
| | | ); |
| | | |
| | | $out .= Form::render( |
| | | $row['id'], |
| | | '', |
| | | [ |
| | | 'type' => 'quantity', |
| | | 'label' => $row['title'], |
| | | 'min' => 0, |
| | | 'max' => 30, |
| | | 'data' => [ |
| | | 'price' => $row['price'], |
| | | 'id' => $row['id'], |
| | | 'sku' => $row['sku'], |
| | | 'name' => $row['title'], |
| | | 'add-to-cart' => '' |
| | | ], |
| | | 'add' => 'Add 1 '.$row['title'].' to cart', |
| | | 'remove' => 'Remove 1 '.$row['title']. ' from cart', |
| | | ] |
| | | ); |
| | | // $out .= Form::render( |
| | | // $row['id'], |
| | | // '', |
| | | // [ |
| | | // 'type' => 'quantity', |
| | | // 'label' => $row['title'], |
| | | // 'min' => 0, |
| | | // 'max' => 30, |
| | | // 'data' => [ |
| | | // 'price' => $row['price'], |
| | | // 'id' => $row['id'], |
| | | // 'sku' => $row['sku'], |
| | | // 'name' => $row['title'], |
| | | // 'add-to-cart' => '' |
| | | // ], |
| | | // 'add' => 'Add 1 '.$row['title'].' to cart', |
| | | // 'remove' => 'Remove 1 '.$row['title']. ' from cart', |
| | | // ] |
| | | // ); |
| | | $out .= '</div>'; |
| | | } |
| | | } else { |
| | | $out .= Form::render( |
| | | $out .= sprintf( |
| | | '<a href="?add-to-cart=%d" |
| | | data-quantity="1" |
| | | data-product_id="%d" |
| | | class="btn chip add_to_cart_button ajax_add_to_cart add-to-cart"> |
| | | %s |
| | | <span>Add to Cart</span> |
| | | </a>', |
| | | $ID, |
| | | '', |
| | | [ |
| | | 'type' => 'quantity', |
| | | 'label' => 'Add to Order', |
| | | 'min' => 0, |
| | | 'max' => 30, |
| | | 'step' => 1, |
| | | 'data' => [ |
| | | 'price' => $fields['_price'], |
| | | 'id' => $ID, |
| | | 'name' => $fields['post_title'], |
| | | 'step' => 1, |
| | | 'add-to-cart' => '', |
| | | ], |
| | | 'add' => 'Add 1 '.$fields['post_title'].' to cart', |
| | | 'remove' => 'Remove 1 '.$fields['title']. ' from cart', |
| | | ] |
| | | $ID, |
| | | jvbIcon('plus-square') |
| | | ); |
| | | // $out .= Form::render( |
| | | // $ID, |
| | | // '', |
| | | // [ |
| | | // 'type' => 'quantity', |
| | | // 'label' => 'Add to Order', |
| | | // 'min' => 0, |
| | | // 'max' => 30, |
| | | // 'step' => 1, |
| | | // 'data' => [ |
| | | // 'price' => $fields['_price'], |
| | | // 'id' => $ID, |
| | | // 'name' => $fields['post_title'], |
| | | // 'step' => 1, |
| | | // 'add-to-cart' => '', |
| | | // ], |
| | | // 'add' => 'Add 1 '.$fields['post_title'].' to cart', |
| | | // 'remove' => 'Remove 1 '.$fields['title']. ' from cart', |
| | | // ] |
| | | // ); |
| | | } |
| | | |
| | | $out .= '</div></div>'; |
| | |
| | | } |
| | | echo jvbOnThisPage(array_keys($this->details)); |
| | | } |
| | | |
| | | public function renderGallery():void |
| | | { |
| | | jvbRenderGallery(); |
| | | } |
| | | } |