Choose an Elementor event template during or after migration
All checks were successful
WordPress release / release (push) Successful in 7s

This commit is contained in:
josh
2026-11-12 15:34:43 -05:00
parent 6df287d530
commit db4747d85f
8 changed files with 242 additions and 47 deletions

View File

@@ -5,14 +5,44 @@
const endpoint = form.getAttribute('action');
const {__, sprintf} = wp.i18n;
const button = form.querySelector('button[type="submit"]');
const buttons = Array.from(form.querySelectorAll('[data-be-operation]'));
const template = form.querySelector('[name="template_id"]');
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;
const messages = {
copy: {
count: 'copied',
starting: __('Copying events…', 'burn-events'),
progress: __('Copying events. %1$d copied, %2$d already copied, %3$d need review.', 'burn-events'),
finished: __('Migration finished. %1$d copied, %2$d already copied, %3$d need review.', 'burn-events'),
paused: __('Migration paused. %1$d copied, %2$d already copied. %3$s', 'burn-events'),
},
apply: {
count: 'applied',
starting: __('Applying the template…', 'burn-events'),
progress: __('Applying the template. %1$d applied, %2$d already assigned, %3$d need review.', 'burn-events'),
finished: __('Template assignment finished. %1$d applied, %2$d already assigned, %3$d need review.', 'burn-events'),
paused: __('Template assignment paused. %1$d applied, %2$d already assigned. %3$s', 'burn-events'),
},
};
let operation = '';
let templateId = '0';
let hasCopies = form.dataset.hasCopies === '1';
let processed = 0;
let skipped = 0;
let after = 0;
button.disabled = false;
function updateControls(busy) {
if (template) template.disabled = busy;
for (const button of buttons) {
const canApply = hasCopies && template && template.value !== '0';
button.disabled = busy || (button.value === 'apply' && !canApply);
}
}
template?.addEventListener('change', () => updateControls(false));
updateControls(false);
async function requestBatch(body) {
for (let attempt = 0; ; attempt++) {
@@ -29,16 +59,16 @@
if (requestedDelay > 10000) return response;
if (requestedDelay > delay) delay = requestedDelay;
}
status.textContent = __('Waiting to retry the last migration batch…', 'burn-events');
status.textContent = __('Waiting to retry the last batch…', 'burn-events');
await new Promise(resolve => setTimeout(resolve, delay));
}
}
async function readBatch(response) {
async function readBatch(response, count) {
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];
const counts = [batch?.[count], 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;
}
@@ -55,39 +85,41 @@
form.addEventListener('submit', async event => {
event.preventDefault();
if (button.disabled) return;
button.disabled = true;
if (after === 0) {
copied = 0;
const button = event.submitter ?? buttons.find(control => !control.disabled);
if (!button || button.disabled) return;
const selectedTemplate = template?.value ?? '0';
if (after === 0 || operation !== button.value || templateId !== selectedTemplate) {
after = 0;
processed = 0;
skipped = 0;
errors.replaceChildren();
}
status.textContent = __('Copying events…', 'burn-events');
operation = button.value;
templateId = selectedTemplate;
const labels = messages[operation];
const body = new URLSearchParams(new FormData(form));
body.set('operation', operation);
updateControls(true);
status.textContent = labels.starting;
try {
do {
body.set('after', String(after));
const batch = await readBatch(await requestBatch(body));
copied += batch.copied;
const batch = await readBatch(await requestBatch(body), labels.count);
processed += batch[labels.count];
skipped += batch.skipped;
if (operation === 'copy' && (processed || skipped)) hasCopies = true;
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
);
status.textContent = sprintf(after ? labels.progress : labels.finished, processed, 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
);
status.textContent = sprintf(labels.paused, processed, skipped, error.message);
} finally {
button.disabled = false;
updateControls(false);
}
});
})();