| @@ -30,9 +30,9 @@ | ||
| 30 | 30 | */ |
| 31 | 31 | public function add_wp_users_to_approved_list() { |
| 32 | 32 | $options = Options::get_instance(); |
| 33 | 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(); | |
| 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 | 35 | $auth_settings_access_users_pending = $options->get( 'access_users_pending', Helper::SINGLE_CONTEXT ); |
| 36 | 36 | $auth_settings_access_users_approved = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ); |
| 37 | 37 | $auth_settings_access_users_blocked = $options->get( 'access_users_blocked', Helper::SINGLE_CONTEXT ); |
| 38 | 38 | $updated = false; |
| @@ -64,10 +64,10 @@ | ||
| 64 | 64 | $updated = true; |
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | if ( $updated ) { |
| 68 | - update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending, false ); | |
| 69 | - update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved, false ); | |
| 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 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | |
| @@ -155,42 +155,16 @@ | ||
| 155 | 155 | $recently_sent_emails[] = array( |
| 156 | 156 | 'email' => $email, |
| 157 | 157 | 'time' => time(), |
| 158 | 158 | ); |
| 159 | - update_option( 'auth_settings_recently_sent_emails', $recently_sent_emails, false ); | |
| 159 | + update_option( 'auth_settings_recently_sent_emails', $recently_sent_emails ); | |
| 160 | 160 | |
| 161 | 161 | // Get welcome email subject and body text. |
| 162 | 162 | $subject = $options->get( 'access_email_approved_users_subject' ); |
| 163 | 163 | $body = apply_filters( 'the_content', $options->get( 'access_email_approved_users_body' ) ); |
| 164 | 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 | 165 | // Fail if the subject/body options don't exist or are empty. |
| 192 | - if ( empty( $subject ) || empty( $body ) ) { | |
| 166 | + if ( is_null( $subject ) || is_null( $body ) || strlen( $subject ) === 0 || strlen( $body ) === 0 ) { | |
| 193 | 167 | return false; |
| 194 | 168 | } |
| 195 | 169 | |
| 196 | 170 | // Replace approved shortcode patterns in subject and body. |
| @@ -224,8 +198,55 @@ | ||
| 224 | 198 | } |
| 225 | 199 | |
| 226 | 200 | |
| 227 | 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' ) || ! property_exists( $user, 'ID' ) ) { | |
| 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 | + /** | |
| 228 | 249 | * Update user role in approved list if it's changed via bulk action on the |
| 229 | 250 | * WordPress list users page. |
| 230 | 251 | * |
| 231 | 252 | * @hook set_user_role |
| @@ -262,9 +283,9 @@ | ||
| 262 | 283 | } |
| 263 | 284 | } |
| 264 | 285 | } |
| 265 | 286 | if ( $changed ) { |
| 266 | - update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved, false ); | |
| 287 | + update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved ); | |
| 267 | 288 | } |
| 268 | 289 | } |
| 269 | 290 | } |
| 270 | 291 | |
| @@ -307,9 +328,9 @@ | ||
| 307 | 328 | if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) { |
| 308 | 329 | unset( $auth_multisite_settings_access_users_approved[ $key ] ); |
| 309 | 330 | } |
| 310 | 331 | } |
| 311 | - update_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved ); | |
| 332 | + update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved ); | |
| 312 | 333 | } |
| 313 | 334 | |
| 314 | 335 | // Go through all approved lists on individual sites and sync this user there. |
| 315 | 336 | // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| @@ -338,22 +359,24 @@ | ||
| 338 | 359 | if ( $updated ) { |
| 339 | 360 | update_blog_option( $blog_id, 'auth_settings_access_users_approved', $auth_settings_access_users_approved ); |
| 340 | 361 | } |
| 341 | 362 | } |
| 342 | - } elseif ( Authorization::get_instance()->is_email_in_list( $user['user_email'], 'approved' ) ) { | |
| 363 | + } else { | |
| 343 | 364 | // 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'] ); | |
| 365 | + if ( Authorization::get_instance()->is_email_in_list( $user['user_email'], 'approved' ) ) { | |
| 366 | + $auth_settings_access_users_approved = $options->sanitize_user_list( $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ) ); | |
| 367 | + foreach ( $auth_settings_access_users_approved as $key => $check_user ) { | |
| 368 | + // Update old user email in approved list to the new email. | |
| 369 | + if ( 0 === strcasecmp( $check_user['email'], $user['user_email'] ) ) { | |
| 370 | + $auth_settings_access_users_approved[ $key ]['email'] = Helper::lowercase( $userdata['user_email'] ); | |
| 371 | + } | |
| 372 | + // If new user email is already in approved list, remove that entry. | |
| 373 | + if ( 0 === strcasecmp( $check_user['email'], $userdata['user_email'] ) ) { | |
| 374 | + unset( $auth_settings_access_users_approved[ $key ] ); | |
| 375 | + } | |
| 349 | 376 | } |
| 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 | - } | |
| 377 | + update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved ); | |
| 354 | 378 | } |
| 355 | - update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved, false ); | |
| 356 | 379 | } |
| 357 | 380 | |
| 358 | 381 | // We're hooking into this filter merely for its location in the codebase, |
| 359 | 382 | // so make sure to return the filter value unmodified. |
| @@ -385,9 +408,9 @@ | ||
| 385 | 408 | unset( $user_list[ $key ] ); |
| 386 | 409 | } |
| 387 | 410 | } |
| 388 | 411 | if ( $list_changed ) { |
| 389 | - update_option( 'auth_settings_' . $list_name, $user_list, false ); | |
| 412 | + update_option( 'auth_settings_' . $list_name, $user_list ); | |
| 390 | 413 | } |
| 391 | 414 | } |
| 392 | 415 | } |
| 393 | 416 | |
| @@ -416,9 +439,9 @@ | ||
| 416 | 439 | unset( $auth_multisite_settings_access_users_approved[ $key ] ); |
| 417 | 440 | } |
| 418 | 441 | } |
| 419 | 442 | 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 ); | |
| 443 | + update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved ); | |
| 421 | 444 | } |
| 422 | 445 | |
| 423 | 446 | // Go through all pending/approved lists on individual sites and remove this user from them. |
| 424 | 447 | // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| @@ -426,8 +449,9 @@ | ||
| 426 | 449 | foreach ( $sites as $site ) { |
| 427 | 450 | $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 428 | 451 | $this->remove_network_user_from_site_when_removed( $user_id, $blog_id ); |
| 429 | 452 | } |
| 453 | + | |
| 430 | 454 | } |
| 431 | 455 | |
| 432 | 456 | |
| 433 | 457 | /** |
| @@ -488,9 +512,9 @@ | ||
| 488 | 512 | 'date_added' => wp_date( 'M Y', strtotime( $user->user_registered ) ), |
| 489 | 513 | 'local_user' => true, |
| 490 | 514 | ); |
| 491 | 515 | array_push( $auth_settings_access_users_approved, $approved_user ); |
| 492 | - update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved, false ); | |
| 516 | + update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved ); | |
| 493 | 517 | } |
| 494 | 518 | |
| 495 | 519 | // Restore original blog. |
| 496 | 520 | restore_current_blog(); |
| @@ -578,9 +602,9 @@ | ||
| 578 | 602 | * @param array $default_role Default role, if no role specified. |
| 579 | 603 | */ |
| 580 | 604 | protected function add_user_to_authorizer_when_created( $user_email, $date_registered, $user_roles = array(), $default_role = array() ) { |
| 581 | 605 | $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(); | |
| 606 | + $auth_multisite_settings_access_users_approved = is_multisite() ? get_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', array() ) : array(); | |
| 583 | 607 | $auth_settings_access_users_pending = $options->get( 'access_users_pending', Helper::SINGLE_CONTEXT ); |
| 584 | 608 | $auth_settings_access_users_approved = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT ); |
| 585 | 609 | $auth_settings_access_users_blocked = $options->get( 'access_users_blocked', Helper::SINGLE_CONTEXT ); |
| 586 | 610 | |
| @@ -593,14 +617,12 @@ | ||
| 593 | 617 | // name to save (and default to no role if the display name isn't found). |
| 594 | 618 | global $wp_roles; |
| 595 | 619 | $default_role_display_name = $default_role['name']; |
| 596 | 620 | $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 | - } | |
| 621 | + foreach ( $wp_roles->role_names as $role_name => $display_name ) { | |
| 622 | + if ( $default_role_display_name === $display_name ) { | |
| 623 | + $default_role = $role_name; | |
| 624 | + break; | |
| 603 | 625 | } |
| 604 | 626 | } |
| 605 | 627 | } |
| 606 | 628 | |
| @@ -633,10 +655,10 @@ | ||
| 633 | 655 | $updated = true; |
| 634 | 656 | } |
| 635 | 657 | |
| 636 | 658 | if ( $updated ) { |
| 637 | - update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending, false ); | |
| 638 | - update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved, false ); | |
| 659 | + update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending ); | |
| 660 | + update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved ); | |
| 639 | 661 | } |
| 640 | 662 | } |
| 641 | 663 | |
| 642 | 664 | |
| @@ -666,9 +688,9 @@ | ||
| 666 | 688 | 'date_added' => wp_date( 'M Y', strtotime( $user->user_registered ) ), |
| 667 | 689 | 'local_user' => true, |
| 668 | 690 | ); |
| 669 | 691 | 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 ); | |
| 692 | + update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved ); | |
| 671 | 693 | } |
| 672 | 694 | |
| 673 | 695 | // Go through all pending/approved lists on individual sites and remove this user from them. |
| 674 | 696 | // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| @@ -676,8 +698,9 @@ | ||
| 676 | 698 | foreach ( $sites as $site ) { |
| 677 | 699 | $blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 678 | 700 | $this->remove_network_user_from_site_when_removed( $user_id, $blog_id ); |
| 679 | 701 | } |
| 702 | + | |
| 680 | 703 | } |
| 681 | 704 | |
| 682 | 705 | |
| 683 | 706 | /** |
| @@ -706,9 +729,9 @@ | ||
| 706 | 729 | unset( $auth_multisite_settings_access_users_approved[ $key ] ); |
| 707 | 730 | } |
| 708 | 731 | } |
| 709 | 732 | 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 ); | |
| 733 | + update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved ); | |
| 711 | 734 | } |
| 712 | 735 | |
| 713 | 736 | // Go through this user's current sites and add them to the approved list |
| 714 | 737 | // (since they are no longer on the network approved list). |
| @@ -717,5 +740,6 @@ | ||
| 717 | 740 | $blog_id = $site->userblog_id; |
| 718 | 741 | $this->add_network_user_to_site( $user_id, $blog_id ); |
| 719 | 742 | } |
| 720 | 743 | } |
| 744 | + | |
| 721 | 745 | } |