<?php
|
namespace JVBase\blocks;
|
|
use JVBase\managers\CacheManager;
|
use JVBase\forms\TaxonomySelector;
|
use JVBase\meta\MetaManager;
|
use WP_Block;
|
use WP_Query;
|
|
if (!defined('ABSPATH')) {
|
exit; // Exit if accessed directly
|
}
|
|
class GlossaryBlock
|
{
|
protected CacheManager $cache;
|
protected string $config;
|
protected string $type;
|
protected string $path = JVB_DIR . '/build/glossary';
|
protected string $image;
|
protected string $header;
|
protected array $details;
|
protected array|false $sections = false;
|
|
public function __construct()
|
{
|
$this->cache = CacheManager::for('glossary_terms', WEEK_IN_SECONDS)->connectTo('post', 'terms');
|
add_action('init', [ $this, 'registerBlock' ]);
|
}
|
|
public function registerBlock()
|
{
|
register_block_type($this->path, [
|
'render_callback' => [ $this, 'render' ]
|
]);
|
}
|
|
public function render(array $attributes, string $content, WP_Block $block)
|
{
|
$cache = $this->cache->get('all');
|
if ($cache) {
|
return $cache;
|
}
|
|
ob_start();
|
$this->renderBlock();
|
$content = ob_get_clean();
|
$this->cache->set('all', $content);
|
return $content;
|
}
|
|
protected function renderBlock():void
|
{
|
$this->renderHeader();
|
$this->renderGlossary();
|
$this->renderOnThisPage();
|
}
|
|
protected function renderHeader():void
|
{
|
?>
|
<header id="top">
|
<h1>Glossary of Terms</h1>
|
<p class="font-large">Lost in laser lingo? We'll translate.</p>
|
<p>Understanding how tattoo removal works helps you make better decisions about your treatment. When we mention wavelengths targeting specific pigments, or explain why your lymphatic system matters, we're describing real biological processes - not just throwing around fancy words.</p>
|
<p>This glossary explains the terms you'll hear during your removal journey in plain language. We're not trying to impress you with jargon - we just want you to know what we're talking about when we say things like "photoacoustic energy" or "oxidation."</p>
|
<nav class="glossary-index"><ul>
|
<?php
|
$glossary = $this->getGlossary();
|
foreach ($glossary as $slug => $term) {
|
?>
|
<li>
|
<a href="#<?=$slug?>" title="Jump to <?=$term['post_title']?>">
|
<?=$term['post_title']?>
|
</a>
|
</li>
|
<?php
|
}
|
?>
|
</ul></nav>
|
</header>
|
<?php
|
}
|
protected function getGlossary():array
|
{
|
$posts = new WP_Query([
|
'post_type' => BASE.'terms',
|
'posts_per_page' => -1,
|
'post_status' => 'publish',
|
'orderby' => 'title',
|
'order' => 'asc',
|
// 'fields' => 'ids'
|
]);
|
$glossary = [];
|
if ($posts->have_posts()) {
|
foreach($posts->posts as $post) {
|
// $meta = new MetaManager($post, 'post');
|
// $fields = $meta->getAll();
|
// $glossary[$fields['post_slug']] = $fields;
|
$glossary[$post->post_name] = [
|
'post_title' => $post->post_title,
|
'post_content' => $post->post_content,
|
'pronunciation' => '',
|
'type' => '',
|
];
|
}
|
}
|
return $glossary;
|
}
|
|
protected function renderGlossary():void
|
{
|
$glossary = $this->getGlossary();
|
?>
|
<dl class="glossary">
|
<?php
|
foreach ($glossary as $slug => $term) {
|
?>
|
<dt id="<?=$slug?>">
|
<h2><?=$term['post_title']?></h2>
|
<?php
|
$out = '';
|
if ($term['pronunciation'] !== '' && !empty($term['pronunciation'])) {
|
$out = '[ '.str_replace(' · ·', ' ',implode(' · ', $term['pronunciation'])).' ]';
|
}
|
if ($term['type'] !== '') {
|
$out .= ' <span>'.$term['type'].'</span>';
|
}
|
if ($out !== ''){
|
echo '<span>'.$out.'</span>';
|
}
|
?>
|
</dt>
|
<dd>
|
<?= $term['post_content']?>
|
</dd>
|
<?php
|
}
|
?>
|
</dl>
|
<?php
|
}
|
|
|
protected function renderOnThisPage():void
|
{
|
if (empty($this->details)) {
|
return;
|
}
|
echo jvbOnThisPage(array_keys($this->details));
|
}
|
}
|