Jake Vanderwerf
2026-02-17 a24a06002081ad71a78ffeff9072725ba39cf121
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
<?php
namespace JVBase\managers\SEO;
 
use JVBase\managers\Cache;
use JVBase\utility\Features;
use WP_Post;
use WP_Term;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Breadcrumb Manager
 *
 * Generates breadcrumb navigation arrays and HTML output
 * Integrates with SchemaOutputManager for structured data
 */
class BreadcrumbManager
{
    private Cache $cache;
    private static ?self $instance = null;
 
    private function __construct()
    {
        $this->cache = Cache::for('breadcrumbs', MONTH_IN_SECONDS)->connect('post')->connect('taxonomy')->connect('user');
        if (JVB_TESTING) {
            $this->cache->flush();
        }
    }
 
    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
 
    /**
     * Get breadcrumb array for current page
     *
     * @return array Array of breadcrumb items with 'name', 'url', optional 'icon' and 'id'
     */
    public function getCrumbs(): array
    {
        if (is_front_page()) {
            return [];
        }
 
        switch (true) {
            case is_singular():
                $key = get_queried_object_id();
                break;
            case is_post_type_archive():
                $obj = get_queried_object();
                $key = $obj->name;
                break;
            case is_tax():
                $obj = get_queried_object();
                $key = $obj->taxonomy;
                break;
            default:
                $key = 'home';
                break;
        }
 
        return $this->cache->remember(
            $key,
            function() {
                $crumbs = $this->buildCrumbs();
                return apply_filters('jvbBreadcrumbs',$crumbs);
            }
        );
    }
 
    /**
     * Build breadcrumb array based on current page context
     */
    private function buildCrumbs(): array
    {
        $crumbs = [];
 
        // Always start with home
        $crumbs[] = [
            'name' => 'Home',
            'icon' => jvbIcon('house'),
            'url'  => get_home_url(),
        ];
 
        $obj = get_queried_object();
        if (is_tax()) {
            $crumbs = $this->addTaxonomyCrumbs($crumbs, $obj);
        } elseif (is_singular()) {
            $crumbs = $this->addArchiveCrumbs($crumbs, $obj);
            $hierarchy = $this->addSingularCrumbs($crumbs, $obj);
            $crumbs = $crumbs + $hierarchy;
        } elseif (is_post_type_archive() && !is_post_type_archive(BASE.'dash')) {
            $crumbs = $this->addArchiveCrumbs($crumbs, $obj);
        }
 
        return $crumbs;
    }
 
    /**
     * Add taxonomy-specific breadcrumbs
     */
    private function addTaxonomyCrumbs(array $crumbs, WP_Term $term): array
    {
        $tax = jvbNoBase($term->taxonomy);
        $config = Features::getConfig($tax, 'term');
 
        // Add parent content archive if taxonomy is for single content type
        if (count($config['for_content']) === 1) {
            $contentConfig = JVB_CONTENT[$config['for_content'][0]];
            $crumbs[] = [
                'name' => $contentConfig['breadcrumb'] ?? $contentConfig['plural'],
                'url'  => get_post_type_archive_link(jvbCheckBase($config['for_content'][0])),
            ];
            $crumbs[] = [
                'name' => 'By ' . $config['singular'],
                'url'  => false,
            ];
        }
 
        // Add directory if exists
        if (Features::forTaxonomy($tax)->has('directory')) {
            $directory = JVB()->directories()?->directories($tax);
            $crumbs[] = [
                'name' => $directory['title'],
                'url'  => $directory['url']
            ];
        }
 
        // Add term hierarchy
        return array_merge($crumbs, $this->buildTermHierarchy($term));
    }
 
    /**
     * Add singular post breadcrumbs
     */
    private function addSingularCrumbs(array $crumbs, WP_Post $post): array
    {
        // Add directory if exists
        $content = jvbNoBase($post->post_type);
        if(Features::forContent($content)->has('show_directory')) {
            $directory = JVB()->directories()->getDirectoryList()[$content]??[];
            if (!empty($directory)) {
                $crumbs[] = [
                    'name'  => $directory['title'],
                    'url'   =>$directory['url']
                ];
            }
        }
 
        // Handle directory posts specially
        if (JVB()->directories()->isDirectory()) {
            $pos = jvbGetDirectoryInfo();
            if (!empty($pos)) {
                // Special case for map
                if ($pos['title'] == 'Map') {
                    $crumbs[] = [
                        'name' => 'Tattoo Shops',
                        'url'  => JVB()->directories()?->directories(BASE.'shop')['url']
                    ];
                }
 
                $crumbs[] = [
                    'name' => $pos['title'],
                    'url'  => $pos['url']
                ];
            }
        } else {
            $name = jvbNoBase($post->post_type);
            if (array_key_exists($name, JVB_CONTENT) && array_key_exists('addCrumb', JVB_CONTENT[$name])) {
                $crumbs = $this->addTaxToCrumbs($crumbs, JVB_CONTENT[$name]['addCrumb']);
            }
            // Add post hierarchy
            $crumbs = array_merge($crumbs, $this->buildPostHierarchy($post));
        }
 
        return $crumbs;
    }
 
