| 1 |
jQuery(document).ready(function ($) { |
| 2 |
|
| 3 |
function setHashedCookie(password) { |
| 4 |
$.ajax({ |
| 5 |
type: "post", dataType: "json", url: ps_ajax.ajax_url, data: { |
| 6 |
'action': 'hash_password', 'hash_nonce': ps_ajax.hash_nonce, 'password': password, |
| 7 |
}, success: function (response) { |
| 8 |
Cookies.set('passster', response.password, { |
| 9 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 10 |
}); |
| 11 |
}, async: false, |
| 12 |
}); |
| 13 |
} |
| 14 |
|
| 15 |
// Check if we have an unlock link. |
| 16 |
if (ps_ajax.link_pass) { |
| 17 |
if ('on' === ps_ajax.use_cookie) { |
| 18 |
if (ps_ajax.link_pass.length < 25) { |
| 19 |
// It's an old base64 encryption. |
| 20 |
setHashedCookie(atob(ps_ajax.link_pass)); |
| 21 |
} else { |
| 22 |
Cookies.set('passster', ps_ajax.link_pass, { |
| 23 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
window.location.replace(ps_ajax.permalink); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// Passwords |
| 32 |
$('.passster-submit').on('click', function (e) { |
| 33 |
e.preventDefault(); |
| 34 |
|
| 35 |
// Validate form before submitting ajax. |
| 36 |
var form = $(this).parent().parent(); |
| 37 |
|
| 38 |
if (!$(form)[0].checkValidity()) { |
| 39 |
$(form)[0].reportValidity(); |
| 40 |
} |
| 41 |
|
| 42 |
ps_id = $(this).attr('data-psid'); |
| 43 |
form = $("#" + ps_id); |
| 44 |
password = $("#" + ps_id + ' .passster-password').attr('data-password'); |
| 45 |
type = $("#" + ps_id + ' .passster-password').attr('data-protection-type'); |
| 46 |
list = $("#" + ps_id + ' .passster-password').attr('data-list'); |
| 47 |
area = $("#" + ps_id + ' .passster-password').attr('data-area'); |
| 48 |
protection = $("#" + ps_id + ' .passster-password').attr('data-protection'); |
| 49 |
redirect = $(this).attr('data-redirect'); |
| 50 |
input = $("#" + ps_id + ' .passster-password').val(); |
| 51 |
acf = $(this).attr('data-acf'); |
| 52 |
|
| 53 |
$.ajax({ |
| 54 |
type: "post", dataType: "json", url: ps_ajax.ajax_url, data: { |
| 55 |
'action': 'validate_input', |
| 56 |
'nonce': ps_ajax.nonce, |
| 57 |
'input': input, |
| 58 |
'password': password, |
| 59 |
'post_id': ps_ajax.post_id, |
| 60 |
'type': type, |
| 61 |
'list': list, |
| 62 |
'area': area, |
| 63 |
'protection': protection, |
| 64 |
'acf': acf, |
| 65 |
'redirect': redirect |
| 66 |
}, beforeSend: function () { |
| 67 |
form.find(".ps-loader").css('display', 'block'); |
| 68 |
}, success: function (response) { |
| 69 |
form.find(".ps-loader").css('display', 'none'); |
| 70 |
if (true === response.success) { |
| 71 |
// if no ajax. |
| 72 |
if ('on' === ps_ajax.no_ajax) { |
| 73 |
setHashedCookie(input); |
| 74 |
|
| 75 |
window.location.reload(); |
| 76 |
} else { |
| 77 |
|
| 78 |
// set cookie if activated. |
| 79 |
if ('on' === ps_ajax.use_cookie) { |
| 80 |
setHashedCookie(input); |
| 81 |
} |
| 82 |
form.find('.passster-error').hide(); |
| 83 |
|
| 84 |
// replace shortcodes. |
| 85 |
let content = response.content; |
| 86 |
|
| 87 |
if (content) { |
| 88 |
$.each(ps_ajax.shortcodes, function (key, value) { |
| 89 |
content = content.replace(key, value); |
| 90 |
}); |
| 91 |
|
| 92 |
$("#" + ps_id).replaceWith(content); |
| 93 |
} |
| 94 |
|
| 95 |
// Redirect? |
| 96 |
if (response.redirect) { |
| 97 |
window.location.replace(redirect); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
} else { |
| 102 |
form.find('.passster-error').text(response.error); |
| 103 |
form.find('.passster-error').show().fadeOut(3500); |
| 104 |
$("#" + ps_id + ' .passster-password').val(''); |
| 105 |
} |
| 106 |
} |
| 107 |
}); |
| 108 |
}); |
| 109 |
|
| 110 |
// Recaptcha v2 |
| 111 |
if ($('.recaptcha-form-v2').length > 0) { |
| 112 |
grecaptcha.ready(function () { |
| 113 |
grecaptcha.render('ps-recaptcha-v2', { |
| 114 |
'sitekey': ps_ajax.recaptcha_key, 'callback': function (token) { |
| 115 |
ps_id = $('.recaptcha-v2-submit').attr('data-psid'); |
| 116 |
form = $("#" + ps_id); |
| 117 |
protection = $('.recaptcha-v2-submit').attr('data-protection'); |
| 118 |
acf = $('.recaptcha-v2-submit').attr('data-acf'); |
| 119 |
area = $("#" + ps_id).find('.recaptcha-v2-submit').attr('data-area'); |
| 120 |
redirect = $("#" + ps_id).find('.recaptcha-v2-submit').attr('data-redirect'); |
| 121 |
|
| 122 |
$.ajax({ |
| 123 |
type: "post", dataType: "json", url: ps_ajax.ajax_url, data: { |
| 124 |
'action': 'validate_input', |
| 125 |
'nonce': ps_ajax.nonce, |
| 126 |
'token': token, |
| 127 |
'post_id': ps_ajax.post_id, |
| 128 |
'type': 'recaptcha', |
| 129 |
'protection': protection, |
| 130 |
'captcha_id': ps_id, |
| 131 |
'acf': acf, |
| 132 |
'area': area, |
| 133 |
'redirect': redirect |
| 134 |
}, success: function (response) { |
| 135 |
if (true === response.success) { |
| 136 |
// if no ajax. |
| 137 |
if ('on' === ps_ajax.no_ajax) { |
| 138 |
Cookies.set('passster', 'recaptcha', { |
| 139 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 140 |
}); |
| 141 |
window.location.reload(); |
| 142 |
} else { |
| 143 |
// set cookie if activated. |
| 144 |
if ('on' === ps_ajax.use_cookie) { |
| 145 |
Cookies.set('passster', 'recaptcha', { |
| 146 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 147 |
}); |
| 148 |
} |
| 149 |
form.find('.passster-error').hide(); |
| 150 |
|
| 151 |
// replace shortcodes. |
| 152 |
let content = response.content; |
| 153 |
|
| 154 |
if (content) { |
| 155 |
|
| 156 |
$.each(ps_ajax.shortcodes, function (key, value) { |
| 157 |
content = content.replace(key, value); |
| 158 |
}); |
| 159 |
|
| 160 |
$("#" + ps_id).replaceWith(content); |
| 161 |
} |
| 162 |
|
| 163 |
// Redirect? |
| 164 |
if (response.redirect) { |
| 165 |
window.location.replace(redirect); |
| 166 |
} |
| 167 |
} |
| 168 |
} else { |
| 169 |
form.find('.passster-error').text(response.error); |
| 170 |
form.find('.passster-error').show().fadeOut(3500); |
| 171 |
} |
| 172 |
} |
| 173 |
}); |
| 174 |
} |
| 175 |
}); |
| 176 |
}); |
| 177 |
} |
| 178 |
|
| 179 |
// ReCaptcha v3 |
| 180 |
$('.recaptcha-form').on('submit', function (event) { |
| 181 |
event.preventDefault(); |
| 182 |
|
| 183 |
ps_id = $(this).find('.passster-submit-recaptcha').attr('data-psid'); |
| 184 |
form = $("#" + ps_id); |
| 185 |
protection = $(this).find('.passster-submit-recaptcha').attr('data-protection'); |
| 186 |
acf = $(this).find('.passster-submit-recaptcha').attr('data-acf'); |
| 187 |
area = $(this).find('.passster-submit-recaptcha').attr('data-area'); |
| 188 |
redirect = $(this).find('.passster-submit-recaptcha').attr('data-redirect'); |
| 189 |
|
| 190 |
grecaptcha.ready(function () { |
| 191 |
grecaptcha.execute(ps_ajax.recaptcha_key, {action: 'validate_input'}).then(function (token) { |
| 192 |
|
| 193 |
form.prepend('<input type="hidden" name="token" value="' + token + '">'); |
| 194 |
form.prepend('<input type="hidden" name="action" value="validate_input">'); |
| 195 |
|
| 196 |
$.ajax({ |
| 197 |
type: "post", dataType: "json", url: ps_ajax.ajax_url, data: { |
| 198 |
'action': 'validate_input', |
| 199 |
'nonce': ps_ajax.nonce, |
| 200 |
'token': token, |
| 201 |
'post_id': ps_ajax.post_id, |
| 202 |
'type': 'recaptcha', |
| 203 |
'protection': protection, |
| 204 |
'captcha_id': ps_id, |
| 205 |
'acf': acf, |
| 206 |
'area': area, |
| 207 |
'redirect': redirect |
| 208 |
}, success: function (response) { |
| 209 |
if (true === response.success) { |
| 210 |
// if no ajax. |
| 211 |
if ('on' === ps_ajax.no_ajax) { |
| 212 |
Cookies.set('passster', 'recaptcha', { |
| 213 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 214 |
}); |
| 215 |
window.location.reload(); |
| 216 |
} else { |
| 217 |
// set cookie if activated. |
| 218 |
if ('on' === ps_ajax.use_cookie) { |
| 219 |
Cookies.set('passster', 'recaptcha', { |
| 220 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 221 |
}); |
| 222 |
} |
| 223 |
form.find('.passster-error').hide(); |
| 224 |
// replace shortcodes. |
| 225 |
let content = response.content; |
| 226 |
|
| 227 |
if (content) { |
| 228 |
$.each(ps_ajax.shortcodes, function (key, value) { |
| 229 |
content = content.replace(key, value); |
| 230 |
}); |
| 231 |
|
| 232 |
form.replaceWith(content); |
| 233 |
} |
| 234 |
|
| 235 |
// Redirect? |
| 236 |
if (response.redirect) { |
| 237 |
window.location.replace(redirect); |
| 238 |
} |
| 239 |
} |
| 240 |
} else { |
| 241 |
form.find('.passster-error').text(response.error); |
| 242 |
form.find('.passster-error').show().fadeOut(3500); |
| 243 |
} |
| 244 |
} |
| 245 |
}); |
| 246 |
}); |
| 247 |
}); |
| 248 |
}); |
| 249 |
|
| 250 |
// Captcha |
| 251 |
if ($('.passster-captcha-input').length > 0) { |
| 252 |
var captcha = new jCaptcha({ |
| 253 |
el: '.passster-captcha-input', canvasClass: 'jCaptchaCanvas', canvasStyle: { |
| 254 |
// properties for captcha stylings |
| 255 |
width: 100, height: 25, textBaseline: 'top', font: '22px Arial', textAlign: 'left', fillStyle: '#000', |
| 256 |
}, 'requiredValue': '', callback: function (response) { |
| 257 |
if (response == 'success') { |
| 258 |
ps_id = $('.passster-submit-captcha').attr('data-psid'); |
| 259 |
form = $("#" + ps_id); |
| 260 |
protection = $('.passster-submit-captcha').attr('data-protection'); |
| 261 |
acf = $('.passster-submit-captcha').attr('data-acf'); |
| 262 |
area = $("#" + ps_id).find('.passster-captcha-input').attr('data-area'); |
| 263 |
redirect = $('.passster-submit-captcha').attr('data-redirect'); |
| 264 |
|
| 265 |
$.ajax({ |
| 266 |
type: "post", dataType: "json", url: ps_ajax.ajax_url, data: { |
| 267 |
'action': 'validate_input', |
| 268 |
'nonce': ps_ajax.nonce, |
| 269 |
'captcha': 'success', |
| 270 |
'post_id': ps_ajax.post_id, |
| 271 |
'type': 'captcha', |
| 272 |
'protection': protection, |
| 273 |
'captcha_id': ps_id, |
| 274 |
'acf': acf, |
| 275 |
'area': area, |
| 276 |
'redirect': redirect |
| 277 |
}, success: function (response) { |
| 278 |
if (true === response.success) { |
| 279 |
// if no ajax. |
| 280 |
if ('on' === ps_ajax.no_ajax) { |
| 281 |
Cookies.set('passster', 'captcha', { |
| 282 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 283 |
}); |
| 284 |
window.location.reload(); |
| 285 |
} else { |
| 286 |
// set cookie if activated. |
| 287 |
if ('on' === ps_ajax.use_cookie) { |
| 288 |
Cookies.set('passster', 'captcha', { |
| 289 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 290 |
}); |
| 291 |
} |
| 292 |
form.find('.passster-error').hide(); |
| 293 |
// replace shortcodes. |
| 294 |
let content = response.content; |
| 295 |
|
| 296 |
if (content) { |
| 297 |
$.each(ps_ajax.shortcodes, function (key, value) { |
| 298 |
content = content.replace(key, value); |
| 299 |
}); |
| 300 |
|
| 301 |
$("#" + ps_id).replaceWith(content); |
| 302 |
} |
| 303 |
|
| 304 |
// Redirect? |
| 305 |
if (response.redirect) { |
| 306 |
window.location.replace(redirect); |
| 307 |
} |
| 308 |
} |
| 309 |
} else { |
| 310 |
form.find('.passster-error').text(response.error); |
| 311 |
form.find('.passster-error').show().fadeOut(3500); |
| 312 |
} |
| 313 |
} |
| 314 |
}); |
| 315 |
} |
| 316 |
|
| 317 |
if (response == 'error') { |
| 318 |
form.find('.passster-error').text(ps_ajax.captcha_error); |
| 319 |
form.find('.passster-error').show().fadeOut(3500); |
| 320 |
} |
| 321 |
} |
| 322 |
}); |
| 323 |
|
| 324 |
document.querySelector('.captcha-form').addEventListener('submit', function (e) { |
| 325 |
e.preventDefault(); |
| 326 |
captcha.validate() |
| 327 |
}); |
| 328 |
} |
| 329 |
// Concurrent logout. |
| 330 |
$(document).on('click', '#ps-logout', function () { |
| 331 |
$.ajax({ |
| 332 |
type: 'post', |
| 333 |
dataType: 'json', |
| 334 |
url: ps_ajax.ajax_url, |
| 335 |
data: {'action': 'handle_logout', 'logout_nonce': ps_ajax.logout_nonce}, |
| 336 |
success: function (response) { |
| 337 |
if (true === response.success) { |
| 338 |
Cookies.set('passster', '', {expires: 0, sameSite: 'strict'}); |
| 339 |
window.location.reload(); |
| 340 |
} |
| 341 |
} |
| 342 |
}); |
| 343 |
}); |
| 344 |
}); |