jQuery(document).ready(function () { const slug = "yaymail"; const pluginSettingPage = "yaymail-settings"; const REST_URL = window[`${slug}LicenseData`].apiSettings.restUrl; const ADMIN_URL = window[`${slug}LicenseData`].apiSettings.adminUrl; const POST_OPTIONS = { method: "POST", headers: { "Content-type": "application/json", }, }; jQuery(".yaycommerce-license-layout").on( "click", `.yaycommerce-activate-license-button[data-plugin*='${slug}']`, handleActivate ); jQuery(".yaycommerce-license-layout").on( "click", `.yaycommerce-update-license[data-plugin*='${slug}']`, handleUpdate ); jQuery(".yaycommerce-license-layout").on( "click", `.yaycommerce-remove-license[data-plugin*='${slug}']`, handleRemove ); let timeout; async function handleActivate(event) { event.preventDefault(); clearTimeout(timeout); const { plugin } = jQuery(this).data(); beforeCallAPI(plugin, "activate"); hideMessage(plugin); const licenseKey = jQuery(`#${plugin}_license_input`).val(); const response = await fetch(`${REST_URL}/license/activate`, { ...POST_OPTIONS, body: JSON.stringify({ license_key: licenseKey, plugin, }), }).then((response) => response.json()); afterCallAPI(plugin, "activate"); if (response.success) { replaceSuccessfullContent(response); } else { showMessage(response); timeout = setTimeout(()=>{ hideMessage(plugin) },3000); } } async function handleUpdate(event) { event.preventDefault(); const { plugin } = jQuery(this).data(); beforeCallAPI(plugin, "update"); const response = await fetch(`${REST_URL}/license/update`, { ...POST_OPTIONS, body: JSON.stringify({ plugin, }), }).then((response) => response.json()); afterCallAPI(plugin, "update"); if (response.success) { replaceSuccessfullContent(response); } else { replaceActivatorContent(response); } } async function handleRemove(event) { event.preventDefault(); const { plugin } = jQuery(this).data(); beforeCallAPI(plugin, "remove"); const response = await fetch(`${REST_URL}/license/delete`, { ...POST_OPTIONS, body: JSON.stringify({ plugin, }), }).then((response) => response.json()); afterCallAPI(plugin, "remove"); replaceActivatorContent(response); } function replaceSuccessfullContent(data) { jQuery( `#${data.slug}_license_card .yaycommerce-license-card-header-item` ).html(` ${data.name} ${data.is_expired ? "Expired" : "Active"} `); jQuery(`#${data.slug}_license_card .yaycommerce-license-card-body`).html(`
| Your License Key: | ||
| Expiration Date: | ${data.expires} ${ data.is_expired ? `(Expired)` : "" } |
${ !data.is_expired ? `Start customizing` : "" }
Need more licenses? Buy Now
${ data.is_expired ? `Your license is expired! Renew Now!
` : "" } `); } function replaceActivatorContent(data) { jQuery(`#${data.slug}_license_card .yaycommerce-license-card-header-item`).html( `${data.name} activation` ); jQuery(`#${data.slug}_license_card .yaycommerce-license-card-body`).html(`To receive updates, please enter your valid ${data.name} license key