94 lines
3.8 KiB
JavaScript
94 lines
3.8 KiB
JavaScript
(() => {
|
||
const form = document.querySelector('[data-be-migrate]');
|
||
if (!form) return;
|
||
// The hidden WordPress action input shadows the form.action property.
|
||
const endpoint = form.getAttribute('action');
|
||
|
||
const {__, sprintf} = wp.i18n;
|
||
const button = form.querySelector('button[type="submit"]');
|
||
const status = form.querySelector('[data-be-migrate-status]');
|
||
const errors = form.querySelector('[data-be-migrate-errors]');
|
||
const retryableStatuses = new Set([408, 429, 502, 503, 504]);
|
||
let copied = 0;
|
||
let skipped = 0;
|
||
let after = 0;
|
||
button.disabled = false;
|
||
|
||
async function requestBatch(body) {
|
||
for (let attempt = 0; ; attempt++) {
|
||
const response = await fetch(endpoint, {
|
||
method: 'POST', body, credentials: 'same-origin', headers: {Accept: 'application/json'},
|
||
}).catch(() => null);
|
||
if (response && (attempt === 2 || !retryableStatuses.has(response.status))) return response;
|
||
if (attempt === 2) throw new Error(__('Could not reach WordPress. Try the migration again when the connection is available.', 'burn-events'));
|
||
|
||
const retryAfter = response?.headers.get('Retry-After');
|
||
let delay = (attempt + 1) * 1500;
|
||
if (retryAfter) {
|
||
const requestedDelay = /^\d+$/.test(retryAfter) ? Number(retryAfter) * 1000 : Date.parse(retryAfter) - Date.now();
|
||
if (requestedDelay > 10000) return response;
|
||
if (requestedDelay > delay) delay = requestedDelay;
|
||
}
|
||
status.textContent = __('Waiting to retry the last migration batch…', 'burn-events');
|
||
await new Promise(resolve => setTimeout(resolve, delay));
|
||
}
|
||
}
|
||
|
||
async function readBatch(response) {
|
||
const result = await response.json().catch(() => null);
|
||
if (response.ok && result?.success === true) {
|
||
const batch = result.data;
|
||
const counts = [batch?.copied, batch?.skipped, batch?.next];
|
||
if (counts.every(value => Number.isSafeInteger(value) && value >= 0)
|
||
&& (batch.next === 0 || batch.next > after) && Array.isArray(batch.errors)) return batch;
|
||
}
|
||
if (result?.success === false && typeof result.data === 'string' && result.data) throw new Error(result.data);
|
||
if (response.status === 401 || (response.redirected && new URL(response.url).pathname.endsWith('/wp-login.php'))) {
|
||
throw new Error(__('Your WordPress session has ended. Sign in again and reopen the migration page.', 'burn-events'));
|
||
}
|
||
throw new Error(sprintf(
|
||
response.ok ? __('WordPress returned an unexpected response (HTTP %d). Try the migration again.', 'burn-events')
|
||
: __('The migration request failed (HTTP %d). Try the migration again or check the host’s request log for this status.', 'burn-events'),
|
||
response.status
|
||
));
|
||
}
|
||
|
||
form.addEventListener('submit', async event => {
|
||
event.preventDefault();
|
||
if (button.disabled) return;
|
||
button.disabled = true;
|
||
if (after === 0) {
|
||
copied = 0;
|
||
skipped = 0;
|
||
errors.replaceChildren();
|
||
}
|
||
status.textContent = __('Copying events…', 'burn-events');
|
||
const body = new URLSearchParams(new FormData(form));
|
||
try {
|
||
do {
|
||
body.set('after', String(after));
|
||
const batch = await readBatch(await requestBatch(body));
|
||
copied += batch.copied;
|
||
skipped += batch.skipped;
|
||
for (const error of batch.errors) {
|
||
const item = document.createElement('li');
|
||
item.textContent = error;
|
||
errors.append(item);
|
||
}
|
||
after = batch.next;
|
||
status.textContent = sprintf(
|
||
after ? __('Copying events. %1$d copied, %2$d already copied, %3$d need review.', 'burn-events')
|
||
: __('Migration finished. %1$d copied, %2$d already copied, %3$d need review.', 'burn-events'),
|
||
copied, skipped, errors.childElementCount
|
||
);
|
||
} while (after);
|
||
} catch (error) {
|
||
status.textContent = sprintf(
|
||
__('Migration paused. %1$d copied, %2$d already copied. %3$s', 'burn-events'), copied, skipped, error.message
|
||
);
|
||
} finally {
|
||
button.disabled = false;
|
||
}
|
||
});
|
||
})();
|