PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Agent / abilities / content-list.js

content-list.js in Extendify 3.2.1, at src/Agent/abilities/content-list.js

105 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { query, restBase } from '@agent/abilities/rest';
2 import apiFetch from '@wordpress/api-fetch';
3 import { decodeEntities } from '@wordpress/html-entities';
4 import { __ } from '@wordpress/i18n';
5
6 const fields = 'id,title,slug,status,date,link';
7
8 export const contentList = {
9 name: 'extendify/content-list',
10 label: __('Site content', 'extendify-local'),
11 description:
12 'List this site’s posts, pages or any other post type, or count how many match. Returns each one’s id, title, slug, status, date and address, not its content.',
13 inputSchema: {
14 type: 'object',
15 properties: {
16 postType: {
17 type: 'string',
18 description: 'The post type to list. Defaults to post.',
19 },
20 status: {
21 type: 'string',
22 description:
23 'Defaults to publish. Use any to cover every status, including drafts and trash.',
24 },
25 search: {
26 type: 'string',
27 description: 'Words to match against titles and content.',
28 },
29 author: {
30 type: 'integer',
31 description: 'The id of an author.',
32 },
33 after: {
34 type: 'string',
35 description: 'Only what was published after this ISO 8601 date.',
36 },
37 before: {
38 type: 'string',
39 description: 'Only what was published before this ISO 8601 date.',
40 },
41 orderBy: {
42 type: 'string',
43 enum: ['date', 'modified', 'title', 'id', 'author'],
44 },
45 order: { type: 'string', enum: ['asc', 'desc'] },
46 limit: {
47 type: 'integer',
48 minimum: 1,
49 maximum: 100,
50 description: 'How many to return. Defaults to 10.',
51 },
52 count: {
53 type: 'boolean',
54 description: 'Return how many match rather than the list itself.',
55 },
56 },
57 },
58 annotations: { readonly: true },
59 execute: async ({
60 postType = 'post',
61 status = 'publish',
62 search,
63 author,
64 after,
65 before,
66 orderBy,
67 order,
68 limit = 10,
69 count,
70 } = {}) => {
71 const base = await restBase(`/wp/v2/types/${postType}`);
72 if (!base) return { error: `No post type named ${postType} can be read.` };
73
74 const response = await apiFetch({
75 path: `/wp/v2/${base}?${query({
76 status,
77 search,
78 author,
79 after,
80 before,
81 orderby: orderBy,
82 order,
83 per_page: count ? 1 : limit,
84 _fields: count ? 'id' : fields,
85 })}`,
86 parse: false,
87 });
88 const total = Number(response.headers.get('X-WP-Total'));
89 if (count) return { count: total };
90
91 const posts = await response.json();
92 return {
93 total,
94 posts: posts.map(({ id, title, slug, status, date, link }) => ({
95 id,
96 title: decodeEntities(title?.rendered ?? ''),
97 slug,
98 status,
99 date,
100 link,
101 })),
102 };
103 },
104 };
105