Roles.php
557 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Name: Roles and Capabilities |
| 4 | * |
| 5 | * Menu Name: Roles & Capabilities |
| 6 | * |
| 7 | * Description: Create and Manage WordPress User Roles and Capabilities; Uses the '<a href="http://wordpress.org/plugins/members/" target="_blank" rel="noopener noreferrer">Members</a>' plugin filters for additional plugin integrations; Portions of code based on the '<a href="http://wordpress.org/plugins/members/" target="_blank" rel="noopener noreferrer">Members</a>' plugin by Justin Tadlock |
| 8 | * |
| 9 | * Version: 1.0 |
| 10 | * |
| 11 | * Category: Tools |
| 12 | * |
| 13 | * @package Pods\Components |
| 14 | * @subpackage Roles |
| 15 | */ |
| 16 | |
| 17 | if ( class_exists( 'Pods_Roles' ) ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Class Pods_Roles |
| 23 | */ |
| 24 | class Pods_Roles extends PodsComponent { |
| 25 | |
| 26 | /** |
| 27 | * {@inheritdoc} |
| 28 | */ |
| 29 | public function init() { |
| 30 | |
| 31 | add_filter( 'pods_roles_get_capabilities', array( $this, 'remove_deprecated_capabilities' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Enqueue styles |
| 36 | * |
| 37 | * @since 2.0.0 |
| 38 | */ |
| 39 | public function admin_assets() { |
| 40 | |
| 41 | wp_enqueue_style( 'pods-wizard' ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Build admin area |
| 46 | * |
| 47 | * @param $options |
| 48 | * @param $component |
| 49 | * |
| 50 | * @return void |
| 51 | * @since 2.0.0 |
| 52 | */ |
| 53 | public function admin( $options, $component ) { |
| 54 | |
| 55 | global $wp_roles; |
| 56 | |
| 57 | $default_role = get_option( 'default_role' ); |
| 58 | |
| 59 | $roles = array(); |
| 60 | |
| 61 | foreach ( $wp_roles->role_objects as $key => $role ) { |
| 62 | $count = $this->count_users( $key ); |
| 63 | |
| 64 | $roles[ $key ] = array( |
| 65 | 'id' => $key, |
| 66 | 'label' => $wp_roles->role_names[ $key ], |
| 67 | 'name' => $key, |
| 68 | 'capabilities' => count( (array) $role->capabilities ), |
| 69 | 'users' => sprintf( _n( '%s User', '%s Users', $count, 'pods' ), $count ), |
| 70 | ); |
| 71 | |
| 72 | if ( $default_role == $key ) { |
| 73 | $roles[ $key ]['label'] .= ' (site default)'; |
| 74 | } |
| 75 | |
| 76 | if ( 0 < $count && pods_is_admin( array( 'list_users' ) ) ) { |
| 77 | $roles[ $key ]['users'] .= '<br /><a href="' . admin_url( esc_url( 'users.php?role=' . $key ) ) . '">' . __( 'View Users', 'pods' ) . '</a>'; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | $ui = array( |
| 82 | 'component' => $component, |
| 83 | 'data' => $roles, |
| 84 | 'total' => count( $roles ), |
| 85 | 'total_found' => count( $roles ), |
| 86 | 'items' => __( 'Roles', 'pods' ), |
| 87 | 'item' => __( 'Role', 'pods' ), |
| 88 | 'fields' => array( |
| 89 | 'manage' => array( |
| 90 | 'label' => array( 'label' => __( 'Label', 'pods' ) ), |
| 91 | 'name' => array( 'label' => __( 'Name', 'pods' ) ), |
| 92 | 'capabilities' => array( 'label' => __( 'Capabilities', 'pods' ) ), |
| 93 | 'users' => array( |
| 94 | 'label' => __( 'Users', 'pods' ), |
| 95 | 'type' => 'text', |
| 96 | 'options' => array( |
| 97 | 'text_allow_html' => 1, |
| 98 | 'text_allowed_html_tags' => 'strong em a ul ol li b i br', |
| 99 | ), |
| 100 | ), |
| 101 | ), |
| 102 | ), |
| 103 | 'actions_disabled' => array( 'duplicate', 'view', 'export' ), |
| 104 | 'actions_custom' => array( |
| 105 | 'add' => array( $this, 'admin_add' ), |
| 106 | 'edit' => array( $this, 'admin_edit' ), |
| 107 | 'delete' => array( $this, 'admin_delete' ), |
| 108 | ), |
| 109 | 'search' => false, |
| 110 | 'searchable' => false, |
| 111 | 'sortable' => false, |
| 112 | 'pagination' => false, |
| 113 | ); |
| 114 | |
| 115 | if ( isset( $roles[ pods_var( 'id', 'get', - 1 ) ] ) ) { |
| 116 | $ui['row'] = $roles[ pods_var( 'id', 'get', - 1 ) ]; |
| 117 | } |
| 118 | |
| 119 | if ( ! pods_is_admin( array( 'pods_roles_add' ) ) ) { |
| 120 | $ui['actions_disabled'][] = 'add'; |
| 121 | } |
| 122 | |
| 123 | if ( ! pods_is_admin( array( 'pods_roles_edit' ) ) ) { |
| 124 | $ui['actions_disabled'][] = 'edit'; |
| 125 | } |
| 126 | |
| 127 | if ( count( $roles ) < 2 || ! pods_is_admin( array( 'pods_roles_delete' ) ) ) { |
| 128 | $ui['actions_disabled'][] = 'delete'; |
| 129 | } |
| 130 | |
| 131 | pods_ui( $ui ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param $obj |
| 136 | */ |
| 137 | public function admin_add( $obj ) { |
| 138 | |
| 139 | global $wp_roles; |
| 140 | |
| 141 | $capabilities = $this->get_capabilities(); |
| 142 | |
| 143 | $defaults = $this->get_default_capabilities(); |
| 144 | |
| 145 | $component = $obj->x['component']; |
| 146 | |
| 147 | $method = 'add'; |
| 148 | // ajax_add |
| 149 | pods_view( PODS_DIR . 'components/Roles/ui/add.php', compact( array_keys( get_defined_vars() ) ) ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param $duplicate |
| 154 | * @param $obj |
| 155 | * |
| 156 | * @return mixed |
| 157 | */ |
| 158 | public function admin_edit( $duplicate, $obj ) { |
| 159 | |
| 160 | global $wp_roles; |
| 161 | |
| 162 | $id = $obj->id; |
| 163 | |
| 164 | $capabilities = $this->get_capabilities(); |
| 165 | |
| 166 | $role_name = null; |
| 167 | $role_label = null; |
| 168 | $role_capabilities = null; |
| 169 | |
| 170 | foreach ( $wp_roles->role_objects as $key => $role ) { |
| 171 | if ( $key != $id ) { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | $role_name = $key; |
| 176 | $role_label = $wp_roles->role_names[ $key ]; |
| 177 | $role_capabilities = $role->capabilities; |
| 178 | } |
| 179 | |
| 180 | if ( empty( $role ) ) { |
| 181 | return $obj->error( __( 'Role not found, cannot edit it.', 'pods' ) ); |
| 182 | } |
| 183 | |
| 184 | $component = $obj->x['component']; |
| 185 | |
| 186 | $method = 'edit'; |
| 187 | // ajax_edit |
| 188 | pods_view( PODS_DIR . 'components/Roles/ui/edit.php', compact( array_keys( get_defined_vars() ) ) ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param $id |
| 193 | * @param $obj |
| 194 | * |
| 195 | * @return mixed |
| 196 | */ |
| 197 | public function admin_delete( $id, $obj ) { |
| 198 | |
| 199 | global $wp_roles; |
| 200 | |
| 201 | $id = $obj->id; |
| 202 | |
| 203 | if ( ! isset( $obj->data[ $id ] ) ) { |
| 204 | return $obj->error( __( 'Role not found, it cannot be deleted.', 'pods' ) ); |
| 205 | } |
| 206 | |
| 207 | $default_role = get_option( 'default_role' ); |
| 208 | |
| 209 | if ( $id == $default_role ) { |
| 210 | return $obj->error( sprintf( __( 'You cannot remove the <strong>%s</strong> role, you must set a new default role for the site first.', 'pods' ), $obj->data[ $id ]['name'] ) ); |
| 211 | } |
| 212 | |
| 213 | $wp_user_query = new WP_User_Query( array( 'role' => $id ) ); |
| 214 | |
| 215 | $users = $wp_user_query->get_results(); |
| 216 | |
| 217 | if ( ! empty( $users ) && is_array( $users ) ) { |
| 218 | foreach ( $users as $user ) { |
| 219 | $user_object = new WP_User( $user ); |
| 220 | |
| 221 | if ( $user_object->has_cap( $id ) ) { |
| 222 | $user_object->remove_role( $id ); |
| 223 | $user_object->set_role( $default_role ); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | remove_role( $id ); |
| 229 | |
| 230 | $roles = array(); |
| 231 | |
| 232 | foreach ( $wp_roles->role_objects as $key => $role ) { |
| 233 | $count = $this->count_users( $key ); |
| 234 | |
| 235 | $roles[ $key ] = array( |
| 236 | 'id' => $key, |
| 237 | 'label' => $wp_roles->role_names[ $key ], |
| 238 | 'name' => $key, |
| 239 | 'capabilities' => count( (array) $role->capabilities ), |
| 240 | 'users' => sprintf( _n( '%s User', '%s Users', $count, 'pods' ), $count ), |
| 241 | ); |
| 242 | |
| 243 | if ( $default_role == $key ) { |
| 244 | $roles[ $key ]['label'] .= ' (site default)'; |
| 245 | } |
| 246 | |
| 247 | if ( 0 < $count && pods_is_admin( array( 'list_users' ) ) ) { |
| 248 | $roles[ $key ]['users'] .= '<br /><a href="' . admin_url( esc_url( 'users.php?role=' . $key ) ) . '">' . __( 'View Users', 'pods' ) . '</a>'; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | $name = $obj->data[ $id ]['label'] . ' (' . $obj->data[ $id ]['name'] . ')'; |
| 253 | |
| 254 | $obj->data = $roles; |
| 255 | $obj->total = count( $roles ); |
| 256 | $obj->total_found = count( $roles ); |
| 257 | |
| 258 | $obj->message( '<strong>' . $name . '</strong> ' . __( 'role removed from site.', 'pods' ) ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Handle the Add Role AJAX |
| 263 | * |
| 264 | * @param $params |
| 265 | * |
| 266 | * @return mixed|void |
| 267 | */ |
| 268 | public function ajax_add( $params ) { |
| 269 | |
| 270 | global $wp_roles; |
| 271 | |
| 272 | $role_name = pods_var_raw( 'role_name', $params ); |
| 273 | $role_label = pods_var_raw( 'role_label', $params ); |
| 274 | |
| 275 | $params->capabilities = (array) pods_var_raw( 'capabilities', $params, array() ); |
| 276 | |
| 277 | $params->custom_capabilities = (array) pods_var_raw( 'custom_capabilities', $params, array() ); |
| 278 | $params->custom_capabilities = array_filter( array_unique( $params->custom_capabilities ) ); |
| 279 | |
| 280 | $capabilities = array(); |
| 281 | |
| 282 | foreach ( $params->capabilities as $capability => $x ) { |
| 283 | if ( empty( $capability ) || true !== (boolean) $x ) { |
| 284 | continue; |
| 285 | } |
| 286 | |
| 287 | $capabilities[ esc_attr( $capability ) ] = true; |
| 288 | } |
| 289 | |
| 290 | foreach ( $params->custom_capabilities as $x => $capability ) { |
| 291 | if ( empty( $capability ) || '--1' === $x ) { |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | $capabilities[ esc_attr( $capability ) ] = true; |
| 296 | } |
| 297 | |
| 298 | if ( empty( $role_name ) ) { |
| 299 | return pods_error( __( 'Role name is required', 'pods' ) ); |
| 300 | } |
| 301 | |
| 302 | if ( empty( $role_label ) ) { |
| 303 | return pods_error( __( 'Role label is required', 'pods' ) ); |
| 304 | } |
| 305 | |
| 306 | return add_role( $role_name, $role_label, $capabilities ) instanceof WP_Role; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Handle the Edit Role AJAX |
| 311 | * |
| 312 | * @todo allow rename role_label |
| 313 | * |
| 314 | * @param $params |
| 315 | * |
| 316 | * @return bool|mixed|void |
| 317 | */ |
| 318 | public function ajax_edit( $params ) { |
| 319 | |
| 320 | global $wp_roles; |
| 321 | |
| 322 | $capabilities = $this->get_capabilities(); |
| 323 | |
| 324 | $params->capabilities = (array) pods_var_raw( 'capabilities', $params, array() ); |
| 325 | |
| 326 | $params->custom_capabilities = (array) pods_var_raw( 'custom_capabilities', $params, array() ); |
| 327 | $params->custom_capabilities = array_filter( array_unique( $params->custom_capabilities ) ); |
| 328 | |
| 329 | if ( ! isset( $params->id ) || empty( $params->id ) || ! isset( $wp_roles->role_objects[ $params->id ] ) ) { |
| 330 | return pods_error( __( 'Role not found, cannot edit it.', 'pods' ) ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @var $role WP_Role |
| 335 | */ |
| 336 | $role = $wp_roles->role_objects[ $params->id ]; |
| 337 | $role_name = $params->id; |
| 338 | $role_label = $wp_roles->role_names[ $params->id ]; |
| 339 | $role_capabilities = $role->capabilities; |
| 340 | |
| 341 | $new_capabilities = array(); |
| 342 | |
| 343 | foreach ( $params->capabilities as $capability => $x ) { |
| 344 | if ( empty( $capability ) || true !== (boolean) $x ) { |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | $new_capabilities[] = esc_attr( $capability ); |
| 349 | |
| 350 | if ( ! $role->has_cap( $capability ) ) { |
| 351 | $role->add_cap( $capability ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | foreach ( $params->custom_capabilities as $x => $capability ) { |
| 356 | if ( empty( $capability ) ) { |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | if ( in_array( $capability, $new_capabilities, true ) ) { |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | $new_capabilities[] = esc_attr( $capability ); |
| 365 | |
| 366 | if ( ! $role->has_cap( $capability ) ) { |
| 367 | $role->add_cap( $capability ); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | foreach ( $role_capabilities as $capability => $x ) { |
| 372 | if ( ! in_array( $capability, $new_capabilities, true ) && false === strpos( $capability, 'level_' ) ) { |
| 373 | $role->remove_cap( $capability ); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Basic logic from Members plugin, it counts users of a specific role |
| 382 | * |
| 383 | * @param $role |
| 384 | * |
| 385 | * @return array |
| 386 | */ |
| 387 | public function count_users( $role ) { |
| 388 | |
| 389 | $count_users = count_users(); |
| 390 | |
| 391 | $avail_roles = array(); |
| 392 | |
| 393 | foreach ( $count_users['avail_roles'] as $count_role => $count ) { |
| 394 | $avail_roles[ $count_role ] = $count; |
| 395 | } |
| 396 | |
| 397 | if ( empty( $role ) ) { |
| 398 | return $avail_roles; |
| 399 | } |
| 400 | |
| 401 | if ( ! isset( $avail_roles[ $role ] ) ) { |
| 402 | $avail_roles[ $role ] = 0; |
| 403 | } |
| 404 | |
| 405 | return $avail_roles[ $role ]; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * @return array|mixed|void |
| 410 | */ |
| 411 | public function get_capabilities() { |
| 412 | |
| 413 | global $wp_roles; |
| 414 | |
| 415 | $default_caps = $this->get_wp_capabilities(); |
| 416 | |
| 417 | $role_caps = array(); |
| 418 | |
| 419 | foreach ( $wp_roles->role_objects as $key => $role ) { |
| 420 | if ( is_array( $role->capabilities ) ) { |
| 421 | foreach ( $role->capabilities as $cap => $grant ) { |
| 422 | $role_caps[ $cap ] = $cap; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | $role_caps = array_unique( $role_caps ); |
| 428 | |
| 429 | $plugin_caps = array( |
| 430 | 'pods_roles_add', |
| 431 | 'pods_roles_delete', |
| 432 | 'pods_roles_edit', |
| 433 | ); |
| 434 | |
| 435 | $capabilities = array_merge( $default_caps, $role_caps, $plugin_caps ); |
| 436 | |
| 437 | // Gravity Forms. |
| 438 | if ( is_callable( 'GFCommon::all_caps' ) ) { |
| 439 | $capabilities = array_merge( $capabilities, GFCommon::all_caps() ); |
| 440 | } |
| 441 | |
| 442 | // To support Members filters. |
| 443 | $capabilities = apply_filters( 'members_get_capabilities', $capabilities ); |
| 444 | |
| 445 | $capabilities = apply_filters( 'pods_roles_get_capabilities', $capabilities ); |
| 446 | |
| 447 | sort( $capabilities ); |
| 448 | |
| 449 | $capabilities = array_unique( $capabilities ); |
| 450 | |
| 451 | return $capabilities; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * @return array |
| 456 | */ |
| 457 | public function get_wp_capabilities() { |
| 458 | |
| 459 | $defaults = array( |
| 460 | 'activate_plugins', |
| 461 | 'add_users', |
| 462 | 'create_users', |
| 463 | 'delete_others_pages', |
| 464 | 'delete_others_posts', |
| 465 | 'delete_pages', |
| 466 | 'delete_plugins', |
| 467 | 'delete_posts', |
| 468 | 'delete_private_pages', |
| 469 | 'delete_private_posts', |
| 470 | 'delete_published_pages', |
| 471 | 'delete_published_posts', |
| 472 | 'delete_users', |
| 473 | 'edit_dashboard', |
| 474 | 'edit_files', |
| 475 | 'edit_others_pages', |
| 476 | 'edit_others_posts', |
| 477 | 'edit_pages', |
| 478 | 'edit_plugins', |
| 479 | 'edit_posts', |
| 480 | 'edit_private_pages', |
| 481 | 'edit_private_posts', |
| 482 | 'edit_published_pages', |
| 483 | 'edit_published_posts', |
| 484 | 'edit_theme_options', |
| 485 | 'edit_themes', |
| 486 | 'edit_users', |
| 487 | 'import', |
| 488 | 'install_plugins', |
| 489 | 'install_themes', |
| 490 | 'list_users', |
| 491 | 'manage_categories', |
| 492 | 'manage_links', |
| 493 | 'manage_options', |
| 494 | 'moderate_comments', |
| 495 | 'promote_users', |
| 496 | 'publish_pages', |
| 497 | 'publish_posts', |
| 498 | 'read', |
| 499 | 'read_private_pages', |
| 500 | 'read_private_posts', |
| 501 | 'remove_users', |
| 502 | 'switch_themes', |
| 503 | 'unfiltered_html', |
| 504 | 'unfiltered_upload', |
| 505 | 'update_core', |
| 506 | 'update_plugins', |
| 507 | 'update_themes', |
| 508 | 'upload_files', |
| 509 | ); |
| 510 | |
| 511 | return $defaults; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * @return array|mixed|void |
| 516 | */ |
| 517 | public function get_default_capabilities() { |
| 518 | |
| 519 | $capabilities = array( |
| 520 | 'read', |
| 521 | ); |
| 522 | |
| 523 | // To support Members filters |
| 524 | $capabilities = apply_filters( 'members_new_role_default_capabilities', $capabilities ); |
| 525 | |
| 526 | $capabilities = apply_filters( 'pods_roles_default_capabilities', $capabilities ); |
| 527 | |
| 528 | return $capabilities; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * @param $capabilities |
| 533 | * |
| 534 | * @return array |
| 535 | */ |
| 536 | public function remove_deprecated_capabilities( $capabilities ) { |
| 537 | |
| 538 | $deprecated_capabilities = array( |
| 539 | 'level_0', |
| 540 | 'level_1', |
| 541 | 'level_2', |
| 542 | 'level_3', |
| 543 | 'level_4', |
| 544 | 'level_5', |
| 545 | 'level_6', |
| 546 | 'level_7', |
| 547 | 'level_8', |
| 548 | 'level_9', |
| 549 | 'level_10', |
| 550 | ); |
| 551 | |
| 552 | $capabilities = array_diff( $capabilities, $deprecated_capabilities ); |
| 553 | |
| 554 | return $capabilities; |
| 555 | } |
| 556 | } |
| 557 |