| 1 |
<?php |
| 2 |
/** |
| 3 |
* Role logic: retrieving, updating, and checking permission. |
| 4 |
* |
| 5 |
* @package MultipleRoles |
| 6 |
*/ |
| 7 |
class MDMR_Model { |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Grab all WordPress roles. |
| 12 |
* |
| 13 |
* @return array Roles in name => label pairs. |
| 14 |
*/ |
| 15 |
public function get_roles() { |
| 16 |
global $wp_roles; |
| 17 |
return apply_filters( 'mdmr_get_roles', $wp_roles->role_names ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get all editable roles by the current user |
| 22 |
* |
| 23 |
* @return array editable roles |
| 24 |
*/ |
| 25 |
public function get_editable_roles() { |
| 26 |
$editable_roles = get_editable_roles(); |
| 27 |
$final_roles = array(); |
| 28 |
foreach ( $editable_roles as $key => $role ) { |
| 29 |
$final_roles[ $key ] = $role['name']; |
| 30 |
} |
| 31 |
|
| 32 |
return apply_filters( 'mdmr_get_editable_roles', (array) $final_roles ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Grab a particular user's roles. |
| 37 |
* |
| 38 |
* @param object|int $user The user object or ID. |
| 39 |
* @return array Roles in name => label pairs. |
| 40 |
*/ |
| 41 |
public function get_user_roles( $user = 0 ) { |
| 42 |
if ( ! $user ) { |
| 43 |
return array(); |
| 44 |
} |
| 45 |
|
| 46 |
$user = get_user_by( 'id', (int) $user ); |
| 47 |
if ( empty( $user->roles ) ) { |
| 48 |
return array(); |
| 49 |
} |
| 50 |
|
| 51 |
$all_roles = $this->get_roles(); |
| 52 |
$roles = array(); |
| 53 |
foreach ( $user->roles as $role ) { |
| 54 |
$roles[ $role ] = $all_roles[ $role ]; |
| 55 |
} |
| 56 |
|
| 57 |
return apply_filters( 'mdmr_get_user_roles', $roles ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Erase the user's existing roles and replace them with the new array. |
| 62 |
* |
| 63 |
* @param integer $user_id The WordPress user ID. |
| 64 |
* @param array $roles The new array of roles for the user. |
| 65 |
* |
| 66 |
* @return bool |
| 67 |
*/ |
| 68 |
public function update_roles( $user_id = 0, $roles = array() ) { |
| 69 |
do_action( 'mdmr_before_update_roles', $user_id, $roles ); |
| 70 |
|
| 71 |
$roles = array_map( 'sanitize_key', (array) $roles ); |
| 72 |
$roles = array_filter( (array) $roles, 'get_role' ); |
| 73 |
|
| 74 |
$user = get_user_by( 'id', (int) $user_id ); |
| 75 |
|
| 76 |
// Remove all editable roles |
| 77 |
$editable = get_editable_roles(); |
| 78 |
$editable_roles = is_array( $editable ) ? array_keys( $editable ) : array(); |
| 79 |
foreach ( $editable_roles as $role ) { |
| 80 |
$user->remove_role( $role ); |
| 81 |
} |
| 82 |
|
| 83 |
foreach ( $roles as $role ) { |
| 84 |
$user->add_role( $role ); |
| 85 |
} |
| 86 |
|
| 87 |
do_action( 'mdmr_after_update_roles', $user_id, $roles, $user->roles ); |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Check whether or not a user can edit roles. User must have the edit_roles cap and |
| 94 |
* must be on a specific site (and not in the network admin area). Users also can't |
| 95 |
* edit their own roles unless they're a network admin. |
| 96 |
* |
| 97 |
* @return bool True if current user can update roles, false if not. |
| 98 |
*/ |
| 99 |
public function can_update_roles() { |
| 100 |
do_action( 'mdmr_before_can_update_roles' ); |
| 101 |
|
| 102 |
if ( is_network_admin() || ! current_user_can( 'promote_users' ) || ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE && ! current_user_can( 'manage_sites' ) ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|