PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Framework / Permissions / Migrations / AddViewCapabilitiesToAdminRoles.php

AddViewCapabilitiesToAdminRoles.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Framework/Permissions/Migrations/AddViewCapabilitiesToAdminRoles.php

110 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Adds new view capabilities to give_manager and administrator roles.
11 *
12 * This migration adds the new view_give_forms and view_give_payments capabilities that were added to get_core_caps() for existing installations.
13 *
14 * @since 4.14.0
15 */
16 class AddViewCapabilitiesToAdminRoles extends Migration
17 {
18 /**
19 * @inheritdoc
20 */
21 public static function id(): string
22 {
23 return 'add_view_capabilities_to_admin_roles';
24 }
25
26 /**
27 * @inheritdoc
28 */
29 public static function title(): string
30 {
31 return 'Add view capabilities to GiveWP Manager and Administrator roles';
32 }
33
34 /**
35 * @inheritdoc
36 */
37 public static function timestamp(): int
38 {
39 return strtotime('2026-01-16');
40 }
41
42 /**
43 * @inheritdoc
44 */
45 public function run(): void
46 {
47 $this->fixRoleCapabilities();
48 $this->fixUserCapabilities();
49 }
50
51 /**
52 * Add view capabilities at the role level.
53 *
54 * @since 4.14.0
55 */
56 private function fixRoleCapabilities(): void
57 {
58 global $wp_roles;
59
60 if (!class_exists('WP_Roles')) {
61 return;
62 }
63
64 if (!isset($wp_roles)) {
65 $wp_roles = new \WP_Roles();
66 }
67
68 if (!is_object($wp_roles)) {
69 return;
70 }
71
72 // Add new view capabilities to give_manager
73 $wp_roles->add_cap('give_manager', 'view_give_forms');
74 $wp_roles->add_cap('give_manager', 'view_give_payments');
75
76 // Add new view capabilities to administrator
77 $wp_roles->add_cap('administrator', 'view_give_forms');
78 $wp_roles->add_cap('administrator', 'view_give_payments');
79 }
80
81 /**
82 * Add view capabilities at the user level for all give_manager and administrator users.
83 *
84 * @since 4.14.0
85 */
86 private function fixUserCapabilities(): void
87 {
88 $users = get_users([
89 'role__in' => ['give_manager', 'administrator'],
90 'fields' => 'ID',
91 ]);
92
93 foreach ($users as $userId) {
94 $user = get_userdata($userId);
95
96 if (!$user) {
97 continue;
98 }
99
100 if (!$user->has_cap('view_give_forms')) {
101 $user->add_cap('view_give_forms');
102 }
103
104 if (!$user->has_cap('view_give_payments')) {
105 $user->add_cap('view_give_payments');
106 }
107 }
108 }
109 }
110