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(`
Enter a license key

To receive updates, please enter your valid ${data.name} license key

By activating ${data.name}, you'll have:
`); } function showMessage(data) { const { slug, success, message } = data; jQuery(`#${slug}_license_card .yaycommerce-license-message`).addClass("show"); if (!success) { jQuery(`#${slug}_license_card .yaycommerce-license-message`).html(` ${message} `); } } function hideMessage(slug) { jQuery(`#${slug}_license_card .yaycommerce-license-message`).removeClass( "show" ); jQuery(`#${slug}_license_card .yaycommerce-license-message`).html(""); } function beforeCallAPI(plugin, action) { if (action === "activate") { jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`) .find(".activate-loading") .css("display", "inline-flex"); } if (action === "update") { jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`) .find(".activate-loading") .css("display", "inline-flex"); } if (action === "remove") { jQuery(`.yaycommerce-remove-license[data-plugin=${plugin}]`) .find(".activate-loading") .css("display", "inline-flex"); } jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`).attr( "disabled", true ); jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`).attr( "disabled", true ); jQuery(`.yaycommerce-remove-license[data-plugin="${plugin}"]`).attr( "disabled", true ); } function afterCallAPI(plugin, action) { if (action === "activate") { jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`) .find(".activate-loading") .hide(); } if (action === "update") { jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`) .find(".activate-loading") .hide(); } if (action === "remove") { jQuery(`.yaycommerce-remove-license[data-plugin=${plugin}]`) .find(".activate-loading") .hide(); } jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`).attr( "disabled", false ); jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`).attr( "disabled", false ); jQuery(`.yaycommerce-remove-license[data-plugin="${plugin}"]`).attr( "disabled", false ); } });