@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
230 lines (229 loc) • 8.63 kB
JavaScript
"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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Current = void 0;
const config_1 = require("../config");
const component_1 = require("../decorators/component");
const defaults = __importStar(require("../defaults"));
const val = __importStar(require("../validators"));
const components_1 = require("./components");
const logger_1 = require("./logger");
const options_1 = require("./options");
const post_1 = require("./post");
const site_1 = require("./site");
const tables_1 = require("./tables");
const user_1 = require("./user");
const query_util_1 = require("./utils/query.util");
const roles_util_1 = require("./utils/roles.util");
const vars_1 = require("./vars");
const moment_timezone_1 = __importDefault(require("moment-timezone"));
let Current = class Current {
config;
logger;
components;
vars;
#props;
#previous = [];
constructor(config, logger, components, vars) {
this.config = config;
this.logger = logger;
this.components = components;
this.vars = vars;
this.#props = {
tables: this.components.get(tables_1.Tables),
};
}
get blogId() {
if (this.config.isMultiSite()) {
return this.#props.multi?.site.props.blog.blog_id ?? 0;
}
return 0;
}
get siteId() {
if (this.config.isMultiSite()) {
return this.#props.multi?.site.props.site.id ?? 0;
}
return 0;
}
get post() {
return this.#props.post;
}
get user() {
return this.#props.user;
}
get site() {
return this.#props.multi?.site;
}
get role() {
return this.#props.role;
}
get tables() {
return this.#props.tables;
}
async setPost(id) {
this.#props.post = await this.components.asyncGet(post_1.Post, [id]);
}
async assumeUser(userRefOrUser) {
if (!userRefOrUser ||
typeof userRefOrUser == "string" ||
typeof userRefOrUser == "number") {
this.#props.user = await this.components.asyncGet(user_1.User, [userRefOrUser]);
this.#props.role = await this.#props.user.role();
}
else {
this.#props.user = userRefOrUser;
this.#props.role = await userRefOrUser.role();
}
}
async setTimezone() {
let offsetMinutes = 0;
const options = this.components.get(options_1.Options);
const timezoneString = await options.get("timezone_string");
if (timezoneString && timezoneString?.length > 0) {
this.vars.TZ_IDENTIFIER = timezoneString;
try {
// utcOffset - Setting the UTC offset by supplying minutes
offsetMinutes = moment_timezone_1.default.tz(this.vars.TZ_IDENTIFIER).utcOffset();
}
catch (e) {
this.logger.warn(`${e}`, { error: e });
}
}
const gmtOffset = await options.get("gmt_offset");
if (gmtOffset && parseInt(gmtOffset) !== 0) {
offsetMinutes = parseInt(gmtOffset) * 60;
}
this.vars.TIME_OFFSET_MINUTES = offsetMinutes;
const offsetHours = Math.floor(offsetMinutes / 60);
if (!timezoneString) {
this.vars.TZ_IDENTIFIER = `Etc/GMT${offsetHours == 0
? ""
: offsetHours > 0
? "+" + offsetHours
: offsetHours}`;
}
}
setDefaultUserRoles() {
this.vars.USER_ROLES = defaults.roles;
}
async setUserRoles() {
const rolesUtil = this.components.get(roles_util_1.RolesUtil);
const roles = await rolesUtil.get(this.blogId);
// Save user roles to global vars
this.vars.USER_ROLES = roles;
}
async restorePrevious() {
const previous = this.#previous.pop();
if (!previous)
return;
await this.switchSite(previous.siteId, previous.blogId);
if (previous.userId > 0) {
await this.assumeUser(previous.userId);
}
}
async switchBlog(blogRef) {
const previous = {
blogId: this.blogId,
siteId: this.site?.props.site.id ?? this.config.config.multisite.defaultSiteId,
userId: this.user?.props?.ID ?? 0,
};
await this.#props.multi?.site.setBlog(blogRef);
if (this.blogId) {
// Update index at conext scope
this.vars.TABLES_MS_CURRENT_INDEX = this.blogId;
this.#props.tables.index = this.blogId;
}
await this.setUserRoles();
this.#previous.push(previous);
}
async switchSite(siteRef, blogRef) {
if (!this.config.config.multisite.enabled) {
this.logger.info(`Multi site is not enabled`);
return;
}
const previous = {
blogId: this.blogId,
siteId: this.site?.props.site.id ?? this.config.config.multisite.defaultSiteId,
userId: this.user?.props?.ID ?? 0,
};
if (!blogRef) {
// Set default blog id
const queryUtil = this.components.get(query_util_1.QueryUtil);
const siteMeta = await queryUtil.sites((query) => {
query.withMeta().where("meta_key", "main_site").get(siteRef);
}, val.database.wpSiteMeta);
if (!siteMeta || siteMeta.meta_value == null) {
blogRef = 1;
}
else {
blogRef = parseInt(siteMeta.meta_value);
}
}
const site = await this.components.asyncGet(site_1.Site, [siteRef, blogRef]);
if (!site) {
throw new Error(`Site not found: ${siteRef} ${blogRef}`);
}
// Reset user and role
this.#props.role = undefined;
this.#props.user = undefined;
// Reset post
this.#props.post = undefined;
this.#props.multi = { site };
// Switch blog
await this.switchBlog(site.props.blog.blog_id);
this.#previous.push(previous);
return this;
}
};
exports.Current = Current;
exports.Current = Current = __decorate([
(0, component_1.component)(),
__metadata("design:paramtypes", [config_1.Config,
logger_1.Logger,
components_1.Components,
vars_1.Vars])
], Current);