| 1 |
<?php |
| 2 |
/** |
| 3 |
* Inline confirmation handler shared by addon pages. |
| 4 |
* |
| 5 |
* Hooks any element bearing [data-ei-confirm] (e.g. a delete-button inside a |
| 6 |
* form) and replaces native window.confirm() with the project's custom |
| 7 |
* EasyInvoiceConfirmation modal. Falls back to window.confirm() if the |
| 8 |
* confirmation script is unavailable. |
| 9 |
* |
| 10 |
* Supported attributes: |
| 11 |
* data-ei-confirm — message (required) |
| 12 |
* data-ei-confirm-title — modal title (optional) |
| 13 |
* data-ei-confirm-text — confirm button label (optional) |
| 14 |
* data-ei-confirm-class — Tailwind classes for confirm button (optional) |
| 15 |
*/ |
| 16 |
if (!defined('ABSPATH')) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
?> |
| 20 |
<script> |
| 21 |
(function () { |
| 22 |
if (window.__eiConfirmHelperLoaded) return; |
| 23 |
window.__eiConfirmHelperLoaded = true; |
| 24 |
|
| 25 |
document.addEventListener('click', function (e) { |
| 26 |
var btn = e.target && e.target.closest ? e.target.closest('[data-ei-confirm]') : null; |
| 27 |
if (!btn) return; |
| 28 |
if (btn.dataset.eiConfirmed === '1') { |
| 29 |
// Re-entry after user confirmed — let the native action proceed. |
| 30 |
btn.removeAttribute('data-ei-confirmed'); |
| 31 |
return; |
| 32 |
} |
| 33 |
e.preventDefault(); |
| 34 |
e.stopPropagation(); |
| 35 |
|
| 36 |
var message = btn.getAttribute('data-ei-confirm') || 'Are you sure?'; |
| 37 |
var title = btn.getAttribute('data-ei-confirm-title') || 'Confirm action'; |
| 38 |
var confirmText = btn.getAttribute('data-ei-confirm-text') || 'Confirm'; |
| 39 |
var confirmClass = btn.getAttribute('data-ei-confirm-class') || 'bg-red-600 hover:bg-red-700'; |
| 40 |
|
| 41 |
function proceed() { |
| 42 |
btn.dataset.eiConfirmed = '1'; |
| 43 |
var form = btn.closest ? btn.closest('form') : null; |
| 44 |
var isSubmit = btn.tagName === 'BUTTON' ? (btn.type || 'submit') === 'submit' : (btn.tagName === 'INPUT' && btn.type === 'submit'); |
| 45 |
if (btn.tagName === 'A' && btn.href) { |
| 46 |
// A link inside a form (a row action in a bulk-action table) |
| 47 |
// still just follows its href. |
| 48 |
window.location.href = btn.href; |
| 49 |
} else if (form && isSubmit) { |
| 50 |
if (typeof form.requestSubmit === 'function') { |
| 51 |
form.requestSubmit(btn); |
| 52 |
} else { |
| 53 |
form.submit(); |
| 54 |
} |
| 55 |
} else { |
| 56 |
// type="button" and similar: re-fire the click; the marker |
| 57 |
// above lets the element's own handler run this time. |
| 58 |
btn.click(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if (window.EasyInvoiceConfirmation && typeof window.EasyInvoiceConfirmation.show === 'function') { |
| 63 |
window.EasyInvoiceConfirmation.show({ |
| 64 |
title: title, |
| 65 |
message: message, |
| 66 |
confirmText: confirmText, |
| 67 |
confirmClass: confirmClass, |
| 68 |
onConfirm: proceed |
| 69 |
}); |
| 70 |
} else if (window.confirm(message)) { |
| 71 |
proceed(); |
| 72 |
} |
| 73 |
}, true); |
| 74 |
})(); |
| 75 |
</script> |
| 76 |
|