ehssl-static-resources-scan-update.js
260 lines
| 1 | /* global ehssl_non_https_resources_scan_update_js_data */ |
| 2 | |
| 3 | document.addEventListener('DOMContentLoaded', function () { |
| 4 | const {ajaxUrl, texts} = ehssl_non_https_resources_scan_update_js_data; |
| 5 | const scanForm = document.getElementById('ehssl_non_https_resources_scan_form'); |
| 6 | const scanBtn = document.getElementById('ehssl_non_https_resources_scan_btn'); |
| 7 | const resultsBox = document.getElementById('ehssl_scan_results'); |
| 8 | |
| 9 | scanForm.addEventListener('submit', async function (e) { |
| 10 | e.preventDefault(); |
| 11 | scanBtn.disabled = true; |
| 12 | let scanBtnText = scanBtn.textContent; |
| 13 | scanBtn.textContent = texts.scan_btn_loading; |
| 14 | scanBtnText = texts.rescan_btn; |
| 15 | |
| 16 | const checked_post_types = scanForm.querySelectorAll('input[name="ehssl_post_types[]"]:checked'); |
| 17 | const checked_other_tables = scanForm.querySelectorAll('input[name="ehssl_other_tables[]"]:checked'); |
| 18 | |
| 19 | if (!checked_post_types.length && !checked_other_tables.length) { |
| 20 | alert(texts.pls_select_an_item); |
| 21 | scanBtn.disabled = false; |
| 22 | scanBtn.innerText = scanBtnText; |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | resultsBox.innerHTML = ''; |
| 27 | |
| 28 | const formData = new FormData(e.target); |
| 29 | formData.append('action', 'ehssl_non_https_resources_scan'); |
| 30 | formData.append('offset', 0); |
| 31 | formData.append('total', JSON.stringify([])); |
| 32 | |
| 33 | const onComplete = async(resp) => { |
| 34 | // console.log('processBatchScan onComplete:', resp); // Debug Purpose Only |
| 35 | |
| 36 | if (resp.success) { |
| 37 | // Display the rendered table. |
| 38 | await getScannedResourcesTable((tableHTML) => { |
| 39 | resultsBox.innerHTML = tableHTML; |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | scanBtn.disabled = false; |
| 44 | scanBtn.textContent = scanBtnText; |
| 45 | } |
| 46 | |
| 47 | const onError = (response) => { |
| 48 | // return response; |
| 49 | let resp_msg; |
| 50 | |
| 51 | if ( response?.message !== undefined) { |
| 52 | resp_msg = response.message; |
| 53 | } else if ( response?.data?.message !== undefined) { |
| 54 | resp_msg = response.data.message; |
| 55 | } else { |
| 56 | resp_msg = 'Something went wrong'; |
| 57 | } |
| 58 | |
| 59 | console.log(resp_msg, response); |
| 60 | alert(resp_msg); |
| 61 | scanBtn.disabled = false; |
| 62 | scanBtn.innerText = scanBtnText; |
| 63 | } |
| 64 | |
| 65 | await processBatchScan(formData, onComplete, onError); |
| 66 | }); |
| 67 | |
| 68 | async function processBatchScan(formData, onComplete, onError) { |
| 69 | try { |
| 70 | let response = await fetch(ajaxUrl, { |
| 71 | method: 'POST', |
| 72 | body: formData, |
| 73 | }) |
| 74 | |
| 75 | response = await response.json(); |
| 76 | |
| 77 | const {success, data} = response; |
| 78 | if (!success) { |
| 79 | onError(response); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // console.log('processBatchScan response:', response); // Debug Purpose Only |
| 84 | |
| 85 | if (!data.completed) { |
| 86 | formData.append('total', JSON.stringify(data.total)); |
| 87 | formData.append('offset', data.next_offset); |
| 88 | |
| 89 | await processBatchScan(formData, onComplete, onError); |
| 90 | } else { |
| 91 | onComplete(response); |
| 92 | } |
| 93 | |
| 94 | } catch (error) { |
| 95 | onError(error); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | async function getScannedResourcesTable(cb) { |
| 100 | try { |
| 101 | const currentParams = new URLSearchParams(window.location.search); |
| 102 | const currentPage = currentParams.get('page'); |
| 103 | const currentTab = currentParams.get('tab'); |
| 104 | |
| 105 | const url = new URL(ajaxUrl); |
| 106 | url.searchParams.append('action', 'ehssl_get_scanned_resources_table'); |
| 107 | url.searchParams.append('page', currentPage); |
| 108 | url.searchParams.append('tab', currentTab); |
| 109 | |
| 110 | let response = await fetch(url, { |
| 111 | method: 'GET', |
| 112 | }) |
| 113 | |
| 114 | response = await response.text(); |
| 115 | |
| 116 | cb(response); |
| 117 | } catch (error) { |
| 118 | alert(error.message); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * AJAX pagination |
| 124 | */ |
| 125 | document.addEventListener('click', function (e) { |
| 126 | const navLink = e.target.closest('.tablenav-pages a'); |
| 127 | if (!navLink) { |
| 128 | return; |
| 129 | } |
| 130 | e.preventDefault(); |
| 131 | |
| 132 | const currentParams = new URLSearchParams(window.location.search); |
| 133 | |
| 134 | const url = new URL(ajaxUrl); |
| 135 | url.searchParams.append('action', 'ehssl_load_static_resources_table_page'); |
| 136 | url.searchParams.append('page', currentParams.get('page')); |
| 137 | url.searchParams.append('tab', currentParams.get('tab')); |
| 138 | |
| 139 | const navLinkUrl = new URL(navLink.href); |
| 140 | const paged = navLinkUrl.searchParams.get('paged') || 1; |
| 141 | url.searchParams.append('paged', paged); |
| 142 | |
| 143 | fetch(url, { |
| 144 | method: 'GET', |
| 145 | }) |
| 146 | .then(response => response.text()) |
| 147 | .then(html => { |
| 148 | resultsBox.innerHTML = html; |
| 149 | }); |
| 150 | }); |
| 151 | |
| 152 | /** |
| 153 | * Update URLs action. |
| 154 | */ |
| 155 | document.addEventListener('click', function (e) { |
| 156 | if (e.target.matches('#doaction, #doaction2')) { |
| 157 | handle_update_urls(e); |
| 158 | } |
| 159 | |
| 160 | else if (e.target.matches('#ehssl_update_all_found_http_urls')) { |
| 161 | handle_update_urls(e, true); |
| 162 | } |
| 163 | }); |
| 164 | |
| 165 | |
| 166 | function handle_update_urls(e, update_all = false){ |
| 167 | e.preventDefault(); |
| 168 | |
| 169 | const actionBtn = e.target; |
| 170 | const actionBtnText = actionBtn.textContent; |
| 171 | |
| 172 | const nonce_input = document.querySelector('input[name="ehssl_update_all_http_urls_nonce"]'); |
| 173 | const nonce = nonce_input.value || ''; |
| 174 | |
| 175 | const formData = new FormData(); |
| 176 | formData.append('action', 'ehssl_update_http_urls'); |
| 177 | formData.append('nonce', nonce); |
| 178 | formData.append('offset', 0); |
| 179 | |
| 180 | const onComplete = (resp) => { |
| 181 | getScannedResourcesTable((tableHTML) => { |
| 182 | resultsBox.innerHTML = tableHTML; |
| 183 | |
| 184 | disableButtons(false); |
| 185 | }) |
| 186 | } |
| 187 | |
| 188 | if (update_all) { |
| 189 | actionBtn.textContent = texts.update_btn_loading; |
| 190 | } else { |
| 191 | const bulkSelect = actionBtn.id === 'doaction' |
| 192 | ? document.getElementById('bulk-action-selector-top') |
| 193 | : document.getElementById('bulk-action-selector-bottom'); |
| 194 | |
| 195 | if (!bulkSelect || bulkSelect.value !== 'update_to_https') { |
| 196 | alert('Please select an action!'); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | const selected_ids = []; |
| 201 | |
| 202 | const checkboxes = document.querySelectorAll('input[name="ehssl_non_https_resources_scan_ids[]"]:checked'); |
| 203 | checkboxes?.forEach(function (el) { |
| 204 | selected_ids.push(el.value); |
| 205 | }); |
| 206 | |
| 207 | if (!selected_ids.length) { |
| 208 | alert('Please select at least one item!'); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | formData.append('selected_ids', JSON.stringify(selected_ids)); |
| 213 | } |
| 214 | |
| 215 | if (!confirm(texts.confirm_update)) { |
| 216 | disableButtons(false); |
| 217 | actionBtn.textContent = actionBtnText; |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | disableButtons(); |
| 222 | |
| 223 | processBatchUpdate(formData, onComplete); |
| 224 | } |
| 225 | |
| 226 | function disableButtons(value = true){ |
| 227 | document.querySelectorAll('#ehssl_update_all_found_http_urls, #doaction, #doaction2').forEach((btn) => { |
| 228 | btn.disabled = value; |
| 229 | }); |
| 230 | } |
| 231 | |
| 232 | async function processBatchUpdate(formData, onComplete) { |
| 233 | try { |
| 234 | let response = await fetch(ajaxUrl, { |
| 235 | method: 'POST', |
| 236 | body: formData, |
| 237 | }) |
| 238 | |
| 239 | response = await response.json(); |
| 240 | |
| 241 | const {success, data} = response; |
| 242 | if (!success) { |
| 243 | alert('Something went wrong'); |
| 244 | console.log('Something went wrong', response); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // console.log(`${data.processed}/${data.total}`); // Debug purpose only. |
| 249 | |
| 250 | if (!data.completed) { |
| 251 | formData.append('offset', data.next_offset); |
| 252 | await processBatchUpdate(formData, onComplete); |
| 253 | } else { |
| 254 | onComplete(response); |
| 255 | } |
| 256 | } catch (error) { |
| 257 | alert(error.message); |
| 258 | } |
| 259 | } |
| 260 | }); |