| 1 |
/* eslint complexity: ["error", 100] */ |
| 2 |
let apibackups_bulkMaxThreads = 1; // rate limiting. |
| 3 |
let apibackups_bulkTotalThreads = 0; |
| 4 |
let apibackups_bulkCurrentThreads = 0; |
| 5 |
let apibackups_bulkFinishedThreads = 0; |
| 6 |
|
| 7 |
let mainwp_api_backups_do_backups = function (pObj) { |
| 8 |
// init queue status. |
| 9 |
jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]').each(function () { |
| 10 |
jQuery(this).attr('status', 'queue'); |
| 11 |
}); |
| 12 |
|
| 13 |
// Add loading icon to all selected sites. |
| 14 |
jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked').each(function () { |
| 15 |
let parent = jQuery(this).closest('tr'); |
| 16 |
let statusEl = parent.find('.running'); |
| 17 |
statusEl.html('<i class="clock outline icon"></i>'); |
| 18 |
}); |
| 19 |
|
| 20 |
// Get selected sites. |
| 21 |
let selector = '#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked[status=queue]'; |
| 22 |
let selectedIds = jQuery.map(jQuery(selector), function (el) { |
| 23 |
return jQuery(el).val(); |
| 24 |
}); |
| 25 |
|
| 26 |
// Check if there are selected sites. |
| 27 |
if (selectedIds.length == 0) { |
| 28 |
|
| 29 |
// Show message. |
| 30 |
jQuery('#mainwp-api-backups-message-zone').addClass('red').show(); |
| 31 |
jQuery('#mainwp-api-backups-message-zone .content .message') |
| 32 |
.html('Please select at least one website.') |
| 33 |
; |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
// Set queue metadata and proceed. |
| 38 |
apibackups_bulkTotalThreads = selectedIds.length; |
| 39 |
apibackups_bulkCurrentThreads = 0; |
| 40 |
apibackups_bulkFinishedThreads = 0; |
| 41 |
|
| 42 |
|
| 43 |
jQuery(pObj).addClass('disabled'); |
| 44 |
mainwp_api_backups_do_backups_specific_next(selector); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
let mainwp_api_backups_do_backups_specific_next = function (selector) { |
| 49 |
let objProcess = jQuery(selector + ':first'); |
| 50 |
while (objProcess && objProcess.length > 0 && apibackups_bulkCurrentThreads < apibackups_bulkMaxThreads) { // NOSONAR - variables modified outside the function. |
| 51 |
objProcess.attr('status', 'proceed'); |
| 52 |
mainwp_api_backups_do_backups_specific(objProcess, true, selector); |
| 53 |
objProcess = jQuery(selector + ':first'); |
| 54 |
} |
| 55 |
|
| 56 |
if (apibackups_bulkTotalThreads > 0 && apibackups_bulkFinishedThreads == apibackups_bulkTotalThreads) { |
| 57 |
jQuery('#action_backup_selected_sites').removeClass('disabled'); |
| 58 |
|
| 59 |
// Show message. |
| 60 |
jQuery('#mainwp-api-backups-message-zone').removeClass('red').addClass('green').show(); |
| 61 |
jQuery('#mainwp-api-backups-message-zone .content .message') |
| 62 |
.html('API Backup requests sent. Please allow some time for the backup to complete, then Refresh Available Backups.') |
| 63 |
; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Do backup for specific site. |
| 72 |
* |
| 73 |
* This function is triggered by the mainwp_api_backups_do_backups_specific_next() function & is called recursively |
| 74 |
* until all sites are processed. |
| 75 |
* |
| 76 |
* mainwp_api_backups_selected_websites() `action` is defined in assets/classes/class-api-backups-handler.php admin_init(), |
| 77 |
* & fires off ajax_backups_selected_websites() function also defined in assets/classes/class-api-backups-handler.php |
| 78 |
*/ |
| 79 |
let mainwp_api_backups_do_backups_specific = function (pObj, bulk, selector) { |
| 80 |
let parent = pObj.closest('tr'); |
| 81 |
let statusEl = parent.find('.running'); |
| 82 |
let providerName = parent.attr('provider-name'); |
| 83 |
|
| 84 |
let data = mainwp_secure_data({ |
| 85 |
action: 'mainwp_api_backups_selected_websites', |
| 86 |
websiteId: parent.attr('website-id'), |
| 87 |
bulk_backups: 1, |
| 88 |
}); |
| 89 |
|
| 90 |
if (bulk) { |
| 91 |
apibackups_bulkCurrentThreads++; |
| 92 |
} |
| 93 |
|
| 94 |
jQuery(pObj).prop('checked', false); |
| 95 |
statusEl.html('<i class="notched circle loading icon"></i>'); |
| 96 |
|
| 97 |
jQuery.post(ajaxurl, data, function (response) { |
| 98 |
statusEl.html(''); |
| 99 |
let err_msg = ''; |
| 100 |
let succ_msg = ''; |
| 101 |
let rsp = ''; |
| 102 |
|
| 103 |
if (response && !response.success) { |
| 104 |
rsp = response.data; |
| 105 |
// Check for gridPane error.. |
| 106 |
if (rsp && rsp['0'].code === 429) { |
| 107 |
err_msg = 'API rate limit has been met. Please wait 30 seconds and try again.'; |
| 108 |
} else { |
| 109 |
// Everything else. (API provided error message) |
| 110 |
err_msg = rsp['0'].message; |
| 111 |
} |
| 112 |
} else if (response?.success) { |
| 113 |
succ_msg = MainWP.I18n.t('Successful'); |
| 114 |
} else if (response?.error) { // Check for cPanel error.. |
| 115 |
err_msg = MainWP.I18n.t(response.error); |
| 116 |
} else { |
| 117 |
err_msg = _('Undefined error.'); |
| 118 |
} |
| 119 |
|
| 120 |
if ('' != err_msg) { |
| 121 |
statusEl.html('<span data-inverted="" data-position="right center" data-tooltip="' + err_msg + '"><i class="red times icon"></i></span>'); |
| 122 |
} else if ('' != succ_msg) { |
| 123 |
statusEl.html('<span data-inverted="" data-position="right center" data-tooltip="' + succ_msg + '"><i class="green check icon"></i></span>'); |
| 124 |
} |
| 125 |
|
| 126 |
if (bulk) { |
| 127 |
apibackups_bulkCurrentThreads--; |
| 128 |
apibackups_bulkFinishedThreads++; |
| 129 |
if (providerName === 'GridPane') { |
| 130 |
// GridPane API BETA has Strict rate limits. However, rate limits will increase and vary between |
| 131 |
// endpoints once the API is out of BETA. |
| 132 |
setTimeout(function () { |
| 133 |
mainwp_api_backups_do_backups_specific_next(selector); |
| 134 |
}, 30000); // 30 second Rate limit. |
| 135 |
} else { |
| 136 |
mainwp_api_backups_do_backups_specific_next(selector); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
}, 'json'); |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
let mainwp_api_backups_update_button_state = function () { |
| 145 |
let checkedCount = jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked').length; |
| 146 |
if (checkedCount > 0) { |
| 147 |
jQuery('#action_backup_selected_sites').removeClass('disabled'); |
| 148 |
} else { |
| 149 |
jQuery('#action_backup_selected_sites').addClass('disabled'); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
jQuery(document).ready(function () { |
| 154 |
jQuery('#mainwp-3rd-party-backups-table').on('change', 'tbody td.check-column INPUT[type=checkbox]', function () { |
| 155 |
mainwp_api_backups_update_button_state(); |
| 156 |
}); |
| 157 |
|
| 158 |
jQuery('#cb-select-all-top').on('change', function () { |
| 159 |
mainwp_api_backups_update_button_state(); |
| 160 |
}); |
| 161 |
}); |
| 162 |
|
| 163 |
|