Jake Vanderwerf
2025-09-30 2cb91676044ecd0abd9c45b4835abb8b0d042312
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace JVBase\blocks;
 
use JVBase\managers\CacheManager;
use JVBase\forms\TaxonomySelector;
use WP_Block;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
class SummaryBlock
{
    protected CacheManager $cache;
    protected string $config;
    protected string $type;
    protected string $path = JVB_DIR . '/build/summary';
    protected string $image;
    protected string $header;
    protected string $headerExtra;
    protected string $detailsTitle;
    protected array $details;
 
    public function __construct()
    {
        $this->cache = new CacheManager('summary', 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 additonal html (like <small> or <b>)
         */
        $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 === '') ? '' : '<div>'.$headerExtra.'</div>';
 
        /**
         * The HTML string that appears within the <summary> block
         */
        $this->detailsTitle = apply_filters(
            'jvbSummaryDetailsTitle',
            'More',
            $this->getType()
        );
 
        /**
         * The content of the <details> 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 <details> block is rendered
         */
        $this->details = apply_filters(
            'jvbSummaryDetails',
            [],
            $this->getType()
        );
 
 
        $this->icons = new JVBICons();
        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
    {
        ?>
        <header id="top">
            <?php if ($this->image !== ''): ?>
                <?= $this->image ?>
                <div>
            <?php endif; ?>
 
            <h1><?= $this->header ?></h1>
            <?= $this->headerExtra ?>
 
            <?php if ($this->image !==''): ?>
                </div>
            <?php endif; ?>
        </header>
        <?php
    }
 
    protected function renderDetails():void
    {
        $details = array_filter($this->details, function ($value) {
            return $value !== '';
        });
        if (empty($details)) {
            return;
        }
        ?>
        <details class="info">
            <summary class="row btw"><?= $this->detailsTitle ?></summary>
            <?php
            foreach ($this->details as $key => $details) {
                if ($details === '') {
                    continue;
                }
                ?>
                <div id="<?=$key?>">
                  <?= $details ?>
                </div>
            <?php
            }
            ?>
        </details>
        <?php
    }
 
    protected function renderOnThisPage():void
    {
        if (empty($this->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();
    }
}