| 1 |
const data = () => window.extQuickEditData || {}; |
| 2 |
|
| 3 |
export const post = async (path, body) => { |
| 4 |
const { restRoot, nonce } = data(); |
| 5 |
const res = await fetch(`${restRoot}${path}`, { |
| 6 |
method: 'POST', |
| 7 |
credentials: 'same-origin', |
| 8 |
headers: { |
| 9 |
'Content-Type': 'application/json', |
| 10 |
'X-WP-Nonce': nonce, |
| 11 |
}, |
| 12 |
body: JSON.stringify(body), |
| 13 |
}); |
| 14 |
const json = await res.json().catch(() => ({})); |
| 15 |
if (!res.ok) { |
| 16 |
const err = new Error(json?.message || json?.error || `HTTP ${res.status}`); |
| 17 |
err.status = res.status; |
| 18 |
err.body = json; |
| 19 |
throw err; |
| 20 |
} |
| 21 |
return json; |
| 22 |
}; |
| 23 |
|
| 24 |
// The REST save request isn't language-scoped the way the page render is, so |
| 25 |
// forward the context detected at enqueue. The server refuses text-bearing |
| 26 |
// saves when it says translated — failing the corruption path closed even when |
| 27 |
// no fingerprint is sent — and ignores it for image / non-text saves. |
| 28 |
export const save = (payload) => |
| 29 |
post('/quick-edit/save', { |
| 30 |
...payload, |
| 31 |
translatedContext: data().translatedContext ?? null, |
| 32 |
}); |
| 33 |
|
| 34 |
export const get = async (path) => { |
| 35 |
const { restRoot, nonce } = data(); |
| 36 |
const res = await fetch(`${restRoot}${path}`, { |
| 37 |
method: 'GET', |
| 38 |
credentials: 'same-origin', |
| 39 |
headers: { 'X-WP-Nonce': nonce }, |
| 40 |
}); |
| 41 |
const json = await res.json().catch(() => ({})); |
| 42 |
if (!res.ok) { |
| 43 |
const err = new Error(json?.message || json?.error || `HTTP ${res.status}`); |
| 44 |
err.status = res.status; |
| 45 |
err.body = json; |
| 46 |
throw err; |
| 47 |
} |
| 48 |
return json; |
| 49 |
}; |
| 50 |
|
| 51 |
// Identity (title/tagline/logo) lives in wp_options, not post_content, |
| 52 |
// so it bypasses save(). |
| 53 |
export const saveSiteIdentity = (payload) => |
| 54 |
post('/quick-edit/site-identity', payload); |
| 55 |
|
| 56 |
export const loadSiteIdentity = () => get('/quick-edit/site-identity'); |
| 57 |
|
| 58 |
export const loadProduct = (productId) => |
| 59 |
get(`/quick-edit/product?product_id=${encodeURIComponent(productId)}`); |
| 60 |
|
| 61 |
export const saveProduct = ({ productId, field, value }) => |
| 62 |
post('/quick-edit/product', { product_id: productId, field, value }); |
| 63 |
|
| 64 |
// Ref-based nav items live in a separate wp_navigation CPT, out of reach |
| 65 |
// of /quick-edit/save's findBlock walk. |
| 66 |
export const saveWpNavigationItem = ({ |
| 67 |
navPostId, |
| 68 |
itemIndex, |
| 69 |
blockType, |
| 70 |
patches, |
| 71 |
fingerprint, |
| 72 |
}) => |
| 73 |
post('/quick-edit/wp-navigation', { |
| 74 |
navPostId, |
| 75 |
itemIndex, |
| 76 |
blockType, |
| 77 |
patches, |
| 78 |
fingerprint, |
| 79 |
}); |
| 80 |
|
| 81 |
export const loadWpFormsField = ({ formId, fieldId }) => |
| 82 |
get( |
| 83 |
`/quick-edit/wpforms?form_id=${encodeURIComponent(formId)}` + |
| 84 |
`&field_id=${encodeURIComponent(fieldId)}`, |
| 85 |
); |
| 86 |
|
| 87 |
export const saveWpFormsField = ({ formId, fieldId, changes }) => |
| 88 |
post('/quick-edit/wpforms', { |
| 89 |
form_id: formId, |
| 90 |
field_id: fieldId, |
| 91 |
changes, |
| 92 |
}); |
| 93 |
|