    /**
     * Add archive breadcrumbs
     */
    private function addArchiveCrumbs(array $crumbs, object $obj): array
    {
        $type = is_singular() ? $obj->post_type : $obj->name;
        $name = jvbNoBase($type);
 
        if (Features::forSite()->has('is_directory') && $name === 'directory') {
            $crumbs[] = [
                'name'  => JVB()->directories()->referAs(true),
                'url'   => get_post_type_archive_link($type)
            ];
        } elseif ((is_post_type_archive() || !Features::forContent($name)->has('show_directory')) && array_key_exists($name, JVB_CONTENT)) {
            $crumbs[] = [
                'name' => JVB_CONTENT[$name]['breadcrumb'] ?? JVB_CONTENT[$name]['plural'],
                'url'  => get_post_type_archive_link($type)
            ];
        }
 
        return $crumbs;
    }
 
    /**
     * Build term hierarchy recursively
     */
    private function buildTermHierarchy(WP_Term $term, array $crumbs = []): array
    {
        $url = get_term_link($term->term_id);
        array_unshift($crumbs, [
            'name' => html_entity_decode($term->name),
            'url'  => $url,
            'id'   => $term->term_id,
        ]);
 
        if ($term->parent !== 0) {
            $parent = get_term($term->parent, $term->taxonomy);
            if ($parent && !is_wp_error($parent)) {
                $crumbs = $this->buildTermHierarchy($parent, $crumbs);
            }
        }
 
        return $crumbs;
    }
 
    /**
     * Build post hierarchy recursively
     */
    private function buildPostHierarchy(WP_Post $post, array $crumbs = []): array
    {
        array_unshift($crumbs, [
            'name' => $post->post_title,
            'url'  => get_the_permalink($post->ID),
            'id'   => $post->ID,
        ]);
 
        if ($post->post_parent !== 0) {
            $parent = get_post($post->post_parent);
            if ($parent) {
                $crumbs = $this->buildPostHierarchy($parent, $crumbs);
            }
        }
 
        return $crumbs;
    }
 
    /**
     * Render breadcrumb navigation HTML
     *
     * @return string HTML breadcrumb navigation
     */
    public function renderNavigation(): string
    {
        if (is_front_page()) {
            return '';
        }
 
        $crumbs = $this->getCrumbs();
        if (empty($crumbs)) {
            return '';
        }
 
        $out = '<nav id="breadcrumbs">';
        $out .= '<ol itemscope itemtype="https://schema.org/BreadcrumbList">';
 
        $position = 1;
        $total = count($crumbs);
        foreach ($crumbs as $crumb) {
            $label = '<span itemprop="name">' . strtolower($crumb['name']) . '</span>';
 
            // Replace label with icon if present
            if (isset($crumb['icon'])) {
                $label = $crumb['icon'] . '<span class="screen-reader-text" itemprop="name">' . $crumb['name'] . '</span>';
            }
 
            $aOpen = $aClose = '';
 
            // Add link if URL exists and not current page
            if ($crumb['url'] !== false) {
                if ($total !== $position) {
                    $aOpen = '<a itemprop="item" href="' . esc_url($crumb['url']) . '" title="' . esc_attr($crumb['name']) . '">';
                    $aClose = '</a>';
                }
            }
 
            $out .= '<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">';
            $out .= $aOpen . $label . $aClose;
            $out .= '<meta itemprop="position" content="' . $position . '" />';
            $out .= '</li>';
 
            $position++;
        }
 
        $out .= '</ol>';
        $out .= '</nav>';
 
        return $out;
    }
 
    /**
     * Convert breadcrumb array to schema.org format
     * Used by SchemaOutputManager
     *
     * @return array Schema.org BreadcrumbList
     */
    public function toSchema(): array
    {
        $crumbs = $this->getCrumbs();
        if (empty($crumbs)) {
            return [];
        }
 
        $items = [];
        $position = 1;
 
        foreach ($crumbs as $crumb) {
            // Schema requires a URL
            if ($crumb['url'] === false) {
                $crumb['url'] = get_permalink();
            }
 
            $items[] = [
                '@type'    => 'ListItem',
                'position' => $position,
                'name'     => $crumb['name'],
                'item'     => $crumb['url'],
            ];
 
            $position++;
        }
 
        return [
            '@type'           => 'BreadcrumbList',
            '@id'             => get_permalink() . '/#breadcrumbs',
            'itemListElement' => $items
        ];
    }
 
    /**
     * Invalidate breadcrumb cache for specific object
     */
    public function invalidateCache(?int $objectId = null): void
    {
        if ($objectId) {
            $this->cache->forget($objectId);
        } else {
            $this->cache->flush();
        }
    }
 
    public function addTaxToCrumbs(array $crumbs, string $taxonomy):array
    {
        $ID = get_the_ID();
        $taxonomy = jvbCheckBase($taxonomy);
        $terms = get_the_terms($ID, $taxonomy);
        if ($terms && !is_wp_error($terms)) {
            $term = $terms[0];
            $ancestors = get_ancestors($term->term_id, $taxonomy, 'taxonomy');
            $ancestors = array_reverse($ancestors);
            foreach ($ancestors as $ancestor) {
                $aTerm = get_term($ancestor, $taxonomy);
                if ($aTerm && !is_wp_error($aTerm)) {
                    $crumbs[] = [
                        'name' => $aTerm->name,
                        'url'   => get_term_link($ancestor, $taxonomy)
                    ];
                }
            }
            $crumbs[] = [
                'name' => html_entity_decode($term->name),
                'url'   => get_term_link($term, $taxonomy)
            ];
        }
        return $crumbs;
    }
}