| 1 |
import { useSelect } from "@wordpress/data"; |
| 2 |
import { store as coreStore } from "@wordpress/core-data"; |
| 3 |
import { useMemo } from "@wordpress/element"; |
| 4 |
|
| 5 |
export const useQueryTerms = (attributes) => { |
| 6 |
const { |
| 7 |
postType, |
| 8 |
SelectTerms, |
| 9 |
taxonomyType, |
| 10 |
emptyCategory, |
| 11 |
allTaxonomyTerm, |
| 12 |
excludeTerms, |
| 13 |
limit = -1, |
| 14 |
} = attributes; |
| 15 |
const allTaxonomyTerms = useSelect( |
| 16 |
(select) => { |
| 17 |
const { getTaxonomies, getEntityRecords } = select(coreStore); |
| 18 |
const taxonomies = getTaxonomies(); |
| 19 |
if (!Array.isArray(taxonomies)) return []; |
| 20 |
// Filter taxonomies by postType |
| 21 |
const filteredTaxonomies = taxonomies.filter( |
| 22 |
(tax) => Array.isArray(tax.types) && tax.types.includes(postType) |
| 23 |
); |
| 24 |
// Collect terms from each taxonomy and attach taxonomy slug |
| 25 |
return filteredTaxonomies.flatMap((tax) => { |
| 26 |
const terms = getEntityRecords("taxonomy", tax.slug, { |
| 27 |
per_page: -1, |
| 28 |
}); |
| 29 |
if (!Array.isArray(terms)) return []; |
| 30 |
const termsWithTaxonomy = terms.map((term) => ({ |
| 31 |
...term, |
| 32 |
taxonomy: tax.slug, |
| 33 |
})); |
| 34 |
// If emptyCategory is false, filter terms with count > 0 |
| 35 |
return emptyCategory === false ? termsWithTaxonomy.filter((term) => term.count > 0) : termsWithTaxonomy; |
| 36 |
}); |
| 37 |
}, |
| 38 |
[postType, emptyCategory] |
| 39 |
); |
| 40 |
// Defensive fallback if no terms loaded yet |
| 41 |
let filteredTerms = Array.isArray(allTaxonomyTerms) ? allTaxonomyTerms : []; |
| 42 |
// Filter by taxonomyType if specified |
| 43 |
if (taxonomyType) { |
| 44 |
filteredTerms = filteredTerms.filter((term) => term.taxonomy === taxonomyType); |
| 45 |
} |
| 46 |
// Filter by selected terms when not using all terms |
| 47 |
if (!allTaxonomyTerm && Array.isArray(SelectTerms)) { |
| 48 |
const selectedIds = SelectTerms.map((item) => (typeof item === "object" ? item.value : item)); |
| 49 |
|
| 50 |
// Create a map for O(1) lookups |
| 51 |
const termMap = new Map(); |
| 52 |
filteredTerms.forEach((term) => termMap.set(term.id, term)); |
| 53 |
|
| 54 |
// Reorder terms according to SelectTerms sequence |
| 55 |
filteredTerms = SelectTerms.map((item) => { |
| 56 |
const id = typeof item === "object" ? item.value : item; |
| 57 |
return termMap.get(id); |
| 58 |
}).filter((term) => term !== undefined); // Remove any undefined terms |
| 59 |
} |
| 60 |
// Exclude terms if using all terms and excludeTerms provided |
| 61 |
if (allTaxonomyTerm && Array.isArray(excludeTerms)) { |
| 62 |
const excludeIds = excludeTerms.map((item) => (typeof item === "object" ? item.value : item)); |
| 63 |
filteredTerms = filteredTerms.filter((term) => !excludeIds.includes(term.id)); |
| 64 |
} |
| 65 |
// Apply limit if greater than 0 and using allTaxonomyTerm |
| 66 |
if (limit > 0 && allTaxonomyTerm) { |
| 67 |
filteredTerms = filteredTerms.slice(0, limit); |
| 68 |
} |
| 69 |
return filteredTerms; |
| 70 |
}; |
| 71 |
|
| 72 |
export const usePostTaxonomies = (attributes) => { |
| 73 |
const { postType } = attributes; |
| 74 |
|
| 75 |
const allTaxonomies = useSelect((select) => select(coreStore).getTaxonomies(), []); |
| 76 |
|
| 77 |
return useMemo(() => { |
| 78 |
if (!allTaxonomies) return []; |
| 79 |
return allTaxonomies |
| 80 |
.filter((tax) => tax.types.includes(postType)) |
| 81 |
.map((tax) => ({ |
| 82 |
label: tax.name, |
| 83 |
value: tax.slug, |
| 84 |
})); |
| 85 |
}, [postType, allTaxonomies]); |
| 86 |
}; |
| 87 |
|