File "update-block-content.js"

Full Path: /home/fresvfqn/waterdamagerestorationandrepairsmithtown.com/wp-content/plugins/extendify/src/Agent/workflows/dom-highlighter/tools/update-block-content.js
File size: 639 bytes
MIME-type: text/x-java
Charset: utf-8

import apiFetch from '@wordpress/api-fetch';

// TODO: check for post_lock and error that someone is editing
export default async ({ previousContent, newContent }) => {
	const { postType, postId } = window.extAgentData.context;

	const type = await apiFetch({
		path: `/wp/v2/types/${encodeURIComponent(postType)}`,
	});
	const base = type?.rest_base || `${postType}s`;

	const response = await apiFetch({
		path: `/wp/v2/${base}/${postId}?context=edit`,
	});

	const content = response.content.raw.replace(previousContent, newContent);

	await apiFetch({
		path: `/wp/v2/${base}/${postId}`,
		method: 'POST',
		data: { content },
	});
};