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 +31 -38 5.3.1 → 6.3.0 View file →
@@ -46,9 +46,11 @@
46 46 * @return bool
47 47 * @since 2.7.0
48 48 */
49 49 protected function is_hub_request(): bool {
50 - return ! empty( defender_get_data_from_request( 'wpmudev-hub', 'g' ) );
50 + $data = defender_get_data_from_request( 'wpmudev-hub', 'g' );
51 +
52 + return null !== $data && '' !== $data;
51 53 }
52 54
53 55 /**
54 56 * Get source of action. This can be a request from the Hub, a logged-in user.
@@ -99,9 +101,9 @@
99 101 * Get user role.
100 102 *
101 103 * @param null|int $user_id User ID.
102 104 *
103 - * @return array|null
105 + * @return string|null
104 106 */
105 107 public function get_current_user_role( $user_id = null ) {
106 108 $user = $this->get_user( $user_id );
107 109 if ( ! is_object( $user ) ) {
@@ -107,9 +109,9 @@
107 109 if ( ! is_object( $user ) ) {
108 110 return null;
109 111 }
110 112
111 - return empty( $user->roles ) ? null : ucfirst( array_shift( $user->roles ) );
113 + return isset( $user->roles[0] ) ? ucfirst( $user->roles[0] ) : null;
112 114 }
113 115
114 116 /**
115 117 * Todo: compare with method get_current_user_role().
@@ -119,10 +121,10 @@
119 121 * @return string
120 122 */
121 123 public function get_first_user_role( $user ) {
122 124 $role = '';
123 - if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
124 - $role = ucfirst( array_shift( $user->roles ) );
125 + if ( isset( $user->roles[0] ) ) {
126 + $role = ucfirst( $user->roles[0] );
125 127 }
126 128
127 129 return $role;
128 130 }
@@ -146,9 +148,9 @@
146 148 $user = wp_get_current_user();
147 149 } else {
148 150 $cache = Array_Cache::get( $user_id, 'user' );
149 151 if ( null !== $cache ) {
150 - if ( empty( $cache->roles ) ) {
152 + if ( ! isset( $cache->roles ) || ! is_array( $cache->roles ) || 0 === count( $cache->roles ) ) {
151 153 $cache_roles = Array_Cache::get( 'roles_' . $user_id, 'user' );
152 154 if ( null !== $cache_roles ) {
153 155 $cache->roles = $cache_roles;
154 156 }
@@ -198,9 +200,9 @@
198 200 * @return array
199 201 * @since 3.2.0
200 202 */
201 203 public function get_roles( WP_User $user ): array {
202 - $user_roles = (array) $user->roles;
204 + $user_roles = $user->roles;
203 205
204 206 // If user is super admin.
205 207 if ( is_multisite() && is_super_admin( $user->ID ) && ! in_array(
206 208 $this->super_admin_slug,
@@ -241,39 +243,42 @@
241 243 *
242 244 * @return bool
243 245 */
244 246 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 - }
247 + if ( is_int( $user ) ) {
248 + $user = get_userdata( $user );
253 249 }
254 250
255 - return false;
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' );
256 260 }
257 261
258 262 /**
259 263 * Check if the user has a role selected by the admin.
260 264 *
261 - * @param WP_User|stdClass $user User object.
265 + * @param WP_User|\stdClass $user User object.
266 + * @param object $model Model object.
262 267 *
263 268 * @return bool True if the user has a selected role, false otherwise.
264 269 */
265 - private function should_enforce_for_user( $user ): bool {
270 + private function should_enforce_for_user( $user, $model ): bool {
266 271 $roles = array();
267 272 if ( ! is_multisite() ) {
268 273 if ( $user instanceof WP_User ) {
269 274 $roles = $this->get_roles( $user );
270 - } elseif ( empty( $roles ) && $user instanceof stdClass && ! empty( $user->ID ) ) {
275 + } elseif ( isset( $user->ID ) && $user->ID ) {
271 276 $user = get_userdata( $user->ID );
272 277 $roles = $user->roles;
273 - } elseif ( empty( $roles ) ) {
278 + } else {
274 279 $role = defender_get_data_from_request( 'role', 'p' );
275 - if ( ! empty( $role ) ) {
280 + if ( null !== $role && '' !== $role ) {
276 281 $roles = array( $role );
277 282 }
278 283 }
279 284 } elseif ( is_multisite() && isset( $user->ID ) ) {
@@ -284,26 +289,14 @@
284 289 $roles = array_merge( $u->roles, $roles );
285 290 }
286 291 }
287 292
288 - $user_roles = $this->get_user_roles_property();
293 + $user_roles = array();
294 + if ( is_object( $model ) && property_exists( $model, 'user_roles' ) ) {
295 + $user_roles = $model->user_roles;
296 + }
289 297
290 298 $array_intersect = array_intersect( $user_roles, $roles );
291 299
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();
300 + return array() !== $array_intersect;
308 301 }
309 302 }