| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
// TODO: check for post_lock and error that someone is editing |
| 4 |
export default async ({ previousContent, newContent }) => { |
| 5 |
const { postType, postId } = window.extAgentData.context; |
| 6 |
|
| 7 |
const type = await apiFetch({ |
| 8 |
path: `/wp/v2/types/${encodeURIComponent(postType)}`, |
| 9 |
}); |
| 10 |
const base = type?.rest_base || `${postType}s`; |
| 11 |
|
| 12 |
const response = await apiFetch({ |
| 13 |
path: `/wp/v2/${base}/${postId}?context=edit`, |
| 14 |
}); |
| 15 |
|
| 16 |
const content = response.content.raw.replace(previousContent, newContent); |
| 17 |
|
| 18 |
await apiFetch({ |
| 19 |
path: `/wp/v2/${base}/${postId}`, |
| 20 |
method: 'POST', |
| 21 |
data: { content }, |
| 22 |
}); |
| 23 |
}; |
| 24 |
|