| 1 |
/** |
| 2 |
* License activate/update/remove via REST API. |
| 3 |
* Slug-agnostic: discovers all window[`${slug}LicenseData`] objects on the page |
| 4 |
* so one script handles all YayCommerce plugins. |
| 5 |
* |
| 6 |
* Each plugin's LicenseHandler::enqueue_license_scripts() localizes: |
| 7 |
* window[`${slug}LicenseData`] = { slug, apiSettings: { restUrl, restNonce } } |
| 8 |
*/ |
| 9 |
jQuery(document).ready(function () { |
| 10 |
// Discover all registered plugin license data objects |
| 11 |
var dataKeys = Object.keys(window).filter(function (k) { |
| 12 |
return k.endsWith('LicenseData') && window[k] && window[k].apiSettings; |
| 13 |
}); |
| 14 |
|
| 15 |
dataKeys.forEach(function (dataKey) { |
| 16 |
initLicenseUI(window[dataKey]); |
| 17 |
}); |
| 18 |
|
| 19 |
function initLicenseUI(data) { |
| 20 |
var slug = data.slug || data.apiSettings.slug || ''; |
| 21 |
if (!slug) return; |
| 22 |
|
| 23 |
var REST_URL = data.apiSettings.restUrl; |
| 24 |
var REST_NONCE = data.apiSettings.restNonce; |
| 25 |
var POST_OPTIONS = { |
| 26 |
method: 'POST', |
| 27 |
headers: { |
| 28 |
'Content-type': 'application/json', |
| 29 |
'x-wp-nonce': REST_NONCE, |
| 30 |
}, |
| 31 |
}; |
| 32 |
|
| 33 |
var escapedSlug = CSS.escape(slug); |
| 34 |
jQuery('.yaycommerce-license-layout').on( |
| 35 |
'click', |
| 36 |
".yaycommerce-activate-license-button[data-plugin='" + escapedSlug + "']", |
| 37 |
handleActivate |
| 38 |
); |
| 39 |
jQuery('.yaycommerce-license-layout').on( |
| 40 |
'click', |
| 41 |
".yaycommerce-update-license[data-plugin='" + escapedSlug + "']", |
| 42 |
handleUpdate |
| 43 |
); |
| 44 |
jQuery('.yaycommerce-license-layout').on( |
| 45 |
'click', |
| 46 |
".yaycommerce-remove-license[data-plugin='" + escapedSlug + "']", |
| 47 |
handleRemove |
| 48 |
); |
| 49 |
jQuery('.yaycommerce-license-layout').on( |
| 50 |
'click', |
| 51 |
'#' + jQuery.escapeSelector(slug) + '_license_card .yaycommerce-license-message .yaycommerce-license-message__close', |
| 52 |
function () { |
| 53 |
hideMessage(slug); |
| 54 |
} |
| 55 |
); |
| 56 |
|
| 57 |
function handleActivate(event) { |
| 58 |
event.preventDefault(); |
| 59 |
clearMessages(); |
| 60 |
var plugin = jQuery(this).data('plugin'); |
| 61 |
beforeCallAPI(plugin, 'activate'); |
| 62 |
hideMessage(plugin); |
| 63 |
var licenseKey = jQuery('#' + jQuery.escapeSelector(plugin) + '_license_input').val(); |
| 64 |
|
| 65 |
fetch(REST_URL + '/license/activate', Object.assign({}, POST_OPTIONS, { |
| 66 |
body: JSON.stringify({ license_key: licenseKey, plugin: plugin }), |
| 67 |
})) |
| 68 |
.then(function (r) { return r.json(); }) |
| 69 |
.then(function (response) { |
| 70 |
afterCallAPI(plugin, 'activate'); |
| 71 |
if (response.success) { |
| 72 |
replaceContent(response); |
| 73 |
} |
| 74 |
showMessage(response, 'activate'); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
function handleUpdate(event) { |
| 79 |
event.preventDefault(); |
| 80 |
clearMessages(); |
| 81 |
var plugin = jQuery(this).data('plugin'); |
| 82 |
beforeCallAPI(plugin, 'update'); |
| 83 |
|
| 84 |
fetch(REST_URL + '/license/update', Object.assign({}, POST_OPTIONS, { |
| 85 |
body: JSON.stringify({ plugin: plugin }), |
| 86 |
})) |
| 87 |
.then(function (r) { return r.json(); }) |
| 88 |
.then(function (response) { |
| 89 |
afterCallAPI(plugin, 'update'); |
| 90 |
replaceContent(response); |
| 91 |
showMessage(response, 'update'); |
| 92 |
}); |
| 93 |
} |
| 94 |
|
| 95 |
function handleRemove(event) { |
| 96 |
event.preventDefault(); |
| 97 |
clearMessages(); |
| 98 |
var plugin = jQuery(this).data('plugin'); |
| 99 |
beforeCallAPI(plugin, 'remove'); |
| 100 |
|
| 101 |
fetch(REST_URL + '/license/delete', Object.assign({}, POST_OPTIONS, { |
| 102 |
body: JSON.stringify({ plugin: plugin }), |
| 103 |
})) |
| 104 |
.then(function (r) { return r.json(); }) |
| 105 |
.then(function (response) { |
| 106 |
afterCallAPI(plugin, 'remove'); |
| 107 |
replaceContent(response); |
| 108 |
showMessage({ success: true }, 'remove'); |
| 109 |
}); |
| 110 |
} |
| 111 |
|
| 112 |
function replaceContent(data) { |
| 113 |
jQuery('#' + jQuery.escapeSelector(data.slug) + '_license_card').replaceWith(data.html); |
| 114 |
updateImportantNotice(); |
| 115 |
} |
| 116 |
|
| 117 |
function updateImportantNotice() { |
| 118 |
// If any activate card remains on the page → still has inactive licenses |
| 119 |
var hasInactive = jQuery('.yaycommerce-license-settings .yaycommerce-activate-license-button').length > 0; |
| 120 |
var $notice = jQuery('.yaycommerce-license__important-notice'); |
| 121 |
if (hasInactive) { |
| 122 |
$notice.attr('data-display', 'true').show(); |
| 123 |
} else { |
| 124 |
$notice.attr('data-display', 'false').hide(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
function hideMessage(s) { |
| 129 |
jQuery('#' + jQuery.escapeSelector(s) + '_license_card .yaycommerce-license-message') |
| 130 |
.removeClass('show') |
| 131 |
.html(''); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
function clearMessages() { |
| 136 |
jQuery('.message').removeClass('active'); |
| 137 |
} |
| 138 |
|
| 139 |
function showMessage(data, action) { |
| 140 |
var id = 'message-' + action + '-' + (data.success ? 'success' : 'error'); |
| 141 |
var el = document.getElementById(id); |
| 142 |
if (el) { |
| 143 |
el.classList.add('active'); |
| 144 |
setTimeout(function () { el.classList.remove('active'); }, 2000); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
function beforeCallAPI(plugin, action) { |
| 149 |
var sel = "[data-plugin='" + CSS.escape(plugin) + "']"; |
| 150 |
var btnClass = { |
| 151 |
activate: '.yaycommerce-activate-license-button', |
| 152 |
update: '.yaycommerce-update-license', |
| 153 |
remove: '.yaycommerce-remove-license' |
| 154 |
}; |
| 155 |
// Show spinner only on the clicked button |
| 156 |
if (btnClass[action]) { |
| 157 |
jQuery(btnClass[action] + sel + ' .activate-loading').css('display', 'inline-flex'); |
| 158 |
} |
| 159 |
// Disable all buttons for this plugin |
| 160 |
jQuery('.yaycommerce-activate-license-button' + sel).attr('disabled', true); |
| 161 |
jQuery('.yaycommerce-update-license' + sel).attr('disabled', true); |
| 162 |
jQuery('.yaycommerce-remove-license' + sel).attr('disabled', true); |
| 163 |
} |
| 164 |
|
| 165 |
function afterCallAPI(plugin, action) { |
| 166 |
var sel = "[data-plugin='" + CSS.escape(plugin) + "']"; |
| 167 |
// Hide all spinners |
| 168 |
jQuery('.yaycommerce-activate-license-button' + sel + ' .activate-loading').hide(); |
| 169 |
jQuery('.yaycommerce-update-license' + sel + ' .activate-loading').hide(); |
| 170 |
jQuery('.yaycommerce-remove-license' + sel + ' .activate-loading').hide(); |
| 171 |
// Re-enable all buttons |
| 172 |
jQuery('.yaycommerce-activate-license-button' + sel).attr('disabled', false); |
| 173 |
jQuery('.yaycommerce-update-license' + sel).attr('disabled', false); |
| 174 |
jQuery('.yaycommerce-remove-license' + sel).attr('disabled', false); |
| 175 |
} |
| 176 |
}); |
| 177 |
|