| 1 |
/** |
| 2 |
* MxChat licence check — the "Check license now" button and the notice dismissal. |
| 3 |
* |
| 4 |
* Plan c93ede. Loaded on Pro & Extensions and on any admin screen where the |
| 5 |
* licence notice renders. One handler for every `.mxchat-license-check-now` |
| 6 |
* button: it asks the site to check with mxchat.ai right away, prints the |
| 7 |
* answer next to the button, and reloads the page when Pro switched on or off. |
| 8 |
*/ |
| 9 |
(function ($) { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
var cfg = window.MxChatLicenseCheck || {}; |
| 13 |
|
| 14 |
function messageTarget($button) { |
| 15 |
var $scope = $button.closest('.mxchat-license-check-notice, .mxch-license-check-panel, .mxch-license-footer-actions, .mxch-card-body'); |
| 16 |
var $msg = $scope.find('.mxchat-license-check-msg').first(); |
| 17 |
if (!$msg.length) { |
| 18 |
$msg = $('#mxchat-license-check-msg'); |
| 19 |
} |
| 20 |
return $msg; |
| 21 |
} |
| 22 |
|
| 23 |
$(document).on('click', '.mxchat-license-check-now', function () { |
| 24 |
var $button = $(this); |
| 25 |
if ($button.prop('disabled')) { |
| 26 |
return; |
| 27 |
} |
| 28 |
var $msg = messageTarget($button); |
| 29 |
var label = $button.text(); |
| 30 |
|
| 31 |
$button.prop('disabled', true).attr('aria-busy', 'true'); |
| 32 |
$msg.removeClass('is-error').text(cfg.checking || 'Checking…'); |
| 33 |
|
| 34 |
$.post(cfg.ajaxUrl || window.ajaxurl, { |
| 35 |
action: 'mxchat_license_check_now', |
| 36 |
nonce: cfg.nonce |
| 37 |
}).done(function (response) { |
| 38 |
if (!response || !response.success) { |
| 39 |
var detail = response && response.data && response.data.message ? response.data.message : (cfg.failed || 'The check could not run.'); |
| 40 |
$msg.addClass('is-error').text(detail); |
| 41 |
return; |
| 42 |
} |
| 43 |
var data = response.data || {}; |
| 44 |
if (data.message) { |
| 45 |
$msg.text(data.message); |
| 46 |
} |
| 47 |
// Pro & Extensions shows a status pill beside the sentence; keep it in step. |
| 48 |
var $pill = $('#mxchat-license-check-pill'); |
| 49 |
if ($pill.length && data.pillClass) { |
| 50 |
$pill.removeClass('mxch-status-pill-active mxch-status-pill-disabled mxch-status-pill-warning') |
| 51 |
.addClass(data.pillClass) |
| 52 |
.find('.mxchat-license-check-pill-text').text(data.pill || ''); |
| 53 |
} |
| 54 |
if (data.reload) { |
| 55 |
$msg.text(cfg.reloading || 'Reloading…'); |
| 56 |
window.setTimeout(function () { window.location.reload(); }, 1200); |
| 57 |
} |
| 58 |
}).fail(function () { |
| 59 |
$msg.addClass('is-error').text(cfg.failed || 'The check could not run.'); |
| 60 |
}).always(function () { |
| 61 |
$button.prop('disabled', false).removeAttr('aria-busy').text(label); |
| 62 |
}); |
| 63 |
}); |
| 64 |
|
| 65 |
// The WordPress notice's X: remember the dismissal for a day. |
| 66 |
$(document).on('click', '.mxchat-license-check-notice[data-mxchat-license-notice] .notice-dismiss', function () { |
| 67 |
$.post(cfg.ajaxUrl || window.ajaxurl, { |
| 68 |
action: 'mxchat_license_notice_dismiss', |
| 69 |
nonce: cfg.dismissNonce |
| 70 |
}); |
| 71 |
}); |
| 72 |
})(jQuery); |
| 73 |
|