From e87521f92377cea8d20e48edb31595b491f65e65 Mon Sep 17 00:00:00 2001 From: Nonimart Date: Fri, 13 Jun 2025 12:49:22 +0200 Subject: [PATCH] =?UTF-8?q?FEATURE=20Introduction=20de=20plusieurs=20fonct?= =?UTF-8?q?ions=20pour=20r=C3=A9cup=C3=A9rer=20et=20organiser=20les=20term?= =?UTF-8?q?es=20parents/enfants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/utilities.php | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) 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