<?php
|
namespace JVBase\blocks;
|
|
use JVBase\managers\Cache;
|
use JVBase\ui\Navigation;
|
use WP_Block;
|
|
class DrawerMenuBlock {
|
protected Cache $cache;
|
public function __construct()
|
{
|
add_action('init', [ $this, 'registerBlock' ]);
|
}
|
|
/**
|
* Register the FAQ block
|
*/
|
public function registerBlock():void {
|
// Register the block
|
register_block_type(
|
JVB_DIR . '/build/drawer-menu',
|
[
|
'render_callback' => [$this, 'render'],
|
]
|
);
|
}
|
|
/**
|
* Render callback
|
*
|
* @param array $attributes Block attributes
|
* @param string $content Block content
|
* @param WP_Block $block Block instance
|
* @return string Rendered block HTML
|
*/
|
public function render(array $attributes, string $content, WP_Block $block):string
|
{
|
|
$menu_id = $attributes['menuId'] ?? '';
|
$collapsed = $attributes['collapsed'] ?? true;
|
|
// You'd populate this from options, a filter, or however you store menu data
|
$menu_items = apply_filters('jvbDrawerItems', [], $menu_id);
|
|
if (empty($menu_items) || empty($menu_id)) {
|
return '<p>Please configure the drawer menu in block settings.</p>';
|
}
|
|
$cache = Cache::for('drawer');
|
if (JVB_TESTING) {
|
$cache->flush();
|
}
|
|
if (!is_front_page()) {
|
$menu_items[] = [
|
'text' => 'Home',
|
'url' => home_url(),
|
'icon' => 'house-simple',
|
];
|
}
|
$items = array_map(function($item) { return $item['text'];}, $menu_items);
|
|
$key = $cache->generateKey($items);
|
$menu = $cache->remember($key,
|
function () use ($menu_items, $menu_id, $collapsed) {
|
$menu = new Navigation($menu_id);
|
$menu->asDrawer($collapsed)->populateFromArray($menu_items);
|
return $menu->render();
|
});
|
|
global $wp;
|
|
$current = home_url($wp->request.'/');
|
return str_replace($current.'"', $current.'" class="current" aria-current="page"', $menu);
|
}
|
}
|