Give_Roles::remove_caps()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 58

Duplication

Lines 58
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
nc 12
nop 0
dl 58
loc 58
rs 8.2941
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Roles and Capabilities
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Roles
7
 * @copyright   Copyright (c) 2016, GiveWP
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
	/**
30
	 * Class Constructor
31
	 *
32
	 * 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
		add_filter( 'woocommerce_disable_admin_bar', array( $this, 'manage_admin_dashboard' ), 10, 1 );
40
		add_filter( 'woocommerce_prevent_admin_access', array( $this, 'manage_admin_dashboard' ), 10 );
41
	}
42
43
	/**
44
	 * Add Roles
45
	 *
46
	 * Add new shop roles with default WordPress capabilities.
47
	 *
48
	 * @since  1.0
49
	 * @access public
50
	 *
51
	 * @return void
52
	 */
53
	public function add_roles() {
54
		add_role( 'give_manager', __( 'Give Manager', 'give' ), array(
0 ignored issues
show
introduced by
Use wpcom_vip_add_role() instead of add_role()
Loading history...
55
			'read'                   => true,
56
			'edit_posts'             => true,
57
			'delete_posts'           => true,
58
			'unfiltered_html'        => true,
59
			'upload_files'           => true,
60
			'export'                 => false,
61
			'import'                 => false,
62
			'delete_others_pages'    => false,
63
			'delete_others_posts'    => false,
64
			'delete_pages'           => true,
65
			'delete_private_pages'   => true,
66
			'delete_private_posts'   => true,
67
			'delete_published_pages' => true,
68
			'delete_published_posts' => true,
69
			'edit_others_pages'      => false,
70
			'edit_others_posts'      => false,
71
			'edit_pages'             => true,
72
			'edit_private_pages'     => true,
73
			'edit_private_posts'     => true,
74
			'edit_published_pages'   => true,
75
			'edit_published_posts'   => true,
76
			'manage_categories'      => false,
77
			'manage_links'           => true,
78
			'moderate_comments'      => true,
79
			'publish_pages'          => true,
80
			'publish_posts'          => true,
81
			'read_private_pages'     => true,
82
			'read_private_posts'     => true,
83
		) );
84
85
		add_role( 'give_accountant', __( 'Give Accountant', 'give' ), array(
0 ignored issues
show
introduced by
Use wpcom_vip_add_role() instead of add_role()
Loading history...
86
			'read'         => true,
87
			'edit_posts'   => false,
88
			'delete_posts' => false,
89
		) );
90
91
		add_role( 'give_worker', __( 'Give Worker', 'give' ), array(
0 ignored issues
show
introduced by
Use wpcom_vip_add_role() instead of add_role()
Loading history...
92
			'read'         => true,
93
			'edit_posts'   => true,
94
			'edit_pages'   => true,
95
			'upload_files' => true,
96
			'delete_posts' => false,
97
		) );
98
99
		add_role( 'give_donor', __( 'Give Donor', 'give' ), array(
0 ignored issues
show
introduced by
Use wpcom_vip_add_role() instead of add_role()
Loading history...
100
			'read' => true,
101
		) );
102
103
	}
104
105
	/**
106
	 * Add Capabilities
107
	 *
108
	 * Add new shop-specific capabilities.
109
	 *
110
	 * @since  1.0
111
	 * @access public
112
	 *
113
	 * @global WP_Roles $wp_roles
114
	 *
115
	 * @return void
116
	 */
117 View Code Duplication
	public function add_caps() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
		global $wp_roles;
119
120
		if ( class_exists( 'WP_Roles' ) ) {
121
			if ( ! isset( $wp_roles ) ) {
122
				$wp_roles = new WP_Roles();
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
123
			}
124
		}
