/* global subscrptIntegrations */ var SUBSCRPT_SPINNER_SVG = '' + '' + '' + ""; (function () { var style = document.createElement("style"); style.textContent = "@keyframes subscrptSpin{to{transform:rotate(360deg)}}"; document.head.appendChild(style); })(); /** * One-click install and activate a payment gateway plugin from the integrations page. * * @param {HTMLButtonElement} btn The clicked button element. * @param {string} slug The wp.org plugin slug. */ function subscrptInstallPlugin(btn, slug) { var originalHTML = btn.innerHTML; var originalCursor = btn.style.cursor; btn.disabled = true; btn.style.cursor = "wait"; btn.innerHTML = SUBSCRPT_SPINNER_SVG + (btn.dataset.busyLabel || "Installing…"); var data = new FormData(); data.append("action", "subscrpt_install_integration_plugin"); data.append("plugin_slug", slug); data.append("nonce", subscrptIntegrations.nonce); fetch(subscrptIntegrations.ajaxUrl, { method: "POST", body: data }) .then(function (response) { return response.json(); }) .then(function (result) { if (result.success) { var url = new URL(window.location.href); url.searchParams.set("subscrpt_installed", "1"); window.location.href = url.toString(); } else { btn.disabled = false; btn.style.cursor = originalCursor; btn.innerHTML = originalHTML; // eslint-disable-next-line no-alert window.alert((result.data && result.data.message) || "Installation failed. Please try again."); } }) .catch(function () { btn.disabled = false; btn.style.cursor = originalCursor; btn.innerHTML = originalHTML; // eslint-disable-next-line no-alert window.alert("Installation failed. Please try again."); }); } /** * Links and buttons that cannot carry an inline handler — e.g. inside a settings field * description, which is passed through wp_kses_post() — declare the slug in * data-subscrpt-install-plugin instead. */ document.addEventListener("click", function (event) { var btn = event.target.closest("[data-subscrpt-install-plugin]"); if (!btn || btn.disabled) { return; } event.preventDefault(); subscrptInstallPlugin(btn, btn.dataset.subscrptInstallPlugin); }); /** * Activate an already installed integration plugin from the integrations page. * * @param {HTMLButtonElement} btn The clicked button element. * @param {string} pluginFile The plugin basename, e.g. "tutor/tutor.php". */ function subscrptActivatePlugin(btn, pluginFile) { var originalHTML = btn.innerHTML; var originalCursor = btn.style.cursor; btn.disabled = true; btn.style.cursor = "wait"; btn.innerHTML = SUBSCRPT_SPINNER_SVG + "Activating…"; var data = new FormData(); data.append("action", "subscrpt_activate_integration_plugin"); data.append("plugin_file", pluginFile); data.append("nonce", subscrptIntegrations.nonce); fetch(subscrptIntegrations.ajaxUrl, { method: "POST", body: data }) .then(function (response) { return response.json(); }) .then(function (result) { if (result.success) { var url = new URL(window.location.href); url.searchParams.delete("subscrpt_installed"); url.searchParams.set("subscrpt_activated", "1"); window.location.href = url.toString(); } else { btn.disabled = false; btn.style.cursor = originalCursor; btn.innerHTML = originalHTML; // eslint-disable-next-line no-alert window.alert((result.data && result.data.message) || "Activation failed. Please try again."); } }) .catch(function () { btn.disabled = false; btn.style.cursor = originalCursor; btn.innerHTML = originalHTML; // eslint-disable-next-line no-alert window.alert("Activation failed. Please try again."); }); } function wpSubsInstallPaypalIntegration() { jQuery .post(wpSubsIntegrations.ajax_url, { action_callback: "wp_subs_install_paypal_integration", nonce: wpSubsIntegrations.nonce, }) .done(function (response) { console.log(response); if (response.success) { console.log("Option updated:", response.data); } else { console.error("Failed to update:", response.data); } }) .fail(function () { console.error("AJAX request failed"); }); }