PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.0
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 5.4.0, at src/traits/user.php

310 lines 6.9 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 return ! empty( defender_get_data_from_request( 'wpmudev-hub', 'g' ) );
51 }
52
53 /**
54 * Get source of action. This can be a request from the Hub, a logged-in user.
55 * Todo: expand for WP-CLI, REST sources.
56 *
57 * @return string
58 * @since 2.7.0
59 */
60 public function get_source_of_action(): string {
61 return $this->is_hub_request()
62 ? esc_html__( 'Hub', 'defender-security' )
63 : $this->get_user_display( get_current_user_id() );
64 }
65
66 /**
67 * Get user login.
68 *
69 * @param null|int $user_id User ID.
70 *
71 * @return bool|mixed|string|WP_User|null
72 */
73 public function get_user_login( $user_id = null ) {
74 $user = $this->get_user( $user_id );
75 if ( ! is_object( $user ) ) {
76 return $user;
77 }
78
79 return $user->user_login;
80 }
81
82 /**
83 * Get user email.
84 *
85 * @param null|int $user_id User ID.
86 *
87 * @return string|null
88 */
89 public function get_current_user_email( $user_id = null ) {
90 $user = $this->get_user( $user_id );
91 if ( ! is_object( $user ) ) {
92 return $user;
93 }
94
95 return $user->user_email;
96 }
97
98 /**
99 * Get user role.
100 *
101 * @param null|int $user_id User ID.
102 *
103 * @return array|null
104 */
105 public function get_current_user_role( $user_id = null ) {
106 $user = $this->get_user( $user_id );
107 if ( ! is_object( $user ) ) {
108 return null;
109 }
110
111 return empty( $user->roles ) ? null : ucfirst( array_shift( $user->roles ) );
112 }
113
114 /**
115 * Todo: compare with method get_current_user_role().
116 *
117 * @param object $user User object.
118 *
119 * @return string
120 */
121 public function get_first_user_role( $user ) {
122 $role = '';
123 if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
124 $role = ucfirst( array_shift( $user->roles ) );
125 }
126
127 return $role;
128 }
129
130 /**
131 * Get user object by user ID.
132 *
133 * @param null|int|WP_User $user_id User ID or WP_User object.
134 *
135 * @return bool|mixed|WP_User|null
136 */
137 private function get_user( $user_id ) {
138 if ( $user_id instanceof WP_User ) {
139 return $user_id;
140 }
141
142 if ( ! is_user_logged_in() ) {
143 return esc_html__( 'Guest', 'defender-security' );
144 }
145 if ( null === $user_id ) {
146 $user = wp_get_current_user();
147 } else {
148 $cache = Array_Cache::get( $user_id, 'user' );
149 if ( null !== $cache ) {
150 if ( empty( $cache->roles ) ) {
151 $cache_roles = Array_Cache::get( 'roles_' . $user_id, 'user' );
152 if ( null !== $cache_roles ) {
153 $cache->roles = $cache_roles;
154 }
155 }
156
157 $user = $cache;
158 } else {
159 $user = get_user_by( 'id', $user_id );
160 if ( is_object( $user ) ) {
161 Array_Cache::set( $user_id, $user, 'user' );
162 Array_Cache::set( 'roles_' . $user_id, $user->roles, 'user' );
163 }
164 }
165 }
166
167 return $user;
168 }
169
170 /**
171 * Return the default user for recipient, should be the current user.
172 *
173 * @return array
174 */
175 public function get_default_recipient(): array {
176 // @since 3.0.0 Fix 'Guest'-line.
177 if ( ! is_user_logged_in() ) {
178 return array();
179 }
180 $user_id = get_current_user_id();
181
182 return array(
183 'name' => $this->get_user_display( $user_id ),
184 'id' => $user_id,
185 'email' => $this->get_current_user_email( $user_id ),
186 'role' => $this->get_current_user_role( $user_id ),
187 'avatar' => get_avatar_url( $this->get_current_user_email( $user_id ) ),
188 'status' => Notification::USER_SUBSCRIBED,
189 );
190 }
191
192 /**
193 * Get user roles.
194 * This method will include 'super_admin' role if provided user is super admin.
195 *
196 * @param WP_User $user User object.
197 *
198 * @return array
199 * @since 3.2.0
200 */
201 public function get_roles( WP_User $user ): array {
202 $user_roles = (array) $user->roles;
203
204 // If user is super admin.
205 if ( is_multisite() && is_super_admin( $user->ID ) && ! in_array(
206 $this->super_admin_slug,
207 $user_roles,
208 true
209 ) ) {
210 $user_roles[] = $this->super_admin_slug;
211 }
212
213 return $user_roles;
214 }
215
216 /**
217 * Get all roles editable by current user.
218 *
219 * @return array
220 * @since 3.2.0
221 */
222 public function get_all_editable_roles(): array {
223 $editable_roles = wp_list_pluck( get_editable_roles(), 'name' );
224
225 if ( is_multisite() && is_super_admin() ) {
226 $editable_roles = array_merge(
227 array(
228 $this->super_admin_slug => 'Super Admin',
229 ),
230 $editable_roles
231 );
232 }
233
234 return $editable_roles;
235 }
236
237 /**
238 * Does the current user have admin credentials?
239 *
240 * @param int|WP_User $user User ID or WP_User object.
241 *
242 * @return bool
243 */
244 public function is_admin( $user ): bool {
245 if ( $user ) {
246 if ( is_multisite() ) {
247 if ( user_can( $user, 'manage_network' ) ) {
248 return true;
249 }
250 } elseif ( user_can( $user, 'manage_options' ) ) {
251 return true;
252 }
253 }
254
255 return false;
256 }
257
258 /**
259 * Check if the user has a role selected by the admin.
260 *
261 * @param WP_User|stdClass $user User object.
262 *
263 * @return bool True if the user has a selected role, false otherwise.
264 */
265 private function should_enforce_for_user( $user ): bool {
266 $roles = array();
267 if ( ! is_multisite() ) {
268 if ( $user instanceof WP_User ) {
269 $roles = $this->get_roles( $user );
270 } elseif ( empty( $roles ) && $user instanceof stdClass && ! empty( $user->ID ) ) {
271 $user = get_userdata( $user->ID );
272 $roles = $user->roles;
273 } elseif ( empty( $roles ) ) {
274 $role = defender_get_data_from_request( 'role', 'p' );
275 if ( ! empty( $role ) ) {
276 $roles = array( $role );
277 }
278 }
279 } elseif ( is_multisite() && isset( $user->ID ) ) {
280 $blogs = get_blogs_of_user( $user->ID );
281 foreach ( $blogs as $blog ) {
282 // Get user roles for this blog.
283 $u = new WP_User( $user->ID, '', $blog->userblog_id );
284 $roles = array_merge( $u->roles, $roles );
285 }
286 }
287
288 $user_roles = $this->get_user_roles_property();
289
290 $array_intersect = array_intersect( $user_roles, $roles );
291
292 return ! empty( $array_intersect );
293 }
294
295 /**
296 * Retrieve the user roles property dynamically.
297 *
298 * @return array The user roles from the appropriate property.
299 */
300 private function get_user_roles_property(): array {
301 if ( isset( $this->model ) && property_exists( $this->model, 'user_roles' ) ) {
302 return $this->model->user_roles;
303 } elseif ( isset( $this->settings ) && property_exists( $this->settings, 'user_roles' ) ) {
304 return $this->settings->user_roles;
305 }
306
307 return array();
308 }
309 }
310