UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

286 lines (285 loc) • 12.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); 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 __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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.TermCrud = void 0; const config_1 = require("../config"); const components_1 = require("../core/components"); const post_util_1 = require("../core/utils/post.util"); const query_util_1 = require("../core/utils/query.util"); const taxonomy_util_1 = require("../core/utils/taxonomy.util"); const term_util_1 = require("../core/utils/term.util"); const component_1 = require("../decorators/component"); const transactions_1 = require("../transactions"); const val = __importStar(require("../validators")); const crud_1 = require("./crud"); const error_1 = require("./error"); let TermCrud = class TermCrud extends crud_1.Crud { config; constructor(components, config) { super(components); this.config = config; } async get(termId, options) { const { context = "view", taxonomyName } = options ?? {}; const { user } = await this.getUser(); const termUtil = this.components.get(term_util_1.TermUtil); const term = await termUtil.get(termId, taxonomyName); if (!term.props?.term_id || 0 >= term.props.term_id) { throw new Error("Term not found"); } if (context === "edit" && !(await user.can("edit_term", termId))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Sorry, you are not allowed to edit this term"); } let data = {}; if (context == "edit") { data = { ...term.props, children: await term.children(), metas: await term.meta.props(), }; } else { data = { term_id: term.props.term_id, count: term.props.count, description: term.props.description, name: term.props.name, slug: term.props.slug, parent: term.props.parent, taxonomy: term.taxonomyName, }; } return this.returnValue(data); } async update(termId, taxonomyName, data) { const { name, parent: parentId, slug, description } = data; if (!name && !parent && !slug) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Nothing to update"); } const term = (await this.get(termId, { context: "edit", taxonomyName })) .data; const termTrx = this.components.get(transactions_1.TermTrx); await termTrx.update(term.term_id, taxonomyName, { parentId, slug, name, description, }); return this.returnValue(true); } async canCreateTerm(taxonomyName) { const { user } = await this.getUser(); const taxonomyUtil = this.components.get(taxonomy_util_1.TaxonomyUtil); const taxonomy = await taxonomyUtil.get(taxonomyName); const isHierarchical = taxonomy.props?.hierarchical; if ((isHierarchical && !(await user.can(taxonomy.props?.capabilities?.["edit_terms"]))) || (!isHierarchical && !(await user.can(taxonomy.props?.capabilities?.["assign_terms"])))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Sorry, you are not allowed to create terms in this taxonomy"); } } async create(data) { const { name, taxonomyName, parent: parentId, slug, description } = data; await this.canCreateTerm(taxonomyName); const termTrx = this.components.get(transactions_1.TermTrx); return this.returnValue(await termTrx.insert(name, taxonomyName, { parentId, slug, description, })); } async syncObject(objectId, namesOrTermIds, taxonomyName, append = false) { await this.canCreateTerm(taxonomyName); const termTrx = this.components.get(transactions_1.TermTrx); return this.returnValue(await termTrx.syncObject(objectId, namesOrTermIds, taxonomyName, append)); } async delete(termId, taxonomyName) { const { user } = await this.getUser(); if (!(await user.can("delete_term", termId))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Sorry, you are not allowed to delete this term"); } const termTrx = this.components.get(transactions_1.TermTrx); return this.returnValue(await termTrx.remove(termId, taxonomyName)); } async taxonomies() { const { user } = await this.getUser(); const role = await user.role(); if (role.is("anonymous")) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted"); } const taxonomies = this.config.config.taxonomy.settings; const data = []; for (const entry of Object.entries(taxonomies)) { const [name, taxonomy] = entry; if (!taxonomy.showUi) { continue; } data.push({ name, hierarchical: taxonomy.hierarchical, capabilities: taxonomy.capabilities, }); } return this.returnValue(data); } async list(taxonomyName, args, options) { const { context = "view" } = options ?? {}; const queryUtil = this.components.get(query_util_1.QueryUtil); const termUtil = this.components.get(term_util_1.TermUtil); const taxonomyUtil = this.components.get(taxonomy_util_1.TaxonomyUtil); const parsedArgs = val.crud.termListParams.parse(args ?? {}); const { user } = await this.getUser(); const taxonomy = await taxonomyUtil.get(taxonomyName); if (context === "edit" && !(await user.can(taxonomy.props?.capabilities?.["edit_terms"]))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Sorry, you are not allowed to edit this term"); } if (parsedArgs.post) { const postUtil = this.components.get(post_util_1.PostUtil); const post = await postUtil.get(parsedArgs.post); if (!post.props?.ID || 0 >= post.props.ID) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Invalid post ID"); } if (taxonomy.props?.objectType !== post.props.post_type) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "post isn't associated with this taxonomy"); } if (!(await postUtil.isPubliclyViewable(post)) && !(await user.can("read_post", post.props.ID))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Sorry, you are not allowed to view terms for this post"); } } if (parsedArgs.orderby == "term_order" && !parsedArgs.post) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "term_order can only be used with post"); } const buildQuery = (query) => { const { column } = query.alias; const offset = (parsedArgs.page - 1) * parsedArgs.per_page; const limit = parsedArgs.per_page; query.where("taxonomy", taxonomyName); query.builder .offset(offset) .limit(limit) .groupBy(column("terms", "term_id")); if (parsedArgs.orderby) { query.builder.orderBy(parsedArgs.orderby == "description" ? column("term_taxonomy", parsedArgs.orderby) : parsedArgs.orderby == "term_order" ? column("term_relationships", parsedArgs.orderby) : column("terms", parsedArgs.orderby), parsedArgs.order); } if (Array.isArray(parsedArgs.include)) { query.whereIn("term_id", parsedArgs.include); } if (Array.isArray(parsedArgs.exclude)) { query.andWhereNot((query) => query.whereIn("term_id", parsedArgs.exclude)); } if (parsedArgs.slug) { query.where("slug", parsedArgs.slug); } if (parsedArgs.hide_empty) { query.where("count", 0, ">"); } if (parsedArgs.parent && taxonomy.props?.hierarchical) { query.where("parent", parsedArgs.parent); } if (parsedArgs.post) { query.withObjectIds([parsedArgs.post]); } if (parsedArgs.search) { query.andWhere((query) => { const searchColumns = ["slug", "name"]; for (const searchColumn of searchColumns) { parsedArgs.search && query.or.whereLike(searchColumn, parsedArgs.search); } }); } }; const terms = (await queryUtil.terms((query) => { buildQuery(query); })) ?? []; const counts = await queryUtil.terms((query) => { buildQuery(query); query.count("terms", "term_id"); }, val.query.resultCount); const data = []; for (const term of await termUtil.toTerms(terms)) { const props = term.props; if (!props) continue; if (context == "edit") { data.push({ ...props, metas: await term.meta.props(), }); } else { data.push({ term_id: props.term_id, count: props.count, description: props.description, name: props.name, slug: props.slug, parent: props.parent, taxonomy: taxonomyName, }); } } const pagination = this.pagination({ page: parsedArgs.page, limit: parsedArgs.per_page, count: counts?.count ?? 0, }); return this.returnValue(data, { pagination, context, }); } }; exports.TermCrud = TermCrud; exports.TermCrud = TermCrud = __decorate([ (0, component_1.component)(), __metadata("design:paramtypes", [components_1.Components, config_1.Config]) ], TermCrud);