cache = CacheManager::for('summary_block', WEEK_IN_SECONDS); add_action('init', [ $this, 'registerBlock' ]); } public function registerBlock() { register_block_type($this->path, [ 'render_callback' => [ $this, 'render' ] ]); } protected function getConfig():string { return (is_tax()) ? 'tax' : 'content'; } protected function generateKey():string { $obj = get_queried_object(); return match (true) { is_tax() => jvbNoBase($obj->taxonomy) . $obj->term_id, is_a($obj, 'WP_Post_type') => jvbNoBase($obj->name), is_a($obj, 'WP_User') => jvbUserRole($obj->ID), default => jvbNoBase($obj->post_type) . $obj->ID, }; } public function render(array $attributes, string $content, WP_Block $block) { $this->config = $this->getConfig(); $key = $this->generateKey(); $cache = $this->cache->get($key); $cache = false; if ($cache) { return $cache; } $this->image = apply_filters( 'jvbSummaryImage', '', $this->getType() ); /** * The h1 title element, excluding the h1 tag. * Can include additional html (like or ) */ $this->header = apply_filters( 'jvbSummaryHeader', $this->defaultTitle(), $this->getType() ); /** * Anything that appears after the h1 tag, but before the details block (optional)) */ $headerExtra = apply_filters( 'jvbSummaryHeaderExtra', '', $this->getType() ); $this->headerExtra = ($headerExtra === '') ? '' : '
'.$headerExtra.'
'; /** * The HTML string that appears within the block */ $this->detailsTitle = apply_filters( 'jvbSummaryDetailsTitle', 'More', $this->getType() ); /** * The content of the
block. * This should return either an empty array for no information (so no details block) * Or an array with slug => (string) html content, excluding outer div shell * * If empty, no
block is rendered */ $this->details = apply_filters( 'jvbSummaryDetails', [], $this->getType() ); ob_start(); $this->renderBlock(); $content = ob_get_clean(); $this->cache->set($key, $content); return $content; } protected function renderBlock():void { $this->renderHeader(); $this->renderDetails(); $this->renderOnThisPage(); } protected function renderHeader():void { ?>
image !== ''): ?> image ?>

header ?>

headerExtra ?> image !==''): ?>
details, function ($value) { return $value !== ''; }); if (empty($details)) { return; } ?>
detailsTitle ?> details as $key => $details) { if ($details === '') { continue; } ?>
details)) { return; } echo jvbOnThisPage(array_keys($this->details)); } protected function getType():string { if (empty($this->type)) { $this->type = match (true) { is_tax() => jvbNoBase(get_queried_object()->taxonomy), is_post_type_archive() => jvbNoBase(get_post_type()), default => jvbNoBase(get_queried_object()->post_type), }; } return $this->type; } protected function defaultTitle():string { return (is_singular()) ? get_the_title() : get_the_archive_title(); } }