125
126
		if ( is_object( $wp_roles ) ) {
127
			$wp_roles->add_cap( 'give_manager', 'view_give_reports' );
128
			$wp_roles->add_cap( 'give_manager', 'view_give_sensitive_data' );
129
			$wp_roles->add_cap( 'give_manager', 'export_give_reports' );
130
			$wp_roles->add_cap( 'give_manager', 'manage_give_settings' );
131
			$wp_roles->add_cap( 'give_manager', 'view_give_payments' );
132
133
			$wp_roles->add_cap( 'administrator', 'view_give_reports' );
134
			$wp_roles->add_cap( 'administrator', 'view_give_sensitive_data' );
135
			$wp_roles->add_cap( 'administrator', 'export_give_reports' );
136
			$wp_roles->add_cap( 'administrator', 'manage_give_settings' );
137
			$wp_roles->add_cap( 'administrator', 'view_give_payments' );
138
139
			// Add the main post type capabilities.
140
			$capabilities = $this->get_core_caps();
141
			foreach ( $capabilities as $cap_group ) {
142
				foreach ( $cap_group as $cap ) {
143
					$wp_roles->add_cap( 'administrator', $cap );
144
					$wp_roles->add_cap( 'give_manager', $cap );
145
				}
146
			}
147
148
			// Add Capabilities to Give Workers User Role.
149
			$wp_roles->add_cap( 'give_worker', 'edit_give_payments' );
150
			$wp_roles->add_cap( 'give_worker', 'delete_give_forms' );
151
			$wp_roles->add_cap( 'give_worker', 'delete_others_give_forms' );
152
			$wp_roles->add_cap( 'give_worker', 'delete_private_give_forms' );
153
			$wp_roles->add_cap( 'give_worker', 'delete_published_give_forms' );
154
			$wp_roles->add_cap( 'give_worker', 'edit_give_forms' );
155
			$wp_roles->add_cap( 'give_worker', 'edit_others_give_forms' );
156
			$wp_roles->add_cap( 'give_worker', 'edit_private_give_forms' );
157
			$wp_roles->add_cap( 'give_worker', 'edit_published_give_forms' );
158
			$wp_roles->add_cap( 'give_worker', 'publish_give_forms' );
159
			$wp_roles->add_cap( 'give_worker', 'read_private_give_forms' );
160
161
			// Add Capabilities to Give Accountant User Role.
162
			$wp_roles->add_cap( 'give_accountant', 'edit_give_forms' );
163
			$wp_roles->add_cap( 'give_accountant', 'read_private_give_forms' );
164
			$wp_roles->add_cap( 'give_accountant', 'view_give_reports' );
165
			$wp_roles->add_cap( 'give_accountant', 'export_give_reports' );
166
			$wp_roles->add_cap( 'give_accountant', 'edit_give_payments' );
167
			$wp_roles->add_cap( 'give_accountant', 'view_give_payments' );
168
169
		}
170
	}
171
172
	/**
173
	 * Get Core Capabilities
174
	 *
175
	 * Retrieve core post type capabilities.
176
	 *
177
	 * @since  1.0
178
	 * @access public
179
	 *
180
	 * @return array $capabilities Core post type capabilities.
181
	 */
182
	public function get_core_caps() {
183
		$capabilities = array();
184
185
		$capability_types = array( 'give_form', 'give_payment' );
186
187
		foreach ( $capability_types as $capability_type ) {
188
			$capabilities[ $capability_type ] = array(
189
				// Post type.
190
				"edit_{$capability_type}",
191
				"read_{$capability_type}",
192
				"delete_{$capability_type}",
193
				"edit_{$capability_type}s",
194
				"edit_others_{$capability_type}s",
195
				"publish_{$capability_type}s",
196
				"read_private_{$capability_type}s",
197
				"delete_{$capability_type}s",
198
				"delete_private_{$capability_type}s",
199
				"delete_published_{$capability_type}s",
200
				"delete_others_{$capability_type}s",
201
				"edit_private_{$capability_type}s",
202
				"edit_published_{$capability_type}s",
203
204
				// Terms / taxonomies.
205
				"manage_{$capability_type}_terms",
206
				"edit_{$capability_type}_terms",
207
				"delete_{$capability_type}_terms",
208
				"assign_{$capability_type}_terms",
209
210
				// Custom capabilities.
211
				"view_{$capability_type}_stats",
212
				"import_{$capability_type}s",
213
			);
214
		}
215
216
		return $capabilities;
217
	}
218
219
	/**
220
	 * Meta Capabilities
221
	 *
222
	 * Map meta capabilities to primitive capabilities.
223
	 *
224
	 * @since  1.0
225
	 * @access public
226
	 *
227
	 * @param  array  $caps    Returns the user's actual capabilities.
228
	 * @param  string $cap     Capability name.
229
	 * @param  int    $user_id The user ID.
230
	 * @param  array  $args    Adds the context to the cap. Typically the object ID.
231
	 *
232
	 * @return array  $caps    Meta capabilities.
233
	 */
234
	public function meta_caps( $caps, $cap, $user_id, $args ) {
235
236
		switch ( $cap ) {
237
238
			case 'view_give_form_stats' :
239
240
				if ( empty( $args[0] ) ) {
241
					break;
242
				}
243
244
				$form = get_post( $args[0] );
245
				if ( empty( $form ) ) {
246
					break;
247
				}
248
249
				if ( user_can( $user_id, 'view_give_reports' ) || $user_id == $form->post_author ) {
250
					$caps = array();
251
				}
252
253
				break;
254
		}
255
256
		return $caps;
257
258
	}
