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

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