| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
const postTypes = { page: 'pages', post: 'posts' }; |
| 4 |
|
| 5 |
export default async ({ postId, postType }) => { |
| 6 |
// Get all nav menus that are presented in the post/page. |
| 7 |
const postNavigations = Array.from( |
| 8 |
window.document?.querySelectorAll('nav[data-extendify-menu-id]') ?? [], |
| 9 |
) |
| 10 |
.map((nav) => nav.dataset.extendifyMenuId) |
| 11 |
.filter(Boolean); |
| 12 |
|
| 13 |
// get the data about those nav menus |
| 14 |
const navigations = |
| 15 |
(await apiFetch({ |
| 16 |
method: 'POST', |
| 17 |
path: '/extendify/v1/agent/site-navigation', |
| 18 |
data: { only: postNavigations.join(',') }, |
| 19 |
})) ?? []; |
| 20 |
|
| 21 |
const postInfo = await apiFetch({ |
| 22 |
path: `/wp/v2/${postTypes[postType]}/${postId}`, |
| 23 |
}); |
| 24 |
|
| 25 |
return { |
| 26 |
navigations, |
| 27 |
postTitle: postInfo.title.rendered, |
| 28 |
postURL: postInfo.link, |
| 29 |
}; |
| 30 |
}; |
| 31 |
|