diff --git a/includes/utilities.php b/includes/utilities.php index eeb1ffc..95c99b7 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -1,5 +1,66 @@ parent == 0; + }); + return $parentTerms; +} + +function get_the_children_terms($taxonomy, $postID) +{ + if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null; + + $terms = get_the_terms($postID, $taxonomy); + if (!$terms || empty($terms)) return []; + + $childrenTerms = array_filter($terms, function ($term) { + return $term->parent !== 0; + }); + return $childrenTerms; +} + +function get_children_terms_from_specific_parent($taxonomy, $postID, $parentTerm) +{ + if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null; + + $childrenTerms = get_the_children_terms($taxonomy, $postID); + $filteredChildrenTerms = array_filter($childrenTerms, function ($term) use ($parentTerm) { + return $term->parent === $parentTerm->term_id; + }); + return $filteredChildrenTerms; +} + +function get_the_terms_organised_by_parent($taxonomy, $postID = null) +{ + if (!$postID) return; + + $terms = get_the_terms($postID, $taxonomy); + + $sortedTermsByParent = []; + if (!$terms) return $sortedTermsByParent; + + $parentTerms = get_the_parent_terms($taxonomy, $postID); + + foreach ($parentTerms as $parentTerm) { + $sortedTermsByParent[] = $parentTerm; + + $childrenTerms = get_children_terms_from_specific_parent($taxonomy, $postID, $parentTerm); + + foreach ($childrenTerms as $childTerm) { + $sortedTermsByParent[] = $childTerm; + } + } + + return $sortedTermsByParent; +} + function getAllBatimentsTermsByParents() { // Récupérer tous les termes de la taxonomie 'elementsbatiments' avec une hiérarchie