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, [
'render_callback' => [ $this, 'render' ]
]);
}
protected function buildParams():void
{
// if (is_post_type_archive(BASE.'menu_item') ||
if (is_post_type_archive('product') ||
(function_exists('is_shop') && is_shop())) {
$this->params = ['all'];
// } elseif (is_tax(BASE.'section')) {
} elseif (is_tax('product_cat')) {
$this->params = [];
} else {
$this->params = [];
}
}
public function render(array $attributes, string $content, WP_Block $block)
{
$this->buildParams();
if (empty($this->params)) {
return '';
}
jvbInlineStyles('forms');
return $this->cache->remember(
$this->params,
function() {
ob_start();
$this->renderBlock();
return ob_get_clean();
}
);
}
protected function renderBlock():void
{
$this->renderHeader();
$this->renderMenu();
$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';
if(!in_array('all', $this->params)) {
$title = ''.$title.'';
}
?>
=$title?>