Skip to content

Commit

Permalink
enhance(crumb): Add post/term IDs to breadcrumb items where applicabl…
Browse files Browse the repository at this point in the history
…e (Supersedes #6) (#11)

* chore(deps): Bump dev-dependencies
* fix(docs): Fix build status badge
* chore(ci): Test against 8.1 and 8.2
* chore(ci): Drop test against 7.3
  • Loading branch information
Log1x authored Jul 29, 2023
2 parents 2c949ff + ece2aaa commit b67320a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
php: ["7.3", "7.4", "8.0"]
php: ['7.4', '8.0', '8.1', '8.2']

steps:
- name: Checkout the project
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Crumb

![Latest Stable Version](https://img.shields.io/packagist/v/log1x/crumb?style=flat-square)
![Build Status](https://img.shields.io/github/workflow/status/log1x/crumb/Main)
![Build Status](https://img.shields.io/github/actions/workflow/status/log1x/crumb/main.yml?branch=master&style=flat-square)
![Total Downloads](https://img.shields.io/packagist/dt/log1x/crumb?style=flat-square)

A simple breadcrumb package for Sage 10.
Expand Down
15 changes: 8 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 37 additions & 9 deletions src/Crumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public function __construct(array $config)
*
* @param string $key
* @param string $value
* @param int $id
* @param bool $blog
* @return $this
*/
protected function add($key, $value = null, $blog = false)
protected function add($key, $value = null, $id = null, $blog = false)
{
if (
($value === true && empty($blog) || $blog === true) &&
Expand All @@ -49,6 +50,7 @@ protected function add($key, $value = null, $blog = false)
}

$this->breadcrumb->push([
'id' => $id,
'label' => $key,
'url' => $value,
]);
Expand Down Expand Up @@ -90,26 +92,37 @@ public function build()
$ancestors->each(function ($item) {
$this->add(
get_the_title($item),
get_permalink($item)
get_permalink($item),
$item->ID
);
});
}

return $this->add(
get_the_title()
get_the_title(),
null,
get_the_ID()
);
}

if (is_category()) {
$category = single_cat_title('', false);

return $this->add(
single_cat_title('', false),
$category,
null,
get_cat_ID($category),
true
);
}

if (is_tag()) {
$tag = single_tag_title('', false);

return $this->add(
single_tag_title('', false),
$tag,
null,
get_term_by('title', $tag, 'post_tag')->term_id,
true
);
}
Expand All @@ -136,8 +149,12 @@ public function build()
}

if (is_tax()) {
$term = single_term_title('', false);

return $this->add(
single_term_title('', false)
$term,
null,
get_term_by('title', $term, get_query_var('taxonomy'))->term_id
);
}

Expand All @@ -150,6 +167,8 @@ public function build()
if (is_author()) {
return $this->add(
sprintf($this->config['author'], get_the_author()),
null,
get_the_author_meta('ID'),
true
);
}
Expand All @@ -171,19 +190,25 @@ public function build()

if (! empty($categories)) {
$category = array_shift($categories);

$this->add(
$category->name,
get_category_link($category),
$category->term_id,
true
);

return $this->add(
get_the_title()
get_the_title(),
null,
get_the_ID()
);
}

return $this->add(
get_the_title(),
null,
get_the_ID(),
true
);
}
Expand All @@ -195,12 +220,15 @@ public function build()
if (! empty($type)) {
$this->add(
$type->label,
get_post_type_archive_link($type->name)
get_post_type_archive_link($type->name),
$type->ID
);
}

return $this->add(
get_the_title()
get_the_title(),
null,
get_the_ID()
);
}
}

0 comments on commit b67320a

Please sign in to comment.