@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
149 lines (148 loc) • 5.93 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.LinkUtil = 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 vars_1 = require("../vars");
const post_util_1 = require("./post.util");
let LinkUtil = class LinkUtil {
components;
postUtil;
vars;
config;
constructor(components, postUtil, vars, config) {
this.components = components;
this.postUtil = postUtil;
this.vars = vars;
this.config = config;
}
// get_permalink
async getPermalink(post) {
if (!post.props?.ID) {
return this.getHomeUrl();
}
if ("page" === post.props.post_type) {
return await this.getPageLink(post);
}
if ("attachment" === post.props.post_type) {
return await this.getAttachmentLink(post);
}
return this.getHomeUrl({
path: `?p=${post.props.ID}`,
});
}
// get_attachment_link
async getAttachmentLink(post) {
return !post
? await this.getHomeUrl()
: await this.getHomeUrl({ path: `/?attachment_id=${post.props?.ID}` });
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getPageLink(post, leavename = false, sample = false) {
if (!post || !post.props?.ID) {
return await this.getHomeUrl();
}
const options = this.components.get(options_1.Options);
if ("page" === (await options.get("show_on_from")) &&
(await options.get("page_on_from")) === post.props.ID) {
return await this.getHomeUrl();
}
return await this.getHomeUrl({ path: `?page_id=${post.props.ID}` });
}
forcePlainPostPermalink(post) {
const postUtil = this.components.get(post_util_1.PostUtil);
const current = this.components.get(current_1.Current);
const sample = post.filter == "sample";
if (!post.props?.ID && post.props && 0 >= post.props.ID) {
return true;
}
const statusObject = postUtil.getStatusObject(post.props?.post_status);
const typeObject = postUtil.getTypeObject(post.props?.post_type);
if (!statusObject || !typeObject) {
return true;
}
if (
// Publicly viewable links never have plain permalinks.
postUtil.isStatusViewable(statusObject) ||
// Private posts don't have plain permalinks if the user can read them.
(true == statusObject.private &&
post.props?.ID &&
current.user?.can("read_post", post.props.ID)) ||
// Protected posts don't have plain links if getting a sample URL.
(true == statusObject.protected && sample)) {
return false;
}
return true;
}
async getHomeUrl(args) {
let { scheme = undefined } = args ?? {};
const { blogId = undefined, path = "" } = args ?? {};
const options = this.components.get(options_1.Options);
if (blogId && this.config.isMultiSite()) {
options.usingBlog(blogId);
}
let url = (await options.get("home")) ?? "";
options.resetBlog();
if (!["http", "https", "relative"].includes(scheme ?? "")) {
scheme = this.config.isSsl() ? "https" : "http";
}
else {
try {
const protocol = new URL(url).protocol.replace(/:$/, "");
scheme = ["http", "https", "relative", "rest"].includes(protocol)
? protocol
: (scheme = this.config.isSsl() ? "https" : "http");
}
catch (e) {
scheme = this.config.isSsl() ? "https" : "http";
}
}
url = this.setUrlScheme(url, scheme);
if (typeof path === "string") {
url = `${url}/${path.replace(/^\//, "")}`;
}
return url;
}
setUrlScheme(url, scheme = null) {
if (!scheme ||
["admin", "login", "login_post", "rpc"].includes(scheme) ||
!["http", "https", "relative"].includes(scheme)) {
scheme = this.config.isSsl() ? "https" : "http";
}
url = url.trim();
if (url.startsWith("//")) {
url = "https:" + url;
}
if (scheme === "relative") {
url = url.replace(/^\w+:\/\//, ""); // Remove the scheme
if (url !== "" && url[0] === "/") {
// eslint-disable-next-line no-control-regex
url = "/" + url.replace(/^[/ \t\n\r\0\x0B]+/, "");
}
}
else {
url = url.replace(/^\w+:\/\//, scheme + "://");
}
return url;
}
};
exports.LinkUtil = LinkUtil;
exports.LinkUtil = LinkUtil = __decorate([
(0, component_1.component)(),
__metadata("design:paramtypes", [components_1.Components,
post_util_1.PostUtil,
vars_1.Vars,
config_1.Config])
], LinkUtil);