PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-role.php

class-role.php in Sessions 2.3.1, at includes/system/class-role.php

144 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Roles handling
4 *
5 * Handles all roles operations and detection.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace POSessions\System;
13
14 /**
15 * Define the roles functionality.
16 *
17 * Handles all roles operations and detection.
18 *
19 * @package System
20 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
21 * @since 1.0.0
22 */
23 class Role {
24
25 /**
26 * The super (network) admin.
27 *
28 * @since 1.0.0
29 */
30 const SUPER_ADMIN = 4;
31
32 /**
33 * The single site admin.
34 *
35 * @since 1.0.0
36 */
37 const SINGLE_ADMIN = 2;
38
39 /**
40 * The local admin (in network site).
41 *
42 * @since 1.0.0
43 */
44 const LOCAL_ADMIN = 1;
45
46 /**
47 * Initializes the class and set its properties.
48 *
49 * @since 1.0.0
50 */
51 public function __construct() {
52 }
53
54 /**
55 * Get a user nice name.
56 *
57 * @param integer $user_id Optional. The user id.
58 * @return integer The type of admin.
59 * @since 1.0.0
60 */
61 public static function admin_type( $user_id = false ) {
62 if ( ! $user_id ) {
63 $user = wp_get_current_user();
64 } else {
65 $user = get_userdata( $user_id );
66 }
67 if ( ! $user || ! $user->exists() ) {
68 return 0;
69 }
70 if ( is_multisite() ) {
71 $super_admins = get_super_admins();
72 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) ) {
73 return self::SUPER_ADMIN;
74 } elseif ( in_array( 'administrator', $user->roles ) ) {
75 return self::LOCAL_ADMIN;
76 }
77 } else {
78 if ( in_array( 'administrator', $user->roles ) ) {
79 return self::SINGLE_ADMIN;
80 }
81 }
82 return 0;
83 }
84
85 /**
86 * Get a list of available roles.
87 *
88 * @return array The list of available roles.
89 * @since 1.0.0
90 */
91 public static function get_all() {
92 global $wp_roles;
93 if ( ! isset( $wp_roles ) ) {
94 $wp_roles = new \WP_Roles();
95 }
96 $result = [];
97 foreach ( $wp_roles->get_names() as $role => $name ) {
98 $result[ $role ] = [];
99 $result[ $role ]['name'] = $name;
100 $result[ $role ]['l10n_name'] = translate_user_role( $name );
101 }
102 return $result;
103 }
104
105 /**
106 * Get user main role.
107 *
108 * @param integer $user_id The user id.
109 * @return string The role id.
110 * @since 1.0.0
111 */
112 public static function get_user_main( $user_id ) {
113 $user = get_user_by( 'id', $user_id );
114 $role = '';
115 foreach ( self::get_all() as $key => $detail ) {
116 if ( in_array( $key, $user->roles, true ) ) {
117 $role = $key;
118 break;
119 }
120 }
121 return $role;
122 }
123
124 /**
125 * Get all user roles.
126 *
127 * @param integer $user_id The user id.
128 * @return array The roles id.
129 * @since 1.0.0
130 */
131 public static function get_user_all( $user_id ) {
132 $user = get_user_by( 'id', $user_id );
133 $role = [];
134 foreach ( self::get_all() as $key => $detail ) {
135 if ( in_array( $key, $user->roles, true ) ) {
136 $role[] = $key;
137 break;
138 }
139 }
140 return $role;
141 }
142
143 }
144