nextpress-core 1.0.0
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/lib/acf-functions/core/acf-builder.ts +105 -0
- package/lib/acf-functions/core/acf-component-autoloader.ts +33 -0
- package/lib/acf-functions/core/acf-field-group-autoloader.ts +28 -0
- package/lib/acf-functions/services/define-field-group.ts +10 -0
- package/lib/acf-functions/services/define-layout.ts +10 -0
- package/lib/acf-functions/services/map-fields/helpers/map-choice-object.ts +20 -0
- package/lib/acf-functions/services/map-fields/map-fields.ts +359 -0
- package/lib/acf-functions/types/acf-field-group.d.ts +118 -0
- package/lib/acf-functions/types/acf-field.d.ts +2851 -0
- package/lib/acf-functions/types/acf-layout.d.ts +20 -0
- package/lib/acf-functions/types/acf-values.d.ts +4 -0
- package/lib/acf-functions/types/components/field-props.d.ts +189 -0
- package/lib/acf-functions/types/components/nextpress-component.d.ts +11 -0
- package/lib/ambient.d.ts +15 -0
- package/lib/entities/common.d.ts +30 -0
- package/lib/entities/option/option.interface.d.ts +8 -0
- package/lib/entities/option/option.ts +43 -0
- package/lib/entities/post/post.interface.d.ts +72 -0
- package/lib/entities/post/post.ts +209 -0
- package/lib/entities/term/term.interface.d.ts +8 -0
- package/lib/entities/term/term.ts +82 -0
- package/lib/entities/user/user.interface.d.ts +10 -0
- package/lib/entities/user/user.ts +86 -0
- package/lib/globals/entity-loader/entity-loader-base.ts +141 -0
- package/lib/globals/entity-loader/entity-loader.d.ts +50 -0
- package/lib/globals/entity-loader/option-loader.ts +56 -0
- package/lib/globals/entity-loader/post-loader.ts +59 -0
- package/lib/globals/entity-loader/term-loader.ts +59 -0
- package/lib/globals/entity-loader/user-loader.ts +60 -0
- package/lib/globals/get-field/get-field.ts +75 -0
- package/lib/globals/globals.ts +25 -0
- package/lib/globals/nextpress-config/nextpress-config.interface.d.ts +18 -0
- package/lib/globals/nextpress-config/nextpress-config.ts +7 -0
- package/lib/globals/queried-object/queried-object.ts +124 -0
- package/lib/repository/optionquery/option-query-args.d.ts +21 -0
- package/lib/repository/optionquery/option-query.ts +29 -0
- package/lib/repository/postquery/post-query-args.d.ts +108 -0
- package/lib/repository/postquery/post-query.ts +281 -0
- package/lib/repository/termquery/term-query-args.d.ts +61 -0
- package/lib/repository/termquery/term-query.ts +243 -0
- package/lib/repository/userquery/user-query-args.d.ts +75 -0
- package/lib/repository/userquery/user-query.ts +195 -0
- package/lib/router/helpers.ts +20 -0
- package/lib/router/nextpress-layout.tsx +48 -0
- package/lib/router/nextpress-proxy.ts +45 -0
- package/lib/router/nextpress-static-params.ts +62 -0
- package/lib/router/router.tsx +61 -0
- package/lib/router/routes/api/api-get-admin-bar.ts +29 -0
- package/lib/router/routes/api/api-get-draft-mode.ts +47 -0
- package/lib/router/routes/api/api-get-field-groups.ts +29 -0
- package/lib/router/routes/api/api-post-revalidate.ts +30 -0
- package/lib/router/routes/api/helpers.ts +17 -0
- package/lib/router/routes/author-archive.tsx +75 -0
- package/lib/router/routes/not-found-page.tsx +22 -0
- package/lib/router/routes/post-index-page.tsx +58 -0
- package/lib/router/routes/singular-page.tsx +78 -0
- package/lib/router/routes/site-front-page.tsx +51 -0
- package/lib/router/routes/term-archive.tsx +93 -0
- package/lib/router/types.d.ts +9 -0
- package/lib/services/get-menu.ts +104 -0
- package/lib/services/get-theme-mods.ts +16 -0
- package/lib/services/metadata/get-blogname.ts +9 -0
- package/lib/services/metadata/get-favicon-url.ts +13 -0
- package/lib/services/metadata/get-language-attribute.ts +9 -0
- package/lib/services/services.ts +7 -0
- package/lib/services/utilities/capitalise-first-letter.ts +10 -0
- package/lib/services/utilities/esc-html.ts +12 -0
- package/lib/services/utilities/get-date-time-formatter.ts +49 -0
- package/lib/services/utilities/index.ts +0 -0
- package/lib/services/utilities/kses-post.ts +13 -0
- package/lib/services/utilities/process-url.ts +15 -0
- package/lib/template-heirarchy/_autoloader/template-autoloader.ts +54 -0
- package/lib/template-heirarchy/archive/archive.tsx +28 -0
- package/lib/template-heirarchy/archive/author.tsx +28 -0
- package/lib/template-heirarchy/archive/category.tsx +29 -0
- package/lib/template-heirarchy/archive/posttypearchive.tsx +33 -0
- package/lib/template-heirarchy/archive/tag.tsx +30 -0
- package/lib/template-heirarchy/archive/taxonomy.tsx +28 -0
- package/lib/template-heirarchy/home/home.tsx +28 -0
- package/lib/template-heirarchy/index.tsx +30 -0
- package/lib/template-heirarchy/not-found.tsx/not-found.tsx +28 -0
- package/lib/template-heirarchy/page/page.tsx +28 -0
- package/lib/template-heirarchy/page/posttype.tsx +34 -0
- package/lib/template-heirarchy/page/single.tsx +28 -0
- package/lib/template-heirarchy/page/singular.tsx +28 -0
- package/lib/ui/render-attachment-image.tsx +29 -0
- package/lib/ui/render-components.tsx +12 -0
- package/lib/ui/render-the-admin-bar.tsx +79 -0
- package/lib/ui/render-the-logo.tsx +24 -0
- package/lib/wpdb/wpdb.interface.d.ts +171 -0
- package/lib/wpdb/wpdb.ts +36 -0
- package/package.json +38 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { wpdb } from "@/wpdb/wpdb";
|
|
2
|
+
import { ITerm } from "./term.interface";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Implementation of ITerm entity.
|
|
6
|
+
*/
|
|
7
|
+
export class Term implements ITerm
|
|
8
|
+
{
|
|
9
|
+
constructor(
|
|
10
|
+
public termId: number
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
/** Raw term data from database. */
|
|
14
|
+
private termData?: Record<string, any>;
|
|
15
|
+
/** Map of term meta key-value pairs. */
|
|
16
|
+
private metaMap?: Map<string, string>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves array of Term instances by IDs.
|
|
20
|
+
*
|
|
21
|
+
* @param {number[]} ids - Array of term IDs to retrieve.
|
|
22
|
+
* @returns {Promise<Term[]>} Promise resolving to array of Term instances.
|
|
23
|
+
*/
|
|
24
|
+
static async get(ids: number[]): Promise<Term[]> {
|
|
25
|
+
ids = ids.filter(Boolean);
|
|
26
|
+
if (!ids || !ids.length) return [];
|
|
27
|
+
|
|
28
|
+
const combinedData = await wpdb
|
|
29
|
+
.selectFrom('wpTerms as term')
|
|
30
|
+
.innerJoin('wpTermTaxonomy as taxonomy', 'term.termId', 'taxonomy.termId')
|
|
31
|
+
.where('term.termId', 'in', ids)
|
|
32
|
+
.select([
|
|
33
|
+
'term.termId as termId',
|
|
34
|
+
'term.name as name',
|
|
35
|
+
'term.slug as slug',
|
|
36
|
+
'term.termGroup as termGroup',
|
|
37
|
+
'taxonomy.termTaxonomyId as termTaxonomyId',
|
|
38
|
+
'taxonomy.taxonomy as taxonomy',
|
|
39
|
+
'taxonomy.description as description',
|
|
40
|
+
'taxonomy.parent as parent',
|
|
41
|
+
'taxonomy.count as count'
|
|
42
|
+
])
|
|
43
|
+
.execute();
|
|
44
|
+
|
|
45
|
+
const metaData = await wpdb
|
|
46
|
+
.selectFrom('wpTermmeta')
|
|
47
|
+
.where('termId', 'in', ids)
|
|
48
|
+
.where('metaKey', '=', '_nextpress_path')
|
|
49
|
+
.select(['termId', 'metaKey', 'metaValue'])
|
|
50
|
+
.execute();
|
|
51
|
+
|
|
52
|
+
const metaByTermId = new Map<number, Map<string, string>>();
|
|
53
|
+
for (const row of metaData) {
|
|
54
|
+
const termId = Number(row.termId);
|
|
55
|
+
if (!metaByTermId.has(termId)) {
|
|
56
|
+
metaByTermId.set(termId, new Map());
|
|
57
|
+
}
|
|
58
|
+
metaByTermId.get(termId)?.set(row.metaKey || '', row.metaValue || '');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const combinedDataMap = new Map(combinedData.map(row => [Number(row.termId), row]));
|
|
62
|
+
|
|
63
|
+
return ids.map(id => {
|
|
64
|
+
const instance = new Term(id);
|
|
65
|
+
|
|
66
|
+
instance.termData = combinedDataMap.get(id);
|
|
67
|
+
instance.metaMap = metaByTermId.get(id) || new Map<string, string>();
|
|
68
|
+
|
|
69
|
+
return instance;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get path(): string { return this.metaMap?.get('_nextpress_path') ?? ''; }
|
|
74
|
+
get name(): string { return this.termData?.['name'] ?? ''; }
|
|
75
|
+
get slug(): string { return this.termData?.['slug'] ?? ''; }
|
|
76
|
+
get termGroup(): number { return this.termData?.['termGroup'] ? Number(this.termData['termGroup']) : 0; }
|
|
77
|
+
get parent(): number { return this.termData?.['parent'] ? Number(this.termData['parent']) : 0; }
|
|
78
|
+
get description(): string { return this.termData?.['description'] ?? ''; }
|
|
79
|
+
get termTaxonomyId(): number { return this.termData?.['termTaxonomyId'] ? Number(this.termData['termTaxonomyId']) : 0; }
|
|
80
|
+
get count(): number { return this.termData?.['count'] ? Number(this.termData['count']) : 0; }
|
|
81
|
+
get taxonomy(): string { return this.termData?.['taxonomy'] ?? ''; }
|
|
82
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { unserialize } from "php-serialize";
|
|
2
|
+
import { IUser } from "./user.interface";
|
|
3
|
+
import { wpdb } from "@/wpdb/wpdb";
|
|
4
|
+
|
|
5
|
+
export class User implements IUser {
|
|
6
|
+
constructor(
|
|
7
|
+
public ID: number
|
|
8
|
+
) {}
|
|
9
|
+
|
|
10
|
+
/** Raw user data from database. */
|
|
11
|
+
private userData?: Record<string, any>;
|
|
12
|
+
/** Map of user meta key-value pairs. */
|
|
13
|
+
private metaMap?: Map<string, string>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves array of User instances by IDs.
|
|
17
|
+
*
|
|
18
|
+
* @param {number[]} ids - Array of user IDs to retrieve.
|
|
19
|
+
* @returns {Promise<User[]>} Promise resolving to array of User instances.
|
|
20
|
+
*/
|
|
21
|
+
static async get(ids: number[]): Promise<User[]> {
|
|
22
|
+
ids = ids.filter(Boolean);
|
|
23
|
+
if (!ids || !ids.length) return [];
|
|
24
|
+
|
|
25
|
+
const usersData = await wpdb
|
|
26
|
+
.selectFrom('wpUsers')
|
|
27
|
+
.where('ID', 'in', ids)
|
|
28
|
+
.selectAll()
|
|
29
|
+
.execute();
|
|
30
|
+
|
|
31
|
+
const metaData = await wpdb
|
|
32
|
+
.selectFrom('wpUsermeta')
|
|
33
|
+
.where('userId', 'in', ids)
|
|
34
|
+
.where('metaKey', 'in', [
|
|
35
|
+
'wp_capabilities',
|
|
36
|
+
'show_admin_bar_front'
|
|
37
|
+
])
|
|
38
|
+
.select(['userId', 'metaKey', 'metaValue'])
|
|
39
|
+
.execute();
|
|
40
|
+
|
|
41
|
+
const metaByUserId = new Map<number, Map<string, string>>();
|
|
42
|
+
for (const row of metaData) {
|
|
43
|
+
const userId = Number(row.userId);
|
|
44
|
+
if (!metaByUserId.has(userId)) {
|
|
45
|
+
metaByUserId.set(userId, new Map());
|
|
46
|
+
}
|
|
47
|
+
metaByUserId.get(userId)?.set(row.metaKey || '', row.metaValue || '');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const userDataMap = new Map(usersData.map(user => [Number(user.ID), user]));
|
|
51
|
+
|
|
52
|
+
return ids.map(id => {
|
|
53
|
+
const instance = new User(id);
|
|
54
|
+
|
|
55
|
+
instance.userData = userDataMap.get(id);
|
|
56
|
+
instance.metaMap = metaByUserId.get(id) || new Map<string, string>();
|
|
57
|
+
|
|
58
|
+
return instance;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get roles(): string[] {
|
|
63
|
+
const rawCapabilities = this.metaMap?.get('wp_capabilities');
|
|
64
|
+
if (!rawCapabilities) return [];
|
|
65
|
+
try {
|
|
66
|
+
return Object.keys(unserialize(rawCapabilities));
|
|
67
|
+
} catch (error: any) {
|
|
68
|
+
console.warn('Error while getting user: Could not unserialize php: ', error.message);
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get showAdminBar(): boolean {
|
|
74
|
+
return this.metaMap?.get('show_admin_bar_front') === 'true';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get displayName(): string { return this.userData?.['displayName'] ?? ''; }
|
|
78
|
+
get userActivationKey(): string { return this.userData?.['userActivationKey'] ?? ''; }
|
|
79
|
+
get userEmail(): string { return this.userData?.['userEmail'] ?? ''; }
|
|
80
|
+
get userLogin(): string { return this.userData?.['userLogin'] ?? ''; }
|
|
81
|
+
get userNicename(): string { return this.userData?.['userNicename'] ?? ''; }
|
|
82
|
+
get userPass(): string { return this.userData?.['userPass'] ?? ''; }
|
|
83
|
+
get userRegistered(): Date { return this.userData?.['userRegistered'] ?? new Date(); }
|
|
84
|
+
get userStatus(): number { return this.userData?.['userStatus'] ? Number(this.userData['userStatus']) : 0; }
|
|
85
|
+
get userUrl(): string { return this.userData?.['userUrl'] ?? ''; }
|
|
86
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { EntityLoader, EntityQuery } from "./entity-loader";
|
|
2
|
+
import { queriedObjectState } from "../globals";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Constructor type for entity queries.
|
|
6
|
+
*/
|
|
7
|
+
type QueryConstructor<TArgs> = new (args: TArgs) => EntityQuery<TArgs>;
|
|
8
|
+
|
|
9
|
+
export interface ILoaderStorage<TEntity> {
|
|
10
|
+
loadQueue: Set<number>;
|
|
11
|
+
entityCache: Map<number, TEntity>;
|
|
12
|
+
promisedEntities: Map<number, Promise<TEntity[]>>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export abstract class EntityLoaderBase<TEntity, TArgs> implements EntityLoader<TEntity, TArgs>
|
|
16
|
+
{
|
|
17
|
+
/** Query class used to fetch entity IDs. */
|
|
18
|
+
protected abstract queryClass: QueryConstructor<TArgs>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Fetches entities from the database by ID.
|
|
22
|
+
*
|
|
23
|
+
* @param {number[]} ids Array of entity IDs.
|
|
24
|
+
*
|
|
25
|
+
* @returns {Promise<TEntity[]>} Array of fetched entities.
|
|
26
|
+
* @throws {Error} If database fetch fails.
|
|
27
|
+
*/
|
|
28
|
+
protected abstract fetchFromDatabase(ids: number[]): Promise<TEntity[]>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Gets the ID of an entity.
|
|
32
|
+
*
|
|
33
|
+
* @param {TEntity} entity The entity.
|
|
34
|
+
*
|
|
35
|
+
* @returns {number} The entity ID.
|
|
36
|
+
*/
|
|
37
|
+
protected abstract getEntityId(entity: TEntity): number;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Retrieves the local state for the loader from the global cache.
|
|
41
|
+
*
|
|
42
|
+
* @returns {ILoaderStorage<TEntity>} The loader storage.
|
|
43
|
+
*/
|
|
44
|
+
private getLocalState(): ILoaderStorage<TEntity> {
|
|
45
|
+
const state = queriedObjectState();
|
|
46
|
+
const loaderKey = `__loader_${this.constructor.name}`;
|
|
47
|
+
|
|
48
|
+
if (!state.loaderStates[loaderKey]) {
|
|
49
|
+
state.loaderStates[loaderKey] = {
|
|
50
|
+
loadQueue: new Set(),
|
|
51
|
+
entityCache: new Map(),
|
|
52
|
+
promisedEntities: new Map()
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return state.loaderStates[loaderKey];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Adds IDs to the loading queue.
|
|
60
|
+
*
|
|
61
|
+
* @param {number[]} ids Array of IDs to prime.
|
|
62
|
+
*/
|
|
63
|
+
public prime(ids: number[]): void {
|
|
64
|
+
const state = this.getLocalState();
|
|
65
|
+
for (const id of ids) {
|
|
66
|
+
if (state.loadQueue.has(id) || state.entityCache.has(id) || state.promisedEntities.has(id)) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
state.loadQueue.add(id);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Finds entity IDs matching the arguments and queues them for loading.
|
|
75
|
+
*
|
|
76
|
+
* @param {TArgs} args Query arguments.
|
|
77
|
+
*
|
|
78
|
+
* @returns {Promise<{ ids: number[]; count: number }>} Matching IDs and total count.
|
|
79
|
+
* @throws {Error} If query execution fails.
|
|
80
|
+
*/
|
|
81
|
+
public async findAndPrime(args: TArgs): Promise<{ ids: number[]; count: number }> {
|
|
82
|
+
const query = new this.queryClass(args);
|
|
83
|
+
|
|
84
|
+
const ids = await query.getIds();
|
|
85
|
+
const count = query.getCount() ?? ids.length;
|
|
86
|
+
|
|
87
|
+
this.get(ids);
|
|
88
|
+
|
|
89
|
+
return { ids, count };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves entities by ID.
|
|
94
|
+
*
|
|
95
|
+
* @param {number[]} ids Array of entity IDs.
|
|
96
|
+
*
|
|
97
|
+
* @returns {Promise<TEntity[]>} Array of entities.
|
|
98
|
+
* @throws {Error} If entity fetching fails.
|
|
99
|
+
*/
|
|
100
|
+
public async get(ids: number[]): Promise<TEntity[]> {
|
|
101
|
+
const state = this.getLocalState();
|
|
102
|
+
|
|
103
|
+
const newIds = ids.filter(id => !state.entityCache.has(id) && !state.promisedEntities.has(id));
|
|
104
|
+
|
|
105
|
+
if (newIds.length > 0) {
|
|
106
|
+
this.prime(newIds);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (state.loadQueue.size > 0) {
|
|
110
|
+
const queue = Array.from(state.loadQueue);
|
|
111
|
+
state.loadQueue.clear();
|
|
112
|
+
|
|
113
|
+
const fetchPromise = this.fetchFromDatabase(queue).then(queuedEntities => {
|
|
114
|
+
queuedEntities.forEach(entity => state.entityCache.set(this.getEntityId(entity), entity));
|
|
115
|
+
queue.forEach(id => state.promisedEntities.delete(id));
|
|
116
|
+
return queuedEntities;
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
queue.forEach(id => {
|
|
120
|
+
state.promisedEntities.set(id, fetchPromise);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const activePromises = Array.from(
|
|
125
|
+
new Set(
|
|
126
|
+
ids
|
|
127
|
+
.map(id => state.promisedEntities.get(id))
|
|
128
|
+
.filter((p): p is Promise<TEntity[]> => !!p)
|
|
129
|
+
)
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (activePromises.length > 0) {
|
|
133
|
+
await Promise.all(activePromises);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return ids.flatMap(id => {
|
|
137
|
+
const entity = state.entityCache.get(id);
|
|
138
|
+
return entity ? [entity] : [];
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the contract for an entity loader.
|
|
3
|
+
*
|
|
4
|
+
* @template EntityT The type of entity being loaded.
|
|
5
|
+
* @template QueryArgsT The type of arguments used for querying the entity.
|
|
6
|
+
*/
|
|
7
|
+
export interface EntityLoader<EntityT, QueryArgsT> {
|
|
8
|
+
/**
|
|
9
|
+
* Queues entity IDs for loading.
|
|
10
|
+
*
|
|
11
|
+
* @param {number[]} ids Array of entity IDs.
|
|
12
|
+
*/
|
|
13
|
+
prime: (ids: number[]) => void;
|
|
14
|
+
/**
|
|
15
|
+
* Finds entity IDs based on query arguments and queues them for loading.
|
|
16
|
+
*
|
|
17
|
+
* @param {QueryArgsT} args Query arguments.
|
|
18
|
+
* @returns {Promise<{ids: number[], count: number}>} Array of found IDs and the total count.
|
|
19
|
+
* @throws {Error} If the query fails.
|
|
20
|
+
*/
|
|
21
|
+
findAndPrime: (args: QueryArgsT) => Promise<{ids: number[], count: number}>;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves entities by their IDs.
|
|
24
|
+
*
|
|
25
|
+
* @param {number[]} ids Array of entity IDs.
|
|
26
|
+
* @returns {Promise<EntityT[]>} Array of entities.
|
|
27
|
+
* @throws {Error} If retrieval fails.
|
|
28
|
+
*/
|
|
29
|
+
get: (ids: number[]) => Promise<EntityT[]>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Defines the contract for an entity query.
|
|
34
|
+
*
|
|
35
|
+
* @template TArgs The type of query arguments.
|
|
36
|
+
*/
|
|
37
|
+
export interface EntityQuery<TArgs> {
|
|
38
|
+
/**
|
|
39
|
+
* Executes the query and returns the matching entity IDs.
|
|
40
|
+
*
|
|
41
|
+
* @returns {Promise<number[]>} Array of entity IDs.
|
|
42
|
+
* @throws {Error} If the query fails.
|
|
43
|
+
*/
|
|
44
|
+
getIds(): Promise<number[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the total count of entities matching the query.
|
|
47
|
+
* @returns {number | undefined} Total count, or undefined if not computed.
|
|
48
|
+
*/
|
|
49
|
+
getCount(): number | undefined;
|
|
50
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { OptionQuery } from "@/repository/optionquery/option-query";
|
|
2
|
+
import { Option } from "../../entities/option/option";
|
|
3
|
+
import { IOption } from "../../entities/option/option.interface";
|
|
4
|
+
import { OptionQueryArgs } from "../../repository/optionquery/option-query-args";
|
|
5
|
+
import { EntityLoader } from "./entity-loader";
|
|
6
|
+
import { EntityLoaderBase } from "./entity-loader-base";
|
|
7
|
+
|
|
8
|
+
class OptionLoader extends EntityLoaderBase<IOption, OptionQueryArgs> {
|
|
9
|
+
private static _instance: OptionLoader;
|
|
10
|
+
|
|
11
|
+
protected queryClass = OptionQuery;
|
|
12
|
+
|
|
13
|
+
private constructor() {
|
|
14
|
+
super();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static instance(): OptionLoader {
|
|
18
|
+
if (!this._instance) {
|
|
19
|
+
this._instance = new OptionLoader();
|
|
20
|
+
}
|
|
21
|
+
return this._instance;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected async fetchFromDatabase(ids: number[]): Promise<IOption[]> {
|
|
25
|
+
return await Option.get(ids);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected getEntityId(option: IOption): number {
|
|
29
|
+
return option.optionId;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare global {
|
|
34
|
+
/** Global instance of the OptionLoader. */
|
|
35
|
+
var optionLoader: EntityLoader<IOption, OptionQueryArgs>
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Retrieves an option value by name.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} name The option name.
|
|
41
|
+
*
|
|
42
|
+
* @returns {Promise<string | undefined>} The option value or undefined.
|
|
43
|
+
*/
|
|
44
|
+
var getOption: (name: string) => Promise<string | undefined>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
globalThis.optionLoader = OptionLoader.instance();
|
|
48
|
+
globalThis.getOption = async (name) => {
|
|
49
|
+
const foundOptions = await optionLoader.findAndPrime({
|
|
50
|
+
column: 'optionName',
|
|
51
|
+
operand: '=',
|
|
52
|
+
value: name
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
return (await optionLoader.get(foundOptions.ids))[0]?.optionValue;
|
|
56
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Post } from "@/entities/post/post";
|
|
2
|
+
import { IPost } from "../../entities/post/post.interface";
|
|
3
|
+
import { EntityLoader } from "./entity-loader";
|
|
4
|
+
import { EntityLoaderBase } from "./entity-loader-base";
|
|
5
|
+
import { PostQuery } from "@/repository/postquery/post-query";
|
|
6
|
+
|
|
7
|
+
class PostLoader extends EntityLoaderBase<IPost, PostQueryArgs> {
|
|
8
|
+
private static _instance: PostLoader;
|
|
9
|
+
|
|
10
|
+
protected queryClass = PostQuery;
|
|
11
|
+
|
|
12
|
+
private constructor() {
|
|
13
|
+
super();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public static instance(): PostLoader {
|
|
17
|
+
if (!this._instance) {
|
|
18
|
+
this._instance = new PostLoader();
|
|
19
|
+
}
|
|
20
|
+
return this._instance;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected async fetchFromDatabase(ids: number[]): Promise<IPost[]> {
|
|
24
|
+
return await Post.get(ids);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
protected getEntityId(post: IPost): number {
|
|
28
|
+
return post.ID;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare global {
|
|
33
|
+
/** Global instance of the PostLoader. */
|
|
34
|
+
var postLoader: EntityLoader<IPost, PostQueryArgs>
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves posts by their IDs.
|
|
37
|
+
*
|
|
38
|
+
* @param {number[]} ids Array of post IDs.
|
|
39
|
+
* @returns {Promise<IPost[]>} Array of posts.
|
|
40
|
+
*/
|
|
41
|
+
var getPosts: (ids: number[]) => Promise<IPost[]>
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves a single post by ID.
|
|
44
|
+
*
|
|
45
|
+
* @param {number} id The post ID.
|
|
46
|
+
* @returns {Promise<IPost | undefined>} The post or undefined.
|
|
47
|
+
*/
|
|
48
|
+
var getPost: (id: number) => Promise<IPost | undefined>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
globalThis.postLoader = PostLoader.instance();
|
|
52
|
+
globalThis.getPosts = async (ids) => {
|
|
53
|
+
return await postLoader.get(ids);
|
|
54
|
+
}
|
|
55
|
+
globalThis.getPost = async (id) => {
|
|
56
|
+
return (await postLoader.get([id]))[0];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Term } from "@/entities/term/term";
|
|
2
|
+
import { ITerm } from "../../entities/term/term.interface";
|
|
3
|
+
import { EntityLoader } from "./entity-loader";
|
|
4
|
+
import { EntityLoaderBase } from "./entity-loader-base";
|
|
5
|
+
import { TermQuery } from "@/repository/termquery/term-query";
|
|
6
|
+
|
|
7
|
+
class TermLoader extends EntityLoaderBase<ITerm, TermQueryArgs> {
|
|
8
|
+
private static _instance: TermLoader;
|
|
9
|
+
|
|
10
|
+
protected queryClass = TermQuery;
|
|
11
|
+
|
|
12
|
+
private constructor() {
|
|
13
|
+
super();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public static instance(): TermLoader {
|
|
17
|
+
if (!this._instance) {
|
|
18
|
+
this._instance = new TermLoader();
|
|
19
|
+
}
|
|
20
|
+
return this._instance;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected async fetchFromDatabase(ids: number[]): Promise<ITerm[]> {
|
|
24
|
+
return await Term.get(ids);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
protected getEntityId(term: ITerm): number {
|
|
28
|
+
return term.termId;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare global {
|
|
33
|
+
/** Global instance of the TermLoader. */
|
|
34
|
+
var termLoader: EntityLoader<ITerm, TermQueryArgs>
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves terms by their IDs.
|
|
37
|
+
*
|
|
38
|
+
* @param {number[]} ids Array of term IDs.
|
|
39
|
+
* @returns {Promise<ITerm[]>} Array of terms.
|
|
40
|
+
*/
|
|
41
|
+
var getTerms: (ids: number[]) => Promise<ITerm[]>
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves a single term by ID.
|
|
44
|
+
*
|
|
45
|
+
* @param {number} id The term ID.
|
|
46
|
+
* @returns {Promise<ITerm | undefined>} The term or undefined.
|
|
47
|
+
*/
|
|
48
|
+
var getTerm: (id: number) => Promise<ITerm | undefined>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
globalThis.termLoader = TermLoader.instance();
|
|
52
|
+
globalThis.getTerms = async (ids) => {
|
|
53
|
+
return await termLoader.get(ids);
|
|
54
|
+
}
|
|
55
|
+
globalThis.getTerm = async (id) => {
|
|
56
|
+
return (await termLoader.get([id]))[0];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { User } from "@/entities/user/user";
|
|
2
|
+
import { IUser } from "../../entities/user/user.interface";
|
|
3
|
+
import { EntityLoader } from "./entity-loader";
|
|
4
|
+
import { EntityLoaderBase } from "./entity-loader-base";
|
|
5
|
+
import { UserQuery } from "@/repository/userquery/user-query";
|
|
6
|
+
|
|
7
|
+
class UserLoader extends EntityLoaderBase<IUser, UserQueryArgs> {
|
|
8
|
+
private static _instance: UserLoader;
|
|
9
|
+
|
|
10
|
+
protected queryClass = UserQuery;
|
|
11
|
+
|
|
12
|
+
private constructor() {
|
|
13
|
+
super();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public static instance(): UserLoader {
|
|
17
|
+
if (!this._instance) {
|
|
18
|
+
this._instance = new UserLoader();
|
|
19
|
+
}
|
|
20
|
+
return this._instance;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected async fetchFromDatabase(ids: number[]): Promise<IUser[]> {
|
|
24
|
+
return await User.get(ids);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
protected getEntityId(user: IUser): number {
|
|
28
|
+
return user.ID;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare global {
|
|
33
|
+
/** Global instance of the UserLoader. */
|
|
34
|
+
var userLoader: EntityLoader<IUser, UserQueryArgs>
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves users by their IDs.
|
|
37
|
+
*
|
|
38
|
+
* @param {number[]} ids Array of user IDs.
|
|
39
|
+
* @returns {Promise<IUser[]>} Array of users.
|
|
40
|
+
*/
|
|
41
|
+
var getUsers: (ids: number[]) => Promise<IUser[]>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves a single user by ID.
|
|
45
|
+
*
|
|
46
|
+
* @param {number} id The user ID.
|
|
47
|
+
* @returns {Promise<IUser | undefined>} The user or undefined.
|
|
48
|
+
*/
|
|
49
|
+
var getUser: (id: number) => Promise<IUser | undefined>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
globalThis.userLoader = UserLoader.instance();
|
|
53
|
+
globalThis.getUsers = async (ids) => {
|
|
54
|
+
return await userLoader.get(ids);
|
|
55
|
+
}
|
|
56
|
+
globalThis.getUser = async (id) => {
|
|
57
|
+
return (await userLoader.get([id]))[0];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { mapField } from "../../acf-functions/services/map-fields/map-fields";
|
|
2
|
+
import { IFieldLocation } from "../../entities/common";
|
|
3
|
+
import { FieldProps } from "../../acf-functions/types/components/field-props";
|
|
4
|
+
|
|
5
|
+
/** Specifies location to retrieve fields from. */
|
|
6
|
+
type Location = IFieldLocation | 'options';
|
|
7
|
+
|
|
8
|
+
declare global {
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves, parses, and maps an Advanced Custom Fields (ACF) value to its corresponding component properties.
|
|
11
|
+
*
|
|
12
|
+
* @param {T} fieldGroup The configuration object defining the ACF field group and its schema.
|
|
13
|
+
* @param {K} selector The key name of the field to retrieve.
|
|
14
|
+
* @param {Location} [location] The database entity or context from which to load the field data. Accepts an `IFieldLocation` object or the string `'options'`. Defaults to the current queried object if omitted.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Promise<FieldProps<T>[K]>} A promise resolving to the mapped field properties.
|
|
17
|
+
*/
|
|
18
|
+
var getField: <
|
|
19
|
+
T extends NextpressFieldGroup,
|
|
20
|
+
K extends keyof FieldProps<T> & string
|
|
21
|
+
>(
|
|
22
|
+
fieldGroup: T,
|
|
23
|
+
selector: K,
|
|
24
|
+
location?: Location
|
|
25
|
+
) => Promise<FieldProps<T>[K]>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
globalThis.getField = async (fieldGroup, selector, location) => {
|
|
29
|
+
if (!location) {
|
|
30
|
+
switch(queriedObject.objectType) {
|
|
31
|
+
case 'post':
|
|
32
|
+
location = await getThePost();
|
|
33
|
+
break;
|
|
34
|
+
// TODO: Add support for Term and User
|
|
35
|
+
// case 'term':
|
|
36
|
+
// location = await getTheTerm();
|
|
37
|
+
// break;
|
|
38
|
+
// case 'user':
|
|
39
|
+
// location = await getTheUser();
|
|
40
|
+
// break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (!location) return [];
|
|
44
|
+
|
|
45
|
+
const values = await (async () => {
|
|
46
|
+
if (location === 'options') {
|
|
47
|
+
const prefix = 'options_';
|
|
48
|
+
|
|
49
|
+
const foundOptions = await optionLoader.findAndPrime({
|
|
50
|
+
column: 'optionName',
|
|
51
|
+
operand: 'like',
|
|
52
|
+
value: `${prefix}%`
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const options = await optionLoader.get(foundOptions.ids);
|
|
56
|
+
|
|
57
|
+
return options.map(option => ({
|
|
58
|
+
key: option.optionName.slice(prefix.length),
|
|
59
|
+
value: option.optionValue
|
|
60
|
+
}))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return await location.getFields(selector);
|
|
64
|
+
})() ?? [];
|
|
65
|
+
|
|
66
|
+
const valueMap: Map<string, string> = values.reduce((map, value) => {
|
|
67
|
+
map.set(value.key, value.value);
|
|
68
|
+
return map;
|
|
69
|
+
}, new Map());
|
|
70
|
+
|
|
71
|
+
const field = fieldGroup.fields.find(field => field.name === selector);
|
|
72
|
+
if (!field) return;
|
|
73
|
+
|
|
74
|
+
return mapField(field, valueMap) as any;
|
|
75
|
+
}
|