File: /home/edtir/public_html/nabilico.ir/wp-content/plugins/wp-network-guard/assets/admin.js
(function($){
function toast($wrap, msg, ok){
var $t = $wrap.find('.wng-toast');
$t.removeClass('is-ok is-bad').text(msg);
if(ok === true) $t.addClass('is-ok');
if(ok === false) $t.addClass('is-bad');
clearTimeout($t.data('t'));
$t.data('t', setTimeout(function(){ $t.text('').removeClass('is-ok is-bad'); }, 2500));
}
function collect(panel){
var data = {};
$(panel).find('input, textarea, select').each(function(){
var $el = $(this);
var name = $el.attr('name');
if(!name) return;
if($el.attr('type') === 'checkbox') {
data[name] = $el.is(':checked') ? 1 : 0;
} else {
data[name] = $el.val();
}
});
return data;
}
function setQuickTips(tab){
try {
var list = (WNG && WNG.tips && WNG.tips[tab]) ? WNG.tips[tab] : [];
var $ul = $('#wng-quick-tips');
if(!$ul.length) return;
$ul.empty();
(list||[]).forEach(function(t){
$('<li/>').text(t).appendTo($ul);
});
} catch(e) {}
}
// init quick tips based on active tab
setQuickTips($('.wng-tab.is-active').data('tab') || 'general');
$(document).on('click', '.wng-tab', function(){
var tab = $(this).data('tab');
$('.wng-tab').removeClass('is-active');
$(this).addClass('is-active');
$('.wng-panel').removeClass('is-active');
$('.wng-panel[data-panel="'+tab+'"]').addClass('is-active');
setQuickTips(tab);
});
$(document).on('click', '.wng-save', function(){
var tab = $(this).data('save-tab');
var panel = $('.wng-panel[data-panel="'+tab+'"]').get(0);
var $panel = $(panel);
toast($panel, WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, {
action: 'wng_save_tab',
nonce: WNG.nonce,
tab: tab,
data: collect(panel)
}).done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.saved, true);
} else {
toast($panel, (res && res.data && res.data.message) ? res.data.message : WNG.i18n.error, false);
}
}).fail(function(){
toast($panel, WNG.i18n.error, false);
});
});
$(document).on('click', '.wng-clear-log', function(){
var $panel = $('.wng-panel[data-panel="logs"]');
$.post(WNG.ajaxUrl, {
action: 'wng_clear_log',
nonce: WNG.nonce
}).done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.cleared, true);
// simple reload to refresh list
window.location.reload();
} else {
toast($panel, WNG.i18n.error, false);
}
}).fail(function(){
toast($panel, WNG.i18n.error, false);
});
});
$(document).on('click', '.wng-monitor-clear', function(){
var $panel = $('.wng-panel[data-panel="monitor"]');
$.post(WNG.ajaxUrl, { action: 'wng_monitor_clear', nonce: WNG.nonce })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.cleared, true);
// simple reload to refresh table
window.location.reload();
} else {
toast($panel, WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
});
function monitorOp(op, host, tag){
var $panel = $('.wng-panel[data_panel="monitor"]');
$panel = $('.wng-panel[data-panel="monitor"]');
toast($panel, WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_monitor_action', nonce:WNG.nonce, op:op, host:host, tag:tag||'' })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.saved, true);
// بدون رفرش: وضعیت دکمهها را آپدیت کن
try {
var lists = (res && res.data && res.data.lists) ? res.data.lists : null;
if (lists) {
var $row = $('.wng-mon-op[data-host="' + host + '"]').closest('tr');
if ($row.length) {
var $btnA = $row.find('.wng-mon-op[data-op="allow"]');
var $btnD = $row.find('.wng-mon-op[data-op="deny"]');
if ($btnA.length) $btnA.text(lists.in_allow ? (WNG.i18n.allowed || 'Allowed') : (WNG.i18n.allow || 'Allow'));
if ($btnD.length) $btnD.text(lists.in_deny ? (WNG.i18n.denied || 'Denied') : (WNG.i18n.deny || 'Deny'));
}
}
} catch(e) {}
} else {
toast($panel, (res && res.data && res.data.message) ? res.data.message : WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
}
$(document).on('click', '.wng-mon-op', function(){
monitorOp($(this).data('op'), $(this).data('host'), '');
});
$(document).on('click', '.wng-mon-tag', function(){
monitorOp('', $(this).data('host'), $(this).data('tag'));
});
function assetOp(op, host, tag){
var $panel = $('.wng-panel[data-panel="browser"]');
toast($panel, WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_asset_action', nonce:WNG.nonce, op:op, host:host, tag:tag||'' })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.saved, true);
// بدون رفرش: وضعیت دکمهها را آپدیت کن
try {
var lists = (res && res.data && res.data.lists) ? res.data.lists : null;
if (lists) {
var $row = $('.wng-mon-op[data-host="' + host + '"]').closest('tr');
if ($row.length) {
var $btnA = $row.find('.wng-mon-op[data-op="allow"]');
var $btnD = $row.find('.wng-mon-op[data-op="deny"]');
if ($btnA.length) $btnA.text(lists.in_allow ? (WNG.i18n.allowed || 'Allowed') : (WNG.i18n.allow || 'Allow'));
if ($btnD.length) $btnD.text(lists.in_deny ? (WNG.i18n.denied || 'Denied') : (WNG.i18n.deny || 'Deny'));
}
}
} catch(e) {}
} else {
toast($panel, (res && res.data && res.data.message) ? res.data.message : WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
}
$(document).on('click', '.wng-asset-op', function(){
assetOp($(this).data('op'), $(this).data('host'), '');
});
$(document).on('click', '.wng-asset-tag', function(){
assetOp('', $(this).data('host'), $(this).data('tag'));
});
$(document).on('click', '.wng-asset-clear', function(){
var $panel = $('.wng-panel[data-panel="browser"]');
$.post(WNG.ajaxUrl, { action:'wng_asset_clear', nonce:WNG.nonce })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.cleared, true);
// بدون رفرش: وضعیت دکمهها را آپدیت کن
try {
var lists = (res && res.data && res.data.lists) ? res.data.lists : null;
if (lists) {
var $row = $('.wng-mon-op[data-host="' + host + '"]').closest('tr');
if ($row.length) {
var $btnA = $row.find('.wng-mon-op[data-op="allow"]');
var $btnD = $row.find('.wng-mon-op[data-op="deny"]');
if ($btnA.length) $btnA.text(lists.in_allow ? (WNG.i18n.allowed || 'Allowed') : (WNG.i18n.allow || 'Allow'));
if ($btnD.length) $btnD.text(lists.in_deny ? (WNG.i18n.denied || 'Denied') : (WNG.i18n.deny || 'Deny'));
}
}
} catch(e) {}
} else {
toast($panel, WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
});
function level2Op(op, host, tag){
var $panel = $('.wng-panel[data-panel="level2"]');
toast($panel, WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_l2_action', nonce:WNG.nonce, op:op, host:host, tag:tag||'' })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.saved, true);
} else {
toast($panel, (res && res.data && res.data.message) ? res.data.message : WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
}
$(document).on('click', '.wng-l2-op', function(){
level2Op($(this).data('op'), $(this).data('host'), '');
});
$(document).on('click', '.wng-l2-tag', function(){
level2Op('', $(this).data('host'), $(this).data('tag'));
});
$(document).on('click', '.wng-l2-clear', function(){
var $panel = $('.wng-panel[data-panel="level2"]');
$.post(WNG.ajaxUrl, { action:'wng_l2_clear', nonce:WNG.nonce })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.cleared, true);
window.location.reload();
} else {
toast($panel, WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
});
function cacheOp(op, key){
var $panel = $('.wng-panel[data-panel="cache"]');
toast($panel, WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_cache_action', nonce:WNG.nonce, op:op, key:key })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.saved, true);
// بدون رفرش: وضعیت دکمهها را آپدیت کن
try {
var lists = (res && res.data && res.data.lists) ? res.data.lists : null;
if (lists) {
var $row = $('.wng-mon-op[data-host="' + host + '"]').closest('tr');
if ($row.length) {
var $btnA = $row.find('.wng-mon-op[data-op="allow"]');
var $btnD = $row.find('.wng-mon-op[data-op="deny"]');
if ($btnA.length) $btnA.text(lists.in_allow ? (WNG.i18n.allowed || 'Allowed') : (WNG.i18n.allow || 'Allow'));
if ($btnD.length) $btnD.text(lists.in_deny ? (WNG.i18n.denied || 'Denied') : (WNG.i18n.deny || 'Deny'));
}
}
} catch(e) {}
} else {
toast($panel, (res && res.data && res.data.message) ? res.data.message : WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
}
// =============== Manual Asset Mirror (Media Library) ==================
var manualFrame = null;
function manualToast(msg, ok){
var $panel = $('.wng-panel[data-panel="cache"]');
toast($panel, msg, ok);
}
$(document).on('click', '#wng-manual-pick', function(e){
e.preventDefault();
if (!window.wp || !wp.media) {
manualToast(WNG.i18n.error, false);
return;
}
if (manualFrame) { manualFrame.open(); return; }
manualFrame = wp.media({
title: WNG.i18n.choose_file || 'Choose file',
button: { text: WNG.i18n.set_file || 'Use file' },
multiple: false
});
manualFrame.on('select', function(){
var att = manualFrame.state().get('selection').first().toJSON();
if (!att || !att.id) return;
$('#wng-manual-attachment').val(att.id);
var name = att.filename || att.title || ('Attachment #' + att.id);
$('#wng-manual-picked').text((WNG.i18n.picked || 'Picked') + ': ' + name);
});
manualFrame.open();
});
$(document).on('click', '#wng-manual-save', function(){
var url = ($('#wng-manual-remote').val() || '').trim();
var att = parseInt($('#wng-manual-attachment').val() || '0', 10);
if (!url || !att) {
manualToast('URL و فایل را انتخاب کنید.', false);
return;
}
manualToast(WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_manual_map', nonce:WNG.nonce, op:'upsert', url:url, attachment_id:att })
.done(function(res){
if (res && res.success) {
manualToast(WNG.i18n.saved, true);
// simplest: reload cache tab to render new table
window.location.reload();
} else {
manualToast((res && res.data && res.data.message) ? res.data.message : WNG.i18n.error, false);
}
}).fail(function(){ manualToast(WNG.i18n.error, false); });
});
$(document).on('click', '.wng-manual-del', function(){
var url = $(this).data('url');
if (!url) return;
manualToast(WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_manual_map', nonce:WNG.nonce, op:'delete', url:url })
.done(function(res){
if (res && res.success) {
manualToast(WNG.i18n.saved, true);
// remove row without full reload
$('#wng-manual-table tr[data-remote="'+url.replace(/"/g,'\\"')+'"]').remove();
} else {
manualToast((res && res.data && res.data.message) ? res.data.message : WNG.i18n.error, false);
}
}).fail(function(){ manualToast(WNG.i18n.error, false); });
});
$(document).on('click', '.wng-cache-op', function(e){
e.preventDefault();
cacheOp($(this).data('op'), $(this).data('key'));
});
$(document).on('click', '.wng-cache-clear', function(e){
e.preventDefault();
var $panel = $('.wng-panel[data-panel="cache"]');
toast($panel, WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_cache_clear', nonce:WNG.nonce })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.cleared, true);
// بدون رفرش: وضعیت دکمهها را آپدیت کن
try {
var lists = (res && res.data && res.data.lists) ? res.data.lists : null;
if (lists) {
var $row = $('.wng-mon-op[data-host="' + host + '"]').closest('tr');
if ($row.length) {
var $btnA = $row.find('.wng-mon-op[data-op="allow"]');
var $btnD = $row.find('.wng-mon-op[data-op="deny"]');
if ($btnA.length) $btnA.text(lists.in_allow ? (WNG.i18n.allowed || 'Allowed') : (WNG.i18n.allow || 'Allow'));
if ($btnD.length) $btnD.text(lists.in_deny ? (WNG.i18n.denied || 'Denied') : (WNG.i18n.deny || 'Deny'));
}
}
} catch(e) {}
} else {
toast($panel, WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
});
$(document).on('click', '.wng-cache-update-all', function(e){
e.preventDefault();
var $panel = $('.wng-panel[data-panel="cache"]');
toast($panel, WNG.i18n.saving, null);
$.post(WNG.ajaxUrl, { action:'wng_cache_update_all', nonce:WNG.nonce, limit:200 })
.done(function(res){
if(res && res.success){
toast($panel, WNG.i18n.saved, true);
// بدون رفرش: وضعیت دکمهها را آپدیت کن
try {
var lists = (res && res.data && res.data.lists) ? res.data.lists : null;
if (lists) {
var $row = $('.wng-mon-op[data-host="' + host + '"]').closest('tr');
if ($row.length) {
var $btnA = $row.find('.wng-mon-op[data-op="allow"]');
var $btnD = $row.find('.wng-mon-op[data-op="deny"]');
if ($btnA.length) $btnA.text(lists.in_allow ? (WNG.i18n.allowed || 'Allowed') : (WNG.i18n.allow || 'Allow'));
if ($btnD.length) $btnD.text(lists.in_deny ? (WNG.i18n.denied || 'Denied') : (WNG.i18n.deny || 'Deny'));
}
}
} catch(e) {}
} else {
toast($panel, WNG.i18n.error, false);
}
}).fail(function(){ toast($panel, WNG.i18n.error, false); });
});
})(jQuery);