@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
114 lines (113 loc) • 4.55 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TermUtil = void 0;
const component_1 = require("../../decorators/component");
const components_1 = require("../components");
const term_1 = require("../term");
const query_util_1 = require("./query.util");
const taxonomy_util_1 = require("./taxonomy.util");
let TermUtil = class TermUtil {
components;
taxonomyUtil;
constructor(components, taxonomyUtil) {
this.components = components;
this.taxonomyUtil = taxonomyUtil;
}
async get(id, taxonomyName) {
if (taxonomyName) {
return await this.components.asyncGet(term_1.Term, [id, taxonomyName]);
}
else {
return await this.components.asyncGet(term_1.Term, [id]);
}
}
async toTerms(terms) {
const arr = [];
for (const term of terms) {
arr.push(await this.components.asyncGet(term_1.Term, [
term.term_id,
term.taxonomy,
term,
]));
}
return arr;
}
async getUniqueSlug(slug, term) {
const queryUtil = this.components.get(query_util_1.QueryUtil);
const taxonomyName = term.taxonomy?.name;
let parentId = term.props?.parent ?? 0;
if (!taxonomyName) {
throw new Error(`Invalid Term - ${taxonomyName}`);
}
const needsSuffix = ((await queryUtil.terms((query) => {
query.exists("slug", slug, taxonomyName);
})) ?? []).length > 0;
/*
* If the taxonomy supports hierarchy and the term has a parent, make the slug unique
* by incorporating parent slugs.
*/
let suffix = "", parentSuffix = "";
const maxLoop = 10;
let index = 0;
if (needsSuffix &&
(await this.taxonomyUtil.isHierarchical(taxonomyName)) &&
0 < parentId) {
for (; index < maxLoop; index++) {
const parentTerm = await queryUtil.terms((query) => {
query.exists("term_id", parentId, taxonomyName);
});
if (!parentTerm) {
break;
}
parentSuffix = `${parentSuffix}-${parentTerm[0].slug}`;
if (!(await queryUtil.terms((query) => {
query.exists("slug", `${slug}${parentSuffix}`, taxonomyName);
}))) {
break;
}
parentId = parentTerm[0].parent ?? 0;
if (0 >= parentId) {
break;
}
}
}
if (needsSuffix) {
suffix = `${parentSuffix}`;
for (index = 1; index < maxLoop + 1; index++) {
const terms = await queryUtil.terms((query) => {
query
.where("taxonomy", taxonomyName)
.where("slug", `${slug}${suffix}`);
if (term.props?.term_id) {
query.builder.not
.__ref(query)
.where("term_id", term.props?.term_id);
}
});
if (!terms) {
break;
}
suffix = `${parentSuffix}-${index + 1}`;
}
}
if (index >= maxLoop) {
return `${slug}-${Math.floor(Math.random() * (maxLoop + 999990010 - maxLoop + 1) + maxLoop + 1)}`;
}
return `${slug}${suffix}`;
}
};
exports.TermUtil = TermUtil;
exports.TermUtil = TermUtil = __decorate([
(0, component_1.component)(),
__metadata("design:paramtypes", [components_1.Components,
taxonomy_util_1.TaxonomyUtil])
], TermUtil);