Completed
Push — hotfix/give_currency_filter ( 2ab3ef )
by Ravinder
49:07 queued 29:09
created

Give_Roles::add_roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 40
nc 1
nop 0
dl 0
loc 47
rs 9.0303
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 14.

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.

Loading history...
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(
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
81
		) );
82
83
		add_role( 'give_accountant', esc_html__( 'Give Accountant', 'give' ), array(
84
			'read'         => true,
85
			'edit_posts'   => false,
86
			'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
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
	public function add_caps() {
112
		global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
113
114
		if ( class_exists( 'WP_Roles' ) ) {
115
			if ( ! isset( $wp_roles ) ) {
116
				$wp_roles = new WP_Roles();
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
	public function remove_caps() {
251
252
		global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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