UNPKG

@rnaga/wp-node

Version:

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

154 lines (153 loc) • 6.31 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.SettingsCrud = void 0; const zod_1 = require("zod"); const config_1 = require("../config"); const components_1 = require("../core/components"); const query_util_1 = require("../core/utils/query.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"); const vars_1 = require("../core/vars"); let SettingsCrud = class SettingsCrud extends crud_1.Crud { config; constructor(components, config) { super(components); this.config = config; } // These records are visible to public. // Use options.crud for private settings. #mapping = { title: "blogname", description: "blogdescription", url: "siteurl", home: "home", email: "admin_email", timezone: "timezone_string", date_format: "date_format", time_format: "time_format", start_of_week: "start_of_week", use_smilies: "use_smilies", default_category: "default_category", default_post_format: "default_post_format", posts_per_page: "posts_per_page", show_on_front: "show_on_front", page_on_front: "page_on_front", page_for_posts: "page_on_front", default_ping_status: "default_ping_status", default_comment_status: "default_comment_status", site_icon: "site_icon", }; optionNames() { return Object.entries(this.#mapping).map((v) => v[1]); } get reversedMapping() { return Object.entries(this.#mapping) .map((v) => ({ [v[1]]: v[0] })) .reduce((obj, item) => ({ ...obj, ...item }), {}); } async checkPermission() { const { user } = await this.getUser(); if (!(await user.can("manage_options"))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted"); } } async get(options) { const { blogId } = options ?? {}; const { user } = await this.getUser(); const vars = this.components.get(vars_1.Vars); try { if (blogId) { await this.switchBlog({ blogId }); } const role = await user.role(); if (role.is("anonymous")) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted"); } const queryUtil = this.components.get(query_util_1.QueryUtil); const optionsCore = (await queryUtil.options((query) => { query.whereIn(this.optionNames()); }, zod_1.z.array(val.query.optionsResult))) ?? []; const data = optionsCore .map((option) => ({ [this.reversedMapping[option.option_name]]: option.option_value, })) .reduce((obj, item) => ({ ...obj, ...item }), {}); data.timezone = vars.TZ_IDENTIFIER; data.time_offset_minutes = vars.TIME_OFFSET_MINUTES; return this.returnValue(data); } finally { await this.restoreBlog(); } } async update(input, options) { const { blogId } = options ?? {}; try { if (blogId) { await this.switchBlog({ blogId }); } await this.checkPermission(); const data = val.crud.settings.parse(input); const optionsTrx = this.components.get(transactions_1.OptionsTrx); for (const [key, value] of Object.entries(data)) { await optionsTrx.insert(this.#mapping[key], value, { upsert: true, }); } return this.returnValue(true); } finally { await this.restoreBlog(); } } }; exports.SettingsCrud = SettingsCrud; exports.SettingsCrud = SettingsCrud = __decorate([ (0, component_1.component)(), __metadata("design:paramtypes", [components_1.Components, config_1.Config]) ], SettingsCrud);