Jake Vanderwerf
2025-11-23 d7dbe7fee362d587dfc334135d9581b6216a4295
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
namespace JVBase\blocks;
 
use JVBase\managers\CacheManager;
use JVBase\utility\Features;
use JVBase\utility\Checker;
use JVBase\forms\TaxonomySelector;
use WP_Block;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class FeedBlock
{
    protected CacheManager $cache;
    protected array $config;
    protected string $path = JVB_DIR.'/build/feed';
 
    public function __construct()
    {
        // Initialize cache with connections
        $this->cache = CacheManager::for('feed_block', WEEK_IN_SECONDS);
 
        // Set up cache connections for all feed content types
        $this->setupCacheConnections();
 
        add_action('init', [$this, 'registerBlock']);
    }
 
    /**
     * Set up cache connections for feed content
     */
    protected function setupCacheConnections(): void
    {
        // Connect to all content types that show in feed
        $contentTypes = Features::getTypesWithFeature('show_feed', 'content');
        foreach ($contentTypes as $type) {
            CacheManager::for('feed_content')->connectTo('post', $type);
        }
 
        // Connect to all taxonomies that show in feed
        $taxonomies = Features::getTypesWithFeature('show_feed', 'taxonomy');
        foreach ($taxonomies as $tax) {
            CacheManager::for('feed_taxonomy')->connectTo('taxonomy', $tax);
        }
    }
 
    public function registerBlock()
    {
        register_block_type($this->path, [
            'render_callback' => [$this, 'render']
        ]);
    }
 
    protected function buildParams(array $attributes): array
    {
        if (!jvbCheck('inheritQuery', $attributes)) {
            return [
                'title' => $attributes['title'],
                'content' => $attributes['contentTypes'],
                'taxonomies' => $this->getTaxonomies($attributes['contentTypes'])
            ];
        }
        $config = [
            'is_gallery'    => false,
            'content'       => '',
            'taxonomies'    => []
        ];
        $type = get_queried_object();
 
        if (is_post_type_archive() || is_singular()) {
            $content = is_singular() ? jvbNoBase($type->post_type) : jvbNoBase($type->name);
            $mainConfig = JVB_CONTENT[$content]??false;
            if ($mainConfig && array_key_exists('feed', $mainConfig) && array_key_exists('config', $mainConfig['feed'])){
                $config = array_merge($config, $mainConfig['feed']['config']);
            } else {
                $config['content'] = $content;
                $config['icon'] = JVB_CONTENT[$content]['icon']??['logo-triangle'];
            }
            if (is_singular()) {
                $config['source'] = $type->ID;
            }
 
            $config['taxonomies'] = $this->getTaxonomies([$content]);
        } elseif (is_tax()) {
            $content = jvbNoBase($type->taxonomy);
            $mainConfig = JVB_TAXONOMY[$content]??false;
            if ($mainConfig) {
                $config['content'] = $mainConfig['for_content'];
                $config['context'] = $content;  // ← ADD THIS
                $config['taxonomies'] = $this->getTaxonomies($mainConfig['for_content']);
                if (array_key_exists('feed', $mainConfig) && array_key_exists('config', $mainConfig['feed'])){
                    $config = array_merge($config, $mainConfig['feed']['config']);
                }
            }
            $config['source'] = $type->term_id;
        }
 
        if (!is_array($config['content'])) {
            $config['content'] = [$config['content']];
        }
 
        return $config;
    }
 
    /**
     * Get taxonomies for given content types
     * Uses Checker instead of globals
     */
    protected function getTaxonomies(array $content): array
    {
        $checker = Checker::getInstance();
        $taxonomies = [];
 
        foreach ($content as $contentType) {
            $contentTaxonomies = $checker->getTaxonomiesForContent($contentType);
            $contentTaxonomies = array_filter($contentTaxonomies, function($taxonomy) {
                return array_key_exists('show_feed', JVB_TAXONOMY[$taxonomy]) && JVB_TAXONOMY[$taxonomy]['show_feed'];
            });
            $taxonomies = array_merge($taxonomies, $contentTaxonomies);
        }
 
        return array_unique($taxonomies);
    }
 
    public function render(array $attributes, string $content, WP_Block $block)
    {
        $this->config = $this->buildParams($attributes);
        return $this->cache->remember(
            $this->config,
            function() {
                return $this->renderBlock();
            }
        );
    }
 
    protected function renderBlock(): string
    {
        $ids = (array_key_exists('ids', $this->config) && !empty($this->config['ids'])) ? ' id="'.implode(' ',$this->config['ids']).'"' : '';
        $classes = (array_key_exists('classes', $this->config) && !empty($this->config['classes'])) ? ' class="'.implode(' ',$this->config['classes']).'"' : '';
        $source = (array_key_exists('source', $this->config)) ? ' data-source="'.$this->config['source'].'"' : '';
        $context = (array_key_exists('context', $this->config)) ? ' data-context="'.$this->config['context'].'"' : '';
        $icons = (array_key_exists('icon', $this->config)) ? ' data-icon="'.$this->config['icon'].'"' : ' data-icon="logo-triangle"';
        $gallery = (array_key_exists('is_gallery', $this->config) && $this->config['is_gallery']) ? ' data-gallery' : '';
        $content = (array_key_exists('content', $this->config)) ? ' data-content="'.implode(',',$this->config['content']).'"' : '';
        ob_start();
        ?>
        <section<?= $ids.$classes ?> class="feed-block"<?= $content.$source.$context.$gallery.$icons ?>>
            <?php
            $this->renderFilters();
            $this->renderGrid();
            $this->renderLoader();
            $this->renderTemplates();
            echo TaxonomySelector::outputSelectorModal();
            ?>
        </section>
        <?php
        return ob_get_clean();
    }
 
    protected function renderFilters(): void
    {
        if (empty($this->config)) {
            return;
        }
 
        $feedContent = $this->getFeedContent();
        $hasMany = count($this->config['content']) > 1;
        ?>
        <form class="feed-filters" data-save="feed-<?=$this->config['context']?>">
            <?php if ($hasMany) {
                //If we have multiple content, only show the content first
                ?>
                <details class="col a-start">
                <summary class="row btw">
                    <span class="label">SHOWING: </span>
                    <?php
                    $labels = [];
                    foreach ($this->config['content'] as $i => $type) :
 
                        $checked = $i === 0 ? ' checked' : '';
                        $label = $feedContent[$type]['plural'] ?? ucfirst($type);
                        ?>
                        <input type="radio"
                               id="filter-<?= esc_attr($type) ?>"
                               class="btn"
                               name="content"
                               data-filter="content"
                               value="<?= esc_attr($type) ?>"
                            <?= $checked ?>>
                        <label for="filter-<?= esc_attr($type) ?>" title="Show <?= $label ?>" class="row">
                            <?= jvbIcon($feedContent[$type]['icon']) ?>
                            <span class="screen-reader-text"><?= $label ?></span>
                        </label>
                        <?php
                        $labels['filter-'.$type] = $label;
                    endforeach;
                    ?>
                    <ul class="filter-label">
                        <?php
                        $i = 0;
                        foreach ($labels as $id => $label) {
                            $active = $i === 0 ? ' class="active"' : '';
                            ?>
                            <li id="<?= $id ?>"<?= $active ?>>
                                <?= $label ?>
                            </li>
                            <?php
                            $i++;
                        }
                        ?>
                    </ul>
            <?php } ?>
 
 
                    <?php if (Features::forSite()->has('favourites') && is_user_logged_in()) : ?>
                        <input type="checkbox" id="favourites" class="btn" name="favourites" value="on"
                               data-filter="favourites">
                        <label for="favourites" title="Show Favourites" class="row">
                            <?= jvbIcon('heart').jvbIcon('heart', ['style' => 'fill']) ?>
                            <span class="screen-reader-text">Show Favourites Only</span>
                        </label>
                    <?php endif; ?>
                <?php if ($hasMany) { ?>
                </summary>
                <?php } ?>
 
                    <div class="filters">
                        <div class="filter-group row start">
                            <span class="label">FILTER BY:</span>
 
                            <?php
                            $checker = Checker::getInstance();
                            foreach ($this->config['taxonomies'] as $tax) :
                                $taxConfig = JVB_TAXONOMY[$tax] ?? null;
                                if (!$taxConfig) continue;
 
                                $contentForTax = $checker->getContentForTaxonomy($tax);
                                $hidden = empty($contentForTax) ? ' hidden' : '';
 
                                $taxSelector = new TaxonomySelector(
                                    'feed-'.$tax,
                                    $tax,
                                    [
                                        'icon'          => $taxConfig['icon']??'logo-triangle',
                                        'update'        => '.selected-items-section .selected-items',
                                        'types'         => $contentForTax,
                                        'autocomplete'  => false,
                                        'hidden'        => $hidden,
                                        'output'        => 'minimal'
                                    ]
                                );
                                echo $taxSelector->render();
                            endforeach;
                            ?>
                        </div>
                        <div class="selected-items-section">
                            <div class="selected-items row"></div>
                            <div class="filter-actions row">
                                <?= jvbRenderToggleTextField('match', 'Match', 'Filters', 'ALL', 'ANY', false, ['filter' => 'match']) ?>
                                <button type="button" class="clear-filters row">
                                    <?= jvbIcon('x') ?>
                                    Clear All Filters
                                </button>
                            </div>
                        </div>
                    </div>
 
                    <div class="row btw nowrap">
                        <div class="order-by filter-group row start w-full">
                            <span class="label">ORDER BY:</span>
                            <?php
                            //TODO: Get content types that can be sorted alphabetically
                            ?>
                            <input type="radio" id="order-title" class="btn" name="orderby" value="title" data-for="artist,shop" data-filter="orderby" hidden>
                            <label for="order-title" title="Order by Name" class="row">
                                <?= jvbIcon('alphabetical') ?>
                                <span class="label">Name</span>
                            </label>
 
                            <input type="radio" id="order-date" class="btn" name="orderby" value="date" data-filter="orderby" checked>
                            <label for="order-date" title="Order by Date" class="row">
                                <?= jvbIcon('calendar', ['title' => 'Date']) ?>
                                <span class="label">Date</span>
                            </label>
 
                            <input type="radio" id="order-random" class="btn" name="orderby" value="random" data-filter="orderby">
                            <label for="order-random" title="Random Order" class="row">
                                <?= jvbIcon('shuffle') ?>
                                <span class="label">Random</span>
                            </label>
                        </div>
 
                        <div class="order-direction filter-group row start w-full" data-for-order="date,title">
                            <span class="label">ORDER:</span>
                            <input type="radio" id="order-desc" class="btn" name="order" value="desc" data-filter="order" checked>
                            <label for="order-desc" title="Sort Descending (A-Z, 1-10)" class="row">
                                <?= jvbIcon('sort-descending') ?>
                                <span class="label" >DESC (A-Z)</span>
                            </label>
 
                            <input type="radio" id="order-asc" class="btn" name="order" value="asc" data-filter="order">
                            <label for="order-asc" title="Sort Ascending (Z-A, 10-1)" class="row">
                                <?= jvbIcon('sort-ascending') ?>
                                <span class="label" >ASC (Z-A)</span>
                            </label>
                        </div>
                    </div>
            <?php if ($hasMany) { ?>
                </details>
            <?php } ?>
        </form>
        <?php
    }
 
    protected function renderGrid(): void
    {
        ?>
        <div class="item-grid"></div>
        <?php
    }
 
    protected function renderLoader(): void
    {
        ?>
        <button type="button" class="load-more">
            <?= jvbIcon('arrow-elbow-left-down') ?>
            Show Me More
            <?= jvbIcon('arrow-elbow-right-down') ?>
        </button>
 
        <?= jvbLoadingScreen() ?>
        <?php
        if (array_key_exists('is_gallery', $this->config)) {
            jvbRenderGallery();
        }
    }
 
    protected function renderTemplates(): void
    {
        echo '<template class="feed-item">'.apply_filters('jvbFeedItem', '<details class="item feed" data-umami-event="view_feed">
            <summary class="row btw">
                <span class="handle">DETAILS</span>
                <button class="favourite" title="Add to favourites" onclick="toggleFavourite(this)">
                    '.jvbIcon('heart')
                .jvbIcon('heart', ['style'=>'fill']).'
                </button>
                <div class="feed-images">
                    <a>
                        <img width="300px" height="300px" loading="lazy" decoding="async">
                    </a>
                </div>
            </summary>
 
            <div class="item-info">
                <h3><a></a></h3>
                <div class="item">
                    <span class="label"></span>
                    <a></a>
                    <p></p>
                </div>
                <div class="item-list">
                    <span class="label"></span>
                        <ul>
                            <li>
                                <a></a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </details>', $this->config).'</template>';
 
        echo '<template class="emptyState">'.apply_filters('jvbFeedEmptyState', '<div class="feed-empty-state">
                <h3>NOTHING HERE...</h3>
                <p>Try tweaking those filters a bit.</p>
                <p>Edmonton\'s got talent - let\'s find it.</p>
            </div>', $this->config). '</template>';
 
        echo '<template class="placeholderTemplate"><div class="placeholder">'.apply_filters('jvbFeedPlaceholder', '').'</div></template>';
    }
 
    /**
     * Get feed content using Features instead of get_option
     * Returns array of slug => config for types that show in feed
     */
    public function getFeedContent(): array
    {
        return JVB()->routes('feed')->getFeedTypesConfig();
    }
}