| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
|
| 5 |
const paths = { |
| 6 |
settings: '/wp/v2/settings?context=edit', |
| 7 |
themes: '/wp/v2/themes?status=active', |
| 8 |
plugins: '/wp/v2/plugins', |
| 9 |
}; |
| 10 |
|
| 11 |
const facts = { |
| 12 |
title: { from: 'settings', read: (settings) => settings.title }, |
| 13 |
tagline: { from: 'settings', read: (settings) => settings.description }, |
| 14 |
url: { from: 'settings', read: (settings) => settings.url }, |
| 15 |
language: { from: 'settings', read: (settings) => settings.language }, |
| 16 |
timezone: { from: 'settings', read: (settings) => settings.timezone }, |
| 17 |
frontPage: { |
| 18 |
from: 'settings', |
| 19 |
read: (settings) => |
| 20 |
settings.show_on_front === 'page' |
| 21 |
? { shows: 'page', pageId: settings.page_on_front } |
| 22 |
: { shows: 'posts' }, |
| 23 |
}, |
| 24 |
theme: { |
| 25 |
from: 'themes', |
| 26 |
read: (themes) => decodeEntities(themes[0]?.name?.rendered ?? ''), |
| 27 |
}, |
| 28 |
plugins: { |
| 29 |
from: 'plugins', |
| 30 |
read: (plugins) => |
| 31 |
plugins.map(({ name, status }) => ({ |
| 32 |
name: decodeEntities(name), |
| 33 |
status, |
| 34 |
})), |
| 35 |
}, |
| 36 |
wpVersion: { read: () => window.extSharedData.wpVersion }, |
| 37 |
}; |
| 38 |
|
| 39 |
export const siteInfo = { |
| 40 |
name: 'extendify/site-info', |
| 41 |
label: __('Site details', 'extendify-local'), |
| 42 |
description: |
| 43 |
'What this WordPress site is: its title, tagline, address, language, timezone, active theme, installed plugins, WordPress version, and what its front page shows.', |
| 44 |
inputSchema: { |
| 45 |
type: 'object', |
| 46 |
properties: { |
| 47 |
include: { |
| 48 |
type: 'array', |
| 49 |
items: { type: 'string', enum: Object.keys(facts) }, |
| 50 |
description: 'Which of these to return. Omit it for all of them.', |
| 51 |
}, |
| 52 |
}, |
| 53 |
}, |
| 54 |
annotations: { readonly: true }, |
| 55 |
execute: async ({ include } = {}) => { |
| 56 |
const wanted = include?.length |
| 57 |
? include.filter((key) => facts[key]) |
| 58 |
: Object.keys(facts); |
| 59 |
const sources = [ |
| 60 |
...new Set(wanted.map((key) => facts[key].from).filter(Boolean)), |
| 61 |
]; |
| 62 |
const read = Object.fromEntries( |
| 63 |
await Promise.all( |
| 64 |
sources.map(async (source) => [ |
| 65 |
source, |
| 66 |
await apiFetch({ path: paths[source] }), |
| 67 |
]), |
| 68 |
), |
| 69 |
); |
| 70 |
|
| 71 |
return Object.fromEntries( |
| 72 |
wanted.map((key) => [key, facts[key].read(read[facts[key].from])]), |
| 73 |
); |
| 74 |
}, |
| 75 |
}; |
| 76 |
|