PluginProbe
Multiple Roles / trunk
Multiple Roles vtrunk
trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8
multiple-roles / model.php

model.php in Multiple Roles trunk, at model.php

117 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( ! $user ) {
77 return false;
78 }
79
80 // Remove all editable roles
81 $editable = get_editable_roles();
82 $editable_roles = is_array( $editable ) ? array_keys( $editable ) : array();
83 foreach ( $editable_roles as $role ) {
84 $user->remove_role( $role );
85 }
86
87 // Only add back roles the current user is actually allowed to grant.
88 $roles = array_intersect( $roles, $editable_roles );
89
90 foreach ( $roles as $role ) {
91 $user->add_role( $role );
92 }
93
94 do_action( 'mdmr_after_update_roles', $user_id, $roles, $user->roles );
95
96 return true;
97 }
98
99 /**
100 * Check whether or not a user can edit roles. User must have the edit_roles cap and
101 * must be on a specific site (and not in the network admin area). Users also can't
102 * edit their own roles unless they're a network admin.
103 *
104 * @return bool True if current user can update roles, false if not.
105 */
106 public function can_update_roles() {
107 do_action( 'mdmr_before_can_update_roles' );
108
109 if ( is_network_admin() || ! current_user_can( 'promote_users' ) || ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE && ! current_user_can( 'manage_sites' ) ) ) {
110 return false;
111 }
112
113 return true;
114 }
115
116 }
117