PluginProbe
Authorizer / 3.13.0
Authorizer v3.13.0
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 3.13.0, at src/authorizer/class-sync-userdata.php

722 lines 28.7 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 Singleton {
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_main_site_id( get_main_network_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' => wp_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 // Allow overriding the email subject via filter or constant.
166 if ( defined( 'AUTHORIZER_EMAIL_APPROVED_USERS_SUBJECT' ) ) {
167 $subject = \AUTHORIZER_EMAIL_APPROVED_USERS_SUBJECT;
168 }
169 /**
170 * Filters the email subject sent to new users when approving them.
171 *
172 * @since 3.11.0
173 *
174 * @param string $subject The email subject.
175 */
176 $subject = apply_filters( 'authorizer_email_approved_users_subject', $subject );
177
178 // Allow overriding the email body via filter or constant.
179 if ( defined( 'AUTHORIZER_EMAIL_APPROVED_USERS_BODY' ) ) {
180 $body = \AUTHORIZER_EMAIL_APPROVED_USERS_BODY;
181 }
182 /**
183 * Filters the email body sent to new users when approving them.
184 *
185 * @since 3.11.0
186 *
187 * @param string $body The email body.
188 */
189 $body = apply_filters( 'authorizer_email_approved_users_body', $body );
190
191 // Fail if the subject/body options don't exist or are empty.
192 if ( empty( $subject ) || empty( $body ) ) {
193 return false;
194 }
195
196 // Replace approved shortcode patterns in subject and body.
197 $site_name = get_bloginfo( 'name' );
198 $site_url = get_site_url();
199 $subject = str_replace( '[site_name]', $site_name, $subject );
200 $body = str_replace( '[site_name]', $site_name, $body );
201 $body = str_replace( '[site_url]', $site_url, $body );
202 $body = str_replace( '[user_email]', $email, $body );
203 $headers = 'Content-type: text/html' . "\r\n";
204
205 // Send email.
206 wp_mail( $email, $subject, $body, $headers );
207
208 // Indicate mail was sent.
209 return true;
210 }
211
212
213 /**
214 * When they successfully log in, make sure WordPress users are in the approved list.
215 *
216 * Action: wp_login
217 *
218 * @param string $user_login Username of the user logging in.
219 * @param object $user WP_User object of the user logging in.
220 * @return void
221 */
222 public function ensure_wordpress_user_in_approved_list_on_login( $user_login, $user ) {
223 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles );
224 }
225
226
227 /**
228 * Update user role in approved list if it's changed via bulk action on the
229 * WordPress list users page.
230 *
231 * @hook set_user_role
232 *
233 * @param integer $user_id The user ID.
234 * @param string $role The new role.
235 * @param array $old_roles An array of the user's previous roles.
236 */
237 public function set_user_role_sync_role( $user_id = 0, $role = '', $old_roles = array() ) {
238 // Ensure valid user ID and user has permission to edit this user.
239 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) {
240 return;
241 }
242
243 // Get original user object (fail if not a real WordPress user).
244 $userdata = get_userdata( $user_id );
245 if ( ! $userdata ) {
246 return;
247 }
248
249 // If user is in approved list, update his/her associated role.
250 if ( Authorization::get_instance()->is_email_in_list( $userdata->user_email, 'approved' ) ) {
251 $changed = false;
252 $options = Options::get_instance();
253 $auth_settings_access_users_approved = $options->sanitize_user_list( $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ) );
254 foreach ( $auth_settings_access_users_approved as $key => $check_user ) {
255 if ( 0 === strcasecmp( $check_user['email'], $userdata->user_email ) ) {
256 if ( empty( $role ) ) {
257 unset( $auth_settings_access_users_approved[ $key ] );
258 $changed = true;
259 } elseif ( $auth_settings_access_users_approved[ $key ]['role'] !== $role ) {
260 $auth_settings_access_users_approved[ $key ]['role'] = $role;
261 $changed = true;
262 }
263 }
264 }
265 if ( $changed ) {
266 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
267 }
268 }
269 }
270
271
272 /**
273 * Sync any email address changes to WordPress accounts to the corresponding
274 * entry in the Authorizer approved list.
275 *
276 * Note: This filter fires in wp_update_user() if the update includes an
277 * email address change, and fires after all security and integrity checks
278 * have been performed, so we can simply update the Authorizer approved
279 * list, changing the email address on the approved entry, and removing any
280 * existing entries that also have the new email address (duplicates).
281 *
282 * Filter: send_email_change_email
283 *
284 * @param bool $send Whether to send the email.
285 * @param array $user The original user array.
286 * @param array $userdata The updated user array.
287 */
288 public function edit_user_profile_update_email( $send, $user, $userdata ) {
289 $options = Options::get_instance();
290
291 // If we're in multisite, update the email on all sites in the network
292 // (and remove from any subsites if it's a network-approved user).
293 if ( is_multisite() ) {
294 // If it's a multisite approved user, sync the email there.
295 $changed_user_is_multisite_user = false;
296 if ( Authorization::get_instance()->is_email_in_list( $user['user_email'], 'approved', 'multisite' ) ) {
297 $changed_user_is_multisite_user = true;
298 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
299 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
300 );
301 foreach ( $auth_multisite_settings_access_users_approved as $key => $check_user ) {
302 // Update old user email in approved list to the new email.
303 if ( 0 === strcasecmp( $check_user['email'], $user['user_email'] ) ) {
304 $auth_multisite_settings_access_users_approved[ $key ]['email'] = Helper::lowercase( $userdata['user_email'] );
305 }
306 // If new user email is already in approved list, remove that entry.
307 if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) {
308 unset( $auth_multisite_settings_access_users_approved[ $key ] );
309 }
310 }
311 update_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
312 }
313
314 // Go through all approved lists on individual sites and sync this user there.
315 // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound
316 $sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) );
317 foreach ( $sites as $site ) {
318 $updated = false;
319 $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id'];
320 $auth_settings_access_users_approved = get_blog_option( $blog_id, 'auth_settings_access_users_approved', array() );
321 foreach ( $auth_settings_access_users_approved as $key => $check_user ) {
322 // Update old user email in approved list to the new email.
323 if ( 0 === strcasecmp( $check_user['email'], $user['user_email'] ) ) {
324 // But if the user is already a multisite user, just remove the entry in the subsite.
325 if ( $changed_user_is_multisite_user ) {
326 unset( $auth_settings_access_users_approved[ $key ] );
327 } else {
328 $auth_settings_access_users_approved[ $key ]['email'] = Helper::lowercase( $userdata['user_email'] );
329 }
330 $updated = true;
331 }
332 // If new user email is already in approved list, remove that entry.
333 if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) {
334 unset( $auth_settings_access_users_approved[ $key ] );
335 $updated = true;
336 }
337 }
338 if ( $updated ) {
339 update_blog_option( $blog_id, 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
340 }
341 }
342 } elseif ( Authorization::get_instance()->is_email_in_list( $user['user_email'], 'approved' ) ) {
343 // In a single site environment, just find the old user in the approved list and update the email.
344 $auth_settings_access_users_approved = $options->sanitize_user_list( $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ) );
345 foreach ( $auth_settings_access_users_approved as $key => $check_user ) {
346 // Update old user email in approved list to the new email.
347 if ( 0 === strcasecmp( $check_user['email'], $user['user_email'] ) ) {
348 $auth_settings_access_users_approved[ $key ]['email'] = Helper::lowercase( $userdata['user_email'] );
349 }
350 // If new user email is already in approved list, remove that entry.
351 if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) {
352 unset( $auth_settings_access_users_approved[ $key ] );
353 }
354 }
355 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
356 }
357
358 // We're hooking into this filter merely for its location in the codebase,
359 // so make sure to return the filter value unmodified.
360 return $send;
361 }
362
363
364 /**
365 * Remove user from authorizer lists when that user is deleted in WordPress.
366 *
367 * Action: delete_user
368 *
369 * @param int $user_id User ID to remove.
370 * @return void
371 */
372 public function remove_user_from_authorizer_when_deleted( $user_id ) {
373 $options = Options::get_instance();
374 $user = get_user_by( 'id', $user_id );
375 $deleted_email = $user->user_email;
376
377 // Remove user from pending/approved lists and save.
378 $list_names = array( 'access_users_pending', 'access_users_approved' );
379 foreach ( $list_names as $list_name ) {
380 $user_list = $options->sanitize_user_list( $options->get( $list_name, Helper::SINGLE_CONTEXT ) );
381 $list_changed = false;
382 foreach ( $user_list as $key => $existing_user ) {
383 if ( 0 === strcasecmp( $deleted_email, $existing_user['email'] ) ) {
384 $list_changed = true;
385 unset( $user_list[ $key ] );
386 }
387 }
388 if ( $list_changed ) {
389 update_option( 'auth_settings_' . $list_name, $user_list );
390 }
391 }
392 }
393
394
395 /**
396 * Remove multisite user from authorizer lists when that user is deleted from Network Users.
397 *
398 * Action: wpmu_delete_user
399 *
400 * @param int $user_id User ID to remove.
401 * @return void
402 */
403 public function remove_network_user_from_authorizer_when_deleted( $user_id ) {
404 $options = Options::get_instance();
405 $user = get_user_by( 'id', $user_id );
406 $deleted_email = $user->user_email;
407
408 // Go through multisite approved user list and remove this user.
409 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
410 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
411 );
412 $list_changed = false;
413 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
414 if ( 0 === strcasecmp( $deleted_email, $existing_user['email'] ) ) {
415 $list_changed = true;
416 unset( $auth_multisite_settings_access_users_approved[ $key ] );
417 }
418 }
419 if ( $list_changed ) {
420 update_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
421 }
422
423 // Go through all pending/approved lists on individual sites and remove this user from them.
424 // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound
425 $sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) );
426 foreach ( $sites as $site ) {
427 $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id'];
428 $this->remove_network_user_from_site_when_removed( $user_id, $blog_id );
429 }
430 }
431
432
433 /**
434 * Remove multisite user from a specific site's lists when that user is removed from the site.
435 *
436 * Action: remove_user_from_blog
437 *
438 * @param int $user_id User ID to remove.
439 * @param int $blog_id Blog ID to remove from.
440 * @return void
441 */
442 public function remove_network_user_from_site_when_removed( $user_id, $blog_id ) {
443 $user = get_user_by( 'id', $user_id );
444 $deleted_email = $user->user_email;
445
446 $list_names = array( 'access_users_pending', 'access_users_approved' );
447 foreach ( $list_names as $list_name ) {
448 $user_list = get_blog_option( $blog_id, 'auth_settings_' . $list_name, array() );
449 $list_changed = false;
450 foreach ( $user_list as $key => $existing_user ) {
451 if ( 0 === strcasecmp( $deleted_email, $existing_user['email'] ) ) {
452 $list_changed = true;
453 unset( $user_list[ $key ] );
454 }
455 }
456 if ( $list_changed ) {
457 update_blog_option( $blog_id, 'auth_settings_' . $list_name, $user_list );
458 }
459 }
460 }
461
462
463 /**
464 * Helper: Add multisite user to a specific site's approved list.
465 *
466 * @param int $user_id User ID to add.
467 * @param int $blog_id Blog ID to add to.
468 * @return void
469 */
470 protected function add_network_user_to_site( $user_id, $blog_id ) {
471 // Switch to blog.
472 switch_to_blog( $blog_id );
473
474 // Get user details and role.
475 $options = Options::get_instance();
476 $access_default_role = $options->get( 'access_default_role', Helper::SINGLE_CONTEXT, 'allow override' );
477 $user = get_user_by( 'id', $user_id );
478 $user_email = $user->user_email;
479 $user_role = $user && is_array( $user->roles ) && count( $user->roles ) > 0 ? $user->roles[0] : $access_default_role;
480
481 // Add user to approved list if not already there and not in blocked list.
482 $auth_settings_access_users_approved = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT );
483 $auth_settings_access_users_blocked = $options->get( 'access_users_blocked', Helper::SINGLE_CONTEXT );
484 if ( ! Helper::in_multi_array( $user_email, $auth_settings_access_users_approved ) && ! Helper::in_multi_array( $user_email, $auth_settings_access_users_blocked ) ) {
485 $approved_user = array(
486 'email' => Helper::lowercase( $user_email ),
487 'role' => $user_role,
488 'date_added' => wp_date( 'M Y', strtotime( $user->user_registered ) ),
489 'local_user' => true,
490 );
491 array_push( $auth_settings_access_users_approved, $approved_user );
492 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
493 }
494
495 // Restore original blog.
496 restore_current_blog();
497 }
498
499
500 /**
501 * Multisite:
502 * When an existing user is invited to the current site (or a new user is created),
503 * add them to the authorizer approved list. This action fires when the admin
504 * doesn't select the "Skip Confirmation Email" option.
505 *
506 * Action: invite_user
507 *
508 * @param int $user_id The invited user's ID.
509 * @param array $role The role of the invited user (or none if a new user creation).
510 * @param string $newuser_key The key of the invitation.
511 */
512 public function add_existing_user_to_authorizer_when_created( $user_id, $role = array(), $newuser_key = '' ) {
513 $user = get_user_by( 'id', $user_id );
514 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles, $role );
515 }
516
517
518 /**
519 * Multisite:
520 * When an existing user is invited to the current site (or a new user is created),
521 * add them to the authorizer approved list. This action fires when the admin
522 * selects the "Skip Confirmation Email" option.
523 *
524 * Action: added_existing_user
525 *
526 * @param int $user_id The invited user's ID.
527 * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
528 */
529 public function add_existing_user_to_authorizer_when_created_noconfirmation( $user_id, $result ) {
530 $user = get_user_by( 'id', $user_id );
531 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles );
532 }
533
534
535 /**
536 * Multisite:
537 * When a new user is invited to the current site (or a new user is created),
538 * add them to the authorizer approved list.
539 *
540 * Action: after_signup_user
541 *
542 * @param string $user User's requested login name.
543 * @param string $user_email User's email address.
544 * @param string $key User's activation key.
545 * @param array $meta Additional signup meta, including initially set roles.
546 */
547 public function add_new_user_to_authorizer_when_created( $user, $user_email, $key, $meta ) {
548 $user_roles = isset( $meta['new_role'] ) ? array( $meta['new_role'] ) : array();
549 $this->add_user_to_authorizer_when_created( $user_email, time(), $user_roles );
550 }
551
552
553 /**
554 * Single site:
555 * When a new user is added in single site mode, add them to the authorizer
556 * approved list.
557 *
558 * Action: edit_user_created_user
559 *
560 * @param int $user_id ID of the newly created user.
561 * @param string $notify Type of notification that should happen. See
562 * wp_send_new_user_notifications() for more
563 * information on possible values.
564 */
565 public function add_new_user_to_authorizer_when_created_single_site( $user_id, $notify ) {
566 $user = get_user_by( 'id', $user_id );
567 $this->add_user_to_authorizer_when_created( $user->user_email, $user->user_registered, $user->roles );
568 }
569
570
571 /**
572 * Helper: When a new user is added/invited to the current site (or a new
573 * user is created), add them to the authorizer approved list.
574 *
575 * @param string $user_email Email address of user to add.
576 * @param string $date_registered Date user registered.
577 * @param array $user_roles Role to add for user.
578 * @param array $default_role Default role, if no role specified.
579 */
580 protected function add_user_to_authorizer_when_created( $user_email, $date_registered, $user_roles = array(), $default_role = array() ) {
581 $options = Options::get_instance();
582 $auth_multisite_settings_access_users_approved = is_multisite() ? get_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved', array() ) : array();
583 $auth_settings_access_users_pending = $options->get( 'access_users_pending', Helper::SINGLE_CONTEXT );
584 $auth_settings_access_users_approved = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT );
585 $auth_settings_access_users_blocked = $options->get( 'access_users_blocked', Helper::SINGLE_CONTEXT );
586
587 // Get default role if one isn't specified.
588 if ( count( $default_role ) < 1 ) {
589 $default_role = '';
590 } else {
591 // If default role was provided, it came from the invite_user hook, and
592 // only contains the role's display name. Here we look up the actual role
593 // name to save (and default to no role if the display name isn't found).
594 global $wp_roles;
595 $default_role_display_name = $default_role['name'];
596 $default_role = '';
597 if ( ! empty( $wp_roles ) && is_array( $wp_roles->role_names ) ) {
598 foreach ( $wp_roles->role_names as $role_name => $display_name ) {
599 if ( $default_role_display_name === $display_name ) {
600 $default_role = $role_name;
601 break;
602 }
603 }
604 }
605 }
606
607 $updated = false;
608
609 // Skip if user is in blocked list.
610 if ( Helper::in_multi_array( $user_email, $auth_settings_access_users_blocked ) ) {
611 return;
612 }
613 // Remove from pending list if there.
614 foreach ( $auth_settings_access_users_pending as $key => $pending_user ) {
615 if ( 0 === strcasecmp( $pending_user['email'], $user_email ) ) {
616 unset( $auth_settings_access_users_pending[ $key ] );
617 $updated = true;
618 }
619 }
620 // Skip if user is in multisite approved list.
621 if ( Helper::in_multi_array( $user_email, $auth_multisite_settings_access_users_approved ) ) {
622 return;
623 }
624 // Add to approved list if not there.
625 if ( ! Helper::in_multi_array( $user_email, $auth_settings_access_users_approved ) ) {
626 $approved_user = array(
627 'email' => Helper::lowercase( $user_email ),
628 'role' => is_array( $user_roles ) && count( $user_roles ) > 0 ? $user_roles[0] : $default_role,
629 'date_added' => wp_date( 'M Y', strtotime( $date_registered ) ),
630 'local_user' => true,
631 );
632 array_push( $auth_settings_access_users_approved, $approved_user );
633 $updated = true;
634 }
635
636 if ( $updated ) {
637 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
638 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
639 }
640 }
641
642
643 /**
644 * Multisite:
645 * When a user is granted super admin status (checkbox on network user edit
646 * screen), add them to the authorizer network approved list. Also remove
647 * them from pending/approved list on any individual sites.
648 *
649 * Action: grant_super_admin
650 *
651 * @param int $user_id The user's ID.
652 */
653 public function grant_super_admin__add_to_network_approved( $user_id ) {
654 $options = Options::get_instance();
655 $user = get_user_by( 'id', $user_id );
656 $user_email = $user->user_email;
657
658 // Add user to multisite approved user list (if not already there).
659 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
660 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
661 );
662 if ( ! Helper::in_multi_array( $user_email, $auth_multisite_settings_access_users_approved ) ) {
663 $multisite_approved_user = array(
664 'email' => Helper::lowercase( $user_email ),
665 'role' => count( $user->roles ) > 0 ? $user->roles[0] : 'administrator',
666 'date_added' => wp_date( 'M Y', strtotime( $user->user_registered ) ),
667 'local_user' => true,
668 );
669 array_push( $auth_multisite_settings_access_users_approved, $multisite_approved_user );
670 update_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
671 }
672
673 // Go through all pending/approved lists on individual sites and remove this user from them.
674 // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound
675 $sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) );
676 foreach ( $sites as $site ) {
677 $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id'];
678 $this->remove_network_user_from_site_when_removed( $user_id, $blog_id );
679 }
680 }
681
682
683 /**
684 * Multisite:
685 * When a user's super admin status is revoked (checkbox on network user edit
686 * screen), remove them from the authorizer network approved list. Also add
687 * them to approved list on any individual sites they are already a part of.
688 *
689 * Action: revoke_super_admin
690 *
691 * @param int $user_id The user's ID.
692 */
693 public function revoke_super_admin__remove_from_network_approved( $user_id ) {
694 $options = Options::get_instance();
695 $user = get_user_by( 'id', $user_id );
696 $revoked_email = $user->user_email;
697
698 // Go through multisite approved user list and remove this user.
699 $auth_multisite_settings_access_users_approved = $options->sanitize_user_list(
700 $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT )
701 );
702 $list_changed = false;
703 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
704 if ( 0 === strcasecmp( $revoked_email, $existing_user['email'] ) ) {
705 $list_changed = true;
706 unset( $auth_multisite_settings_access_users_approved[ $key ] );
707 }
708 }
709 if ( $list_changed ) {
710 update_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
711 }
712
713 // Go through this user's current sites and add them to the approved list
714 // (since they are no longer on the network approved list).
715 $sites_of_user = get_blogs_of_user( $user_id );
716 foreach ( $sites_of_user as $site ) {
717 $blog_id = $site->userblog_id;
718 $this->add_network_user_to_site( $user_id, $blog_id );
719 }
720 }
721 }
722