document.addEventListener("DOMContentLoaded", () => { const { activating, installing, done, activationUrl, ajaxUrl, pluginSlug, nonce, pluginStatus } = window.ariaFse; const notice = document.querySelector(".ariafse-welcome-notice"); const dismissButton = notice?.querySelector(".notice-dismiss"); const installButton = notice?.querySelector("#ariafse-install-ablocks"); const buttonText = installButton?.querySelector(".text"); const removeNotice = () => { notice?.remove(); }; const activatePlugin = async () => { if (!buttonText) return; buttonText.textContent = activating; const response = await fetch(activationUrl); if (response.ok) { installButton.classList.remove("updating-message"); installButton.classList.add("updated-message"); buttonText.textContent = done; setTimeout(removeNotice, 2000); } else { console.error("Activation failed"); } }; const installPlugin = async () => { return new Promise((resolve) => { wp.updates.ajax("install-plugin", { slug: pluginSlug, success: () => resolve({ success: true }), error: (err) => resolve({ success: false, code: err.errorCode }), }); }); }; if (installButton) { installButton.addEventListener("click", async () => { installButton.classList.add("updating-message", "disabled"); if (pluginStatus !== "installed") { buttonText.textContent = installing; const result = await installPlugin(); if (!result.success) { console.error("Installation failed"); return; } } await activatePlugin(); }); } if (dismissButton) { dismissButton.addEventListener("click", async () => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", }, body: `action=ariafse_dismiss_welcome_notice&nonce=${nonce}`, }; try { const response = await fetch(ajaxUrl, requestOptions); if (response.ok) { removeNotice(); } } catch (err) { console.error("Dismiss failed", err); } }); } });