Jake Vanderwerf
5 days ago 75a097a018a0090f5902758353c578fce4aa2a25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace JVBase\blocks;
 
use JVBase\managers\Cache;
use WP_Block;
use WP_Query;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
class GlossaryBlock
{
    protected Cache $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 = Cache::for('glossary_terms', WEEK_IN_SECONDS)->connect('post', true);
        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',
        ]);
        $glossary = [];
        if ($posts->have_posts()) {
            foreach($posts->posts as $post) {
//              $meta = Meta::forPost($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(' ·  ·', '&emsp;',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));
    }
}