Make WordPress Core

Ticket #54164: 54164.diff

File 54164.diff, 2.3 KB (added by dd32, 2 years ago)
  • wp-includes/class-wp-user.php

    class WP_User { 
    529529        }
    530530
    531531        /**
    532532         * Add role to user.
    533533         *
    534534         * Updates the user's meta data option with capabilities and roles.
    535535         *
    536536         * @since 2.0.0
    537537         *
    538538         * @param string $role Role name.
    539539         */
    540540        public function add_role( $role ) {
    541541                if ( empty( $role ) ) {
    542542                        return;
    543543                }
     544                if ( in_array( $role, $this->roles, true ) ) {
     545                        return;
     546                }
    544547
    545548                $this->caps[ $role ] = true;
    546549                update_user_meta( $this->ID, $this->cap_key, $this->caps );
    547550                $this->get_role_caps();
    548551                $this->update_user_level_from_caps();
    549552
    550553                /**
    551554                 * Fires immediately after the user has been given a new role.
    552555                 *
    553556                 * @since 4.3.0
    554557                 *
    555558                 * @param int    $user_id The user ID.
    556559                 * @param string $role    The new role.
    557560                 */
    558561                do_action( 'add_user_role', $this->ID, $role );
    class WP_User { 
    604607                foreach ( (array) $this->roles as $oldrole ) {
    605608                        unset( $this->caps[ $oldrole ] );
    606609                }
    607610
    608611                $old_roles = $this->roles;
    609612                if ( ! empty( $role ) ) {
    610613                        $this->caps[ $role ] = true;
    611614                        $this->roles         = array( $role => true );
    612615                } else {
    613616                        $this->roles = false;
    614617                }
    615618                update_user_meta( $this->ID, $this->cap_key, $this->caps );
    616619                $this->get_role_caps();
    617620                $this->update_user_level_from_caps();
    618621
     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
    619636                /**
    620637                 * Fires after the user's role has changed.
    621638                 *
    622639                 * @since 2.9.0
    623640                 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
    624641                 *
    625642                 * @param int      $user_id   The user ID.
    626643                 * @param string   $role      The new role.
    627644                 * @param string[] $old_roles An array of the user's previous roles.
    628645                 */
    629646                do_action( 'set_user_role', $this->ID, $role, $old_roles );
    630647        }
    631648
    632649        /**
    633650         * Choose the maximum level the user has.