AddViewCapabilitiesToAdminRoles.php
5 months ago
FixGiveAccountantCapabilities.php
5 months ago
FixGiveWorkerCapabilities.php
5 months ago
FixGiveAccountantCapabilities.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Give\Framework\Permissions\Migrations; |
| 6 | |
| 7 | use Give\Framework\Migrations\Contracts\Migration; |
| 8 | |
| 9 | /** |
| 10 | * Fixes give_accountant role capabilities. |
| 11 | * |
| 12 | * The give_accountant role should only be able to view donation forms, |
| 13 | * not edit them. This migration removes the incorrectly assigned edit_give_forms |
| 14 | * capability from the give_accountant role and replaces it with view_give_forms. |
| 15 | * |
| 16 | * @since 4.14.0 |
| 17 | */ |
| 18 | class FixGiveAccountantCapabilities extends Migration |
| 19 | { |
| 20 | /** |
| 21 | * @inheritdoc |
| 22 | */ |
| 23 | public static function id(): string |
| 24 | { |
| 25 | return 'fix_give_accountant_capabilities'; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @inheritdoc |
| 30 | */ |
| 31 | public static function title(): string |
| 32 | { |
| 33 | return 'Fix GiveWP Accountant role capabilities'; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @inheritdoc |
| 38 | */ |
| 39 | public static function timestamp(): int |
| 40 | { |
| 41 | return strtotime('2026-01-12'); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @inheritdoc |
| 46 | */ |
| 47 | public function run(): void |
| 48 | { |
| 49 | $this->fixRoleCapabilities(); |
| 50 | $this->fixUserCapabilities(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Fix capabilities at the role level. |
| 55 | * |
| 56 | * @since 4.14.0 |
| 57 | */ |
| 58 | private function fixRoleCapabilities(): void |
| 59 | { |
| 60 | global $wp_roles; |
| 61 | |
| 62 | if (!class_exists('WP_Roles')) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | if (!isset($wp_roles)) { |
| 67 | $wp_roles = new \WP_Roles(); |
| 68 | } |
| 69 | |
| 70 | if (!is_object($wp_roles)) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Remove edit_give_forms from give_accountant - they should only be able to view forms |
| 75 | $wp_roles->remove_cap('give_accountant', 'edit_give_forms'); |
| 76 | |
| 77 | // Ensure give_accountant has view_give_forms for viewing donation forms |
| 78 | $wp_roles->add_cap('give_accountant', 'view_give_forms'); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Fix capabilities at the user level for all give_accountant users. |
| 83 | * |
| 84 | * This handles cases where edit_give_forms was manually granted to individual users. |
| 85 | * |
| 86 | * @since 4.14.0 |
| 87 | */ |
| 88 | private function fixUserCapabilities(): void |
| 89 | { |
| 90 | $giveAccountants = get_users([ |
| 91 | 'role' => 'give_accountant', |
| 92 | 'fields' => 'ID', |
| 93 | ]); |
| 94 | |
| 95 | foreach ($giveAccountants as $userId) { |
| 96 | $user = get_userdata($userId); |
| 97 | |
| 98 | if (!$user) { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | // Remove edit_give_forms if it was individually granted |
| 103 | if ($user->has_cap('edit_give_forms')) { |
| 104 | $user->remove_cap('edit_give_forms'); |
| 105 | } |
| 106 | |
| 107 | // Ensure view_give_forms is present |
| 108 | if (!$user->has_cap('view_give_forms')) { |
| 109 | $user->add_cap('view_give_forms'); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 |