PluginProbe
Authorizer / 2.9.2
Authorizer v2.9.2
3.15.3 3.15.2 3.15.1 3.15.0 3.14.3 3.14.4 3.14.2 3.14.1 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.10 2.9.11 2.9.12 2.9.13 2.9.2 2.9.3 2.9.6 All 126 releases
authorizer / src / authorizer / class-sync-userdata.php

class-sync-userdata.php in Authorizer 2.9.2, at src/authorizer/class-sync-userdata.php

702 lines 28.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Authorizer
4 *
5 * @license GPL-2.0+
6 * @link https://github.com/uhm-coe/authorizer
7 * @package authorizer
8 */
9
10 namespace Authorizer;
11
12 use Authorizer\Helper;
13 use Authorizer\Options;
14 use Authorizer\Authorization;
15
16 /**
17 * Contains functions for interfacing with WordPress users and syncing between
18 * them and users in the Authorizer lists.
19 */
20 class Sync_Userdata extends Static_Instance {
21
22 /**
23 * Adds all WordPress users in the current site to the approved list,
24 * unless they are already in the blocked list. Also removes them
25 * from the pending list if they are there.
26 *
27 * Runs in plugin activation hook.
28 *
29 * @return void
30 */
31 public function add_wp_users_to_approved_list() {
32 $options = Options::get_instance();
33 // Add current WordPress users to the approved list.
34 $auth_multisite_settings_access_users_approved = is_multisite() ? get_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', array() ) : array();
35 $auth_settings_access_users_pending = $options->get( 'access_users_pending', Helper::SINGLE_CONTEXT );
36 $auth_settings_access_users_approved = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT );
37 $auth_settings_access_users_blocked = $options->get( 'access_users_blocked', Helper::SINGLE_CONTEXT );
38 $updated = false;
39 foreach ( get_users() as $user ) {
40 // Skip if user is in blocked list.
41 if ( Helper::in_multi_array( $user->user_email, $auth_settings_access_users_blocked ) ) {
42 continue;
43 }
44 // Remove from pending list if there.
45 foreach ( $auth_settings_access_users_pending as $key => $pending_user ) {
46 if ( 0 === strcasecmp( $pending_user['email'], $user->user_email ) ) {
47 unset( $auth_settings_access_users_pending[ $key ] );
48 $updated = true;
49 }
50 }
51 // Skip if user is in multisite approved list.
52 if ( Helper::in_multi_array( $user->user_email, $auth_multisite_settings_access_users_approved ) ) {
53 continue;
54 }
55 // Add to approved list if not there.
56 if ( ! Helper::in_multi_array( $user->user_email, $auth_settings_access_users_approved ) ) {
57 $approved_user = array(
58 'email' => Helper::lowercase( $user->user_email ),
59 'role' => count( $user->roles ) > 0 ? $user->roles[0] : '',
60 'date_added' => date( 'M Y', strtotime( $user->user_registered ) ),
61 'local_user' => true,
62 );
63 array_push( $auth_settings_access_users_approved, $approved_user );
64 $updated = true;
65 }
66 }
67 if ( $updated ) {
68 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
69 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
70 }
71 }
72
73
74 /**
75 * On an admin page load, check for edge case (network-approved user who has
76 * not yet been added to this particular blog in a multisite). Note: we do
77 * this because check_user_access() runs on the parse_request hook, which
78 * does not fire on wp-admin pages.
79 *
80 * Action: init
81 *
82 * @return void
83 */
84 public function init__maybe_add_network_approved_user() {
85 global $current_user;
86 $options = Options::get_instance();
87
88 // If this is a multisite install and we have a logged in user that's not
89 // a member of this blog, but is (network) approved, add them to this blog.
90 if (
91 is_admin() &&
92 is_multisite() &&
93 is_user_logged_in() &&
94 ! is_user_member_of_blog() &&
95 Authorization::get_instance()->is_email_in_list( $current_user->user_email, 'approved' )
96 ) {
97 // Get all approved users.
98 $auth_settings_access_users_approved = $options->sanitize_user_list(
99 array_merge(
100 $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ),
101 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
102 )
103 );
104
105 // Get user info (we need user role).
106 $user_info = Helper::get_user_info_from_list(
107 $current_user->user_email,
108 $auth_settings_access_users_approved
109 );
110
111 // Add user to blog.
112 add_user_to_blog( get_current_blog_id(), $current_user->ID, $user_info['role'] );
113
114 // Refresh user permissions.
115 $current_user = new \WP_User( $current_user->ID ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
116 }
117 }
118
119
120 /**
121 * Send a welcome email message to a newly approved user (if the "Should
122 * email approved users" setting is enabled).
123 *
124 * @param string $email Email address to send welcome email to.
125 * @return bool Whether the email was sent.
126 */
127 public function maybe_email_welcome_message( $email ) {
128 // Get option for whether to email welcome messages.
129 $options = Options::get_instance();
130 $should_email_new_approved_users = $options->get( 'access_should_email_approved_users' );
131
132 // Do not send welcome email if option not enabled.
133 if ( '1' !== $should_email_new_approved_users ) {
134 return false;
135 }
136
137 // Make sure we didn't just email this user (can happen with
138 // multiple admins saving at the same time, or by clicking
139 // Approve button too rapidly).
140 $recently_sent_emails = get_option( 'auth_settings_recently_sent_emails' );
141 if ( false === $recently_sent_emails ) {
142 $recently_sent_emails = array();
143 }
144 foreach ( $recently_sent_emails as $key => $recently_sent_email ) {
145 if ( $recently_sent_email['time'] < strtotime( 'now -1 minutes' ) ) {
146 // Remove emails sent more than 1 minute ago.
147 unset( $recently_sent_emails[ $key ] );
148 } elseif ( $recently_sent_email['email'] === $email ) {
149 // Sent an email to this user within the last 1 minute, so
150 // quit without sending.
151 return false;
152 }
153 }
154 // Add the email we're about to send to the list.
155 $recently_sent_emails[] = array(
156 'email' => $email,
157 'time' => time(),
158 );
159 update_option( 'auth_settings_recently_sent_emails', $recently_sent_emails );
160
161 // Get welcome email subject and body text.
162 $subject = $options->get( 'access_email_approved_users_subject' );
163 $body = apply_filters( 'the_content', $options->get( 'access_email_approved_users_body' ) );
164
165 // Fail if the subject/body options don't exist or are empty.
166 if ( is_null( $subject ) || is_null( $body ) || strlen( $subject ) === 0 || strlen( $body ) === 0 ) {
167 return false;
168 }
169
170 // Replace approved shortcode patterns in subject and body.
171 $site_name = get_bloginfo( 'name' );
172 $site_url = get_site_url();
173 $subject = str_replace( '[site_name]', $site_name, $subject );
174 $body = str_replace( '[site_name]', $site_name, $body );
175 $body = str_replace( '[site_url]', $site_url, $body );
176 $body = str_replace( '[user_email]', $email, $body );
177 $headers = 'Content-type: text/html' . "\r\n";
178
179 // Send email.
180 wp_mail( $email, $subject, $body, $headers );
181
182 // Indicate mail was sent.
183 return true;
184 }
185
186
187 /**
188 * When they successfully log in, make sure WordPress users are in the approved list.
189 *
190 * Action: wp_login
191 *
192 * @param string $user_login Username of the user logging in.
193 * @param object $user WP_User object of the user logging in.
194 * @return void
195 */
196 public function ensure_wordpress_user_in_approved_list_on_login( $user_login, $user ) {
197 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles );
198 }
199
200
201 /**
202 * Keep authorizer approved users' roles in sync with WordPress roles
203 * if someone changes the role via the WordPress Edit User page
204 * (wp-admin/user-edit.php or wp-admin/profile.php).
205 *
206 * Action: user_profile_update_errors
207 *
208 * @param WP_Error $errors Errors object to add any custom errors to (passed by reference).
209 * @param bool $update True if updating existing user, false if saving a new one.
210 * @param stdClass $user Updated WP_User object for user being edited (passed by reference).
211 */
212 public function edit_user_profile_update_role( &$errors, $update, &$user ) {
213 // Do nothing if we're not updating role.
214 if ( ! property_exists( $user, 'role' ) ) {
215 return;
216 }
217
218 // Safety check; will likely not fire if we reach this function.
219 if ( ! current_user_can( 'edit_user', $user->ID ) ) {
220 return;
221 }
222
223 // Don't perform Authorizer updates if we have a WordPress error.
224 $errors_on_user_update = $errors->get_error_codes();
225 if ( ! empty( $errors_on_user_update ) ) {
226 return;
227 }
228
229 // Get original user object (fail if not a real WordPress user).
230 $userdata = get_userdata( $user->ID );
231 if ( ! $userdata ) {
232 return;
233 }
234
235 // If user is in approved list, update his/her associated role.
236 if ( Authorization::get_instance()->is_email_in_list( $userdata->user_email, 'approved' ) ) {
237 $options = Options::get_instance();
238 $auth_settings_access_users_approved = $options->sanitize_user_list( $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ) );
239 foreach ( $auth_settings_access_users_approved as $key => $check_user ) {
240 if ( 0 === strcasecmp( $check_user['email'], $userdata->user_email ) ) {
241 $auth_settings_access_users_approved[ $key ]['role'] = $user->role;
242 }
243 }
244 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
245 }
246 }
247
248
249 /**
250 * Sync any email address changes to WordPress accounts to the corresponding
251 * entry in the Authorizer approved list.
252 *
253 * Note: This filter fires in wp_update_user() if the update includes an
254 * email address change, and fires after all security and integrity checks
255 * have been performed, so we can simply update the Authorizer approved
256 * list, changing the email address on the approved entry, and removing any
257 * existing entries that also have the new email address (duplicates).
258 *
259 * Filter: send_email_change_email
260 *
261 * @param bool $send Whether to send the email.
262 * @param array $user The original user array.
263 * @param array $userdata The updated user array.
264 */
265 public function edit_user_profile_update_email( $send, $user, $userdata ) {
266 $options = Options::get_instance();
267
268 // If we're in multisite, update the email on all sites in the network
269 // (and remove from any subsites if it's a network-approved user).
270 if ( is_multisite() ) {
271 // If it's a multisite approved user, sync the email there.
272 $changed_user_is_multisite_user = false;
273 if ( Authorization::get_instance()->is_email_in_list( $user['user_email'], 'approved', 'multisite' ) ) {
274 $changed_user_is_multisite_user = true;
275 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
276 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
277 );
278 foreach ( $auth_multisite_settings_access_users_approved as $key => $check_user ) {
279 // Update old user email in approved list to the new email.
280 if ( 0 === strcasecmp( $check_user['email'], $user['user_email'] ) ) {
281 $auth_multisite_settings_access_users_approved[ $key ]['email'] = Helper::lowercase( $userdata['user_email'] );
282 }
283 // If new user email is already in approved list, remove that entry.
284 if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) {
285 unset( $auth_multisite_settings_access_users_approved[ $key ] );
286 }
287 }
288 update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
289 }
290
291 // Go through all approved lists on individual sites and sync this user there.
292 // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound
293 $sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) );
294 foreach ( $sites as $site ) {
295 $updated = false;
296 $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id'];
297 $auth_settings_access_users_approved = get_blog_option( $blog_id, 'auth_settings_access_users_approved', array() );
298 foreach ( $auth_settings_access_users_approved as $key => $check_user ) {
299 // Update old user email in approved list to the new email.
300 if ( 0 === strcasecmp( $check_user['email'], $user['user_email'] ) ) {
301 // But if the user is already a multisite user, just remove the entry in the subsite.
302 if ( $changed_user_is_multisite_user ) {
303 unset( $auth_settings_access_users_approved[ $key ] );
304 } else {
305 $auth_settings_access_users_approved[ $key ]['email'] = Helper::lowercase( $userdata['user_email'] );
306 }
307 $updated = true;
308 }
309 // If new user email is already in approved list, remove that entry.
310 if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) {
311 unset( $auth_settings_access_users_approved[ $key ] );
312 $updated = true;
313 }
314 }
315 if ( $updated ) {
316 update_blog_option( $blog_id, 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
317 }
318 }
319 } else {
320 // In a single site environment, just find the old user in the approved list and update the email.
321 if ( Authorization::get_instance()->is_email_in_list( $user['user_email'], 'approved' ) ) {
322 $auth_settings_access_users_approved = $options->sanitize_user_list( $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ) );
323 foreach ( $auth_settings_access_users_approved as $key => $check_user ) {
324 // Update old user email in approved list to the new email.
325 if ( 0 === strcasecmp( $check_user['email'], $user['user_email'] ) ) {
326 $auth_settings_access_users_approved[ $key ]['email'] = Helper::lowercase( $userdata['user_email'] );
327 }
328 // If new user email is already in approved list, remove that entry.
329 if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) {
330 unset( $auth_settings_access_users_approved[ $key ] );
331 }
332 }
333 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
334 }
335 }
336
337 // We're hooking into this filter merely for its location in the codebase,
338 // so make sure to return the filter value unmodified.
339 return $send;
340 }
341
342
343 /**
344 * Remove user from authorizer lists when that user is deleted in WordPress.
345 *
346 * Action: delete_user
347 *
348 * @param int $user_id User ID to remove.
349 * @return void
350 */
351 public function remove_user_from_authorizer_when_deleted( $user_id ) {
352 $options = Options::get_instance();
353 $user = get_user_by( 'id', $user_id );
354 $deleted_email = $user->user_email;
355
356 // Remove user from pending/approved lists and save.
357 $list_names = array( 'access_users_pending', 'access_users_approved' );
358 foreach ( $list_names as $list_name ) {
359 $user_list = $options->sanitize_user_list( $options->get( $list_name, Helper::SINGLE_CONTEXT ) );
360 $list_changed = false;
361 foreach ( $user_list as $key => $existing_user ) {
362 if ( 0 === strcasecmp( $deleted_email, $existing_user['email'] ) ) {
363 $list_changed = true;
364 unset( $user_list[ $key ] );
365 }
366 }
367 if ( $list_changed ) {
368 update_option( 'auth_settings_' . $list_name, $user_list );
369 }
370 }
371 }
372
373
374 /**
375 * Remove multisite user from authorizer lists when that user is deleted from Network Users.
376 *
377 * Action: wpmu_delete_user
378 *
379 * @param int $user_id User ID to remove.
380 * @return void
381 */
382 public function remove_network_user_from_authorizer_when_deleted( $user_id ) {
383 $options = Options::get_instance();
384 $user = get_user_by( 'id', $user_id );
385 $deleted_email = $user->user_email;
386
387 // Go through multisite approved user list and remove this user.
388 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
389 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
390 );
391 $list_changed = false;
392 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
393 if ( 0 === strcasecmp( $deleted_email, $existing_user['email'] ) ) {
394 $list_changed = true;
395 unset( $auth_multisite_settings_access_users_approved[ $key ] );
396 }
397 }
398 if ( $list_changed ) {
399 update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
400 }
401
402 // Go through all pending/approved lists on individual sites and remove this user from them.
403 // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound
404 $sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) );
405 foreach ( $sites as $site ) {
406 $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id'];
407 $this->remove_network_user_from_site_when_removed( $user_id, $blog_id );
408 }
409
410 }
411
412
413 /**
414 * Remove multisite user from a specific site's lists when that user is removed from the site.
415 *
416 * Action: remove_user_from_blog
417 *
418 * @param int $user_id User ID to remove.
419 * @param int $blog_id Blog ID to remove from.
420 * @return void
421 */
422 public function remove_network_user_from_site_when_removed( $user_id, $blog_id ) {
423 $user = get_user_by( 'id', $user_id );
424 $deleted_email = $user->user_email;
425
426 $list_names = array( 'access_users_pending', 'access_users_approved' );
427 foreach ( $list_names as $list_name ) {
428 $user_list = get_blog_option( $blog_id, 'auth_settings_' . $list_name, array() );
429 $list_changed = false;
430 foreach ( $user_list as $key => $existing_user ) {
431 if ( 0 === strcasecmp( $deleted_email, $existing_user['email'] ) ) {
432 $list_changed = true;
433 unset( $user_list[ $key ] );
434 }
435 }
436 if ( $list_changed ) {
437 update_blog_option( $blog_id, 'auth_settings_' . $list_name, $user_list );
438 }
439 }
440 }
441
442
443 /**
444 * Helper: Add multisite user to a specific site's approved list.
445 *
446 * @param int $user_id User ID to add.
447 * @param int $blog_id Blog ID to add to.
448 * @return void
449 */
450 protected function add_network_user_to_site( $user_id, $blog_id ) {
451 // Switch to blog.
452 switch_to_blog( $blog_id );
453
454 // Get user details and role.
455 $options = Options::get_instance();
456 $access_default_role = $options->get( 'access_default_role', Helper::SINGLE_CONTEXT, 'allow override' );
457 $user = get_user_by( 'id', $user_id );
458 $user_email = $user->user_email;
459 $user_role = $user && is_array( $user->roles ) && count( $user->roles ) > 0 ? $user->roles[0] : $access_default_role;
460
461 // Add user to approved list if not already there and not in blocked list.
462 $auth_settings_access_users_approved = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT );
463 $auth_settings_access_users_blocked = $options->get( 'access_users_blocked', Helper::SINGLE_CONTEXT );
464 if ( ! Helper::in_multi_array( $user_email, $auth_settings_access_users_approved ) && ! Helper::in_multi_array( $user_email, $auth_settings_access_users_blocked ) ) {
465 $approved_user = array(
466 'email' => Helper::lowercase( $user_email ),
467 'role' => $user_role,
468 'date_added' => date( 'M Y', strtotime( $user->user_registered ) ),
469 'local_user' => true,
470 );
471 array_push( $auth_settings_access_users_approved, $approved_user );
472 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
473 }
474
475 // Restore original blog.
476 restore_current_blog();
477 }
478
479
480 /**
481 * Multisite:
482 * When an existing user is invited to the current site (or a new user is created),
483 * add them to the authorizer approved list. This action fires when the admin
484 * doesn't select the "Skip Confirmation Email" option.
485 *
486 * Action: invite_user
487 *
488 * @param int $user_id The invited user's ID.
489 * @param array $role The role of the invited user (or none if a new user creation).
490 * @param string $newuser_key The key of the invitation.
491 */
492 public function add_existing_user_to_authorizer_when_created( $user_id, $role = array(), $newuser_key = '' ) {
493 $user = get_user_by( 'id', $user_id );
494 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles, $role );
495 }
496
497
498 /**
499 * Multisite:
500 * When an existing user is invited to the current site (or a new user is created),
501 * add them to the authorizer approved list. This action fires when the admin
502 * selects the "Skip Confirmation Email" option.
503 *
504 * Action: added_existing_user
505 *
506 * @param int $user_id The invited user's ID.
507 * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
508 */
509 public function add_existing_user_to_authorizer_when_created_noconfirmation( $user_id, $result ) {
510 $user = get_user_by( 'id', $user_id );
511 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles );
512 }
513
514
515 /**
516 * Multisite:
517 * When a new user is invited to the current site (or a new user is created),
518 * add them to the authorizer approved list.
519 *
520 * Action: after_signup_user
521 *
522 * @param string $user User's requested login name.
523 * @param string $user_email User's email address.
524 * @param string $key User's activation key.
525 * @param array $meta Additional signup meta, including initially set roles.
526 */
527 public function add_new_user_to_authorizer_when_created( $user, $user_email, $key, $meta ) {
528 $user_roles = isset( $meta['new_role'] ) ? array( $meta['new_role'] ) : array();
529 $this->add_user_to_authorizer_when_created( $user_email, time(), $user_roles );
530 }
531
532
533 /**
534 * Single site:
535 * When a new user is added in single site mode, add them to the authorizer
536 * approved list.
537 *
538 * Action: edit_user_created_user
539 *
540 * @param int $user_id ID of the newly created user.
541 * @param string $notify Type of notification that should happen. See
542 * wp_send_new_user_notifications() for more
543 * information on possible values.
544 */
545 public function add_new_user_to_authorizer_when_created_single_site( $user_id, $notify ) {
546 $user = get_user_by( 'id', $user_id );
547 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles );
548 }
549
550
551 /**
552 * Helper: When a new user is added/invited to the current site (or a new
553 * user is created), add them to the authorizer approved list.
554 *
555 * @param string $user_email Email address of user to add.
556 * @param string $date_registered Date user registered.
557 * @param array $user_roles Role to add for user.
558 * @param array $default_role Default role, if no role specified.
559 */
560 protected function add_user_to_authorizer_when_created( $user_email, $date_registered, $user_roles = array(), $default_role = array() ) {
561 $options = Options::get_instance();
562 $auth_multisite_settings_access_users_approved = is_multisite() ? get_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', array() ) : array();
563 $auth_settings_access_users_pending = $options->get( 'access_users_pending', Helper::SINGLE_CONTEXT );
564 $auth_settings_access_users_approved = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT );
565 $auth_settings_access_users_blocked = $options->get( 'access_users_blocked', Helper::SINGLE_CONTEXT );
566
567 // Get default role if one isn't specified.
568 if ( count( $default_role ) < 1 ) {
569 $default_role = '';
570 } else {
571 // If default role was provided, it came from the invite_user hook, and
572 // only contains the role's display name. Here we look up the actual role
573 // name to save (and default to no role if the display name isn't found).
574 global $wp_roles;
575 $default_role_display_name = $default_role['name'];
576 $default_role = '';
577 foreach ( $wp_roles->role_names as $role_name => $display_name ) {
578 if ( $default_role_display_name === $display_name ) {
579 $default_role = $role_name;
580 break;
581 }
582 }
583 }
584
585 $updated = false;
586
587 // Skip if user is in blocked list.
588 if ( Helper::in_multi_array( $user_email, $auth_settings_access_users_blocked ) ) {
589 return;
590 }
591 // Remove from pending list if there.
592 foreach ( $auth_settings_access_users_pending as $key => $pending_user ) {
593 if ( 0 === strcasecmp( $pending_user['email'], $user_email ) ) {
594 unset( $auth_settings_access_users_pending[ $key ] );
595 $updated = true;
596 }
597 }
598 // Skip if user is in multisite approved list.
599 if ( Helper::in_multi_array( $user_email, $auth_multisite_settings_access_users_approved ) ) {
600 return;
601 }
602 // Add to approved list if not there.
603 if ( ! Helper::in_multi_array( $user_email, $auth_settings_access_users_approved ) ) {
604 $approved_user = array(
605 'email' => Helper::lowercase( $user_email ),
606 'role' => is_array( $user_roles ) && count( $user_roles ) > 0 ? $user_roles[0] : $default_role,
607 'date_added' => date( 'M Y', strtotime( $date_registered ) ),
608 'local_user' => true,
609 );
610 array_push( $auth_settings_access_users_approved, $approved_user );
611 $updated = true;
612 }
613
614 if ( $updated ) {
615 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
616 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
617 }
618 }
619
620
621 /**
622 * Multisite:
623 * When a user is granted super admin status (checkbox on network user edit
624 * screen), add them to the authorizer network approved list. Also remove
625 * them from pending/approved list on any individual sites.
626 *
627 * Action: grant_super_admin
628 *
629 * @param int $user_id The user's ID.
630 */
631 public function grant_super_admin__add_to_network_approved( $user_id ) {
632 $options = Options::get_instance();
633 $user = get_user_by( 'id', $user_id );
634 $user_email = $user->user_email;
635
636 // Add user to multisite approved user list (if not already there).
637 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
638 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
639 );
640 if ( ! Helper::in_multi_array( $user_email, $auth_multisite_settings_access_users_approved ) ) {
641 $multisite_approved_user = array(
642 'email' => Helper::lowercase( $user_email ),
643 'role' => count( $user->roles ) > 0 ? $user->roles[0] : 'administrator',
644 'date_added' => date( 'M Y', strtotime( $user->user_registered ) ),
645 'local_user' => true,
646 );
647 array_push( $auth_multisite_settings_access_users_approved, $multisite_approved_user );
648 update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
649 }
650
651 // Go through all pending/approved lists on individual sites and remove this user from them.
652 // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound
653 $sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) );
654 foreach ( $sites as $site ) {
655 $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id'];
656 $this->remove_network_user_from_site_when_removed( $user_id, $blog_id );
657 }
658
659 }
660
661
662 /**
663 * Multisite:
664 * When a user's super admin status is revoked (checkbox on network user edit
665 * screen), remove them from the authorizer network approved list. Also add
666 * them to approved list on any individual sites they are already a part of.
667 *
668 * Action: revoke_super_admin
669 *
670 * @param int $user_id The user's ID.
671 */
672 public function revoke_super_admin__remove_from_network_approved( $user_id ) {
673 $options = Options::get_instance();
674 $user = get_user_by( 'id', $user_id );
675 $revoked_email = $user->user_email;
676
677 // Go through multisite approved user list and remove this user.
678 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
679 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
680 );
681 $list_changed = false;
682 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
683 if ( 0 === strcasecmp( $revoked_email, $existing_user['email'] ) ) {
684 $list_changed = true;
685 unset( $auth_multisite_settings_access_users_approved[ $key ] );
686 }
687 }
688 if ( $list_changed ) {
689 update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
690 }
691
692 // Go through this user's current sites and add them to the approved list
693 // (since they are no longer on the network approved list).
694 $sites_of_user = get_blogs_of_user( $user_id );
695 foreach ( $sites_of_user as $site ) {
696 $blog_id = $site->userblog_id;
697 $this->add_network_user_to_site( $user_id, $blog_id );
698 }
699 }
700
701 }
702