| 1 |
<?php |
| 2 |
|
| 3 |
namespace WcDPD; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Capabilities |
| 9 |
* |
| 10 |
* Centralizes DPD plugin capability management. Uses classic WP role capability |
| 11 |
* grant (a `user_has_cap` filter is intentionally NOT used; that approach is |
| 12 |
* fragile -- `map_meta_cap` can interfere, and the cap becomes hard to introspect |
| 13 |
* via standard WP role UIs). |
| 14 |
* |
| 15 |
* Defense-in-depth principle used in this plugin: |
| 16 |
* - Capability checks live at the ENTRY POINTS (admin actions, AJAX, REST). |
| 17 |
* - Data-layer methods (Order::export, Order::reset, Order::bulkDownloadLabels) |
| 18 |
* are deliberately NOT gated by `current_user_can` -- they may also be called |
| 19 |
* by system actions like AutoExport, which is not a user action. |
| 20 |
*/ |
| 21 |
class Capabilities |
| 22 |
{ |
| 23 |
public const CAPABILITY = 'manage_dpd_export'; |
| 24 |
|
| 25 |
public const SETTINGS_CAPABILITY = 'manage_woocommerce'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Wire up the admin_init hook that grants the role capability once. |
| 29 |
* |
| 30 |
* Called from Core::init() after the plugin is fully bootstrapped. |
| 31 |
*/ |
| 32 |
public static function init(): void |
| 33 |
{ |
| 34 |
add_action('admin_init', [__CLASS__, 'maybeGrantCaps']); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Grant the DPD capability to the standard WC roles. |
| 39 |
* |
| 40 |
* Idempotent: runs once per installation, gated by the `wc_dpd_caps_granted_version` |
| 41 |
* option. Removing the option (or lowering the value) forces a re-grant. |
| 42 |
*/ |
| 43 |
public static function maybeGrantCaps(): void |
| 44 |
{ |
| 45 |
$granted_version = get_option('wc_dpd_caps_granted_version'); |
| 46 |
|
| 47 |
if ($granted_version === '9.0.0') { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
self::addCaps(); |
| 52 |
|
| 53 |
update_option('wc_dpd_caps_granted_version', '9.0.0', false); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add the `manage_dpd_export` capability to the standard WC roles. |
| 58 |
* |
| 59 |
* Exposed as a public static method so the activation hook (in wc-dpd.php) |
| 60 |
* can call it directly without going through the admin_init path. |
| 61 |
*/ |
| 62 |
public static function addCaps(): void |
| 63 |
{ |
| 64 |
$roles_to_grant = ['administrator', 'shop_manager']; |
| 65 |
|
| 66 |
foreach ($roles_to_grant as $role_name) { |
| 67 |
$role = get_role($role_name); |
| 68 |
|
| 69 |
if (!$role) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
if (!$role->has_cap(self::CAPABILITY)) { |
| 74 |
$role->add_cap(self::CAPABILITY); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Remove the `manage_dpd_export` capability from all roles. |
| 81 |
* |
| 82 |
* Called from the uninstall hook (wc-dpd.php) -- NOT from activation reversal. |
| 83 |
*/ |
| 84 |
public static function removeCaps(): void |
| 85 |
{ |
| 86 |
global $wp_roles; |
| 87 |
|
| 88 |
if (!isset($wp_roles) || !is_object($wp_roles)) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
foreach ($wp_roles->role_objects as $role) { |
| 93 |
if ($role->has_cap(self::CAPABILITY)) { |
| 94 |
$role->remove_cap(self::CAPABILITY); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
delete_option('wc_dpd_caps_granted_version'); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Whether the current user may manage DPD export actions. |
| 103 |
*/ |
| 104 |
public static function currentUserCanManageExport(): bool |
| 105 |
{ |
| 106 |
return current_user_can(self::CAPABILITY); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Whether the current user may modify DPD plugin settings. |
| 111 |
*/ |
| 112 |
public static function currentUserCanManageDpdSettings(): bool |
| 113 |
{ |
| 114 |
return current_user_can(self::SETTINGS_CAPABILITY); |
| 115 |
} |
| 116 |
} |
| 117 |
|