PluginProbe
Extendify / 3.1.6
Extendify v3.1.6
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 / taxonomies.js

taxonomies.js in Extendify 3.1.6, at src/Agent/abilities/taxonomies.js

80 lines 2.1 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,name,slug,count,parent';
7
8 export const taxonomies = {
9 name: 'extendify/taxonomies',
10 label: __('Categories and tags', 'extendify-local'),
11 description:
12 'The ways this site groups its content — its categories and tags, and whatever else its theme or plugins added. Ask without naming one for the list of them, name one for the terms in it, or name a post for the terms that post carries.',
13 inputSchema: {
14 type: 'object',
15 properties: {
16 taxonomy: {
17 type: 'string',
18 description:
19 'Which one to read terms from, such as category or post_tag. Omit it to list them instead.',
20 },
21 postId: {
22 type: 'integer',
23 description: 'Only the terms this post carries.',
24 },
25 search: {
26 type: 'string',
27 description: 'Words to match against term names.',
28 },
29 limit: {
30 type: 'integer',
31 minimum: 1,
32 maximum: 100,
33 description: 'How many terms to return. Defaults to 20.',
34 },
35 },
36 },
37 annotations: { readonly: true },
38 execute: async ({ taxonomy, postId, search, limit = 20 } = {}) => {
39 if (!taxonomy) {
40 const registered = await apiFetch({ path: '/wp/v2/taxonomies' });
41 return {
42 taxonomies: Object.values(registered).map(
43 ({ name, slug, types, hierarchical }) => ({
44 name: decodeEntities(name),
45 slug,
46 postTypes: types,
47 hierarchical,
48 }),
49 ),
50 };
51 }
52
53 const base = await restBase(`/wp/v2/taxonomies/${taxonomy}`);
54 if (!base) return { error: `No taxonomy named ${taxonomy} can be read.` };
55
56 const response = await apiFetch({
57 path: `/wp/v2/${base}?${query({
58 search,
59 post: postId,
60 per_page: limit,
61 _fields: fields,
62 })}`,
63 parse: false,
64 });
65 const terms = await response.json();
66
67 return {
68 taxonomy,
69 total: Number(response.headers.get('X-WP-Total')),
70 terms: terms.map(({ id, name, slug, count, parent }) => ({
71 id,
72 name: decodeEntities(name),
73 slug,
74 count,
75 parent,
76 })),
77 };
78 },
79 };
80