Test Failed
Push — feature/background-processing ( ddf6d7 )
by Ravinder
06:04
created

Give_Roles::remove_caps()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 42
Code Lines 24

Duplication

Lines 42
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 12
nop 0
dl 42
loc 42
rs 8.439
c 0
b 0
f 0
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
	/**
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
	}
40
41
	/**
42
	 * Add Roles
43
	 *
44
	 * Add new shop roles with default WordPress capabilities.
45
	 *
46
	 * @since  1.0
47
	 * @access public
48
	 *
49
	 * @return void
50
	 */
51
	public function add_roles() {
52
		add_role( 'give_manager', esc_html__( 'Give Manager', 'give' ), array(
0 ignored issues
show
introduced by
Use wpcom_vip_add_role() instead of add_role()
Loading history...
53
			'read'                   => true,
54
			'edit_posts'             => true,
55
			'delete_posts'           => true,
56
			'unfiltered_html'        => true,
57
			'upload_files'           => true,
58
			'export'                 => true,
59
			'import'                 => true,
60
			'delete_others_pages'    => true,
61
			'delete_others_posts'    => true,
62
			'delete_pages'           => true,
63
			'delete_private_pages'   => true,
64
			'delete_private_posts'   => true,
65
			'delete_published_pages' => true,
66
			'delete_published_posts' => true,
67
			'edit_others_pages'      => true,
68
			'edit_others_posts'      => true,
69
			'edit_pages'             => true,
70
			'edit_private_pages'     => true,
71
			'edit_private_posts'     => true,
72
			'edit_published_pages'   => true,
73
			'edit_published_posts'   => true,
74
			'manage_categories'      => true,
75
			'manage_links'           => true,
76
			'moderate_comments'      => true,
77
			'publish_pages'          => true,
78
			'publish_posts'          => true,
79
			'read_private_pages'     => true,
80
			'read_private_posts'     => true
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
81
		) );
82
83
		add_role( 'give_accountant', esc_html__( 'Give Accountant', 'give' ), array(
0 ignored issues
show
introduced by
Use wpcom_vip_add_role() instead of add_role()
Loading history...
84
			'read'         => true,
85
			'edit_posts'   => false,
86
			'delete_posts' => false
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
87
		) );
88
89
		add_role( 'give_worker', esc_html__( 'Give Worker', 'give' ), array(
0 ignored issues
show
introduced by
Use wpcom_vip_add_role() instead of add_role()
Loading history...
90
			'read'         => true,
91
			'edit_posts'   => true,
92
            'edit_pages'   => true,
93
			'upload_files' => true,
94
			'delete_posts' => false
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
95
		) );
96
97
	}
98
99
	/**
100
	 * Add Capabilities
101
	 *
102
	 * Add new shop-specific capabilities.
103
	 *
104
	 * @since  1.0
105
	 * @access public
106
	 *
107
	 * @global WP_Roles $wp_roles
108
	 *
109
	 * @return void
110
	 */
111 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...
112
		global $wp_roles;
113
114
		if ( class_exists( 'WP_Roles' ) ) {
115
			if ( ! isset( $wp_roles ) ) {
116
				$wp_roles = new WP_Roles();
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
117
			}
118
		}
119
120
		if ( is_object( $wp_roles ) ) {
121
			$wp_roles->add_cap( 'give_manager', 'view_give_reports' );
122
			$wp_roles->add_cap( 'give_manager', 'view_give_sensitive_data' );
123
			$wp_roles->add_cap( 'give_manager', 'export_give_reports' );
124
			$wp_roles->add_cap( 'give_manager', 'manage_give_settings' );
125
126
			$wp_roles->add_cap( 'administrator', 'view_give_reports' );
127
			$wp_roles->add_cap( 'administrator', 'view_give_sensitive_data' );
128
			$wp_roles->add_cap( 'administrator', 'export_give_reports' );
129
			$wp_roles->add_cap( 'administrator', 'manage_give_settings' );
130
131
			// Add the main post type capabilities.
132
			$capabilities = $this->get_core_caps();
133
			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
			$wp_roles->add_cap( 'give_accountant', 'read_private_give_forms' );
143
			$wp_roles->add_cap( 'give_accountant', 'view_give_reports' );
144
			$wp_roles->add_cap( 'give_accountant', 'export_give_reports' );
145
			$wp_roles->add_cap( 'give_accountant', 'edit_give_payments' );
146
147
		}
148
	}
149
150
	/**
151
	 * Get Core Capabilities
152
	 *
153
	 * Retrieve core post type capabilities.
154
	 *
155
	 * @since  1.0
156
	 * @access public
157
	 *
158
	 * @return array $capabilities Core post type capabilities.
159
	 */
160
	public function get_core_caps() {
161
		$capabilities = array();
162
163
		$capability_types = array( 'give_form', 'give_payment' );
164
165
		foreach ( $capability_types as $capability_type ) {
166
			$capabilities[ $capability_type ] = array(
167
				// Post type.
168
				"edit_{$capability_type}",
169
				"read_{$capability_type}",
170
				"delete_{$capability_type}",
171
				"edit_{$capability_type}s",
172
				"edit_others_{$capability_type}s",
173
				"publish_{$capability_type}s",
174
				"read_private_{$capability_type}s",
175
				"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 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...
251
252
		global $wp_roles;
253
254
		if ( class_exists( 'WP_Roles' ) ) {
255
			if ( ! isset( $wp_roles ) ) {
256
				$wp_roles = new WP_Roles();
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
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