@rnaga/wp-node 1.3.17 → 1.3.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -46,10 +46,12 @@ export declare class PostTrx extends Trx {
|
|
|
46
46
|
updateMenuOrder(postId: number, menuOrder: number, options?: {
|
|
47
47
|
checkParent?: boolean;
|
|
48
48
|
reOrder?: boolean;
|
|
49
|
+
postTypes?: string[];
|
|
49
50
|
}): Promise<boolean>;
|
|
50
51
|
swapMenuOrder(postId1: number, postId2: number, options?: {
|
|
51
52
|
reOrderOnEqual?: boolean;
|
|
52
53
|
checkParent?: boolean;
|
|
54
|
+
postTypes?: string[];
|
|
53
55
|
}): Promise<boolean>;
|
|
54
56
|
}
|
|
55
57
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"post.trx.d.ts","sourceRoot":"","sources":["../../src/transactions/post.trx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGhD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAGzD,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAE3D,OAAO,QAAQ,MAAM,aAAa,CAAC;AAEnC,OAAO,KAAK,GAAG,MAAM,eAAe,CAAC;AAMrC,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAE5B,OAAO,KAAK,KAAK,KAAK,MAAM,UAAU,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,KAAK,UAAU,GAAG,CAAC,CAAC,KAAK,C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,KAAK,CAAC;KAChB,GACD;QACE,IAAI,CAAC,EAAE,KAAK,CAAC;QACb,MAAM,EAAE,IAAI,CAAC;KACd;IAmBD,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,UAAQ;IAiH9C,OAAO,CAAC,MAAM,EAAE,MAAM;IA2CtB,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;IA0D3C,KAAK,CAAC,MAAM,EAAE,MAAM;IA6CpB,aAAa,CAAC,QAAQ,EAAE,IAAI,GAAG,MAAM;IAoErC,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAwB7C,cAAc,CAClB,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EACnC,MAAM,UAAQ;IAuBV,SAAS,CACb,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EACnC,YAAY,GAAE,KAAK,CAAC,YAAyB,EAC7C,MAAM,UAAQ;IAkCV,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE;QACR,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;KACtB;IA2HG,aAAa,CACjB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;KACtB;CAkGJ"}
|
package/transactions/post.trx.js
CHANGED
|
@@ -190,8 +190,8 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
190
190
|
else if (update && !parsedInput.post_category) {
|
|
191
191
|
postCategory = !postBefore
|
|
192
192
|
? []
|
|
193
|
-
: (await postBefore.terms("category"))?.map((term) => term.term_id) ??
|
|
194
|
-
[];
|
|
193
|
+
: ((await postBefore.terms("category"))?.map((term) => term.term_id) ??
|
|
194
|
+
[]);
|
|
195
195
|
}
|
|
196
196
|
// Make sure we set a valid category.
|
|
197
197
|
if (0 >= postCategory.length &&
|
|
@@ -258,7 +258,7 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
258
258
|
data.post_author =
|
|
259
259
|
parsedInput.post_author > 0
|
|
260
260
|
? parsedInput.post_author
|
|
261
|
-
: current.user?.props?.ID ?? -1;
|
|
261
|
+
: (current.user?.props?.ID ?? -1);
|
|
262
262
|
data.ping_status =
|
|
263
263
|
parsedInput.ping_status ??
|
|
264
264
|
this.commentUtil.getDefaultStatus(data.post_type, "pingback");
|
|
@@ -902,6 +902,10 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
902
902
|
if (!post.props) {
|
|
903
903
|
throw new Error(`Post not found - ${postId}`);
|
|
904
904
|
}
|
|
905
|
+
const postTypes = options?.postTypes ?? [post.props.post_type];
|
|
906
|
+
if (!postTypes.includes(post.props.post_type)) {
|
|
907
|
+
throw new Error(`Post type of the post should be in ${postTypes.join(", ")} - ${postId}`);
|
|
908
|
+
}
|
|
905
909
|
if (!reOrder) {
|
|
906
910
|
const trx = await this.database.transaction;
|
|
907
911
|
try {
|
|
@@ -925,7 +929,7 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
925
929
|
try {
|
|
926
930
|
const query = trx
|
|
927
931
|
.table(this.tables.get("posts"))
|
|
928
|
-
.
|
|
932
|
+
.whereIn("post_type", postTypes)
|
|
929
933
|
.where("menu_order", ">", oldMenuOrder)
|
|
930
934
|
.where("menu_order", "<", menuOrder)
|
|
931
935
|
.whereNot("ID", postId);
|
|
@@ -946,7 +950,7 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
946
950
|
const queryUtil = this.components.get(query_util_1.QueryUtil);
|
|
947
951
|
const collisionPosts = await queryUtil.posts((query) => {
|
|
948
952
|
query
|
|
949
|
-
.
|
|
953
|
+
.whereIn("post_type", postTypes)
|
|
950
954
|
.where("menu_order", menuOrder)
|
|
951
955
|
.where("ID", postId, "!=");
|
|
952
956
|
if (checkParent) {
|
|
@@ -959,7 +963,7 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
959
963
|
try {
|
|
960
964
|
const query = trx
|
|
961
965
|
.table(this.tables.get("posts"))
|
|
962
|
-
.
|
|
966
|
+
.whereIn("post_type", postTypes)
|
|
963
967
|
.where("menu_order", ">=", menuOrder)
|
|
964
968
|
.whereNot("ID", postId);
|
|
965
969
|
if (oldMenuOrder > menuOrder) {
|
|
@@ -991,36 +995,40 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
991
995
|
await trxUpdate.commit();
|
|
992
996
|
return true;
|
|
993
997
|
}
|
|
994
|
-
// Note:
|
|
998
|
+
// Note: post1 and post2 are compared and the smaller menu_order is assigned to post1.
|
|
995
999
|
// Performance note: not suitable for high-frequency or bulk use.
|
|
996
1000
|
// - Fetches both posts individually before writing (2 extra DB reads per call).
|
|
997
1001
|
// - Uses 2 separate transactions instead of one, creating a TOCTOU window
|
|
998
1002
|
// where concurrent writes between the reads and updates can cause inconsistency.
|
|
999
1003
|
async swapMenuOrder(postId1, postId2, options) {
|
|
1000
1004
|
const { reOrderOnEqual = true, checkParent = true } = options ?? {};
|
|
1001
|
-
|
|
1002
|
-
|
|
1005
|
+
let post1 = await this.postUtil.get(postId1);
|
|
1006
|
+
let post2 = await this.postUtil.get(postId2);
|
|
1003
1007
|
if (!post1.props || !post2.props) {
|
|
1004
1008
|
throw new Error(`Posts not found - ${postId1}, ${postId2}`);
|
|
1005
1009
|
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1010
|
+
const postTypes = options?.postTypes ??
|
|
1011
|
+
Array.from(new Set([post1.props.post_type, post2.props.post_type]));
|
|
1012
|
+
if (!postTypes.includes(post1.props.post_type) ||
|
|
1013
|
+
!postTypes.includes(post2.props.post_type)) {
|
|
1014
|
+
throw new Error(`Post types of both posts should be in [${postTypes.join(", ")}] - ${postId1}, ${postId2}`);
|
|
1008
1015
|
}
|
|
1009
1016
|
if (checkParent && post1.props.post_parent !== post2.props.post_parent) {
|
|
1010
1017
|
throw new Error(`Posts should have the same parent - ${postId1}, ${postId2}`);
|
|
1011
1018
|
}
|
|
1019
|
+
// Compare menu order and set menuOrder1 as smaller one.
|
|
1012
1020
|
if (post1.props.menu_order > post2.props.menu_order) {
|
|
1013
|
-
|
|
1021
|
+
// Swap post1 and post2 to make sure menu order of post1 is smaller than or equal to post2.
|
|
1022
|
+
[post1, post2] = [post2, post1];
|
|
1023
|
+
// Swap postId1 and postId2 to keep consistent with post1 and post2 after swap.
|
|
1024
|
+
[postId1, postId2] = [postId2, postId1];
|
|
1014
1025
|
}
|
|
1015
1026
|
const menuOrder1 = post1.props.menu_order;
|
|
1016
1027
|
const menuOrder2 = post2.props.menu_order;
|
|
1017
1028
|
// Update menu order of post1.
|
|
1018
1029
|
let trx = await this.database.transaction;
|
|
1019
1030
|
try {
|
|
1020
|
-
await trx
|
|
1021
|
-
.table(this.tables.get("posts"))
|
|
1022
|
-
.where("ID", postId1)
|
|
1023
|
-
.update({
|
|
1031
|
+
await trx.table(this.tables.get("posts")).where("ID", postId1).update({
|
|
1024
1032
|
menu_order: menuOrder2,
|
|
1025
1033
|
});
|
|
1026
1034
|
}
|
|
@@ -1030,17 +1038,11 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
1030
1038
|
}
|
|
1031
1039
|
await trx.commit();
|
|
1032
1040
|
// Update menu order of post2.
|
|
1033
|
-
// If
|
|
1034
|
-
// 1. menu order of post2 should be increased by 1.
|
|
1035
|
-
// 2. then the rest of posts with the same parent_post and menu order that's same or larger than menu order of post2 (&post1) should be increased by 1
|
|
1041
|
+
// If the orders differ, or reOrderOnEqual is false, perform a plain swap: give post2 post1's old order.
|
|
1036
1042
|
if (!reOrderOnEqual || menuOrder1 !== menuOrder2) {
|
|
1037
|
-
// If menu order of post1 and post2 are different, just swap them.
|
|
1038
1043
|
const trx = await this.database.transaction;
|
|
1039
1044
|
try {
|
|
1040
|
-
await trx
|
|
1041
|
-
.table(this.tables.get("posts"))
|
|
1042
|
-
.where("ID", postId2)
|
|
1043
|
-
.update({
|
|
1045
|
+
await trx.table(this.tables.get("posts")).where("ID", postId2).update({
|
|
1044
1046
|
menu_order: menuOrder1,
|
|
1045
1047
|
});
|
|
1046
1048
|
}
|
|
@@ -1058,9 +1060,9 @@ let PostTrx = class PostTrx extends trx_1.Trx {
|
|
|
1058
1060
|
try {
|
|
1059
1061
|
const query = trxRest
|
|
1060
1062
|
.table(this.tables.get("posts"))
|
|
1061
|
-
.where("post_type", post1.props.post_type)
|
|
1062
1063
|
.where("menu_order", ">=", menuOrder2)
|
|
1063
|
-
.whereNot("ID", postId1)
|
|
1064
|
+
.whereNot("ID", postId1)
|
|
1065
|
+
.whereIn("post_type", postTypes);
|
|
1064
1066
|
if (checkParent) {
|
|
1065
1067
|
query.where("post_parent", post1.props.post_parent);
|
|
1066
1068
|
}
|
package/validators/crud.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare const postListParams: z.ZodObject<{
|
|
|
13
13
|
include: z.ZodOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodNumber>, z.ZodPipe<z.ZodString, z.ZodTransform<number[], string>>]>>;
|
|
14
14
|
offset: z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>]>>;
|
|
15
15
|
order: z.ZodDefault<z.ZodUnion<readonly [z.ZodLiteral<"asc">, z.ZodLiteral<"desc">]>>;
|
|
16
|
-
orderby: z.ZodDefault<z.ZodUnion<readonly [z.ZodLiteral<"post_author">, z.ZodLiteral<"post_date">, z.ZodLiteral<"ID">, z.ZodLiteral<"post_modified">, z.ZodLiteral<"post_parent">, z.ZodLiteral<"post_name">, z.ZodLiteral<"post_title">]>>;
|
|
16
|
+
orderby: z.ZodDefault<z.ZodUnion<readonly [z.ZodLiteral<"post_author">, z.ZodLiteral<"post_date">, z.ZodLiteral<"ID">, z.ZodLiteral<"post_modified">, z.ZodLiteral<"post_parent">, z.ZodLiteral<"post_name">, z.ZodLiteral<"post_title">, z.ZodLiteral<"menu_order">]>>;
|
|
17
17
|
slug: z.ZodOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>]>>;
|
|
18
18
|
status: z.ZodOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodPipe<z.ZodString, z.ZodTransform<string[], string>>]>>;
|
|
19
19
|
status_exclude: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
package/validators/crud.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../src/validators/crud.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../src/validators/crud.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuCzB,CAAC;AAEH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4BzB,CAAC;AAEH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;iBASzB,CAAC;AAEH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqBzB,CAAC;AAEH,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+B5B,CAAC;AAEH,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BnB,CAAC;AAEH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAsBzB,CAAC;AAEH,eAAO,MAAM,UAAU;;;;;;;;;;;;;;iBAItB,CAAC;AAEF,eAAO,MAAM,UAAU;;;;;iBASrB,CAAC;AAEH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;iBAYzB,CAAC;AAEH,eAAO,MAAM,UAAU;;;;iBAOrB,CAAC;AAEH,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;iBAkB7B,CAAC;AAEH,eAAO,MAAM,oBAAoB;;;iBAG/B,CAAC;AAEH,eAAO,MAAM,eAAe;;iBAE1B,CAAC"}
|
package/validators/crud.js
CHANGED