|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Roles and Capabilities |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Classes/Give_Roles |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Give_Roles Class |
|
19
|
|
|
* |
|
20
|
|
|
* This class handles the role creation and assignment of capabilities for those roles. |
|
21
|
|
|
* |
|
22
|
|
|
* These roles let us have Give Accountants, Give Workers, etc, each of whom can do |
|
23
|
|
|
* certain things within the plugin. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 1.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
class Give_Roles { |
|
28
|
|
|
|
|
29
|
18 |
|
/** |
|
30
|
|
|
* Class Constructor |
|
31
|
18 |
|
* |
|
32
|
18 |
|
* Set up the Give Roles Class. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 1.0 |
|
35
|
|
|
* @access public |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct() { |
|
38
|
|
|
add_filter( 'give_map_meta_cap', array( $this, 'meta_caps' ), 10, 4 ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
18 |
|
/** |
|
42
|
18 |
|
* Add Roles |
|
43
|
18 |
|
* |
|
44
|
18 |
|
* Add new shop roles with default WordPress capabilities. |
|
45
|
18 |
|
* |
|
46
|
18 |
|
* @since 1.0 |
|
47
|
18 |
|
* @access public |
|
48
|
18 |
|
* |
|
49
|
18 |
|
* @return void |
|
50
|
18 |
|
*/ |
|
51
|
18 |
|
public function add_roles() { |
|
52
|
18 |
|
add_role( 'give_manager', esc_html__( 'Give Manager', 'give' ), array( |
|
53
|
18 |
|
'read' => true, |
|
54
|
18 |
|
'edit_posts' => true, |
|
55
|
18 |
|
'delete_posts' => true, |
|
56
|
18 |
|
'unfiltered_html' => true, |
|
57
|
18 |
|
'upload_files' => true, |
|
58
|
18 |
|
'export' => true, |
|
59
|
18 |
|
'import' => true, |
|
60
|
18 |
|
'delete_others_pages' => true, |
|
61
|
18 |
|
'delete_others_posts' => true, |
|
62
|
18 |
|
'delete_pages' => true, |
|
63
|
18 |
|
'delete_private_pages' => true, |
|
64
|
18 |
|
'delete_private_posts' => true, |
|
65
|
18 |
|
'delete_published_pages' => true, |
|
66
|
18 |
|
'delete_published_posts' => true, |
|
67
|
18 |
|
'edit_others_pages' => true, |
|
68
|
18 |
|
'edit_others_posts' => true, |
|
69
|
18 |
|
'edit_pages' => true, |
|
70
|
|
|
'edit_private_pages' => true, |
|
71
|
18 |
|
'edit_private_posts' => true, |
|
72
|
|
|
'edit_published_pages' => true, |
|
73
|
18 |
|
'edit_published_posts' => true, |
|
74
|
18 |
|
'manage_categories' => true, |
|
75
|
18 |
|
'manage_links' => true, |
|
76
|
|
|
'moderate_comments' => true, |
|
77
|
18 |
|
'publish_pages' => true, |
|
78
|
|
|
'publish_posts' => true, |
|
79
|
18 |
|
'read_private_pages' => true, |
|
80
|
18 |
|
'read_private_posts' => true |
|
81
|
18 |
|
) ); |
|
82
|
18 |
|
|
|
83
|
|
|
add_role( 'give_accountant', esc_html__( 'Give Accountant', 'give' ), array( |
|
84
|
18 |
|
'read' => true, |
|
85
|
|
|
'edit_posts' => false, |
|
86
|
18 |
|
'delete_posts' => false |
|
87
|
|
|
) ); |
|
88
|
|
|
|
|
89
|
|
|
add_role( 'give_worker', esc_html__( 'Give Worker', 'give' ), array( |
|
90
|
|
|
'read' => true, |
|
91
|
|
|
'edit_posts' => true, |
|
92
|
|
|
'edit_pages' => true, |
|
93
|
|
|
'upload_files' => true, |
|
94
|
|
|
'delete_posts' => false |
|
95
|
|
|
) ); |
|
96
|
18 |
|
|
|
97
|
18 |
|
} |
|
98
|
|
|
|
|
99
|
18 |
|
/** |
|
100
|
18 |
|
* Add Capabilities |
|
101
|
|
|
* |
|
102
|
|
|
* Add new shop-specific capabilities. |
|
103
|
18 |
|
* |
|
104
|
|
|
* @since 1.0 |
|
105
|
18 |
|
* @access public |
|
106
|
18 |
|
* |
|
107
|
18 |
|
* @global WP_Roles $wp_roles |
|
108
|
18 |
|
* |
|
109
|
18 |
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
18 |
|
public function add_caps() { |
|
112
|
18 |
|
global $wp_roles; |
|
|
|
|
|
|
113
|
18 |
|
|
|
114
|
18 |
|
if ( class_exists( 'WP_Roles' ) ) { |
|
115
|
|
|
if ( ! isset( $wp_roles ) ) { |
|
116
|
|
|
$wp_roles = new WP_Roles(); |
|
117
|
18 |
|
} |
|
118
|
18 |
|
} |
|
119
|
18 |
|
|
|
120
|
18 |
|
if ( is_object( $wp_roles ) ) { |
|
121
|
18 |
|
$wp_roles->add_cap( 'give_manager', 'view_give_reports' ); |
|
122
|
18 |
|
$wp_roles->add_cap( 'give_manager', 'view_give_sensitive_data' ); |
|
123
|
18 |
|
$wp_roles->add_cap( 'give_manager', 'export_give_reports' ); |
|
124
|
18 |
|
$wp_roles->add_cap( 'give_manager', 'manage_give_settings' ); |
|
125
|
|
|
|
|
126
|
18 |
|
$wp_roles->add_cap( 'administrator', 'view_give_reports' ); |
|
127
|
18 |
|
$wp_roles->add_cap( 'administrator', 'view_give_sensitive_data' ); |
|
128
|
18 |
|
$wp_roles->add_cap( 'administrator', 'export_give_reports' ); |
|
129
|
18 |
|
$wp_roles->add_cap( 'administrator', 'manage_give_settings' ); |
|
130
|
18 |
|
|
|
131
|
|
|
// Add the main post type capabilities. |
|
132
|
18 |
|
$capabilities = $this->get_core_caps(); |
|
133
|
18 |
|
foreach ( $capabilities as $cap_group ) { |
|
134
|
|
|
foreach ( $cap_group as $cap ) { |
|
135
|
|
|
$wp_roles->add_cap( 'administrator', $cap ); |
|
136
|
|
|
$wp_roles->add_cap( 'give_manager', $cap ); |
|
137
|
|
|
$wp_roles->add_cap( 'give_worker', $cap ); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$wp_roles->add_cap( 'give_accountant', 'edit_give_forms' ); |
|
142
|
18 |
|
$wp_roles->add_cap( 'give_accountant', 'read_private_give_forms' ); |
|
143
|
18 |
|
$wp_roles->add_cap( 'give_accountant', 'view_give_reports' ); |
|
144
|
|
|
$wp_roles->add_cap( 'give_accountant', 'export_give_reports' ); |
|
145
|
18 |
|
$wp_roles->add_cap( 'give_accountant', 'edit_give_payments' ); |
|
146
|
|
|
|
|
147
|
18 |
|
} |
|
148
|
18 |
|
} |
|
149
|
|
|
|
|
150
|
18 |
|
/** |
|
151
|
18 |
|
* Get Core Capabilities |
|
152
|
18 |
|
* |
|
153
|
18 |
|
* Retrieve core post type capabilities. |
|
154
|
18 |
|
* |
|
155
|
18 |
|
* @since 1.0 |
|
156
|
18 |
|
* @access public |
|
157
|
18 |
|
* |
|
158
|
18 |
|
* @return array $capabilities Core post type capabilities. |
|
159
|
18 |
|
*/ |
|
160
|
18 |
|
public function get_core_caps() { |
|
161
|
18 |
|
$capabilities = array(); |
|
162
|
18 |
|
|
|
163
|
|
|
$capability_types = array( 'give_form', 'give_payment' ); |
|
164
|
|
|
|
|
165
|
18 |
|
foreach ( $capability_types as $capability_type ) { |
|
166
|
18 |
|
$capabilities[ $capability_type ] = array( |
|
167
|
18 |
|
// Post type. |
|
168
|
18 |
|
"edit_{$capability_type}", |
|
169
|
|
|
"read_{$capability_type}", |
|
170
|
|
|
"delete_{$capability_type}", |
|
171
|
18 |
|
"edit_{$capability_type}s", |
|
172
|
18 |
|
"edit_others_{$capability_type}s", |
|
173
|
18 |
|
"publish_{$capability_type}s", |
|
174
|
|
|
"read_private_{$capability_type}s", |
|
175
|
18 |
|
"delete_{$capability_type}s", |
|
176
|
|
|
"delete_private_{$capability_type}s", |
|
177
|
|
|
"delete_published_{$capability_type}s", |
|
178
|
|
|
"delete_others_{$capability_type}s", |
|
179
|
|
|
"edit_private_{$capability_type}s", |
|
180
|
|
|
"edit_published_{$capability_type}s", |
|
181
|
|
|
|
|
182
|
|
|
// Terms / taxonomies. |
|
183
|
|
|
"manage_{$capability_type}_terms", |
|
184
|
|
|
"edit_{$capability_type}_terms", |
|
185
|
|
|
"delete_{$capability_type}_terms", |
|
186
|
|
|
"assign_{$capability_type}_terms", |
|
187
|
|
|
|
|
188
|
|
|
// Custom capabilities. |
|
189
|
|
|
"view_{$capability_type}_stats", |
|
190
|
|
|
"import_{$capability_type}s", |
|
191
|
|
|
); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $capabilities; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Meta Capabilities |
|
199
|
|
|
* |
|
200
|
|
|
* Map meta capabilities to primitive capabilities. |
|
201
|
|
|
* |
|
202
|
|
|
* @since 1.0 |
|
203
|
|
|
* @access public |
|
204
|
|
|
* |
|
205
|
|
|
* @param array $caps Returns the user's actual capabilities. |
|
206
|
|
|
* @param string $cap Capability name. |
|
207
|
|
|
* @param int $user_id The user ID. |
|
208
|
|
|
* @param array $args Adds the context to the cap. Typically the object ID. |
|
209
|
|
|
* |
|
210
|
|
|
* @return array $caps Meta capabilities. |
|
211
|
|
|
*/ |
|
212
|
|
|
public function meta_caps( $caps, $cap, $user_id, $args ) { |
|
213
|
|
|
|
|
214
|
|
|
switch ( $cap ) { |
|
215
|
|
|
|
|
216
|
|
|
case 'view_give_form_stats' : |
|
217
|
|
|
|
|
218
|
|
|
if ( empty( $args[0] ) ) { |
|
219
|
|
|
break; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
$form = get_post( $args[0] ); |
|
223
|
|
|
if ( empty( $form ) ) { |
|
224
|
|
|
break; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
if ( user_can( $user_id, 'view_give_reports' ) || $user_id == $form->post_author ) { |
|
228
|
|
|
$caps = array(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
break; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $caps; |
|
235
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Remove Capabilities |
|
240
|
|
|
* |
|
241
|
|
|
* Remove core post type capabilities (called on uninstall). |
|
242
|
|
|
* |
|
243
|
|
|
* @since 1.0 |
|
244
|
|
|
* @access public |
|
245
|
|
|
* |
|
246
|
|
|
* @global WP_Roles $wp_roles |
|
247
|
|
|
* |
|
248
|
|
|
* @return void |
|
249
|
|
|
*/ |
|
250
|
|
|
public function remove_caps() { |
|
251
|
|
|
|
|
252
|
|
|
global $wp_roles; |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
if ( class_exists( 'WP_Roles' ) ) { |
|
255
|
|
|
if ( ! isset( $wp_roles ) ) { |
|
256
|
|
|
$wp_roles = new WP_Roles(); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
if ( is_object( $wp_roles ) ) { |
|
261
|
|
|
// Give Manager Capabilities. |
|
262
|
|
|
$wp_roles->remove_cap( 'give_manager', 'view_give_reports' ); |
|
263
|
|
|
$wp_roles->remove_cap( 'give_manager', 'view_give_sensitive_data' ); |
|
264
|
|
|
$wp_roles->remove_cap( 'give_manager', 'export_give_reports' ); |
|
265
|
|
|
$wp_roles->remove_cap( 'give_manager', 'manage_give_settings' ); |
|
266
|
|
|
|
|
267
|
|
|
// Site Administrator Capabilities. |
|
268
|
|
|
$wp_roles->remove_cap( 'administrator', 'view_give_reports' ); |
|
269
|
|
|
$wp_roles->remove_cap( 'administrator', 'view_give_sensitive_data' ); |
|
270
|
|
|
$wp_roles->remove_cap( 'administrator', 'export_give_reports' ); |
|
271
|
|
|
$wp_roles->remove_cap( 'administrator', 'manage_give_settings' ); |
|
272
|
|
|
|
|
273
|
|
|
// Remove the Main Post Type Capabilities. |
|
274
|
|
|
$capabilities = $this->get_core_caps(); |
|
275
|
|
|
|
|
276
|
|
|
foreach ( $capabilities as $cap_group ) { |
|
277
|
|
|
foreach ( $cap_group as $cap ) { |
|
278
|
|
|
$wp_roles->remove_cap( 'give_manager', $cap ); |
|
279
|
|
|
$wp_roles->remove_cap( 'administrator', $cap ); |
|
280
|
|
|
$wp_roles->remove_cap( 'give_worker', $cap ); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** Give Accountant Capabilities */ |
|
285
|
|
|
$wp_roles->remove_cap( 'give_accountant', 'edit_give_forms' ); |
|
286
|
|
|
$wp_roles->remove_cap( 'give_accountant', 'read_private_give_forms' ); |
|
287
|
|
|
$wp_roles->remove_cap( 'give_accountant', 'view_give_reports' ); |
|
288
|
|
|
$wp_roles->remove_cap( 'give_accountant', 'export_give_reports' ); |
|
289
|
|
|
|
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
} |
|
294
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.