| 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 |
// Redirect? |
| 76 |
if (response.redirect) { |
| 77 |
window.location.replace(redirect); |
| 78 |
} else { |
| 79 |
window.location.reload(); |
| 80 |
} |
| 81 |
} else { |
| 82 |
|
| 83 |
// set cookie if activated. |
| 84 |
if ('on' === ps_ajax.use_cookie) { |
| 85 |
setHashedCookie(input); |
| 86 |
} |
| 87 |
form.find('.passster-error').hide(); |
| 88 |
|
| 89 |
// replace shortcodes. |
| 90 |
let content = response.content; |
| 91 |
|
| 92 |
if (content) { |
| 93 |
$.each(ps_ajax.shortcodes, function (key, value) { |
| 94 |
content = content.replace(key, value); |
| 95 |
}); |
| 96 |
|
| 97 |
$("#" + ps_id).replaceWith(content); |
| 98 |
} |
| 99 |
|
| 100 |
// Redirect? |
| 101 |
if (response.redirect) { |
| 102 |
window.location.replace(redirect); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
} else { |
| 107 |
form.find('.passster-error').text(response.error); |
| 108 |
form.find('.passster-error').show().fadeOut(3500); |
| 109 |
$("#" + ps_id + ' .passster-password').val(''); |
| 110 |
} |
| 111 |
} |
| 112 |
}); |
| 113 |
}); |
| 114 |
|
| 115 |
// Recaptcha v2 |
| 116 |
if ($('.recaptcha-form-v2').length > 0) { |
| 117 |
grecaptcha.ready(function () { |
| 118 |
grecaptcha.render('ps-recaptcha-v2', { |
| 119 |
'sitekey': ps_ajax.recaptcha_key, 'callback': function (token) { |
| 120 |
ps_id = $('.recaptcha-v2-submit').attr('data-psid'); |
| 121 |
form = $("#" + ps_id); |
| 122 |
protection = $('.recaptcha-v2-submit').attr('data-protection'); |
| 123 |
acf = $('.recaptcha-v2-submit').attr('data-acf'); |
| 124 |
area = $("#" + ps_id).find('.recaptcha-v2-submit').attr('data-area'); |
| 125 |
redirect = $("#" + ps_id).find('.recaptcha-v2-submit').attr('data-redirect'); |
| 126 |
|
| 127 |
$.ajax({ |
| 128 |
type: "post", dataType: "json", url: ps_ajax.ajax_url, data: { |
| 129 |
'action': 'validate_input', |
| 130 |
'nonce': ps_ajax.nonce, |
| 131 |
'token': token, |
| 132 |
'post_id': ps_ajax.post_id, |
| 133 |
'type': 'recaptcha', |
| 134 |
'protection': protection, |
| 135 |
'captcha_id': ps_id, |
| 136 |
'acf': acf, |
| 137 |
'area': area, |
| 138 |
'redirect': redirect |
| 139 |
}, success: function (response) { |
| 140 |
if (true === response.success) { |
| 141 |
// if no ajax. |
| 142 |
if ('on' === ps_ajax.no_ajax) { |
| 143 |
Cookies.set('passster', 'recaptcha', { |
| 144 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 145 |
}); |
| 146 |
window.location.reload(); |
| 147 |
} else { |
| 148 |
// set cookie if activated. |
| 149 |
if ('on' === ps_ajax.use_cookie) { |
| 150 |
Cookies.set('passster', 'recaptcha', { |
| 151 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 152 |
}); |
| 153 |
} |
| 154 |
form.find('.passster-error').hide(); |
| 155 |
|
| 156 |
// replace shortcodes. |
| 157 |
let content = response.content; |
| 158 |
|
| 159 |
if (content) { |
| 160 |
|
| 161 |
$.each(ps_ajax.shortcodes, function (key, value) { |
| 162 |
content = content.replace(key, value); |
| 163 |
}); |
| 164 |
|
| 165 |
$("#" + ps_id).replaceWith(content); |
| 166 |
} |
| 167 |
|
| 168 |
// Redirect? |
| 169 |
if (response.redirect) { |
| 170 |
window.location.replace(redirect); |
| 171 |
} |
| 172 |
} |
| 173 |
} else { |
| 174 |
form.find('.passster-error').text(response.error); |
| 175 |
form.find('.passster-error').show().fadeOut(3500); |
| 176 |
} |
| 177 |
} |
| 178 |
}); |
| 179 |
} |
| 180 |
}); |
| 181 |
}); |
| 182 |
} |
| 183 |
|
| 184 |
// ReCaptcha v3 |
| 185 |
$('.recaptcha-form').on('submit', function (event) { |
| 186 |
event.preventDefault(); |
| 187 |
|
| 188 |
ps_id = $(this).find('.passster-submit-recaptcha').attr('data-psid'); |
| 189 |
form = $("#" + ps_id); |
| 190 |
protection = $(this).find('.passster-submit-recaptcha').attr('data-protection'); |
| 191 |
acf = $(this).find('.passster-submit-recaptcha').attr('data-acf'); |
| 192 |
area = $(this).find('.passster-submit-recaptcha').attr('data-area'); |
| 193 |
redirect = $(this).find('.passster-submit-recaptcha').attr('data-redirect'); |
| 194 |
|
| 195 |
grecaptcha.ready(function () { |
| 196 |
grecaptcha.execute(ps_ajax.recaptcha_key, {action: 'validate_input'}).then(function (token) { |
| 197 |
|
| 198 |
form.prepend('<input type="hidden" name="token" value="' + token + '">'); |
| 199 |
form.prepend('<input type="hidden" name="action" value="validate_input">'); |
| 200 |
|
| 201 |
$.ajax({ |
| 202 |
type: "post", dataType: "json", url: ps_ajax.ajax_url, data: { |
| 203 |
'action': 'validate_input', |
| 204 |
'nonce': ps_ajax.nonce, |
| 205 |
'token': token, |
| 206 |
'post_id': ps_ajax.post_id, |
| 207 |
'type': 'recaptcha', |
| 208 |
'protection': protection, |
| 209 |
'captcha_id': ps_id, |
| 210 |
'acf': acf, |
| 211 |
'area': area, |
| 212 |
'redirect': redirect |
| 213 |
}, success: function (response) { |
| 214 |
if (true === response.success) { |
| 215 |
// if no ajax. |
| 216 |
if ('on' === ps_ajax.no_ajax) { |
| 217 |
Cookies.set('passster', 'recaptcha', { |
| 218 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 219 |
}); |
| 220 |
window.location.reload(); |
| 221 |
} else { |
| 222 |
// set cookie if activated. |
| 223 |
if ('on' === ps_ajax.use_cookie) { |
| 224 |
Cookies.set('passster', 'recaptcha', { |
| 225 |
expires: parseInt(ps_ajax.days), sameSite: 'strict' |
| 226 |
}); |
| 227 |
} |
| 228 |
form.find('.passster-error').hide(); |
| 229 |
// replace shortcodes. |
| 230 |
let content = response.content; |
| 231 |
|
| 232 |
if (content) { |
| 233 |
$.each(ps_ajax.shortcodes, function (key, value) { |
| 234 |
content = content.replace(key, value); |
| 235 |
}); |
| 236 |
|
| 237 |
form.replaceWith(content); |
| 238 |
} |
| 239 |
|
| 240 |
// Redirect? |
| 241 |
if (response.redirect) { |
| 242 |
window.location.replace(redirect); |
| 243 |
} |
| 244 |
} |
| 245 |
} else { |
| 246 |
form.find('.passster-error').text(response.error); |
| 247 |
form.find('.passster-error').show().fadeOut(3500); |
| 248 |
} |
| 249 |
} |
| 250 |
}); |
| 251 |
}); |
| 252 |
}); |
| 253 |
}); |
| 254 |
|
| 255 |
// Concurrent logout. |
| 256 |
$(document).on('click', '#ps-logout', function () { |
| 257 |
$.ajax({ |
| 258 |
type: 'post', |
| 259 |
dataType: 'json', |
| 260 |
url: ps_ajax.ajax_url, |
| 261 |
data: {'action': 'handle_logout', 'logout_nonce': ps_ajax.logout_nonce}, |
| 262 |
success: function (response) { |
| 263 |
if (true === response.success) { |
| 264 |
Cookies.set('passster', '', {expires: 0, sameSite: 'strict'}); |
| 265 |
window.location.reload(); |
| 266 |
} |
| 267 |
} |
| 268 |
}); |
| 269 |
}); |
| 270 |
}); |