| 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( |
| 55 |
'give_manager', |
| 56 |
__( 'GiveWP Manager', 'give' ), |
| 57 |
array( |
| 58 |
'read' => true, |
| 59 |
'edit_posts' => true, |
| 60 |
'delete_posts' => true, |
| 61 |
'unfiltered_html' => true, |
| 62 |
'upload_files' => true, |
| 63 |
'export' => false, |
| 64 |
'import' => false, |
| 65 |
'delete_others_pages' => false, |
| 66 |
'delete_others_posts' => false, |
| 67 |
'delete_pages' => true, |
| 68 |
'delete_private_pages' => true, |
| 69 |
'delete_private_posts' => true, |
| 70 |
'delete_published_pages' => true, |
| 71 |
'delete_published_posts' => true, |
| 72 |
'edit_others_pages' => false, |
| 73 |
'edit_others_posts' => false, |
| 74 |
'edit_pages' => true, |
| 75 |
'edit_private_pages' => true, |
| 76 |
'edit_private_posts' => true, |
| 77 |
'edit_published_pages' => true, |
| 78 |
'edit_published_posts' => true, |
| 79 |
'manage_categories' => false, |
| 80 |
'manage_links' => true, |
| 81 |
'moderate_comments' => true, |
| 82 |
'publish_pages' => true, |
| 83 |
'publish_posts' => true, |
| 84 |
'read_private_pages' => true, |
| 85 |
'read_private_posts' => true, |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
add_role( |
| 90 |
'give_accountant', |
| 91 |
__( 'GiveWP Accountant', 'give' ), |
| 92 |
array( |
| 93 |
'read' => true, |
| 94 |
'edit_posts' => false, |
| 95 |
'delete_posts' => false, |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
add_role( |
| 100 |
'give_worker', |
| 101 |
__( 'GiveWP Worker', 'give' ), |
| 102 |
array( |
| 103 |
'read' => true, |
| 104 |
'edit_posts' => true, |
| 105 |
'edit_pages' => true, |
| 106 |
'upload_files' => true, |
| 107 |
'delete_posts' => false, |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
add_role( |
| 112 |
'give_donor', |
| 113 |
__( 'GiveWP Donor', 'give' ), |
| 114 |
array( |
| 115 |
'read' => true, |
| 116 |
) |
| 117 |
); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Add Capabilities |
| 123 |
* |
| 124 |
* Add new shop-specific capabilities. |
| 125 |
* |
| 126 |
* @since 1.0 |
| 127 |
* @access public |
| 128 |
* |
| 129 |
* @global WP_Roles $wp_roles |
| 130 |
* |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public function add_caps() { |
| 134 |
global $wp_roles; |
| 135 |
|
| 136 |
if ( class_exists( 'WP_Roles' ) ) { |
| 137 |
if ( ! isset( $wp_roles ) ) { |
| 138 |
$wp_roles = new WP_Roles(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if ( is_object( $wp_roles ) ) { |
| 143 |
$wp_roles->add_cap( 'give_manager', 'view_give_reports' ); |
| 144 |
$wp_roles->add_cap( 'give_manager', 'view_give_sensitive_data' ); |
| 145 |
$wp_roles->add_cap( 'give_manager', 'export_give_reports' ); |
| 146 |
$wp_roles->add_cap( 'give_manager', 'manage_give_settings' ); |
| 147 |
$wp_roles->add_cap( 'give_manager', 'view_give_payments' ); |
| 148 |
|
| 149 |
$wp_roles->add_cap( 'administrator', 'view_give_reports' ); |
| 150 |
$wp_roles->add_cap( 'administrator', 'view_give_sensitive_data' ); |
| 151 |
$wp_roles->add_cap( 'administrator', 'export_give_reports' ); |
| 152 |
$wp_roles->add_cap( 'administrator', 'manage_give_settings' ); |
| 153 |
$wp_roles->add_cap( 'administrator', 'view_give_payments' ); |
| 154 |
|
| 155 |
// Add the main post type capabilities. |
| 156 |
$capabilities = $this->get_core_caps(); |
| 157 |
foreach ( $capabilities as $cap_group ) { |
| 158 |
foreach ( $cap_group as $cap ) { |
| 159 |
$wp_roles->add_cap( 'administrator', $cap ); |
| 160 |
$wp_roles->add_cap( 'give_manager', $cap ); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// Add Capabilities to Give Workers User Role. |
| 165 |
// @since 4.14.0 replaced edit_give_payments with view_give_payments, added view_give_forms |
| 166 |
$wp_roles->add_cap( 'give_worker', 'view_give_payments' ); |
| 167 |
$wp_roles->add_cap( 'give_worker', 'view_give_forms' ); |
| 168 |
$wp_roles->add_cap( 'give_worker', 'delete_give_forms' ); |
| 169 |
$wp_roles->add_cap( 'give_worker', 'delete_others_give_forms' ); |
| 170 |
$wp_roles->add_cap( 'give_worker', 'delete_private_give_forms' ); |
| 171 |
$wp_roles->add_cap( 'give_worker', 'delete_published_give_forms' ); |
| 172 |
$wp_roles->add_cap( 'give_worker', 'edit_give_forms' ); |
| 173 |
$wp_roles->add_cap( 'give_worker', 'edit_others_give_forms' ); |
| 174 |
$wp_roles->add_cap( 'give_worker', 'edit_private_give_forms' ); |
| 175 |
$wp_roles->add_cap( 'give_worker', 'edit_published_give_forms' ); |
| 176 |
$wp_roles->add_cap( 'give_worker', 'publish_give_forms' ); |
| 177 |
$wp_roles->add_cap( 'give_worker', 'read_private_give_forms' ); |
| 178 |
|
| 179 |
// Add Capabilities to Give Accountant User Role. |
| 180 |
// @since 4.14.0 replaced edit_give_forms with view_give_forms |
| 181 |
$wp_roles->add_cap( 'give_accountant', 'view_give_forms' ); |
| 182 |
$wp_roles->add_cap( 'give_accountant', 'read_private_give_forms' ); |
| 183 |
$wp_roles->add_cap( 'give_accountant', 'view_give_reports' ); |
| 184 |
$wp_roles->add_cap( 'give_accountant', 'export_give_reports' ); |
| 185 |
$wp_roles->add_cap( 'give_accountant', 'edit_give_payments' ); |
| 186 |
$wp_roles->add_cap( 'give_accountant', 'view_give_payments' ); |
| 187 |
|
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get Core Capabilities |
| 193 |
* |
| 194 |
* Retrieve core post type capabilities. |
| 195 |
* |
| 196 |
* @since 4.14.0 added view to capability types |
| 197 |
* @since 1.0 |
| 198 |
* @access public |
| 199 |
* |
| 200 |
* @return array $capabilities Core post type capabilities. |
| 201 |
*/ |
| 202 |
public function get_core_caps() { |
| 203 |
$capabilities = array(); |
| 204 |
|
| 205 |
$capability_types = array( 'give_form', 'give_payment' ); |
| 206 |
|
| 207 |
foreach ( $capability_types as $capability_type ) { |
| 208 |
$capabilities[ $capability_type ] = array( |
| 209 |
// Post type. |
| 210 |
"view_{$capability_type}s", |
| 211 |
"edit_{$capability_type}s", |
| 212 |
"edit_others_{$capability_type}s", |
| 213 |
"publish_{$capability_type}s", |
| 214 |
"read_private_{$capability_type}s", |
| 215 |
"delete_{$capability_type}s", |
| 216 |
"delete_private_{$capability_type}s", |
| 217 |
"delete_published_{$capability_type}s", |
| 218 |
"delete_others_{$capability_type}s", |
| 219 |
"edit_private_{$capability_type}s", |
| 220 |
"edit_published_{$capability_type}s", |
| 221 |
|
| 222 |
// Terms / taxonomies. |
| 223 |
"manage_{$capability_type}_terms", |
| 224 |
"edit_{$capability_type}_terms", |
| 225 |
"delete_{$capability_type}_terms", |
| 226 |
"assign_{$capability_type}_terms", |
| 227 |
|
| 228 |
// Custom capabilities. |
| 229 |
"view_{$capability_type}_stats", |
| 230 |
"import_{$capability_type}s", |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
return $capabilities; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Meta Capabilities |
| 239 |
* |
| 240 |
* Map meta capabilities to primitive capabilities. |
| 241 |
* |
| 242 |
* @since 1.0 |
| 243 |
* @access public |
| 244 |
* |
| 245 |
* @param array $caps Returns the user's actual capabilities. |
| 246 |
* @param string $cap Capability name. |
| 247 |
* @param int $user_id The user ID. |
| 248 |
* @param array $args Adds the context to the cap. Typically the object ID. |
| 249 |
* |
| 250 |
* @return array $caps Meta capabilities. |
| 251 |
*/ |
| 252 |
public function meta_caps( $caps, $cap, $user_id, $args ) { |
| 253 |
|
| 254 |
switch ( $cap ) { |
| 255 |
|
| 256 |
case 'view_give_form_stats': |
| 257 |
if ( empty( $args[0] ) ) { |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
$form = get_post( $args[0] ); |
| 262 |
if ( empty( $form ) ) { |
| 263 |
break; |
| 264 |
} |
| 265 |
|
| 266 |
if ( user_can( $user_id, 'view_give_reports' ) || $user_id == $form->post_author ) { |
| 267 |
$caps = array(); |
| 268 |
} |
| 269 |
|
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
return $caps; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Remove Capabilities |
| 279 |
* |
| 280 |
* Remove core post type capabilities (called on uninstall). |
| 281 |
* |
| 282 |
* @since 4.14.0 added view_give_payments to remove list |
| 283 |
* @since 1.0 |
| 284 |
* @access public |
| 285 |
* |
| 286 |
* @global WP_Roles $wp_roles |
| 287 |
* |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function remove_caps() { |
| 291 |
|
| 292 |
global $wp_roles; |
| 293 |
|
| 294 |
if ( class_exists( 'WP_Roles' ) ) { |
| 295 |
if ( ! isset( $wp_roles ) ) { |
| 296 |
$wp_roles = new WP_Roles(); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if ( is_object( $wp_roles ) ) { |
| 301 |
// Give Manager Capabilities. |
| 302 |
$wp_roles->remove_cap( 'give_manager', 'view_give_reports' ); |
| 303 |
$wp_roles->remove_cap( 'give_manager', 'view_give_sensitive_data' ); |
| 304 |
$wp_roles->remove_cap( 'give_manager', 'export_give_reports' ); |
| 305 |
$wp_roles->remove_cap( 'give_manager', 'manage_give_settings' ); |
| 306 |
$wp_roles->remove_cap( 'give_manager', 'view_give_payments' ); |
| 307 |
|
| 308 |
// Site Administrator Capabilities. |
| 309 |
$wp_roles->remove_cap( 'administrator', 'view_give_reports' ); |
| 310 |
$wp_roles->remove_cap( 'administrator', 'view_give_sensitive_data' ); |
| 311 |
$wp_roles->remove_cap( 'administrator', 'export_give_reports' ); |
| 312 |
$wp_roles->remove_cap( 'administrator', 'manage_give_settings' ); |
| 313 |
$wp_roles->remove_cap( 'administrator', 'view_give_payments' ); |
| 314 |
|
| 315 |
// Remove the Main Post Type Capabilities. |
| 316 |
$capabilities = $this->get_core_caps(); |
| 317 |
|
| 318 |
foreach ( $capabilities as $cap_group ) { |
| 319 |
foreach ( $cap_group as $cap ) { |
| 320 |
$wp_roles->remove_cap( 'give_manager', $cap ); |
| 321 |
$wp_roles->remove_cap( 'administrator', $cap ); |
| 322 |
|
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
// Remove capabilities from the Give Worker role. |
| 327 |
$wp_roles->remove_cap( 'give_worker', 'view_give_payments' ); |
| 328 |
$wp_roles->remove_cap( 'give_worker', 'view_give_forms' ); |
| 329 |
$wp_roles->remove_cap( 'give_worker', 'delete_give_forms' ); |
| 330 |
$wp_roles->remove_cap( 'give_worker', 'delete_others_give_forms' ); |
| 331 |
$wp_roles->remove_cap( 'give_worker', 'delete_private_give_forms' ); |
| 332 |
$wp_roles->remove_cap( 'give_worker', 'delete_published_give_forms' ); |
| 333 |
$wp_roles->remove_cap( 'give_worker', 'edit_give_forms' ); |
| 334 |
$wp_roles->remove_cap( 'give_worker', 'edit_others_give_forms' ); |
| 335 |
$wp_roles->remove_cap( 'give_worker', 'edit_private_give_forms' ); |
| 336 |
$wp_roles->remove_cap( 'give_worker', 'edit_published_give_forms' ); |
| 337 |
$wp_roles->remove_cap( 'give_worker', 'publish_give_forms' ); |
| 338 |
$wp_roles->remove_cap( 'give_worker', 'read_private_give_forms' ); |
| 339 |
|
| 340 |
// Remove Capabilities from Give Accountant User Role. |
| 341 |
// @since 4.14.0 replaced edit_give_forms with view_give_forms |
| 342 |
$wp_roles->remove_cap( 'give_accountant', 'view_give_forms' ); |
| 343 |
$wp_roles->remove_cap( 'give_accountant', 'read_private_give_forms' ); |
| 344 |
$wp_roles->remove_cap( 'give_accountant', 'view_give_reports' ); |
| 345 |
$wp_roles->remove_cap( 'give_accountant', 'export_give_reports' ); |
| 346 |
$wp_roles->remove_cap( 'give_accountant', 'edit_give_payments' ); |
| 347 |
$wp_roles->remove_cap( 'give_accountant', 'view_give_payments' ); |
| 348 |
|
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Allow admin dashboard to User with Give Accountant Role. |
| 354 |
* |
| 355 |
* Note: WooCommerce doesn't allow the user to access the WP dashboard who holds "Give Accountant" role. |
| 356 |
* |
| 357 |
* @since 1.8.14 |
| 358 |
* @updated 1.8.18 - Fixed Give conflicting by not returning $show_admin_bar https://github.com/impress-org/give/issues/2539 |
| 359 |
* |
| 360 |
* @param bool |
| 361 |
* |
| 362 |
* @return bool |
| 363 |
*/ |
| 364 |
public function manage_admin_dashboard( $show_admin_bar ) { |
| 365 |
|
| 366 |
// Get the current logged user. |
| 367 |
$current_user = wp_get_current_user(); |
| 368 |
|
| 369 |
// If user with "Give Accountant" user role is logged-in . |
| 370 |
if ( 0 !== $current_user->ID && in_array( 'give_accountant', (array) $current_user->roles, true ) ) { |
| 371 |
|
| 372 |
// Return false, means no prevention. |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
return $show_admin_bar; |
| 377 |
|
| 378 |
} |
| 379 |
} |
| 380 |
|