PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.4
Defender Security – Malware Scanner, Login Security & Firewall v6.2.4
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / user.php

user.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.4, at src/traits/user.php

303 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper functions for user related tasks.
4 *
5 * @package WP_Defender\Traits
6 */
7
8 namespace WP_Defender\Traits;
9
10 use WP_User;
11 use Calotes\Helper\Array_Cache;
12 use WP_Defender\Model\Notification;
13
14 trait User {
15
16 /**
17 * Super Admin role slug.
18 *
19 * @var string
20 */
21 public $super_admin_slug = 'super_admin';
22
23 /**
24 * Get user display.
25 *
26 * @param null|int $user_id User ID.
27 *
28 * @return string
29 */
30 public function get_user_display( $user_id = null ): string {
31 if ( is_null( $user_id ) ) {
32 return esc_html__( 'Guest', 'defender-security' );
33 }
34
35 $user = $this->get_user( $user_id );
36 if ( ! is_object( $user ) ) {
37 return esc_html__( 'Guest', 'defender-security' );
38 }
39
40 return $user->display_name;
41 }
42
43 /**
44 * Check if current request is from Hub.
45 *
46 * @return bool
47 * @since 2.7.0
48 */
49 protected function is_hub_request(): bool {
50 $data = defender_get_data_from_request( 'wpmudev-hub', 'g' );
51
52 return null !== $data && '' !== $data;
53 }
54
55 /**
56 * Get source of action. This can be a request from the Hub, a logged-in user.
57 * Todo: expand for WP-CLI, REST sources.
58 *
59 * @return string
60 * @since 2.7.0
61 */
62 public function get_source_of_action(): string {
63 return $this->is_hub_request()
64 ? esc_html__( 'Hub', 'defender-security' )
65 : $this->get_user_display( get_current_user_id() );
66 }
67
68 /**
69 * Get user login.
70 *
71 * @param null|int $user_id User ID.
72 *
73 * @return bool|mixed|string|WP_User|null
74 */
75 public function get_user_login( $user_id = null ) {
76 $user = $this->get_user( $user_id );
77 if ( ! is_object( $user ) ) {
78 return $user;
79 }
80
81 return $user->user_login;
82 }
83
84 /**
85 * Get user email.
86 *
87 * @param null|int $user_id User ID.
88 *
89 * @return string|null
90 */
91 public function get_current_user_email( $user_id = null ) {
92 $user = $this->get_user( $user_id );
93 if ( ! is_object( $user ) ) {
94 return $user;
95 }
96
97 return $user->user_email;
98 }
99
100 /**
101 * Get user role.
102 *
103 * @param null|int $user_id User ID.
104 *
105 * @return string|null
106 */
107 public function get_current_user_role( $user_id = null ) {
108 $user = $this->get_user( $user_id );
109 if ( ! is_object( $user ) ) {
110 return null;
111 }
112
113 return isset( $user->roles[0] ) ? ucfirst( $user->roles[0] ) : null;
114 }
115
116 /**
117 * Todo: compare with method get_current_user_role().
118 *
119 * @param object $user User object.
120 *
121 * @return string
122 */
123 public function get_first_user_role( $user ) {
124 $role = '';
125 if ( isset( $user->roles[0] ) ) {
126 $role = ucfirst( $user->roles[0] );
127 }
128
129 return $role;
130 }
131
132 /**
133 * Get user object by user ID.
134 *
135 * @param null|int|WP_User $user_id User ID or WP_User object.
136 *
137 * @return bool|mixed|WP_User|null
138 */
139 private function get_user( $user_id ) {
140 if ( $user_id instanceof WP_User ) {
141 return $user_id;
142 }
143
144 if ( ! is_user_logged_in() ) {
145 return esc_html__( 'Guest', 'defender-security' );
146 }
147 if ( null === $user_id ) {
148 $user = wp_get_current_user();
149 } else {
150 $cache = Array_Cache::get( $user_id, 'user' );
151 if ( null !== $cache ) {
152 if ( ! isset( $cache->roles ) || ! is_array( $cache->roles ) || 0 === count( $cache->roles ) ) {
153 $cache_roles = Array_Cache::get( 'roles_' . $user_id, 'user' );
154 if ( null !== $cache_roles ) {
155 $cache->roles = $cache_roles;
156 }
157 }
158
159 $user = $cache;
160 } else {
161 $user = get_user_by( 'id', $user_id );
162 if ( is_object( $user ) ) {
163 Array_Cache::set( $user_id, $user, 'user' );
164 Array_Cache::set( 'roles_' . $user_id, $user->roles, 'user' );
165 }
166 }
167 }
168
169 return $user;
170 }
171
172 /**
173 * Return the default user for recipient, should be the current user.
174 *
175 * @return array
176 */
177 public function get_default_recipient(): array {
178 // @since 3.0.0 Fix 'Guest'-line.
179 if ( ! is_user_logged_in() ) {
180 return array();
181 }
182 $user_id = get_current_user_id();
183
184 return array(
185 'name' => $this->get_user_display( $user_id ),
186 'id' => $user_id,
187 'email' => $this->get_current_user_email( $user_id ),
188 'role' => $this->get_current_user_role( $user_id ),
189 'avatar' => get_avatar_url( $this->get_current_user_email( $user_id ) ),
190 'status' => Notification::USER_SUBSCRIBED,
191 );
192 }
193
194 /**
195 * Get user roles.
196 * This method will include 'super_admin' role if provided user is super admin.
197 *
198 * @param WP_User $user User object.
199 *
200 * @return array
201 * @since 3.2.0
202 */
203 public function get_roles( WP_User $user ): array {
204 $user_roles = $user->roles;
205
206 // If user is super admin.
207 if ( is_multisite() && is_super_admin( $user->ID ) && ! in_array(
208 $this->super_admin_slug,
209 $user_roles,
210 true
211 ) ) {
212 $user_roles[] = $this->super_admin_slug;
213 }
214
215 return $user_roles;
216 }
217
218 /**
219 * Get all roles editable by current user.
220 *
221 * @return array
222 * @since 3.2.0
223 */
224 public function get_all_editable_roles(): array {
225 $editable_roles = wp_list_pluck( get_editable_roles(), 'name' );
226
227 if ( is_multisite() && is_super_admin() ) {
228 $editable_roles = array_merge(
229 array(
230 $this->super_admin_slug => 'Super Admin',
231 ),
232 $editable_roles
233 );
234 }
235
236 return $editable_roles;
237 }
238
239 /**
240 * Does the current user have admin credentials?
241 *
242 * @param int|WP_User $user User ID or WP_User object.
243 *
244 * @return bool
245 */
246 public function is_admin( $user ): bool {
247 if ( is_int( $user ) ) {
248 $user = get_userdata( $user );
249 }
250
251 if ( ! $user instanceof WP_User ) {
252 return false;
253 }
254
255 if ( is_multisite() ) {
256 return user_can( $user, 'manage_network' );
257 }
258
259 return user_can( $user, 'manage_options' );
260 }
261
262 /**
263 * Check if the user has a role selected by the admin.
264 *
265 * @param WP_User|\stdClass $user User object.
266 * @param object $model Model object.
267 *
268 * @return bool True if the user has a selected role, false otherwise.
269 */
270 private function should_enforce_for_user( $user, $model ): bool {
271 $roles = array();
272 if ( ! is_multisite() ) {
273 if ( $user instanceof WP_User ) {
274 $roles = $this->get_roles( $user );
275 } elseif ( isset( $user->ID ) && $user->ID ) {
276 $user = get_userdata( $user->ID );
277 $roles = $user->roles;
278 } else {
279 $role = defender_get_data_from_request( 'role', 'p' );
280 if ( null !== $role && '' !== $role ) {
281 $roles = array( $role );
282 }
283 }
284 } elseif ( is_multisite() && isset( $user->ID ) ) {
285 $blogs = get_blogs_of_user( $user->ID );
286 foreach ( $blogs as $blog ) {
287 // Get user roles for this blog.
288 $u = new WP_User( $user->ID, '', $blog->userblog_id );
289 $roles = array_merge( $u->roles, $roles );
290 }
291 }
292
293 $user_roles = array();
294 if ( is_object( $model ) && property_exists( $model, 'user_roles' ) ) {
295 $user_roles = $model->user_roles;
296 }
297
298 $array_intersect = array_intersect( $user_roles, $roles );
299
300 return array() !== $array_intersect;
301 }
302 }
303