Jake Vanderwerf
2026-02-04 2127b1bdd73ecd2423e443992da4b442f5a3c1a3
inc/managers/SEO/BreadcrumbManager.php
@@ -1,7 +1,7 @@
<?php
namespace JVBase\managers\SEO;
use JVBase\managers\CacheManager;
use JVBase\managers\Cache;
use JVBase\utility\Features;
use WP_Post;
use WP_Term;
@@ -18,12 +18,15 @@
 */
class BreadcrumbManager
{
   private CacheManager $cache;
   private Cache $cache;
   private static ?self $instance = null;
   private function __construct()
   {
      $this->cache = CacheManager::for('breadcrumbs', MONTH_IN_SECONDS)->connectTo('all');
      $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
@@ -45,17 +48,30 @@
         return [];
      }
      $key = get_queried_object_id() ?: 'home';
      $crumbs = $this->cache->get($key);
      if ($crumbs !== false) {
         return $crumbs;
      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->buildCrumbs();
      $this->cache->set($key, $crumbs);
      return $crumbs;
      return $this->cache->remember(
         $key,
         function() {
            $crumbs = $this->buildCrumbs();
            return apply_filters('jvbBreadcrumbs',$crumbs);
         }
      );
   }
   /**
@@ -73,12 +89,12 @@
      ];
      $obj = get_queried_object();
      if (is_tax()) {
         $crumbs = $this->addTaxonomyCrumbs($crumbs, $obj);
      } elseif (is_singular()) {
         $crumbs = $this->addArchiveCrumbs($crumbs, $obj);
         $crumbs = $this->addSingularCrumbs($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);
      }
@@ -109,7 +125,7 @@
      // Add directory if exists
      if (Features::forTaxonomy($tax)->has('directory')) {
         $directory = jvbDirectories($tax);
         $directory = JVB()->directories()?->directories($tax);
         $crumbs[] = [
            'name' => $directory['title'],
            'url'  => $directory['url']
@@ -117,9 +133,7 @@
      }
      // Add term hierarchy
      $crumbs = array_merge($crumbs, $this->buildTermHierarchy($term));
      return $crumbs;
      return array_merge($crumbs, $this->buildTermHierarchy($term));
   }
   /**
@@ -128,23 +142,26 @@
   private function addSingularCrumbs(array $crumbs, WP_Post $post): array
   {
      // Add directory if exists
      $directory = jvbDirectories(jvbNoBase($post->post_type));
      if (!empty($directory)) {
         $crumbs[] = [
            'name' => $directory['title'],
            'url'  => $directory['url']
         ];
      $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 (jvbIsDirectory()) {
      if (JVB()->directories()->isDirectory()) {
         $pos = jvbGetDirectoryInfo();
         if (!empty($pos)) {
            // Special case for map
            if ($pos['title'] == 'Map') {
               $crumbs[] = [
                  'name' => 'Tattoo Shops',
                  'url'  => jvbDirectories(BASE.'shop')['url']
                  'url'  => JVB()->directories()?->directories(BASE.'shop')['url']
               ];
            }
@@ -154,6 +171,10 @@
            ];
         }
      } else {
         $name = jvbNoBase($post->post_type);
         if (Features::forContent($name)->has('addCrumb')) {
            $this->addTaxToCrumbs($crumbs, JVB_CONTENT[$name]['addCrumb']);
         }
         // Add post hierarchy
         $crumbs = array_merge($crumbs, $this->buildPostHierarchy($post));
      }
@@ -166,12 +187,18 @@
    */
   private function addArchiveCrumbs(array $crumbs, object $obj): array
   {
      $type = is_singular() ? $obj->post_type : $obj -> name;
      $type = is_singular() ? $obj->post_type : $obj->name;
      $name = jvbNoBase($type);
      if (array_key_exists($name, JVB_CONTENT)) {
      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),
            'url'  => get_post_type_archive_link($type)
         ];
      }
@@ -185,7 +212,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,
      ]);
@@ -241,6 +268,7 @@
      $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>';
@@ -253,8 +281,7 @@
         // Add link if URL exists and not current page
         if ($crumb['url'] !== false) {
            $isCurrent = isset($crumb['id']) && $crumb['id'] === get_queried_object_id();
            if (!$isCurrent) {
            if ($total !== $position) {
               $aOpen = '<a itemprop="item" href="' . esc_url($crumb['url']) . '" title="' . esc_attr($crumb['name']) . '">';
               $aClose = '</a>';
            }
@@ -319,9 +346,35 @@
   public function invalidateCache(?int $objectId = null): void
   {
      if ($objectId) {
         $this->cache->delete($objectId);
         $this->cache->forget($objectId);
      } else {
         $this->cache->clear();
         $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;
   }
}