Skip to content

Commit

Permalink
Merge pull request #52 from helsingborg-stad/feat/hierarchical-term-f…
Browse files Browse the repository at this point in the history
…iltering

feat: hierarchical filtering
  • Loading branch information
sebastianthulin authored Aug 28, 2024
2 parents b4dacc5 + ac332fa commit ab3df50
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
2 changes: 1 addition & 1 deletion source/php/Decorator/EndpointTaxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct()
public function decorate(string $endpoint, array $fields): string
{
$termsToShow = $fields['mod_osm_terms_to_show'];
// echo '<pre>' . print_r( $termsToShow, true ) . '</pre>';die;

if (empty($termsToShow)) {
return $endpoint;
}
Expand Down
47 changes: 31 additions & 16 deletions source/php/Helper/GetTaxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ModularityOpenStreetMap\Helper;

use ModularityOpenStreetMap\Helper\GetPlacePostType as GetPlacePostType;
use WP_Taxonomy;

class GetTaxonomies {
private $postTypesTaxonomies = [];
Expand All @@ -18,38 +19,52 @@ public function getAllTaxonomiesForAllPlacePostTypes(array $postTypes): array

foreach ($postTypes as $slug => $label) {
$this->postTypesTaxonomies[$slug] = $this->getTaxonomiesFromPostTypeArchive($slug);

}

return $this->postTypesTaxonomies;
}

public function getTaxonomiesFromPostTypeArchive(string $postType): array
{
$filterableItems = [];
$activeTaxonomiesForPostType = get_theme_mod('archive_' . $postType . '_taxonomies_to_display', []);

if (!is_array($activeTaxonomiesForPostType)) {
return [];
if (empty($activeTaxonomiesForPostType) || !is_array($activeTaxonomiesForPostType)) {
return $filterableItems;
}

$taxonomies = $this->getTaxonomiesFromSlug($activeTaxonomiesForPostType);
foreach ($activeTaxonomiesForPostType as $taxonomy) {
$taxonomyObject = get_taxonomy($taxonomy);
if (empty($taxonomyObject)) {
continue;
}

return $taxonomies;
$filterableItems[$taxonomyObject->name] = $taxonomyObject->label;
$filterableItems = array_merge($filterableItems, $this->addHierarchicalTerms($taxonomyObject->name));
}

return $filterableItems;
}

private function getTaxonomiesFromSlug(array $activeTaxonomiesForPostType): array
private function addHierarchicalTerms(string $taxonomy): array
{
$arr = [];
if (empty($activeTaxonomiesForPostType)) {
return $arr;
}
$hierarchicalTerms = [];

foreach ($activeTaxonomiesForPostType as $taxonomy) {
$taxonomyObject = get_taxonomy($taxonomy);
$arr[$taxonomyObject->name] = $taxonomyObject->label;
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => 0,
]);

if (empty($terms)) {
return $hierarchicalTerms;
}

return $arr;
foreach ($terms as $term) {
$hierarchicalTerms['_' . $taxonomy . '_' . $term->term_id] = $term->name . ' (' . $taxonomy . ')';
}

return $hierarchicalTerms;
}

public function getAllTermsFromPostTypeArchiveTaxonomies(string $postType)
Expand All @@ -68,11 +83,11 @@ public function getAllTermsFromPostTypeArchiveTaxonomies(string $postType)
return $terms;
}

public function getAllTermsFromTaxonomy(string $taxonomy, string $output = 'name'): array
public function getAllTermsFromTaxonomy(string $taxonomy, string $output = 'name', array|null $args = null): array
{
$filteredTerms = [];

$terms = get_terms([
$terms = get_terms($args ?? [
'taxonomy' => $taxonomy,
'hide_empty' => true,
]);
Expand Down
33 changes: 26 additions & 7 deletions source/php/Module/CreateFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ public function create(array $fields, string $filterByLang): array {

$filters = [];
foreach ($fields['mod_osm_filters'] as $filter) {
if (empty($filter['mod_osm_filter_taxonomy']) && is_string($filter['mod_osm_filter_taxonomy'])) {
if (empty($filter['mod_osm_filter_taxonomy']) || !is_string($filter['mod_osm_filter_taxonomy'])) {
continue;
}

$terms = $this->getTermsFromTaxonomy($filter['mod_osm_filter_taxonomy']);

// Used to get a more specific filter.
[$taxonomy, $termId] = $this->getHierarchicalTermFilter($filter['mod_osm_filter_taxonomy']);
$terms = $this->getTermsFromTaxonomy($taxonomy, $termId);
$label = !empty($filter['mod_osm_filter_text']) ?
$filter['mod_osm_filter_text'] :
$filterByLang . ' ' . $taxonomy;

if ($terms) {
$filters[] = [
'label' => $filter['mod_osm_filter_text'] ?? $filterByLang . $filter['mod_osm_filter_taxonomy'],
'taxonomy' => $filter['mod_osm_filter_taxonomy'],
'label' => $label,
'taxonomy' => $taxonomy,
'terms' => $terms
];
}
Expand All @@ -33,8 +38,22 @@ public function create(array $fields, string $filterByLang): array {
return $filters;
}

private function getTermsFromTaxonomy(string $taxonomy): array
private function getHierarchicalTermFilter(string $taxonomy): array {
if ($taxonomy[0] === '_') {
$taxonomy = explode('_', $taxonomy);
$termId = !empty($taxonomy[2]) ? $taxonomy[2] : "";
$taxonomy = !empty($taxonomy[1]) ? $taxonomy[1] : "";
}

return [$taxonomy, $termId ?? ""];
}

private function getTermsFromTaxonomy(string $taxonomy, $termId): array
{
return $this->getTaxonomiesInstance->getAllTermsFromTaxonomy($taxonomy);
return $this->getTaxonomiesInstance->getAllTermsFromTaxonomy($taxonomy, 'name', [
'taxonomy' => $taxonomy,
'parent' => $termId,
'hide_empty' => true
]);
}
}

0 comments on commit ab3df50

Please sign in to comment.