| 1 |
/* eslint-disable complexity */ |
| 2 |
// current complexity is the only way to achieve desired results, pull request solutions appreciated. |
| 3 |
|
| 4 |
globalThis.mainwpVars = globalThis.mainwpVars || {}; |
| 5 |
|
| 6 |
mainwpVars.maxRunThreads = mainwpParams?.maximumUptimeMonitoringRequests ? mainwpParams.maximumUptimeMonitoringRequests : 10; |
| 7 |
|
| 8 |
globalThis.updateMonitorsBulkActionsState = function() { |
| 9 |
let checkedCount = jQuery('#mainwp-manage-sites-body-table .check-column .cb-uptime-monitor INPUT:checkbox:checked').length; |
| 10 |
let dropdown = jQuery('#mainwp-uptime-monitoring-bulk-actions-menu'); |
| 11 |
let applyButton = jQuery('#mainwp-do-monitors-bulk-actions'); |
| 12 |
|
| 13 |
if (checkedCount > 0) { |
| 14 |
dropdown.removeClass('disabled'); |
| 15 |
dropdown.parent('.ui.dropdown').removeClass('disabled'); |
| 16 |
applyButton.removeClass('disabled'); |
| 17 |
} else { |
| 18 |
dropdown.addClass('disabled'); |
| 19 |
dropdown.parent('.ui.dropdown').addClass('disabled'); |
| 20 |
dropdown.dropdown('set selected', 'none'); |
| 21 |
applyButton.addClass('disabled'); |
| 22 |
} |
| 23 |
}; |
| 24 |
|
| 25 |
jQuery(function () { |
| 26 |
updateMonitorsBulkActionsState(); |
| 27 |
|
| 28 |
jQuery(document).on('change', '#mainwp-manage-sites-body-table .check-column INPUT:checkbox', function() { |
| 29 |
updateMonitorsBulkActionsState(); |
| 30 |
}); |
| 31 |
}); |
| 32 |
|
| 33 |
// Trigger Manage Sites Bulk Actions |
| 34 |
jQuery(document).on('click', '#mainwp-do-monitors-bulk-actions', function () { |
| 35 |
let action = jQuery("#mainwp-uptime-monitoring-bulk-actions-menu").dropdown("get value"); |
| 36 |
if (action) { |
| 37 |
mainwp_managemonitors_doaction(action); |
| 38 |
} |
| 39 |
return false; |
| 40 |
}); |
| 41 |
|
| 42 |
jQuery(document).on('click', '.managemonitors_uptime_checknow', function () { |
| 43 |
let row = jQuery(this).closest('tr'); |
| 44 |
let checkItems = []; |
| 45 |
checkItems.push({ siteid: row.attr('siteid'), id: row.attr('itemid'), url: row.attr('urlpage'), niceurl: row.attr('niceurl') }); |
| 46 |
mainwp_uptime_monitor_check_now(checkItems); |
| 47 |
return false; |
| 48 |
}); |
| 49 |
|
| 50 |
globalThis.mainwp_uptime_monitor_check_now = function (itemsData) { |
| 51 |
let numOfItems = itemsData.length; |
| 52 |
jQuery('#mainwp-sync-sites-modal #sync-sites-status').html(''); |
| 53 |
mainwpPopup('#mainwp-sync-sites-modal').init({ |
| 54 |
title: MainWP.I18n.t('Checking Uptime Status'), |
| 55 |
progressMax: numOfItems, |
| 56 |
statusText: 'checked', |
| 57 |
callback: function () { |
| 58 |
mainwpVars.bulkTaskRunning = false; |
| 59 |
history.pushState("", document.title, globalThis.location.pathname + globalThis.location.search); // to fix issue for url with hash |
| 60 |
mainwp_forceReload(); |
| 61 |
} |
| 62 |
}); |
| 63 |
uptime_monitoring_prepare_items_rows(itemsData); |
| 64 |
mainwp_uptime_monitoring_check_uptime(itemsData); |
| 65 |
}; |
| 66 |
|
| 67 |
let uptime_monitoring_prepare_items_rows = function (itemsData) { |
| 68 |
itemsData.forEach(function (item) { |
| 69 |
jQuery("#sync-sites-status").append(` |
| 70 |
<div class="item"> |
| 71 |
<div class="right floated content"> |
| 72 |
<div class="running-item-status" itemid="` + item.id + `" siteid="` + item.siteid + `" niceurl="` + item.niceurl + `"><span data-position="left center" data-inverted="" data-tooltip="` + MainWP.I18n.t('Pending') + `"><i class="clock outline icon"></i></span></div> |
| 73 |
</div> |
| 74 |
<div class="content">` + item.url + `</div> |
| 75 |
</div>`); |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
globalThis.mainwp_uptime_monitoring_check_uptime = function (itemsData) { |
| 80 |
mainwpVars.itemsToCheck = []; |
| 81 |
mainwpVars.sitesIds = []; |
| 82 |
|
| 83 |
itemsData.forEach(function (item) { |
| 84 |
mainwpVars.itemsToCheck.push(item.id); |
| 85 |
if (item?.siteid) { |
| 86 |
mainwpVars.sitesIds.push(item.siteid); |
| 87 |
} else { |
| 88 |
mainwpVars.sitesIds.push(0); |
| 89 |
} |
| 90 |
}); |
| 91 |
|
| 92 |
mainwpVars.currentItem = 0; |
| 93 |
mainwpVars.itemsDone = 0; |
| 94 |
mainwpVars.itemsTotal = mainwpVars.itemsLeft = mainwpVars.itemsToCheck.length; |
| 95 |
mainwpVars.bulkTaskRunning = true; |
| 96 |
if (mainwpVars.itemsTotal == 0) { |
| 97 |
uptime_monitoring_check_done(); |
| 98 |
} else { |
| 99 |
uptime_monitoring_loop_next(); |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
globalThis.mainwp_uptime_monitoring_check_set_item_status = function (itemId, newStatus, isSuccess) { |
| 104 |
jQuery('.running-item-status[itemid="' + itemId + '"]').html(newStatus); |
| 105 |
// Move successfully synced site to the bottom of the sync list. |
| 106 |
if ( isSuccess !== undefined && isSuccess) { |
| 107 |
let row = jQuery('.running-item-status[itemid="' + itemId + '"]').closest('.item'); |
| 108 |
jQuery(row).insertAfter(jQuery("#sync-sites-status .item").last()); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
let uptime_monitoring_check_done = function () { |
| 113 |
mainwpVars.currentThreads--; |
| 114 |
if (!mainwpVars.bulkTaskRunning) |
| 115 |
return; |
| 116 |
mainwpVars.itemsDone++; |
| 117 |
if (mainwpVars.itemsDone > mainwpVars.itemsTotal) |
| 118 |
mainwpVars.itemsDone = mainwpVars.itemsTotal; |
| 119 |
mainwpPopup('#mainwp-sync-sites-modal').setProgressSite(mainwpVars.itemsDone); |
| 120 |
if (mainwpVars.itemsDone == mainwpVars.itemsTotal) { |
| 121 |
let successSites = jQuery('#mainwp-sync-sites-modal .check.green.icon').length; |
| 122 |
if (mainwpVars.itemsDone == successSites) { |
| 123 |
mainwpVars.bulkTaskRunning = false; |
| 124 |
setTimeout(function () { |
| 125 |
mainwpPopup('#mainwp-sync-sites-modal').close(true); |
| 126 |
}, 3000); |
| 127 |
} else { |
| 128 |
mainwpVars.bulkTaskRunning = false; |
| 129 |
} |
| 130 |
return; |
| 131 |
} |
| 132 |
uptime_monitoring_loop_next(); |
| 133 |
}; |
| 134 |
|
| 135 |
let uptime_monitoring_loop_next = function () { |
| 136 |
while (mainwpVars.bulkTaskRunning && (mainwpVars.currentThreads < mainwpVars.maxRunThreads) && (mainwpVars.itemsLeft > 0)) { |
| 137 |
uptime_monitoring_check_next(); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
let uptime_monitoring_check_next = function () { |
| 142 |
mainwpVars.currentThreads++; |
| 143 |
mainwpVars.itemsLeft--; |
| 144 |
let itemId = mainwpVars.itemsToCheck[mainwpVars.currentItem]; |
| 145 |
let siteId = mainwpVars.sitesIds[mainwpVars.currentItem]; |
| 146 |
|
| 147 |
mainwpVars.currentItem++; |
| 148 |
|
| 149 |
mainwp_uptime_monitoring_check_set_item_status(itemId, '<span data-inverted="" data-position="left center" data-tooltip="' + MainWP.I18n.t('Checking uptime status...', 'mainwp') + '"><i class="sync alternate loading icon"></i></span>'); |
| 150 |
let data = mainwp_secure_data({ |
| 151 |
action: 'mainwp_uptime_monitoring_uptime_check', |
| 152 |
mo_id: itemId, |
| 153 |
wp_id: siteId |
| 154 |
}); |
| 155 |
uptime_monitoring_check_next_int(itemId, data, 0); |
| 156 |
|
| 157 |
}; |
| 158 |
|
| 159 |
let uptime_monitoring_check_next_int = function (itemId, data, errors) { |
| 160 |
jQuery.ajax({ |
| 161 |
type: 'POST', |
| 162 |
url: ajaxurl, |
| 163 |
data: data, |
| 164 |
success: function (pItemId) { |
| 165 |
return function (response) { |
| 166 |
if (response.error) { |
| 167 |
let extErr = response.error; |
| 168 |
mainwp_uptime_monitoring_check_set_item_status(pItemId, '<span data-inverted="" data-position="left center" data-tooltip="' + extErr + '"><i class="exclamation red icon"></i></span>'); |
| 169 |
} else { |
| 170 |
mainwp_uptime_monitoring_check_set_item_status(pItemId, '<span data-inverted="" data-position="left center" data-tooltip="' + MainWP.I18n.t('Checking process completed successfully.', 'mainwp') + '"><i class="check green icon"></i></span>', true); |
| 171 |
} |
| 172 |
uptime_monitoring_check_done(); |
| 173 |
} |
| 174 |
}(itemId), |
| 175 |
error: function (pItemId, pData, pErrors) { |
| 176 |
return function () { |
| 177 |
if (pErrors > 5) { |
| 178 |
mainwp_uptime_monitoring_check_set_item_status(pItemId, '<span data-inverted="" data-position="left center" data-tooltip="' + MainWP.I18n.t('Process timed out. Please try again.', 'mainwp') + '"><i class="exclamation yellow icon"></i></span>'); |
| 179 |
uptime_monitoring_check_done(); |
| 180 |
} else { |
| 181 |
pErrors++; |
| 182 |
uptime_monitoring_check_next_int(pItemId, pData, pErrors); |
| 183 |
} |
| 184 |
} |
| 185 |
}(itemId, data, errors), |
| 186 |
dataType: 'json' |
| 187 |
}); |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
// Manage Sites Bulk Actions |
| 192 |
/* eslint-disable complexity */ |
| 193 |
let mainwp_managemonitors_doaction = function (action) { // NOSONAR - complex. |
| 194 |
|
| 195 |
if (mainwpVars.bulkTaskRunning) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
if (action == 'checknow') { |
| 200 |
let checkItems = []; |
| 201 |
jQuery('#mainwp-manage-sites-body-table .check-column .cb-uptime-monitor INPUT:checkbox:checked').each(function () { |
| 202 |
let row = jQuery(this).closest('tr'); |
| 203 |
checkItems.push({ siteid: row.attr('siteid'), id: row.attr('itemid'), url: row.attr('urlpage'), niceurl: row.attr('niceurl') }); |
| 204 |
}); |
| 205 |
// if number monitors checked boxes is empty, unchecked all others and return. |
| 206 |
if (checkItems.length === 0) { |
| 207 |
jQuery('#mainwp-manage-sites-body-table .check-column .ui.checkbox').each(function () { |
| 208 |
jQuery(this).checkbox('set unchecked'); |
| 209 |
}); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
mainwp_uptime_monitor_check_now(checkItems); |
| 214 |
return false; |
| 215 |
} else if ('sync' === action) { |
| 216 |
managesites_bulk_init(true); // to sync sites not sub pages. |
| 217 |
mainwpVars.bulkManageSitesTaskRunning = true; // to compatible with: mainwp_sync_sites_data(). |
| 218 |
let selectedIds = jQuery.map(jQuery("#mainwp-manage-sites-body-table .check-column INPUT:checkbox:not(.sub-pages-checkbox):checked"), function (el) { |
| 219 |
return jQuery(el).val(); |
| 220 |
}); |
| 221 |
mainwp_sync_sites_data(selectedIds); |
| 222 |
} |
| 223 |
} |