259
260
	/**
261
	 * Remove Capabilities
262
	 *
263
	 * Remove core post type capabilities (called on uninstall).
264
	 *
265
	 * @since  1.0
266
	 * @access public
267
	 *
268
	 * @global WP_Roles $wp_roles
269
	 *
270
	 * @return void
271
	 */
272 View Code Duplication
	public function remove_caps() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
274
		global $wp_roles;
275
276
		if ( class_exists( 'WP_Roles' ) ) {
277
			if ( ! isset( $wp_roles ) ) {
278
				$wp_roles = new WP_Roles();
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
279
			}
280
		}
281
282
		if ( is_object( $wp_roles ) ) {
283
			// Give Manager Capabilities.
284
			$wp_roles->remove_cap( 'give_manager', 'view_give_reports' );
285
			$wp_roles->remove_cap( 'give_manager', 'view_give_sensitive_data' );
286
			$wp_roles->remove_cap( 'give_manager', 'export_give_reports' );
287
			$wp_roles->remove_cap( 'give_manager', 'manage_give_settings' );
288
289
			// Site Administrator Capabilities.
290
			$wp_roles->remove_cap( 'administrator', 'view_give_reports' );
291
			$wp_roles->remove_cap( 'administrator', 'view_give_sensitive_data' );
292
			$wp_roles->remove_cap( 'administrator', 'export_give_reports' );
293
			$wp_roles->remove_cap( 'administrator', 'manage_give_settings' );
294
			$wp_roles->remove_cap( 'administrator', 'view_give_payments' );
295
296
			// Remove the Main Post Type Capabilities.
297
			$capabilities = $this->get_core_caps();
298
299
			foreach ( $capabilities as $cap_group ) {
300
				foreach ( $cap_group as $cap ) {
301
					$wp_roles->remove_cap( 'give_manager', $cap );
302
					$wp_roles->remove_cap( 'administrator', $cap );
303
304
				}
305
			}
306
307
			// Remove capabilities from the Give Worker role.
308
			$wp_roles->remove_cap( 'give_worker', 'edit_give_payments' );
309
			$wp_roles->remove_cap( 'give_worker', 'delete_give_forms' );
310
			$wp_roles->remove_cap( 'give_worker', 'delete_others_give_forms' );
311
			$wp_roles->remove_cap( 'give_worker', 'delete_private_give_forms' );
312
			$wp_roles->remove_cap( 'give_worker', 'delete_published_give_forms' );
313
			$wp_roles->remove_cap( 'give_worker', 'edit_give_forms' );
314
			$wp_roles->remove_cap( 'give_worker', 'edit_others_give_forms' );
315
			$wp_roles->remove_cap( 'give_worker', 'edit_private_give_forms' );
316
			$wp_roles->remove_cap( 'give_worker', 'edit_published_give_forms' );
317
			$wp_roles->remove_cap( 'give_worker', 'publish_give_forms' );
318
			$wp_roles->remove_cap( 'give_worker', 'read_private_give_forms' );
319
320
			// Remove Capabilities from Give Accountant User Role.
321
			$wp_roles->remove_cap( 'give_accountant', 'edit_give_forms' );
322
			$wp_roles->remove_cap( 'give_accountant', 'read_private_give_forms' );
323
			$wp_roles->remove_cap( 'give_accountant', 'view_give_reports' );
324
			$wp_roles->remove_cap( 'give_accountant', 'export_give_reports' );
325
			$wp_roles->remove_cap( 'give_accountant', 'edit_give_payments' );
326
			$wp_roles->remove_cap( 'give_accountant', 'view_give_payments' );
327
328
		}
329
	}
330
331
	/**
332
	 * Allow admin dashboard to User with Give Accountant Role.
333
	 *
334
	 * Note: WooCommerce doesn't allow the user to access the WP dashboard who holds "Give Accountant" role.
335
	 *
336
	 * @since 1.8.14
337
	 * @updated 1.8.18 - Fixed Give conflicting by not returning $show_admin_bar https://github.com/impress-org/give/issues/2539
338
	 *
339
	 * @param bool
340
	 *
341
	 * @return bool
342
	 */
343
	public function manage_admin_dashboard($show_admin_bar) {
344
345
		// Get the current logged user.
346
		$current_user = wp_get_current_user();
347
348
		// If user with "Give Accountant" user role is logged-in .
349
		if ( 0 !== $current_user->ID && in_array( 'give_accountant', (array) $current_user->roles, true ) ) {
350
351
			// Return false, means no prevention.
352
			return false;
353
		}
354
355
		return $show_admin_bar;
356
357
	}
358
}
359