| 1 |
/* global metasyncBotStatsData, jQuery, ajaxurl */ |
| 2 |
/** |
| 3 |
* MetaSync Bot Statistics |
| 4 |
* |
| 5 |
* Extracted for Phase 5, #887. |
| 6 |
* Copy-to-clipboard and reset bot statistics functionality. |
| 7 |
* |
| 8 |
* Localized object: metasyncBotStatsData |
| 9 |
* - resetNonce (string) |
| 10 |
* |
| 11 |
* @since Phase 5 |
| 12 |
*/ |
| 13 |
jQuery(document).ready(function ($) { |
| 14 |
var resetNonce = metasyncBotStatsData.resetNonce; |
| 15 |
|
| 16 |
// Copy button functionality |
| 17 |
$('.copy-btn').on('click', function (e) { |
| 18 |
e.preventDefault(); |
| 19 |
e.stopPropagation(); |
| 20 |
|
| 21 |
var $btn = $(this); |
| 22 |
var textToCopy = $btn.data('copy'); |
| 23 |
|
| 24 |
function showSuccess() { |
| 25 |
$btn.addClass('copied'); |
| 26 |
$btn.find('.copy-tooltip').text('Copied!'); |
| 27 |
setTimeout(function () { |
| 28 |
$btn.removeClass('copied'); |
| 29 |
$btn.find('.copy-tooltip').text('Copy'); |
| 30 |
}, 2000); |
| 31 |
} |
| 32 |
|
| 33 |
if (navigator.clipboard && navigator.clipboard.writeText) { |
| 34 |
navigator.clipboard.writeText(textToCopy).then(function () { |
| 35 |
showSuccess(); |
| 36 |
}).catch(function () { |
| 37 |
fallbackCopy(textToCopy, $btn, showSuccess); |
| 38 |
}); |
| 39 |
} else { |
| 40 |
fallbackCopy(textToCopy, $btn, showSuccess); |
| 41 |
} |
| 42 |
}); |
| 43 |
|
| 44 |
function fallbackCopy(text, $btn, successCallback) { |
| 45 |
var $temp = $('<textarea>'); |
| 46 |
$temp.css({ position: 'absolute', left: '-9999px' }); |
| 47 |
$('body').append($temp); |
| 48 |
$temp.val(text).select(); |
| 49 |
try { |
| 50 |
document.execCommand('copy'); |
| 51 |
successCallback(); |
| 52 |
} catch (err) { |
| 53 |
alert('Failed to copy. Please copy manually.'); |
| 54 |
} |
| 55 |
$temp.remove(); |
| 56 |
} |
| 57 |
|
| 58 |
$('#reset-bot-stats').on('click', function (e) { |
| 59 |
e.preventDefault(); |
| 60 |
|
| 61 |
if (!confirm('Are you sure you want to reset all bot detection statistics? This action cannot be undone.')) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
var $button = $(this); |
| 66 |
var originalText = $button.html(); |
| 67 |
|
| 68 |
$button.prop('disabled', true).html('\u231B Resetting...'); |
| 69 |
|
| 70 |
$.post(ajaxurl, { |
| 71 |
action: 'metasync_reset_bot_stats', |
| 72 |
nonce: resetNonce |
| 73 |
}) |
| 74 |
.done(function (response) { |
| 75 |
if (response.success) { |
| 76 |
location.reload(); |
| 77 |
} else { |
| 78 |
alert('Error: ' + (response.data ? response.data.message : 'Unknown error')); |
| 79 |
$button.prop('disabled', false).html(originalText); |
| 80 |
} |
| 81 |
}) |
| 82 |
.fail(function () { |
| 83 |
alert('Network error while resetting statistics'); |
| 84 |
$button.prop('disabled', false).html(originalText); |
| 85 |
}); |
| 86 |
}); |
| 87 |
}); |
| 88 |
|