| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Roles |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Upstream Hide Meta Boxes |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
function upstream_hide_meta_boxes() { |
| 19 |
remove_meta_box( 'authordiv', 'project', 'normal' ); |
| 20 |
} |
| 21 |
add_action( 'admin_menu', 'upstream_hide_meta_boxes' ); |
| 22 |
|
| 23 |
/** |
| 24 |
* UpStream_Roles Class |
| 25 |
* |
| 26 |
* This class handles the role creation and assignment of capabilities for those roles. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class UpStream_Roles { |
| 31 |
|
| 32 |
/** |
| 33 |
* Add new shop roles with default WP caps |
| 34 |
* Called during installation |
| 35 |
* |
| 36 |
* @access public |
| 37 |
* @since 1.0.0 |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function add_roles() { |
| 41 |
$wp_roles_obj = wp_roles(); |
| 42 |
|
| 43 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 44 |
upstream_install_debug( 'add_roles entry' ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! $wp_roles_obj->is_role( 'upstream_manager' ) ) { |
| 48 |
|
| 49 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 50 |
upstream_install_debug( 'add_roles upstream_manager' ); |
| 51 |
} |
| 52 |
|
| 53 |
add_role( |
| 54 |
'upstream_manager', |
| 55 |
__( 'UpStream Manager', 'upstream' ), |
| 56 |
array( |
| 57 |
'read' => true, |
| 58 |
'edit_posts' => true, |
| 59 |
'delete_posts' => true, |
| 60 |
'unfiltered_html' => true, |
| 61 |
'upload_files' => true, |
| 62 |
'export' => true, |
| 63 |
'import' => true, |
| 64 |
'delete_others_pages' => true, |
| 65 |
'delete_others_posts' => true, |
| 66 |
'delete_pages' => true, |
| 67 |
'delete_private_pages' => true, |
| 68 |
'delete_private_posts' => true, |
| 69 |
'delete_published_pages' => true, |
| 70 |
'delete_published_posts' => true, |
| 71 |
'edit_others_pages' => true, |
| 72 |
'edit_others_posts' => true, |
| 73 |
'edit_pages' => true, |
| 74 |
'edit_private_pages' => true, |
| 75 |
'edit_private_posts' => true, |
| 76 |
'edit_published_pages' => true, |
| 77 |
'edit_published_posts' => true, |
| 78 |
'manage_categories' => true, |
| 79 |
'manage_links' => true, |
| 80 |
'moderate_comments' => true, |
| 81 |
'publish_pages' => true, |
| 82 |
'publish_posts' => true, |
| 83 |
'read_private_pages' => true, |
| 84 |
'read_private_posts' => true, |
| 85 |
) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! $wp_roles_obj->is_role( 'upstream_user' ) ) { |
| 90 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 91 |
upstream_install_debug( 'add_roles upstream_user' ); |
| 92 |
} |
| 93 |
|
| 94 |
add_role( |
| 95 |
'upstream_user', |
| 96 |
__( 'UpStream User', 'upstream' ), |
| 97 |
array( |
| 98 |
'read' => true, |
| 99 |
'edit_posts' => true, |
| 100 |
'upload_files' => true, |
| 101 |
) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! $wp_roles_obj->is_role( 'upstream_client_user' ) ) { |
| 106 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 107 |
upstream_install_debug( 'add_roles upstream_client_user' ); |
| 108 |
} |
| 109 |
|
| 110 |
add_role( |
| 111 |
'upstream_client_user', |
| 112 |
__( 'UpStream Client User', 'upstream' ), |
| 113 |
array( |
| 114 |
'read' => true, |
| 115 |
'upload_files' => true, |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Add default UpStream capabilities |
| 123 |
* |
| 124 |
* @param string $role Role. |
| 125 |
* |
| 126 |
* @access public |
| 127 |
* @since 1.0.0 |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function add_default_caps( $role = null ) { |
| 131 |
$wp_roles_obj = wp_roles(); |
| 132 |
|
| 133 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 134 |
upstream_install_debug( 'add_default_caps entry' ); |
| 135 |
} |
| 136 |
|
| 137 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 138 |
upstream_install_debug( 'wp_roles' ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( is_object( $wp_roles_obj ) ) { |
| 142 |
|
| 143 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 144 |
upstream_install_debug( 'is_object wp_roles' ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( is_null( $role ) ) { |
| 148 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 149 |
upstream_install_debug( 'is_null wp_roles' ); |
| 150 |
} |
| 151 |
|
| 152 |
$roles_name = array( |
| 153 |
'administrator', |
| 154 |
'upstream_manager', |
| 155 |
'upstream_user', |
| 156 |
'upstream_client_user', |
| 157 |
); |
| 158 |
} else { |
| 159 |
$roles_name = array( $role ); |
| 160 |
} |
| 161 |
|
| 162 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 163 |
upstream_install_debug( 'foreach rolesName' ); |
| 164 |
} |
| 165 |
|
| 166 |
foreach ( $roles_name as $role_name ) { |
| 167 |
|
| 168 |
if ( function_exists( 'upstream_install_debug' ) ) { |
| 169 |
upstream_install_debug( 'handling ' . $role_name ); |
| 170 |
} |
| 171 |
|
| 172 |
switch ( $role_name ) { |
| 173 |
case 'administrator': |
| 174 |
// Add the main post type capabilities. |
| 175 |
$capabilities = $this->get_upstream_manager_caps(); |
| 176 |
foreach ( $capabilities as $cap_group ) { |
| 177 |
foreach ( $cap_group as $cap ) { |
| 178 |
$wp_roles_obj->add_cap( $role_name, $cap ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
break; |
| 183 |
|
| 184 |
case 'upstream_manager': |
| 185 |
// Add the main post type capabilities. |
| 186 |
$capabilities = $this->get_upstream_manager_caps(); |
| 187 |
foreach ( $capabilities as $cap_group ) { |
| 188 |
foreach ( $cap_group as $cap ) { |
| 189 |
$wp_roles_obj->add_cap( $role_name, $cap ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
break; |
| 194 |
|
| 195 |
case 'upstream_user': |
| 196 |
// Add the main post type capabilities. |
| 197 |
$capabilities = $this->get_upstream_user_caps(); |
| 198 |
foreach ( $capabilities as $cap_group ) { |
| 199 |
foreach ( $cap_group as $cap ) { |
| 200 |
$wp_roles_obj->add_cap( $role_name, $cap ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
break; |
| 205 |
|
| 206 |
case 'upstream_client_user': |
| 207 |
// Add the main post type capabilities. |
| 208 |
$capabilities = $this->get_upstream_user_caps(); |
| 209 |
foreach ( $capabilities as $cap_group ) { |
| 210 |
foreach ( $cap_group as $cap ) { |
| 211 |
$wp_roles_obj->add_cap( $role_name, $cap ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// Apply the default capabilities only once. |
| 220 |
$not_set = (int) get_option( 'upstream_default_capabilities_set' ) !== 1; |
| 221 |
|
| 222 |
if ( $not_set ) { |
| 223 |
// By default add capability for adding images in comments too all roles. |
| 224 |
$roles = array_keys( get_editable_roles() ); |
| 225 |
foreach ( $roles as $role_name ) { |
| 226 |
$role = get_role( $role_name ); |
| 227 |
$role->add_cap( 'upstream_comment_images', true ); |
| 228 |
} |
| 229 |
update_option( 'upstream_default_capabilities_set', 1 ); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Gets the core post type capabilities |
| 236 |
* |
| 237 |
* @access public |
| 238 |
* @since 1.0.0 |
| 239 |
* @return array $capabilities Core post type capabilities |
| 240 |
*/ |
| 241 |
public function get_upstream_manager_caps() { |
| 242 |
$capabilities = array(); |
| 243 |
|
| 244 |
$capability_types = array( 'project', 'client', 'milestone' ); |
| 245 |
|
| 246 |
foreach ( $capability_types as $capability_type ) { |
| 247 |
$capabilities[ $capability_type ] = array( |
| 248 |
// Post type. |
| 249 |
"edit_{$capability_type}", |
| 250 |
"read_{$capability_type}", |
| 251 |
"delete_{$capability_type}", |
| 252 |
"edit_{$capability_type}s", |
| 253 |
"edit_others_{$capability_type}s", |
| 254 |
"publish_{$capability_type}s", |
| 255 |
"read_private_{$capability_type}s", |
| 256 |
"delete_{$capability_type}s", |
| 257 |
"delete_private_{$capability_type}s", |
| 258 |
"delete_published_{$capability_type}s", |
| 259 |
"delete_others_{$capability_type}s", |
| 260 |
"edit_private_{$capability_type}s", |
| 261 |
"edit_published_{$capability_type}s", |
| 262 |
|
| 263 |
// Terms. |
| 264 |
"manage_{$capability_type}_terms", |
| 265 |
"edit_{$capability_type}_terms", |
| 266 |
"delete_{$capability_type}_terms", |
| 267 |
"assign_{$capability_type}_terms", |
| 268 |
|
| 269 |
'delete_project_discussion', |
| 270 |
'edit_project_author', |
| 271 |
'project_owner_field', |
| 272 |
'project_title_field', |
| 273 |
'project_status_field', |
| 274 |
'project_client_field', |
| 275 |
'project_users_field', |
| 276 |
'project_start_date_field', |
| 277 |
'project_end_date_field', |
| 278 |
// "project_description_field", |
| 279 |
'publish_project_milestones', |
| 280 |
'publish_project_bugs', |
| 281 |
'publish_project_discussion', |
| 282 |
'publish_project_files', |
| 283 |
'publish_project_tasks', |
| 284 |
'manage_upstream', |
| 285 |
|
| 286 |
'task_title_field', |
| 287 |
'task_assigned_to_field', |
| 288 |
'task_status_field', |
| 289 |
'task_progress_field', |
| 290 |
'task_milestone_field', |
| 291 |
'task_start_date_field', |
| 292 |
'task_end_date_field', |
| 293 |
'task_notes_field', |
| 294 |
|
| 295 |
'bug_title_field', |
| 296 |
'bug_assigned_to_field', |
| 297 |
'bug_severity_field', |
| 298 |
'bug_status_field', |
| 299 |
'bug_due_date_field', |
| 300 |
'bug_description_field', |
| 301 |
'bug_files_field', |
| 302 |
|
| 303 |
'milestone_milestone_field', |
| 304 |
'milestone_assigned_to_field', |
| 305 |
'milestone_start_date_field', |
| 306 |
'milestone_end_date_field', |
| 307 |
'milestone_notes_field', |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
return $capabilities; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Gets the core post type capabilities |
| 316 |
* |
| 317 |
* @access public |
| 318 |
* @since 1.0.0 |
| 319 |
* @return array $capabilities Core post type capabilities |
| 320 |
*/ |
| 321 |
public function get_upstream_user_caps() { |
| 322 |
$capabilities['project'] = array( |
| 323 |
'edit_project', |
| 324 |
'read_project', |
| 325 |
'edit_projects', |
| 326 |
// 'edit_others_projects', |
| 327 |
// 'read_private_projects', |
| 328 |
// 'edit_private_projects', |
| 329 |
'edit_published_projects', |
| 330 |
|
| 331 |
// === TERMS === |
| 332 |
'assign_project_terms', |
| 333 |
'manage_project_terms', |
| 334 |
// 'edit_project_terms', |
| 335 |
// 'delete_project_terms', |
| 336 |
|
| 337 |
/* |
| 338 |
* Individual project fields. |
| 339 |
* Giving the role access to these fields, means that |
| 340 |
* they can edit OTHER users tasks, bugs, milestones |
| 341 |
* but only the fields added to their capabilities. |
| 342 |
* And this will only work in the WP admin. |
| 343 |
*/ |
| 344 |
'project_title_field', |
| 345 |
'project_status_field', |
| 346 |
'project_owner_field', |
| 347 |
'project_client_field', |
| 348 |
'project_users_field', |
| 349 |
'project_start_date_field', |
| 350 |
'project_end_date_field', |
| 351 |
// "project_description_field", |
| 352 |
|
| 353 |
'edit_milestone', |
| 354 |
'read_milestone', |
| 355 |
'edit_milestones', |
| 356 |
'edit_others_milestones', |
| 357 |
'read_private_milestones', |
| 358 |
'edit_private_milestones', |
| 359 |
// individual milestone fields. |
| 360 |
'milestone_milestone_field', |
| 361 |
'milestone_assigned_to_field', |
| 362 |
'milestone_start_date_field', |
| 363 |
'milestone_end_date_field', |
| 364 |
'milestone_notes_field', |
| 365 |
|
| 366 |
// individual task fields. |
| 367 |
'task_title_field', |
| 368 |
'task_assigned_to_field', |
| 369 |
'task_status_field', |
| 370 |
'task_progress_field', |
| 371 |
'task_start_date_field', |
| 372 |
'task_end_date_field', |
| 373 |
'task_notes_field', |
| 374 |
'task_milestone_field', |
| 375 |
|
| 376 |
// individual bug fields. |
| 377 |
'bug_title_field', |
| 378 |
'bug_assigned_to_field', |
| 379 |
'bug_description_field', |
| 380 |
'bug_status_field', |
| 381 |
'bug_severity_field', |
| 382 |
'bug_files_field', |
| 383 |
'bug_due_date_field', |
| 384 |
|
| 385 |
// Publish project items. |
| 386 |
'publish_project_milestones', // enables the 'Add Milestone' button within project. |
| 387 |
'publish_project_tasks', // enables the 'Add Task' button within project. |
| 388 |
'publish_project_bugs', // enables the 'Add Bug' button within project. |
| 389 |
'publish_project_files', // enables the 'Add Files' button within project. |
| 390 |
'publish_project_discussion', |
| 391 |
'delete_project_discussion', |
| 392 |
|
| 393 |
// 'delete_project_milestones', |
| 394 |
// 'delete_project_tasks', |
| 395 |
// 'delete_project_bugs', |
| 396 |
// 'delete_project_files', |
| 397 |
|
| 398 |
// 'sort_project_milestones', |
| 399 |
// 'sort_project_tasks', |
| 400 |
// 'sort_project_bugs', |
| 401 |
// 'sort_project_files', |
| 402 |
|
| 403 |
); |
| 404 |
|
| 405 |
$capabilities['client'] = array( |
| 406 |
'edit_client', |
| 407 |
'read_client', |
| 408 |
'edit_clients', |
| 409 |
'edit_others_clients', |
| 410 |
'publish_clients', |
| 411 |
// 'read_private_clients', |
| 412 |
// 'edit_private_clients', |
| 413 |
'edit_published_clients', |
| 414 |
); |
| 415 |
|
| 416 |
return $capabilities; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Remove core post type capabilities (called on uninstall) |
| 421 |
* |
| 422 |
* @access public |
| 423 |
* @since 1.5.2 |
| 424 |
* @return void |
| 425 |
*/ |
| 426 |
public function remove_caps() { |
| 427 |
$wp_roles_obj = wp_roles(); |
| 428 |
|
| 429 |
if ( is_object( $wp_roles_obj ) ) { |
| 430 |
|
| 431 |
// Add the main post type capabilities. |
| 432 |
$manager_caps = $this->get_upstream_manager_caps(); |
| 433 |
$manager_role = get_role( 'upstream_manager' ); |
| 434 |
$admin_role = get_role( 'administrator' ); |
| 435 |
|
| 436 |
foreach ( $manager_caps as $post_type ) { |
| 437 |
foreach ( $post_type as $index => $cap ) { |
| 438 |
if ( $manager_role ) { |
| 439 |
$manager_role->remove_cap( $cap ); |
| 440 |
} |
| 441 |
if ( $admin_role ) { |
| 442 |
$admin_role->remove_cap( $cap ); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
// Add the main post type capabilities. |
| 448 |
$user_caps = $this->get_upstream_user_caps(); |
| 449 |
$user_role = get_role( 'upstream_user' ); |
| 450 |
foreach ( $user_caps as $post_type ) { |
| 451 |
foreach ( $post_type as $index => $cap ) { |
| 452 |
if ( $user_role ) { |
| 453 |
$user_role->remove_cap( $cap ); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|