From 87237f15618e2320967b9380c360b89f7323d2e9 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 30 Aug 2026 16:01:05 +0000
Subject: [PATCH] =Meta overhaul, slight reference fixes in BreadcrumbsManager
---
inc/managers/SEO/BreadcrumbManager.php | 253 +++++++++++++++++++++++++++++--------------------
1 files changed, 149 insertions(+), 104 deletions(-)
diff --git a/inc/managers/SEO/BreadcrumbManager.php b/inc/managers/SEO/BreadcrumbManager.php
index ff9ff03..9d09ed6 100644
--- a/inc/managers/SEO/BreadcrumbManager.php
+++ b/inc/managers/SEO/BreadcrumbManager.php
@@ -1,8 +1,10 @@
<?php
namespace JVBase\managers\SEO;
-use JVBase\managers\CacheManager;
-use JVBase\utility\Features;
+use JVBase\managers\Cache;
+use JVBase\managers\DirectoryManager;
+use JVBase\registrar\Registrar;
+use JVBase\base\Site;
use WP_Post;
use WP_Term;
@@ -18,13 +20,22 @@
*/
class BreadcrumbManager
{
- private CacheManager $cache;
+ private Cache $cache;
+ private Cache $termCache;
+ private Cache $postCache;
+ private Cache $userCache;
private static ?self $instance = null;
+ private static bool $hasDirectory;
+ private static array $directories;
private function __construct()
{
- $this->cache = CacheManager::for('breadcrumbs', MONTH_IN_SECONDS)->connectTo('all');
-// $this->cache->clear();
+ $this->cache = Cache::for('breadcrumbs');
+ $this->termCache = Cache::for('breadcrumbs_tax', 'term');
+ $this->postCache = Cache::for('breadcrumbs_post', 'post');
+ $this->userCache = Cache::for('breadcrumbs_user', 'user');
+ self::$hasDirectory = Site::has('is_directory');
+ self::$directories = self::$hasDirectory ? DirectoryManager::getInstance()->directories() : [];
}
public static function getInstance(): self
@@ -46,33 +57,27 @@
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;
- }
- $crumbs = $this->cache->get($key);
- if ($crumbs !== false) {
- return $crumbs;
- }
+ $key = match(true) {
+ is_singular() => get_queried_object_id(),
+ is_tax() => get_queried_object()->term_id,
+ is_post_type_archive() => get_queried_object()->name,
+ default => 'home'
+ };
+ $cache = match(true) {
+ is_singular() => $this->postCache,
+ is_tax() => $this->termCache,
+ is_author() => $this->userCache,
+ default => $this->cache
+ };
- $crumbs = $this->buildCrumbs();
- $crumbs = apply_filters('jvbBreadcrumbs',$crumbs);
- $this->cache->set($key, $crumbs);
-
- return $crumbs;
+ return $cache->remember(
+ $key,
+ function() {
+ $crumbs = $this->buildCrumbs();
+ return apply_filters('jvbBreadcrumbs',$crumbs);
+ }
+ );
}
/**
@@ -84,15 +89,18 @@
// Always start with home
$crumbs[] = [
- 'name' => 'Home',
+ 'name' => get_bloginfo('name'),
'icon' => jvbIcon('house'),
'url' => get_home_url(),
];
$obj = get_queried_object();
+ if (is_null($obj)) {
+ return $crumbs;
+ }
if (is_tax()) {
$crumbs = $this->addTaxonomyCrumbs($crumbs, $obj);
- } elseif (is_singular()) {
+ } elseif (is_singular() || is_home()) {
$crumbs = $this->addArchiveCrumbs($crumbs, $obj);
$hierarchy = $this->addSingularCrumbs($crumbs, $obj);
$crumbs = $crumbs + $hierarchy;
@@ -109,28 +117,43 @@
private function addTaxonomyCrumbs(array $crumbs, WP_Term $term): array
{
$tax = jvbNoBase($term->taxonomy);
- $config = Features::getConfig($tax, 'term');
-
+ $registrar = Registrar::getInstance($tax);
// 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,
- ];
+ if ($registrar) {
+ if (count($registrar->registrar->for) === 1){
+ $content = is_array($registrar->registrar->for) ? $registrar->registrar->for[0] : $registrar->registrar->for;
+ $contentRegistrar = Registrar::getInstance($content);
+
+ if(self::$hasDirectory && $contentRegistrar && $contentRegistrar->hasFeature('show_directory')) {
+ if (!empty(self::$directories[$content]??[])){
+ $crumbs[] = [
+ 'name' => self::$directories[$content]['title'],
+ 'url' => self::$directories[$content]['url']
+ ];
+ }
+ } else {
+ $crumbs[] = [
+ 'name' => $contentRegistrar->getConfig('breadcrumbs')['title']??$contentRegistrar->getPlural(),
+ 'url' => get_post_type_archive_link(jvbCheckBase($content)),
+ ];
+ }
+// $crumbs[] = [
+// 'name' => 'By ' . $registrar->getSingular(),
+// 'url' => false,
+// ];
+ }
}
+
// Add directory if exists
- if (Features::forTaxonomy($tax)->has('directory')) {
- $directory = JVB()->directories()?->directories($tax);
- $crumbs[] = [
- 'name' => $directory['title'],
- 'url' => $directory['url']
- ];
+ if (self::$hasDirectory && $registrar && $registrar->hasFeature('show_directory')) {
+
+ if (!empty(self::$directories[$tax]??[])) {
+ $crumbs[] = [
+ 'name' => self::$directories[$tax]['title'],
+ 'url' => self::$directories[$tax]['url']
+ ];
+ }
}
// Add term hierarchy
@@ -144,25 +167,21 @@
{
// 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']
- ];
- }
+ $registrar = Registrar::getInstance($content);
+ $crumbConfig = false;
+ if ($registrar){
+ $crumbConfig = $registrar->getConfig('breadcrumbs');
}
// Handle directory posts specially
- if (JVB()->directories()->isDirectory()) {
+ if (self::$hasDirectory && DirectoryManager::getInstance()->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']
+ 'url' => DirectoryManager::getInstance()->directories(BASE.'shop')['url']??''
];
}
@@ -172,9 +191,9 @@
];
}
} else {
- $name = jvbNoBase($post->post_type);
- if (Features::forContent($name)->has('addCrumb')) {
- $this->addTaxToCrumbs($crumbs, JVB_CONTENT[$name]['addCrumb']);
+
+ if ($registrar && !empty($crumbConfig['addCrumb'])) {
+ $crumbs = $this->addTaxToCrumbs($crumbs, $crumbConfig['addCrumb']);
}
// Add post hierarchy
$crumbs = array_merge($crumbs, $this->buildPostHierarchy($post));
@@ -188,19 +207,50 @@
*/
private function addArchiveCrumbs(array $crumbs, object $obj): array
{
+ if (is_singular('page') || is_home()) {
+ return $crumbs;
+ }
$type = is_singular() ? $obj->post_type : $obj->name;
$name = jvbNoBase($type);
- if (Features::forSite()->has('is_directory') && $name === 'directory') {
+ $registrar = Registrar::getInstance($name);
+
+ if(self::$hasDirectory && $registrar && $registrar->hasFeature('show_directory')) {
+
+ if (!empty(self::$directories[$name]??[])){
+ $crumbs[] = [
+ 'name' => self::$directories[$name]['title'],
+ 'url' => self::$directories[$name]['url']
+ ];
+ }
+ } elseif (Site::has('is_directory') && $name === 'directory') {
+
$crumbs[] = [
- 'name' => 'Directory',
+ 'name' => DirectoryManager::getInstance()->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)) {
+ } elseif ($registrar) {
$crumbs[] = [
- 'name' => JVB_CONTENT[$name]['breadcrumb'] ?? JVB_CONTENT[$name]['plural'],
- 'url' => get_post_type_archive_link($type)
+ 'name' => $registrar->getConfig('breadcrumbs')['title'] ?? $registrar->getPlural(),
+ 'url' => get_post_type_archive_link($type)
];
+ } else {
+
+ if (function_exists('is_shop') && is_shop()) {
+ $ID = get_option( 'woocommerce_shop_page_id' );
+ $crumbs[] = [
+ 'name' => get_the_title($ID),
+ 'url' => get_the_permalink($ID)
+ ];
+
+ } else {
+ $postTypeObject = get_post_type_object($type);
+ $crumbs[] = [
+ 'name' => $postTypeObject->label,
+ 'url' => get_post_type_archive_link($type)
+ ];
+ }
+
}
return $crumbs;
@@ -213,7 +263,7 @@
{
$url = get_term_link($term->term_id);
array_unshift($crumbs, [
- 'name' => $term->name,
+ 'name' => html_entity_decode($term->name),
'url' => $url,
'id' => $term->term_id,
]);
@@ -318,17 +368,19 @@
$items = [];
$position = 1;
+ global $wp;
+ $current = home_url( add_query_arg( $_GET, $wp->request ) );
foreach ($crumbs as $crumb) {
// Schema requires a URL
if ($crumb['url'] === false) {
- $crumb['url'] = get_permalink();
+ $crumb['url'] = $current;
}
$items[] = [
- '@type' => 'ListItem',
- 'position' => $position,
- 'name' => $crumb['name'],
- 'item' => $crumb['url'],
+ '@type' => 'ListItem',
+ '@id' => $crumb['url'],
+ 'position' => $position,
+ 'name' => $crumb['name'],
];
$position++;
@@ -336,46 +388,39 @@
return [
'@type' => 'BreadcrumbList',
- '@id' => get_permalink() . '/#breadcrumbs',
+ '@id' => $current . '/#breadcrumbs',
'itemListElement' => $items
];
}
- /**
- * Invalidate breadcrumb cache for specific object
- */
- public function invalidateCache(?int $objectId = null): void
- {
- if ($objectId) {
- $this->cache->delete($objectId);
- } else {
- $this->cache->clear();
- }
- }
- public function addTaxToCrumbs(array $crumbs, string $taxonomy):array
+ public function addTaxToCrumbs(array $crumbs, string|array $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)
- ];
+ $taxonomies = is_string($taxonomy) ? [$taxonomy] : $taxonomy;
+ foreach ($taxonomies as $tax) {
+ $taxonomy = jvbCheckBase($tax);
+ $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)
+ ];
}
- $crumbs[] = [
- 'name' => $term->name,
- 'url' => get_term_link($term, $taxonomy)
- ];
}
+
return $crumbs;
}
}
--
Gitblit v1.10.0