| 1 |
/* global subscrptIntegrations */ |
| 2 |
|
| 3 |
var SUBSCRPT_SPINNER_SVG = |
| 4 |
'<svg xmlns="http://www.w3.org/2000/svg"' + |
| 5 |
' style="animation:subscrptSpin 0.75s linear infinite;vertical-align:-3px;margin-right:5px;display:inline-block;"' + |
| 6 |
' width="13" height="13" viewBox="0 0 13 13" fill="none">' + |
| 7 |
'<circle cx="6.5" cy="6.5" r="5" stroke="currentColor" stroke-opacity="0.25" stroke-width="2"/>' + |
| 8 |
'<path d="M6.5 1.5A5 5 0 0 1 11.5 6.5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>' + |
| 9 |
"</svg>"; |
| 10 |
|
| 11 |
(function () { |
| 12 |
var style = document.createElement("style"); |
| 13 |
style.textContent = "@keyframes subscrptSpin{to{transform:rotate(360deg)}}"; |
| 14 |
document.head.appendChild(style); |
| 15 |
})(); |
| 16 |
|
| 17 |
/** |
| 18 |
* One-click install and activate a payment gateway plugin from the integrations page. |
| 19 |
* |
| 20 |
* @param {HTMLButtonElement} btn The clicked button element. |
| 21 |
* @param {string} slug The wp.org plugin slug. |
| 22 |
*/ |
| 23 |
function subscrptInstallPlugin(btn, slug) { |
| 24 |
var originalHTML = btn.innerHTML; |
| 25 |
var originalCursor = btn.style.cursor; |
| 26 |
|
| 27 |
btn.disabled = true; |
| 28 |
btn.style.cursor = "wait"; |
| 29 |
btn.innerHTML = SUBSCRPT_SPINNER_SVG + (btn.dataset.busyLabel || "Installing…"); |
| 30 |
|
| 31 |
var data = new FormData(); |
| 32 |
data.append("action", "subscrpt_install_integration_plugin"); |
| 33 |
data.append("plugin_slug", slug); |
| 34 |
data.append("nonce", subscrptIntegrations.nonce); |
| 35 |
|
| 36 |
fetch(subscrptIntegrations.ajaxUrl, { method: "POST", body: data }) |
| 37 |
.then(function (response) { |
| 38 |
return response.json(); |
| 39 |
}) |
| 40 |
.then(function (result) { |
| 41 |
if (result.success) { |
| 42 |
var url = new URL(window.location.href); |
| 43 |
url.searchParams.set("subscrpt_installed", "1"); |
| 44 |
window.location.href = url.toString(); |
| 45 |
} else { |
| 46 |
btn.disabled = false; |
| 47 |
btn.style.cursor = originalCursor; |
| 48 |
btn.innerHTML = originalHTML; |
| 49 |
// eslint-disable-next-line no-alert |
| 50 |
window.alert((result.data && result.data.message) || "Installation failed. Please try again."); |
| 51 |
} |
| 52 |
}) |
| 53 |
.catch(function () { |
| 54 |
btn.disabled = false; |
| 55 |
btn.style.cursor = originalCursor; |
| 56 |
btn.innerHTML = originalHTML; |
| 57 |
// eslint-disable-next-line no-alert |
| 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."); |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
function wpSubsInstallPaypalIntegration() { |
| 123 |
jQuery |
| 124 |
.post(wpSubsIntegrations.ajax_url, { |
| 125 |
action_callback: "wp_subs_install_paypal_integration", |
| 126 |
nonce: wpSubsIntegrations.nonce, |
| 127 |
}) |
| 128 |
.done(function (response) { |
| 129 |
console.log(response); |
| 130 |
|
| 131 |
if (response.success) { |
| 132 |
console.log("Option updated:", response.data); |
| 133 |
} else { |
| 134 |
console.error("Failed to update:", response.data); |
| 135 |
} |
| 136 |
}) |
| 137 |
.fail(function () { |
| 138 |
console.error("AJAX request failed"); |
| 139 |
}); |
| 140 |
} |
| 141 |
|