@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
106 lines (105 loc) • 4.22 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);
};
var RevisionUtil_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RevisionUtil = void 0;
const component_1 = require("../../decorators/component");
const components_1 = require("../components");
const post_util_1 = require("./post.util");
const config_1 = require("../../config");
const query_util_1 = require("./query.util");
let RevisionUtil = class RevisionUtil {
static { RevisionUtil_1 = this; }
components;
config;
postUtil;
// _wp_post_revision_fields
static revisionFields = {
post_title: "Title",
post_content: "Content",
post_excerpt: "Excerpt",
};
constructor(components, config, postUtil) {
this.components = components;
this.config = config;
this.postUtil = postUtil;
}
// wp_get_post_autosave
async getAutosave(postId, userId = 0) {
const queryUtil = this.components.get(query_util_1.QueryUtil);
const posts = await queryUtil.posts((query) => {
if (userId > 0) {
query.where("post_author", userId);
}
query
.where("post_parent", postId)
.where("post_type", "revision")
.where("post_status", "inherit")
.where("post_name", `${postId}-autosave-v1`)
.builder.limit(1);
});
if (!posts) {
return undefined;
}
const postUtil = this.components.get(post_util_1.PostUtil);
return await postUtil.get(posts[0].ID);
}
// wp_get_post_revisions
async getList(postOrId, fn) {
const post = typeof postOrId == "number"
? await this.postUtil.get(postOrId)
: postOrId;
if (!post.props || !post.props.ID) {
return [];
}
const postId = post.props.ID;
if (0 >= this.config.config.constants.WP_POST_REVISIONS) {
return [];
}
const queryUtil = this.components.get(query_util_1.QueryUtil);
const revisions = (await queryUtil.posts((query) => {
const { column } = query.alias;
query.from
.withChildren(postId)
.where("post_type", "revision")
.where("post_status", "inherit")
.builder.orderBy(column("posts", "ID"), "desc");
if (fn) {
fn(query);
}
})) ?? [];
return revisions;
}
// _wp_post_revision_data
convertToData(post, autosave = false) {
const revisionData = {};
const commonFields = Object.keys(RevisionUtil_1.revisionFields);
for (const field of commonFields) {
revisionData[field] = post[field] ?? "";
}
revisionData["post_parent"] = post.ID;
revisionData["post_status"] = "inherit";
revisionData["post_type"] = "revision";
revisionData["post_name"] = autosave
? `${post.ID}-autosave-v1`
: `${post.ID}-revision-v1`;
revisionData["post_date"] = post.post_modified || "";
revisionData["post_date_gmt"] = post.post_modified_gmt || "";
return revisionData;
}
};
exports.RevisionUtil = RevisionUtil;
exports.RevisionUtil = RevisionUtil = RevisionUtil_1 = __decorate([
(0, component_1.component)(),
__metadata("design:paramtypes", [components_1.Components,
config_1.Config,
post_util_1.PostUtil])
], RevisionUtil);