| 1 |
/** |
| 2 |
* Protected Content Password Form Handler. |
| 3 |
* |
| 4 |
* Handles password form submission and cookie management. |
| 5 |
* |
| 6 |
* @package King_Addons |
| 7 |
*/ |
| 8 |
(function ($) { |
| 9 |
'use strict'; |
| 10 |
|
| 11 |
/** |
| 12 |
* Initialize password forms. |
| 13 |
*/ |
| 14 |
const initPasswordForms = () => { |
| 15 |
$(document).on('submit', '.king-addons-password-form', function (e) { |
| 16 |
e.preventDefault(); |
| 17 |
|
| 18 |
const $form = $(this); |
| 19 |
const $input = $form.find('.king-addons-password-input'); |
| 20 |
const $error = $form.find('.king-addons-password-error'); |
| 21 |
const $submitText = $form.find('.king-addons-password-submit-text'); |
| 22 |
const $submitLoading = $form.find('.king-addons-password-submit-loading'); |
| 23 |
|
| 24 |
const password = $input.val(); |
| 25 |
const elementId = $form.data('element-id'); |
| 26 |
const postId = $form.data('post-id'); |
| 27 |
const scope = $form.data('scope'); |
| 28 |
const globalKey = $form.data('global-key'); |
| 29 |
const days = $form.data('days') || 7; |
| 30 |
|
| 31 |
if (!password) { |
| 32 |
$error.text('Please enter a password').show(); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Show loading state. |
| 37 |
$submitText.hide(); |
| 38 |
$submitLoading.show(); |
| 39 |
$error.hide(); |
| 40 |
$input.prop('disabled', true); |
| 41 |
|
| 42 |
$.ajax({ |
| 43 |
url: kingAddonsProtectedContent.ajaxUrl, |
| 44 |
type: 'POST', |
| 45 |
data: { |
| 46 |
action: 'king_addons_verify_password', |
| 47 |
nonce: kingAddonsProtectedContent.nonce, |
| 48 |
password: password, |
| 49 |
element_id: elementId, |
| 50 |
post_id: postId, |
| 51 |
scope: scope, |
| 52 |
global_key: globalKey, |
| 53 |
days: days |
| 54 |
}, |
| 55 |
success: function (response) { |
| 56 |
if (response.success) { |
| 57 |
// Reload page to show unlocked content. |
| 58 |
window.location.reload(); |
| 59 |
} else { |
| 60 |
$error.text(response.data.message || 'Incorrect password').show(); |
| 61 |
$input.prop('disabled', false).val('').focus(); |
| 62 |
$submitText.show(); |
| 63 |
$submitLoading.hide(); |
| 64 |
} |
| 65 |
}, |
| 66 |
error: function () { |
| 67 |
$error.text('An error occurred. Please try again.').show(); |
| 68 |
$input.prop('disabled', false); |
| 69 |
$submitText.show(); |
| 70 |
$submitLoading.hide(); |
| 71 |
} |
| 72 |
}); |
| 73 |
}); |
| 74 |
|
| 75 |
// Clear error on input. |
| 76 |
$(document).on('input', '.king-addons-password-input', function () { |
| 77 |
$(this).closest('.king-addons-password-form').find('.king-addons-password-error').hide(); |
| 78 |
}); |
| 79 |
}; |
| 80 |
|
| 81 |
// Initialize when DOM is ready. |
| 82 |
$(document).ready(initPasswordForms); |
| 83 |
|
| 84 |
}(jQuery)); |
| 85 |
|