UNPKG

@rnaga/wp-node

Version:

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

78 lines (77 loc) • 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = void 0; const config_1 = require("../config"); const components_1 = require("./components"); const current_1 = require("./current"); const options_1 = require("./options"); const roles_1 = require("./roles"); const utils_1 = require("./utils/utils"); const vars_1 = require("./vars"); const logger_1 = require("./logger"); class Context { args; config; hooks; current; roles; utils; vars; options; logger; #components; constructor(config, args) { this.args = args; this.#components = new components_1.Components(this.env); this.config = this.#components.get(config_1.Config, [config]); this.roles = this.#components.get(roles_1.Roles); this.current = this.#components.get(current_1.Current); this.utils = this.#components.get(utils_1.Utils); this.options = this.#components.get(options_1.Options); this.vars = this.#components.get(vars_1.Vars); this.logger = this.#components.get(logger_1.Logger); this.hooks = args.hooks; } get components() { return this.#components; } get env() { return this.args.env ?? "default"; } async clone() { return Context.getInstance(this.config.config, { env: this.env, hooks: this.hooks, }); } static async getInstance(config, args) { const { env, hooks, installing = false } = args; const context = new Context(config, { env, hooks }); context.vars.CONTEXT = context; context.hooks.action.do("core_init", context); try { if (config.multisite.enabled) { await context.current.switchSite(config.multisite.defaultSiteId, config.multisite.defaultBlogId); } else { await context.current.setUserRoles(); } // Set current user and role as anonymous await context.current.assumeUser(); } catch (e) { // Roles are defined in DB and an error gets thrown if it doesn't exit. // To be certain, set default roles. context.current.setDefaultUserRoles(); // If installoing is enabled, then ignore the error. // During installation, it's expected the error is thrown since the database is not yet setup. !installing && console.warn(`Failed to initialize the Current component.`, e); } finally { await context.current.setTimezone(); } return context; } } exports.Context = Context;