@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
123 lines (122 loc) • 5.35 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.SiteUtil = void 0;
const config_1 = require("../../config");
const component_1 = require("../../decorators/component");
const components_1 = require("../components");
const current_1 = require("../current");
const options_1 = require("../options");
const site_1 = require("../site");
const blog_util_1 = require("./blog.util");
const query_util_1 = require("./query.util");
let SiteUtil = class SiteUtil {
components;
config;
constructor(components, config) {
this.components = components;
this.config = config;
}
async get(siteRef) {
return await this.components.asyncGet(site_1.Site, [siteRef]);
}
async toSites(sites, blogId) {
const arr = [];
for (const site of sites) {
arr.push(await this.components.asyncGet(site_1.Site, [site.id, blogId]));
}
return arr;
}
// async getList() {
// const queryUtil = this.components.get(QueryUtil);
// const metaUtil = this.components.get(MetaUtil);
// const sites = (await queryUtil.sites((query) => query)) ?? [];
// const result: (types.Tables["site"] & {
// sitename: string;
// blogs: Awaited<ReturnType<InstanceType<typeof SiteUtil>["getBlogs"]>>;
// })[] = [];
// for (const site of sites) {
// result.push({
// ...site,
// sitename:
// (await metaUtil.getValue("site", site.id, "site_name")) ??
// site.domain,
// blogs: (await this.getBlogs(site.id)) ?? [],
// });
// }
// return result;
// }
async getBlogs(siteId) {
const queryUtil = this.components.get(query_util_1.QueryUtil);
const blogUtil = this.components.get(blog_util_1.BlogUtil);
const blogArr = (await queryUtil.blogs((query) => {
query.where("site_id", siteId);
})) ?? [];
const result = [];
for (const item of blogArr) {
const blog = await blogUtil.get(item.blog_id);
blog.props && result.push(blog);
}
return result;
}
async getReservedNames(siteId) {
const options = this.components.get(options_1.Options);
const current = this.components.get(current_1.Current);
const illigalNames = await options.get("illegal_names", {
siteId: siteId ?? current.siteId,
});
const defaultReservedNames = this.config.config.multisite.subdirectoryReservedNames;
return Array.from(new Set([
...(Array.isArray(illigalNames) ? illigalNames : []),
...defaultReservedNames,
]));
}
async getSiteOptions(name, defaultValue, siteId) {
const optionsCore = this.components.get(options_1.Options);
const current = this.components.get(current_1.Current);
return ((await optionsCore.get(name, {
siteId: siteId ?? current.siteId,
})) ?? defaultValue);
}
// is_email_address_unsafe
async isEmailUnsafe(email, options) {
const { siteId } = options ?? {};
let isEmailAddressUnsafe = false;
const bannedNames = await this.getSiteOptions("banned_email_domains", [], siteId);
if (email.includes("@")) {
const normalizedEmail = email.toLowerCase();
const emailDomain = normalizedEmail.split("@")[1];
const bannedDomainsLower = !Array.isArray(bannedNames)
? []
: bannedNames.map((name) => name.toLowerCase());
isEmailAddressUnsafe = bannedDomainsLower.some((bannedDomain) => emailDomain === bannedDomain ||
normalizedEmail.endsWith(`.${bannedDomain}`));
}
return isEmailAddressUnsafe;
}
async isLimitedEmailDomains(email, options) {
const { siteId } = options ?? {};
let limitedEmailDomains = await this.getSiteOptions("limited_email_domains", [], siteId);
if (Array.isArray(limitedEmailDomains) && limitedEmailDomains.length > 0) {
limitedEmailDomains = limitedEmailDomains.map((domain) => domain.toLowerCase());
const emailDomain = email.substring(email.indexOf("@") + 1).toLowerCase();
if (!limitedEmailDomains.includes(emailDomain)) {
return false;
}
}
return true;
}
};
exports.SiteUtil = SiteUtil;
exports.SiteUtil = SiteUtil = __decorate([
(0, component_1.component)(),
__metadata("design:paramtypes", [components_1.Components, config_1.Config])
], SiteUtil);