544 | 547 | |
545 | 548 | $this->caps[ $role ] = true; |
546 | 549 | update_user_meta( $this->ID, $this->cap_key, $this->caps ); |
547 | 550 | $this->get_role_caps(); |
548 | 551 | $this->update_user_level_from_caps(); |
549 | 552 | |
550 | 553 | /** |
551 | 554 | * Fires immediately after the user has been given a new role. |
552 | 555 | * |
553 | 556 | * @since 4.3.0 |
554 | 557 | * |
555 | 558 | * @param int $user_id The user ID. |
556 | 559 | * @param string $role The new role. |
557 | 560 | */ |
558 | 561 | do_action( 'add_user_role', $this->ID, $role ); |
604 | 607 | foreach ( (array) $this->roles as $oldrole ) { |
605 | 608 | unset( $this->caps[ $oldrole ] ); |
606 | 609 | } |
607 | 610 | |
608 | 611 | $old_roles = $this->roles; |
609 | 612 | if ( ! empty( $role ) ) { |
610 | 613 | $this->caps[ $role ] = true; |
611 | 614 | $this->roles = array( $role => true ); |
612 | 615 | } else { |
613 | 616 | $this->roles = false; |
614 | 617 | } |
615 | 618 | update_user_meta( $this->ID, $this->cap_key, $this->caps ); |
616 | 619 | $this->get_role_caps(); |
617 | 620 | $this->update_user_level_from_caps(); |
618 | 621 | |
| 622 | foreach ( $old_roles as $old_role ) { |
| 623 | if ( $old_role === $role ) { |
| 624 | continue; |
| 625 | } |
| 626 | |
| 627 | /** This action is documented in wp-includes/class-wp-user.php */ |
| 628 | do_action( 'remove_user_role', $this->ID, $old_role ); |
| 629 | } |
| 630 | |
| 631 | if ( $role && ! in_array( $role, $old_roles, true ) ) { |
| 632 | /** This action is documented in wp-includes/class-wp-user.php */ |
| 633 | do_action( 'add_user_role', $this->ID, $old_role ); |
| 634 | } |
| 635 | |
619 | 636 | /** |
620 | 637 | * Fires after the user's role has changed. |
621 | 638 | * |
622 | 639 | * @since 2.9.0 |
623 | 640 | * @since 3.6.0 Added $old_roles to include an array of the user's previous roles. |
624 | 641 | * |
625 | 642 | * @param int $user_id The user ID. |
626 | 643 | * @param string $role The new role. |
627 | 644 | * @param string[] $old_roles An array of the user's previous roles. |
628 | 645 | */ |
629 | 646 | do_action( 'set_user_role', $this->ID, $role, $old_roles ); |
630 | 647 | } |
631 | 648 | |
632 | 649 | /** |
633 | 650 | * Choose the maximum level the user has. |