From 2739eae470f06eeea774af77f39e115d008b5f3d Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Fri, 11 Apr 2025 13:19:03 -0500 Subject: [PATCH 1/2] feat:: Add support for custom taxonomies in crumb trail --- .gitignore | 1 + src/Crumb.php | 199 ++++++++++++++++++++++++++------------------------ 2 files changed, 105 insertions(+), 95 deletions(-) diff --git a/.gitignore b/.gitignore index 61ead86..2e07007 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor +.vscode diff --git a/src/Crumb.php b/src/Crumb.php index f1f8a8c..555b9d5 100644 --- a/src/Crumb.php +++ b/src/Crumb.php @@ -21,12 +21,12 @@ class Crumb */ protected $breadcrumb = []; - /** - * Initialize the Crumb instance. - * - * @param array $config - * @return void - */ + /** + * Initialize the Crumb instance. + * + * @param array $config + * @return void + */ public function __construct(array $config) { $this->config = $config; @@ -82,34 +82,27 @@ public function build() home_url() ); - if ( - is_home() && - ! empty($this->config['blog']) - ) { - return $this->add( - $this->config['blog'] - ); + if (is_home() && !empty($this->config['blog'])) { + return $this->add($this->config['blog']); } if (is_page()) { - $ancestors = collect( - get_ancestors(get_the_ID(), 'page') - )->reverse(); + $ancestors = collect(get_ancestors(get_the_ID(), 'page'))->reverse(); if ($ancestors->isNotEmpty()) { - $ancestors->each(function ($item) { - $this->add( - get_the_title($item), - get_permalink($item), - $item - ); - }); + $ancestors->each(function ($item) { + $this->add( + get_the_title($item), + get_permalink($item), + $item + ); + }); } return $this->add( - get_the_title(), - null, - get_the_ID() + get_the_title(), + null, + get_the_ID() ); } @@ -117,10 +110,10 @@ public function build() $category = single_cat_title('', false); return $this->add( - $category, - null, - get_cat_ID($category), - true + $category, + null, + get_cat_ID($category), + true ); } @@ -128,121 +121,137 @@ public function build() $tag = single_tag_title('', false); return $this->add( - $tag, - null, - get_term_by('name', $tag, 'post_tag')->term_id, - true + $tag, + null, + get_term_by('name', $tag, 'post_tag')->term_id, + true ); } if (is_date()) { if (is_month()) { - return $this->add( - get_the_date('F Y'), - null, - null, - true - ); + return $this->add( + get_the_date('F Y'), + null, + null, + true + ); } if (is_year()) { - return $this->add( - get_the_date('Y'), - null, - null, - true - ); - } - return $this->add( - get_the_date(), + get_the_date('Y'), null, null, true ); + } + + return $this->add( + get_the_date(), + null, + null, + true + ); } if (is_tax()) { $term = single_term_title('', false); return $this->add( - $term, - null, - get_term_by('name', $term, get_query_var('taxonomy'))->term_id + $term, + null, + get_term_by('name', $term, get_query_var('taxonomy'))->term_id ); } if (is_search()) { return $this->add( - sprintf($this->config['search'], get_search_query()) + sprintf($this->config['search'], get_search_query()) ); } if (is_author()) { return $this->add( - sprintf($this->config['author'], get_the_author()), - null, - get_the_author_meta('ID'), - true + sprintf($this->config['author'], get_the_author()), + null, + get_the_author_meta('ID'), + true ); } if (is_post_type_archive()) { return $this->add( - post_type_archive_title('', false) + post_type_archive_title('', false) ); } if (is_404()) { return $this->add( - $this->config['not_found'] + $this->config['not_found'] ); } - if (is_singular('post')) { - $categories = get_the_category(get_the_ID()); - - if (! empty($categories)) { - foreach ($categories as $index => $category) { - $this->add( - $category->name, - get_category_link($category), - $category->term_id, - $index === 0 - ); - } + if (is_singular()) { + $postType = get_post_type(); + $postTypeObject = get_post_type_object($postType); - return $this->add( - get_the_title(), - null, - get_the_ID() + // Add post type archive link if it exists + if ($postTypeObject && !empty($postTypeObject->has_archive)) { + $this->add( + $postTypeObject->label, + get_post_type_archive_link($postType) ); } + // Add custom taxonomy terms + $taxonomies = get_object_taxonomies($postType, 'objects'); + foreach ($taxonomies as $taxonomy) { + if ($taxonomy->public) { + $terms = get_the_terms(get_the_ID(), $taxonomy->name); + if (!empty($terms) && !is_wp_error($terms)) { + if ($taxonomy->hierarchical) { + // Handle hierarchical taxonomies + $term = array_shift($terms); // Get the first term + $ancestors = get_ancestors($term->term_id, $taxonomy->name); + + // Add ancestor terms + foreach (array_reverse($ancestors) as $ancestorId) { + $ancestor = get_term($ancestorId, $taxonomy->name); + $this->add( + $ancestor->name, + get_term_link($ancestor, $taxonomy->name), + $ancestor->term_id + ); + } + + // Add the current term + $this->add( + $term->name, + get_term_link($term, $taxonomy->name), + $term->term_id + ); + } else { + // Handle non-hierarchical taxonomies + foreach ($terms as $term) { + $this->add( + $term->name, + get_term_link($term, $taxonomy->name), + $term->term_id + ); + } + } + } + } + } + + // Add the current post return $this->add( get_the_title(), null, - get_the_ID(), - true - ); - } - - $type = get_post_type_object( - get_post_type() - ); - - if (! empty($type)) { - $this->add( - $type->label, - get_post_type_archive_link($type->name), - get_queried_object_id() + get_the_ID() ); } - - return $this->add( - get_the_title(), - null, - get_the_ID() - ); + return $this->breadcrumb; } } From fa9d62fdd0f0b68b94ea56422bdc15c682d8188c Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Tue, 15 Apr 2025 08:45:40 -0500 Subject: [PATCH 2/2] fix: Correct lint errors --- src/Crumb.php | 111 +++++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/src/Crumb.php b/src/Crumb.php index 555b9d5..2a9c718 100644 --- a/src/Crumb.php +++ b/src/Crumb.php @@ -21,12 +21,12 @@ class Crumb */ protected $breadcrumb = []; - /** - * Initialize the Crumb instance. - * - * @param array $config - * @return void - */ + /** + * Initialize the Crumb instance. + * + * @param array $config + * @return void + */ public function __construct(array $config) { $this->config = $config; @@ -82,27 +82,34 @@ public function build() home_url() ); - if (is_home() && !empty($this->config['blog'])) { - return $this->add($this->config['blog']); + if ( + is_home() && + ! empty($this->config['blog']) + ) { + return $this->add( + $this->config['blog'] + ); } if (is_page()) { - $ancestors = collect(get_ancestors(get_the_ID(), 'page'))->reverse(); + $ancestors = collect( + get_ancestors(get_the_ID(), 'page') + )->reverse(); if ($ancestors->isNotEmpty()) { - $ancestors->each(function ($item) { - $this->add( - get_the_title($item), - get_permalink($item), - $item - ); - }); + $ancestors->each(function ($item) { + $this->add( + get_the_title($item), + get_permalink($item), + $item + ); + }); } return $this->add( - get_the_title(), - null, - get_the_ID() + get_the_title(), + null, + get_the_ID() ); } @@ -110,10 +117,10 @@ public function build() $category = single_cat_title('', false); return $this->add( - $category, - null, - get_cat_ID($category), - true + $category, + null, + get_cat_ID($category), + true ); } @@ -121,74 +128,74 @@ public function build() $tag = single_tag_title('', false); return $this->add( - $tag, - null, - get_term_by('name', $tag, 'post_tag')->term_id, - true + $tag, + null, + get_term_by('name', $tag, 'post_tag')->term_id, + true ); } if (is_date()) { if (is_month()) { - return $this->add( - get_the_date('F Y'), - null, - null, - true - ); + return $this->add( + get_the_date('F Y'), + null, + null, + true + ); } if (is_year()) { + return $this->add( + get_the_date('Y'), + null, + null, + true + ); + } + return $this->add( - get_the_date('Y'), + get_the_date(), null, null, true ); - } - - return $this->add( - get_the_date(), - null, - null, - true - ); } if (is_tax()) { $term = single_term_title('', false); return $this->add( - $term, - null, - get_term_by('name', $term, get_query_var('taxonomy'))->term_id + $term, + null, + get_term_by('name', $term, get_query_var('taxonomy'))->term_id ); } if (is_search()) { return $this->add( - sprintf($this->config['search'], get_search_query()) + sprintf($this->config['search'], get_search_query()) ); } if (is_author()) { return $this->add( - sprintf($this->config['author'], get_the_author()), - null, - get_the_author_meta('ID'), - true + sprintf($this->config['author'], get_the_author()), + null, + get_the_author_meta('ID'), + true ); } if (is_post_type_archive()) { return $this->add( - post_type_archive_title('', false) + post_type_archive_title('', false) ); } if (is_404()) { return $this->add( - $this->config['not_found'] + $this->config['not_found'] ); }