From 3d25ad7de4fdbb3eba2cde2a62422e6be5d79a0a Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Tue, 25 Aug 2026 22:57:22 +0000
Subject: [PATCH] =It's been a minute. I went through the Routes and associated Managers and ensured they played together well. Reworked the Cache.php, and I think I caught all the references. Moving away from JVBase() root class, to individuall classes that are Instanced. Double checked for the ABSPATH check in every file, and added index.php to every directory that didn't already have it. Also still working on the MenuBlock to work with WooCommerce for now for Northeh
---
inc/managers/SEO/BreadcrumbManager.php | 234 ++++++++++++++++++++++++++++++++++-----------------------
1 files changed, 139 insertions(+), 95 deletions(-)
diff --git a/inc/managers/SEO/BreadcrumbManager.php b/inc/managers/SEO/BreadcrumbManager.php
index 9fb5d6d..30bd448 100644
--- a/inc/managers/SEO/BreadcrumbManager.php
+++ b/inc/managers/SEO/BreadcrumbManager.php
@@ -2,7 +2,9 @@
namespace JVBase\managers\SEO;
use JVBase\managers\Cache;
-use JVBase\utility\Features;
+use JVBase\managers\DirectoryManager;
+use JVBase\registrar\Registrar;
+use JVBase\base\Site;
use WP_Post;
use WP_Term;
@@ -19,14 +21,17 @@
class BreadcrumbManager
{
private Cache $cache;
+ private Cache $termCache;
+ private Cache $postCache;
+ private Cache $userCache;
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();
- }
+ $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');
}
public static function getInstance(): self
@@ -48,24 +53,21 @@
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 = 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
+ };
+
+ return $cache->remember(
$key,
function() {
$crumbs = $this->buildCrumbs();
@@ -83,15 +85,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;
@@ -108,28 +113,44 @@
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(Site::has('is_directory') && $contentRegistrar && $contentRegistrar->hasFeature('show_directory')) {
+ $directory = DirectoryManager::getInstance();
+ if (!empty($directory->directories($content)??[])){
+ $crumbs[] = [
+ 'name' => $directory->directories($content)['title'],
+ 'url' =>$directory->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 ($registrar && $registrar->hasFeature('show_directory')) {
+ $directory = DirectoryManager::getInstance();
+ if ($directory && !empty($directory->directories($tax)??[])) {
+ $crumbs[] = [
+ 'name' => $directory->directories($tax)['title'],
+ 'url' =>$directory->directories($tax)['url']
+ ];
+ }
}
// Add term hierarchy
@@ -143,25 +164,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 (Site::has('is_directory') && 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']
];
}
@@ -171,9 +188,9 @@
];
}
} 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']);
+
+ if ($registrar && !empty($crumbConfig['addCrumb'])) {
+ $crumbs = $this->addTaxToCrumbs($crumbs, $crumbConfig['addCrumb']);
}
// Add post hierarchy
$crumbs = array_merge($crumbs, $this->buildPostHierarchy($post));
@@ -187,19 +204,51 @@
*/
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($registrar && $registrar->hasFeature('show_directory') && DirectoryManager::getInstance()) {
+ $directory = DirectoryManager::getInstance();
+
+ if ($directory && !empty($directory->directories($name)??[])){
+ $crumbs[] = [
+ 'name' => $directory->directories($name)['title'],
+ 'url' =>$directory->directories($name)['url']
+ ];
+ }
+ } elseif (Site::has('is_directory') && $name === 'directory') {
+
$crumbs[] = [
- 'name' => JVB()->directories()->referAs(true),
+ '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;
@@ -317,17 +366,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++;
@@ -335,46 +386,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->forget($objectId);
- } else {
- $this->cache->flush();
- }
- }
- 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' => html_entity_decode($term->name),
- 'url' => get_term_link($term, $taxonomy)
- ];
}
+
return $crumbs;
}
}
--
Gitblit v1.10.0