| 1 |
<?php |
| 2 |
/** |
| 3 |
* Audit helper — the shared spine that every Easy Invoice / Pro / Addon |
| 4 |
* write-path can call to record a meaningful action. |
| 5 |
* |
| 6 |
* Design: |
| 7 |
* • Free plugin owns the calling surface (`easy_invoice_audit_log(...)`). |
| 8 |
* • The call fires an action hook (`easy_invoice_audit_event`). |
| 9 |
* • The Team Roles addon's AuditLogger subscribes to that hook to write |
| 10 |
* a row to its custom table — but only when the addon is active. |
| 11 |
* • Webhooks (and any future addon) can subscribe to the same hook to |
| 12 |
* fan out to webhook subscribers / SIEM forwarders / Slack alerts. |
| 13 |
* |
| 14 |
* Sites without the Team Roles addon never gain a wp_easy_invoice_audit_log |
| 15 |
* table — the call becomes a no-op write but the action still fires for |
| 16 |
* other listeners. Behaviour identical to before for those sites. |
| 17 |
*/ |
| 18 |
|
| 19 |
if (!defined('ABSPATH')) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
if (!function_exists('easy_invoice_audit_log')) { |
| 24 |
/** |
| 25 |
* Record an auditable event. |
| 26 |
* |
| 27 |
* @param string $action Short verb key, e.g. 'invoice_sent', 'payment_refunded'. ≤ 60 chars. |
| 28 |
* @param string $resource_type e.g. 'invoice', 'quote', 'payment', 'client', 'user', 'system'. |
| 29 |
* @param int|string|null $resource_id Optional resource id (post ID, user ID, etc.). |
| 30 |
* @param array<string,mixed> $details Free-form context. Anything sensitive (PII, card numbers) MUST be redacted by the caller. |
| 31 |
*/ |
| 32 |
function easy_invoice_audit_log(string $action, string $resource_type = '', $resource_id = null, array $details = []): void { |
| 33 |
/** |
| 34 |
* Fired once per auditable event. |
| 35 |
* |
| 36 |
* Subscribers (in order of expected interest): |
| 37 |
* • EasyInvoicePro\Addons\TeamRoles\AuditLogger::log — writes the row |
| 38 |
* • EasyInvoicePro\Addons\Webhooks\EventBridge — fans out 'audit.event_logged' |
| 39 |
* • SIEM / Slack / external forwarders — user-installed |
| 40 |
* |
| 41 |
* @param string $action |
| 42 |
* @param string $resource_type |
| 43 |
* @param int|string|null $resource_id |
| 44 |
* @param array<string,mixed> $details |
| 45 |
*/ |
| 46 |
do_action('easy_invoice_audit_event', $action, $resource_type, $resource_id, $details); |
| 47 |
} |
| 48 |
} |
| 49 |
|