| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 3 |
|
| 4 |
// Root-relative, like the menu links a launched site starts with. |
| 5 |
const pathOf = (link) => { |
| 6 |
const { pathname, search } = new URL(link); |
| 7 |
return pathname + search; |
| 8 |
}; |
| 9 |
|
| 10 |
export const fetchLinkablePages = () => |
| 11 |
apiFetch({ |
| 12 |
path: '/wp/v2/pages?status=publish&per_page=100&_fields=id,title,link', |
| 13 |
}) |
| 14 |
.then((pages) => |
| 15 |
pages.map(({ id, title, link }) => ({ |
| 16 |
id, |
| 17 |
title: decodeEntities(title?.rendered ?? ''), |
| 18 |
url: pathOf(link), |
| 19 |
link, |
| 20 |
})), |
| 21 |
) |
| 22 |
.catch(() => []); |
| 23 |
|