| @@ -25,9 +25,9 @@ | ||
| 25 | 25 | var originalCursor = btn.style.cursor; |
| 26 | 26 | |
| 27 | 27 | btn.disabled = true; |
| 28 | 28 | btn.style.cursor = "wait"; |
| 29 | - btn.innerHTML = SUBSCRPT_SPINNER_SVG + "Installing…"; | |
| 29 | + btn.innerHTML = SUBSCRPT_SPINNER_SVG + (btn.dataset.busyLabel || "Installing…"); | |
| 30 | 30 | |
| 31 | 31 | var data = new FormData(); |
| 32 | 32 | data.append("action", "subscrpt_install_integration_plugin"); |
| 33 | 33 | data.append("plugin_slug", slug); |
| @@ -55,8 +55,68 @@ | ||
| 55 | 55 | btn.style.cursor = originalCursor; |
| 56 | 56 | btn.innerHTML = originalHTML; |
| 57 | 57 | // eslint-disable-next-line no-alert |
| 58 | 58 | window.alert("Installation failed. Please try again."); |
| 59 | + }); | |
| 60 | +} | |
| 61 | + | |
| 62 | +/** | |
| 63 | + * Links and buttons that cannot carry an inline handler — e.g. inside a settings field | |
| 64 | + * description, which is passed through wp_kses_post() — declare the slug in | |
| 65 | + * data-subscrpt-install-plugin instead. | |
| 66 | + */ | |
| 67 | +document.addEventListener("click", function (event) { | |
| 68 | + var btn = event.target.closest("[data-subscrpt-install-plugin]"); | |
| 69 | + if (!btn || btn.disabled) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + event.preventDefault(); | |
| 73 | + subscrptInstallPlugin(btn, btn.dataset.subscrptInstallPlugin); | |
| 74 | +}); | |
| 75 | + | |
| 76 | +/** | |
| 77 | + * Activate an already installed integration plugin from the integrations page. | |
| 78 | + * | |
| 79 | + * @param {HTMLButtonElement} btn The clicked button element. | |
| 80 | + * @param {string} pluginFile The plugin basename, e.g. "tutor/tutor.php". | |
| 81 | + */ | |
| 82 | +function subscrptActivatePlugin(btn, pluginFile) { | |
| 83 | + var originalHTML = btn.innerHTML; | |
| 84 | + var originalCursor = btn.style.cursor; | |
| 85 | + | |
| 86 | + btn.disabled = true; | |
| 87 | + btn.style.cursor = "wait"; | |
| 88 | + btn.innerHTML = SUBSCRPT_SPINNER_SVG + "Activating…"; | |
| 89 | + | |
| 90 | + var data = new FormData(); | |
| 91 | + data.append("action", "subscrpt_activate_integration_plugin"); | |
| 92 | + data.append("plugin_file", pluginFile); | |
| 93 | + data.append("nonce", subscrptIntegrations.nonce); | |
| 94 | + | |
| 95 | + fetch(subscrptIntegrations.ajaxUrl, { method: "POST", body: data }) | |
| 96 | + .then(function (response) { | |
| 97 | + return response.json(); | |
| 98 | + }) | |
| 99 | + .then(function (result) { | |
| 100 | + if (result.success) { | |
| 101 | + var url = new URL(window.location.href); | |
| 102 | + url.searchParams.delete("subscrpt_installed"); | |
| 103 | + url.searchParams.set("subscrpt_activated", "1"); | |
| 104 | + window.location.href = url.toString(); | |
| 105 | + } else { | |
| 106 | + btn.disabled = false; | |
| 107 | + btn.style.cursor = originalCursor; | |
| 108 | + btn.innerHTML = originalHTML; | |
| 109 | + // eslint-disable-next-line no-alert | |
| 110 | + window.alert((result.data && result.data.message) || "Activation failed. Please try again."); | |
| 111 | + } | |
| 112 | + }) | |
| 113 | + .catch(function () { | |
| 114 | + btn.disabled = false; | |
| 115 | + btn.style.cursor = originalCursor; | |
| 116 | + btn.innerHTML = originalHTML; | |
| 117 | + // eslint-disable-next-line no-alert | |
| 118 | + window.alert("Activation failed. Please try again."); | |
| 59 | 119 | }); |
| 60 | 120 | } |
| 61 | 121 | |
| 62 | 122 | function wpSubsInstallPaypalIntegration() { |