PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.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
← All changes | src/traits/user.php +172 -23 3.1.0 → 6.3.0 View file →
@@ -1,27 +1,41 @@
1 1 <?php
2 +/**
3 + * Helper functions for user related tasks.
4 + *
5 + * @package WP_Defender\Traits
6 + */
2 7
3 8 namespace WP_Defender\Traits;
4 9
10 +use WP_User;
5 11 use Calotes\Helper\Array_Cache;
6 12 use WP_Defender\Model\Notification;
7 13
8 14 trait User {
15 +
9 16 /**
17 + * Super Admin role slug.
18 + *
19 + * @var string
20 + */
21 + public $super_admin_slug = 'super_admin';
22 +
23 + /**
10 24 * Get user display.
11 25 *
12 - * @param null|int $user_id
26 + * @param null|int $user_id User ID.
13 27 *
14 28 * @return string
15 29 */
16 - public function get_user_display( $user_id = null ) {
30 + public function get_user_display( $user_id = null ): string {
17 31 if ( is_null( $user_id ) ) {
18 - return __( 'Guest', 'wpdef' );
32 + return esc_html__( 'Guest', 'defender-security' );
19 33 }
20 34
21 35 $user = $this->get_user( $user_id );
22 36 if ( ! is_object( $user ) ) {
23 - return __( 'Guest', 'wpdef' );
37 + return esc_html__( 'Guest', 'defender-security' );
24 38 }
25 39
26 40 return $user->display_name;
27 41 }
@@ -28,13 +42,15 @@
28 42
29 43 /**
30 44 * Check if current request is from Hub.
31 45 *
46 + * @return bool
32 47 * @since 2.7.0
33 - * @return bool
34 48 */
35 - protected function is_hub_request() {
36 - return isset( $_GET['wpmudev-hub'] ) && ! empty( $_GET['wpmudev-hub'] ); // phpcs:ignore
49 + protected function is_hub_request(): bool {
50 + $data = defender_get_data_from_request( 'wpmudev-hub', 'g' );
51 +
52 + return null !== $data && '' !== $data;
37 53 }
38 54
39 55 /**
40 56 * Get source of action. This can be a request from the Hub, a logged-in user.
@@ -39,21 +55,23 @@
39 55 /**
40 56 * Get source of action. This can be a request from the Hub, a logged-in user.
41 57 * Todo: expand for WP-CLI, REST sources.
42 58 *
59 + * @return string
43 60 * @since 2.7.0
44 - * @return string
45 - */
46 - public function get_source_of_action() {
61 + */
62 + public function get_source_of_action(): string {
47 63 return $this->is_hub_request()
48 - ? __( 'Hub', 'wpdef' )
64 + ? esc_html__( 'Hub', 'defender-security' )
49 65 : $this->get_user_display( get_current_user_id() );
50 66 }
51 67
52 68 /**
53 - * @param null|int $user_id
69 + * Get user login.
54 70 *
55 - * @return bool|mixed|string|\WP_User|null
71 + * @param null|int $user_id User ID.
72 + *
73 + * @return bool|mixed|string|WP_User|null
56 74 */
57 75 public function get_user_login( $user_id = null ) {
58 76 $user = $this->get_user( $user_id );
59 77 if ( ! is_object( $user ) ) {
@@ -63,10 +81,12 @@
63 81 return $user->user_login;
64 82 }
65 83
66 84 /**
67 - * @param null|int $user_id
85 + * Get user email.
68 86 *
87 + * @param null|int $user_id User ID.
88 + *
69 89 * @return string|null
70 90 */
71 91 public function get_current_user_email( $user_id = null ) {
72 92 $user = $this->get_user( $user_id );
@@ -77,11 +97,13 @@
77 97 return $user->user_email;
78 98 }
79 99
80 100 /**
81 - * @param null|int $user_id
101 + * Get user role.
82 102 *
83 - * @return array|null
103 + * @param null|int $user_id User ID.
104 + *
105 + * @return string|null
84 106 */
85 107 public function get_current_user_role( $user_id = null ) {
86 108 $user = $this->get_user( $user_id );
87 109 if ( ! is_object( $user ) ) {
@@ -87,23 +109,41 @@
87 109 if ( ! is_object( $user ) ) {
88 110 return null;
89 111 }
90 112
91 - return empty( $user->roles ) ? null : ucfirst( array_shift( $user->roles ) );
113 + return isset( $user->roles[0] ) ? ucfirst( $user->roles[0] ) : null;
92 114 }
93 115
94 116 /**
95 - * @param null|int|\WP_User $user_id
117 + * Todo: compare with method get_current_user_role().
96 118 *
97 - * @return bool|mixed|\WP_User|null
119 + * @param object $user User object.
120 + *
121 + * @return string
98 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 + */
99 139 private function get_user( $user_id ) {
100 - if ( $user_id instanceof \WP_User ) {
140 + if ( $user_id instanceof WP_User ) {
101 141 return $user_id;
102 142 }
103 143
104 144 if ( ! is_user_logged_in() ) {
105 - return __( 'Guest', 'wpdef' );
145 + return esc_html__( 'Guest', 'defender-security' );
106 146 }
107 147 if ( null === $user_id ) {
108 148 $user = wp_get_current_user();
109 149 } else {
@@ -108,9 +148,9 @@
108 148 $user = wp_get_current_user();
109 149 } else {
110 150 $cache = Array_Cache::get( $user_id, 'user' );
111 151 if ( null !== $cache ) {
112 - if ( empty( $cache->roles ) ) {
152 + if ( ! isset( $cache->roles ) || ! is_array( $cache->roles ) || 0 === count( $cache->roles ) ) {
113 153 $cache_roles = Array_Cache::get( 'roles_' . $user_id, 'user' );
114 154 if ( null !== $cache_roles ) {
115 155 $cache->roles = $cache_roles;
116 156 }
@@ -133,9 +173,9 @@
133 173 * Return the default user for recipient, should be the current user.
134 174 *
135 175 * @return array
136 176 */
137 - public function get_default_recipient() {
177 + public function get_default_recipient(): array {
138 178 // @since 3.0.0 Fix 'Guest'-line.
139 179 if ( ! is_user_logged_in() ) {
140 180 return array();
141 181 }
@@ -148,6 +188,115 @@
148 188 'role' => $this->get_current_user_role( $user_id ),
149 189 'avatar' => get_avatar_url( $this->get_current_user_email( $user_id ) ),
150 190 'status' => Notification::USER_SUBSCRIBED,
151 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;
152 301 }
153 302 }