| 1 |
<?php |
| 2 |
/** |
| 3 |
* Capability helper — bridges WordPress's `current_user_can()` with the |
| 4 |
* fine-grained capabilities defined by the Team Roles addon, while preserving |
| 5 |
* the existing "administrator-only" default for sites that aren't using the |
| 6 |
* addon at all. |
| 7 |
* |
| 8 |
* Resolution order for `easy_invoice_user_can($cap)`: |
| 9 |
* 1. If the user has WordPress's `manage_options` → true. Administrators |
| 10 |
* and Super Admins always pass; behavior identical to the legacy checks. |
| 11 |
* 2. If the user has the explicit EI capability ($cap, e.g. `ei_create_invoice`) |
| 12 |
* → true. This is the path the Team Roles addon enables. |
| 13 |
* 3. Otherwise → false. |
| 14 |
* |
| 15 |
* Why both checks? |
| 16 |
* • Sites without the Team Roles addon never grant any `ei_*` capability — |
| 17 |
* so every meaningful action only resolves via `manage_options`, exactly |
| 18 |
* as before. No behavior change for those installs. |
| 19 |
* • Sites WITH the addon get a real role system: an EI Sales user (no |
| 20 |
* `manage_options`) creating an invoice now succeeds via `ei_create_invoice`, |
| 21 |
* where it previously failed. |
| 22 |
* |
| 23 |
* Filterable via `easy_invoice_user_can` so addons / custom code can layer |
| 24 |
* stricter rules (IP allowlist, MFA-step-up, approval workflow gates). |
| 25 |
*/ |
| 26 |
|
| 27 |
if (!defined('ABSPATH')) { |
| 28 |
exit; |
| 29 |
} |
| 30 |
|
| 31 |
if (!function_exists('easy_invoice_user_can')) { |
| 32 |
/** |
| 33 |
* @param string $cap Fine-grained EI capability (e.g. `ei_create_invoice`). |
| 34 |
* @param int|null $user_id Optional — defaults to the current user. |
| 35 |
* @param mixed ...$args Forwarded to WP's user_can() / current_user_can() for object caps. |
| 36 |
* @return bool |
| 37 |
*/ |
| 38 |
function easy_invoice_user_can(string $cap, $user_id = null, ...$args): bool { |
| 39 |
$check = static function (string $c, $args) use ($user_id) { |
| 40 |
if ($user_id === null) { |
| 41 |
return $args |
| 42 |
? call_user_func_array('current_user_can', array_merge([$c], $args)) |
| 43 |
: current_user_can($c); |
| 44 |
} |
| 45 |
return $args |
| 46 |
? call_user_func_array('user_can', array_merge([$user_id, $c], $args)) |
| 47 |
: user_can($user_id, $c); |
| 48 |
}; |
| 49 |
|
| 50 |
// Administrators (and Super Admins via the WP cap-map) always pass — |
| 51 |
// they implicitly hold every meta-capability via `manage_options`. |
| 52 |
$allowed = $check('manage_options', []); |
| 53 |
|
| 54 |
// If not an admin, fall back to the explicit fine-grained cap. |
| 55 |
if (!$allowed && $cap !== '' && $cap !== 'manage_options') { |
| 56 |
$allowed = $check($cap, $args); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Filter the final allow/deny decision. |
| 61 |
* |
| 62 |
* @param bool $allowed Resolution from the cascade above. |
| 63 |
* @param string $cap The fine-grained EI capability. |
| 64 |
* @param int|null $user_id Resolved user id (null = current). |
| 65 |
* @param array $args Object-cap args forwarded from caller. |
| 66 |
*/ |
| 67 |
return (bool) apply_filters('easy_invoice_user_can', $allowed, $cap, $user_id, $args); |
| 68 |
} |
| 69 |
} |
| 70 |
|