| 1 |
const storeItems = async (items, nonce = null, idx = 0) => { |
| 2 |
if (!items.length) { |
| 3 |
return true; |
| 4 |
} |
| 5 |
|
| 6 |
console.log(idx); |
| 7 |
|
| 8 |
if (!nonce) { |
| 9 |
nonce = ag_content_params.nonce; |
| 10 |
} |
| 11 |
|
| 12 |
const { ajaxurl } = window; |
| 13 |
|
| 14 |
const data = new FormData; |
| 15 |
|
| 16 |
data.append('action', 'age_gate_store_terms'); |
| 17 |
data.append('idx', idx); |
| 18 |
data.append('nonce', nonce); |
| 19 |
|
| 20 |
for (const [key, value] of Object.entries(items[0])) { |
| 21 |
data.append(`ag_settings[${key}]`, value); |
| 22 |
} |
| 23 |
|
| 24 |
try { |
| 25 |
const response = await( |
| 26 |
await ( |
| 27 |
fetch ( |
| 28 |
ajaxurl, |
| 29 |
{ |
| 30 |
method: 'POST', |
| 31 |
body: data, |
| 32 |
} |
| 33 |
) |
| 34 |
) |
| 35 |
).json(); |
| 36 |
} catch (e) { |
| 37 |
|
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
items.shift(); |
| 42 |
|
| 43 |
const prog = document.querySelector('.age-gate-progress'); |
| 44 |
|
| 45 |
if (prog) { |
| 46 |
prog.value = idx + 1; |
| 47 |
} |
| 48 |
|
| 49 |
return storeItems(items, nonce, idx + 1); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
window.addEventListener('DOMContentLoaded', () => { |
| 54 |
const contentForm = document.querySelector('[data-form="content"]'); |
| 55 |
|
| 56 |
if (contentForm) { |
| 57 |
contentForm.addEventListener('submit', async (e) => { |
| 58 |
e.preventDefault(); |
| 59 |
|
| 60 |
const fields = Array.from(document.querySelectorAll('[name="ag_settings[terms][]"]')); |
| 61 |
|
| 62 |
let all = []; |
| 63 |
const backup = []; |
| 64 |
|
| 65 |
fields.forEach((field) => { |
| 66 |
const object = JSON.parse(field.value); |
| 67 |
backup.push(field.value); |
| 68 |
|
| 69 |
var values = Object.values(object); |
| 70 |
var portion = {}; |
| 71 |
|
| 72 |
var final = []; |
| 73 |
var counter = 0; |
| 74 |
|
| 75 |
for (var key in object) { |
| 76 |
if (counter !== 0 && counter % 50 === 0) { |
| 77 |
final.push(portion); |
| 78 |
portion = {}; |
| 79 |
} |
| 80 |
portion[key] = values[counter]; |
| 81 |
counter++ |
| 82 |
} |
| 83 |
final.push(portion); |
| 84 |
|
| 85 |
field.value = ''; |
| 86 |
|
| 87 |
all = all.concat(final); |
| 88 |
}) |
| 89 |
|
| 90 |
|
| 91 |
const progress = document.createElement('progress'); |
| 92 |
const wrapper = document.createElement('div'); |
| 93 |
wrapper.className = 'age-gate-overlay'; |
| 94 |
progress.className = 'age-gate-progress'; |
| 95 |
wrapper.append(progress); |
| 96 |
document.body.appendChild(wrapper); |
| 97 |
progress.max = all.length; |
| 98 |
progress.value = 0; |
| 99 |
|
| 100 |
// final.forEach(wl => console.log(wl)); |
| 101 |
|
| 102 |
const r = await storeItems(all); |
| 103 |
|
| 104 |
if (r === false) { |
| 105 |
backup.forEach((d, i) => fields[i].value = d); |
| 106 |
|
| 107 |
const overlay = document.querySelector('.age-gate-overlay'); |
| 108 |
|
| 109 |
if (overlay) { |
| 110 |
overlay.parentNode.removeChild(overlay) |
| 111 |
} |
| 112 |
|
| 113 |
const { save_error } = ag_content_params; |
| 114 |
|
| 115 |
document.querySelector('.wrap h2').insertAdjacentHTML('afterend', `<div class="notice notice-error"><p><strong>${save_error}</strong></p></div>`); |
| 116 |
|
| 117 |
window.scrollTo({ |
| 118 |
top: 0, |
| 119 |
left: 0, |
| 120 |
behavior: "smooth", |
| 121 |
}) |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
contentForm.submit(); |
| 126 |
|
| 127 |
// console.log(r); |
| 128 |
}) |
| 129 |
} |
| 130 |
}); |
| 131 |
|