| 1 |
import apiFetch from "@wordpress/api-fetch"; |
| 2 |
|
| 3 |
const nonce = () => betterdocs_admin.nonce; |
| 4 |
|
| 5 |
function withNonce() { |
| 6 |
apiFetch.use(apiFetch.createNonceMiddleware(nonce())); |
| 7 |
} |
| 8 |
|
| 9 |
// `meta` carries the raw icon/cover attachment ids (registered writable term meta); |
| 10 |
// `thumbnail`/`handbookthumbnail` are read-only resolved URLs for display; |
| 11 |
// `knowledge_base`(slugs)/`knowledge_base_ids` are the Pro KB assignment. |
| 12 |
const CAT_FIELDS = "id,name,slug,description,parent,count,meta,doc_category_order,thumbnail,handbookthumbnail,knowledge_base,knowledge_base_ids,knowledge_base_titles,lang"; |
| 13 |
|
| 14 |
// On multilingual sites, fetch every language's terms in one pass so the admin |
| 15 |
// can group/filter by language client-side (WPML/Polylang otherwise scope terms |
| 16 |
// to the current/default language). |
| 17 |
const allLangParam = () => |
| 18 |
(typeof betterdocs_admin !== "undefined" && betterdocs_admin.is_multilingual) |
| 19 |
? "&bd_all_languages=1" |
| 20 |
: ""; |
| 21 |
|
| 22 |
function fetchCategoryPage(page, per_page) { |
| 23 |
return apiFetch({ |
| 24 |
path: `wp/v2/doc_category?per_page=${per_page}&page=${page}&orderby=id&order=asc&_fields=${CAT_FIELDS}${allLangParam()}`, |
| 25 |
method: "GET", |
| 26 |
parse: false, |
| 27 |
}).then((res) => |
| 28 |
res.json().then((data) => ({ |
| 29 |
data, |
| 30 |
total: parseInt(res.headers.get("X-WP-Total") || "0", 10), |
| 31 |
totalPages: parseInt(res.headers.get("X-WP-TotalPages") || "1", 10), |
| 32 |
})), |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
// Fetch every category, not just the first page. The tree needs the complete set |
| 37 |
// so newly created / empty categories aren't dropped when there are >100 terms. |
| 38 |
export function getDocCategories({ per_page = 100 } = {}) { |
| 39 |
withNonce(); |
| 40 |
return fetchCategoryPage(1, per_page).then((first) => { |
| 41 |
if (first.totalPages <= 1) return first; |
| 42 |
const rest = []; |
| 43 |
for (let p = 2; p <= first.totalPages; p++) rest.push(fetchCategoryPage(p, per_page)); |
| 44 |
return Promise.all(rest).then((pages) => ({ |
| 45 |
data: pages.reduce((acc, pg) => acc.concat(pg.data), first.data), |
| 46 |
total: first.total, |
| 47 |
totalPages: first.totalPages, |
| 48 |
})); |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
export function createDocCategory(payload) { |
| 53 |
withNonce(); |
| 54 |
return apiFetch({ |
| 55 |
path: "wp/v2/doc_category", |
| 56 |
method: "POST", |
| 57 |
data: payload, |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
export function updateDocCategory(id, payload) { |
| 62 |
withNonce(); |
| 63 |
return apiFetch({ |
| 64 |
path: `wp/v2/doc_category/${id}`, |
| 65 |
method: "POST", |
| 66 |
data: payload, |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
export function deleteDocCategory(id, { force = false } = {}) { |
| 71 |
withNonce(); |
| 72 |
return apiFetch({ |
| 73 |
path: `wp/v2/doc_category/${id}?force=${force}`, |
| 74 |
method: "DELETE", |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
// ── Term translation (WPML/Polylang) — shared across all three taxonomy admins ── |
| 79 |
|
| 80 |
// Fetch a term's translation group + "this is a translation of" candidates. |
| 81 |
export function getTermTranslations(params) { |
| 82 |
withNonce(); |
| 83 |
const qs = new URLSearchParams(params).toString(); |
| 84 |
return apiFetch({ path: `betterdocs/v1/term-translations?${qs}` }); |
| 85 |
} |
| 86 |
|
| 87 |
// Authoritative write: set a term's language + link it into a translation group. |
| 88 |
export function setTermLanguage(data) { |
| 89 |
withNonce(); |
| 90 |
return apiFetch({ path: "betterdocs/v1/term-language", method: "POST", data }); |
| 91 |
} |
| 92 |
|