wp/wp-includes/class-wp-user.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
--- a/wp/wp-includes/class-wp-user.php	Wed Sep 21 18:19:35 2022 +0200
+++ b/wp/wp-includes/class-wp-user.php	Tue Sep 27 16:37:53 2022 +0200
@@ -34,6 +34,7 @@
  * @property string $locale
  * @property string $rich_editing
  * @property string $syntax_highlighting
+ * @property string $use_ssl
  */
 class WP_User {
 	/**
@@ -177,7 +178,7 @@
 	}
 
 	/**
-	 * Return only the main user fields
+	 * Returns only the main user fields.
 	 *
 	 * @since 3.3.0
 	 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
@@ -185,8 +186,8 @@
 	 * @global wpdb $wpdb WordPress database abstraction object.
 	 *
 	 * @param string     $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
-	 * @param string|int $value The field value
-	 * @return object|false Raw user object
+	 * @param string|int $value The field value.
+	 * @return object|false Raw user object.
 	 */
 	public static function get_data_by( $field, $value ) {
 		global $wpdb;
@@ -388,7 +389,7 @@
 	}
 
 	/**
-	 * Determine whether the user exists in the database.
+	 * Determines whether the user exists in the database.
 	 *
 	 * @since 3.4.0
 	 *
@@ -399,7 +400,7 @@
 	}
 
 	/**
-	 * Retrieve the value of a property or meta key.
+	 * Retrieves the value of a property or meta key.
 	 *
 	 * Retrieves from the users and usermeta table.
 	 *
@@ -413,13 +414,13 @@
 	}
 
 	/**
-	 * Determine whether a property or meta key is set
+	 * Determines whether a property or meta key is set.
 	 *
 	 * Consults the users and usermeta tables.
 	 *
 	 * @since 3.3.0
 	 *
-	 * @param string $key Property
+	 * @param string $key Property.
 	 * @return bool
 	 */
 	public function has_prop( $key ) {
@@ -427,7 +428,7 @@
 	}
 
 	/**
-	 * Return an array representation.
+	 * Returns an array representation.
 	 *
 	 * @since 3.5.0
 	 *
@@ -454,7 +455,7 @@
 	}
 
 	/**
-	 * Set up capability object properties.
+	 * Sets up capability object properties.
 	 *
 	 * Will set the value for the 'cap_key' property to current database table
 	 * prefix, followed by 'capabilities'. Will then check to see if the
@@ -528,7 +529,7 @@
 	}
 
 	/**
-	 * Add role to user.
+	 * Adds role to user.
 	 *
 	 * Updates the user's meta data option with capabilities and roles.
 	 *
@@ -541,6 +542,10 @@
 			return;
 		}
 
+		if ( in_array( $role, $this->roles, true ) ) {
+			return;
+		}
+
 		$this->caps[ $role ] = true;
 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
 		$this->get_role_caps();
@@ -558,7 +563,7 @@
 	}
 
 	/**
-	 * Remove role from user.
+	 * Removes role from user.
 	 *
 	 * @since 2.0.0
 	 *
@@ -568,6 +573,7 @@
 		if ( ! in_array( $role, $this->roles, true ) ) {
 			return;
 		}
+
 		unset( $this->caps[ $role ] );
 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
 		$this->get_role_caps();
@@ -585,7 +591,7 @@
 	}
 
 	/**
-	 * Set the role of the user.
+	 * Sets the role of the user.
 	 *
 	 * This will remove the previous roles of the user and assign the user the
 	 * new one. You can set the role to an empty string and it will remove all
@@ -605,16 +611,32 @@
 		}
 
 		$old_roles = $this->roles;
+
 		if ( ! empty( $role ) ) {
 			$this->caps[ $role ] = true;
 			$this->roles         = array( $role => true );
 		} else {
-			$this->roles = false;
+			$this->roles = array();
 		}
+
 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
 		$this->get_role_caps();
 		$this->update_user_level_from_caps();
 
+		foreach ( $old_roles as $old_role ) {
+			if ( ! $old_role || $old_role === $role ) {
+				continue;
+			}
+
+			/** This action is documented in wp-includes/class-wp-user.php */
+			do_action( 'remove_user_role', $this->ID, $old_role );
+		}
+
+		if ( $role && ! in_array( $role, $old_roles, true ) ) {
+			/** This action is documented in wp-includes/class-wp-user.php */
+			do_action( 'add_user_role', $this->ID, $role );
+		}
+
 		/**
 		 * Fires after the user's role has changed.
 		 *
@@ -629,7 +651,7 @@
 	}
 
 	/**
-	 * Choose the maximum level the user has.
+	 * Chooses the maximum level the user has.
 	 *
 	 * Will compare the level from the $item parameter against the $max
 	 * parameter. If the item is incorrect, then just the $max parameter value
@@ -656,7 +678,7 @@
 	}
 
 	/**
-	 * Update the maximum user level for the user.
+	 * Updates the maximum user level for the user.
 	 *
 	 * Updates the 'user_level' user metadata (includes prefix that is the
 	 * database table prefix) with the maximum user level. Gets the value from
@@ -673,7 +695,7 @@
 	}
 
 	/**
-	 * Add capability and grant or deny access to capability.
+	 * Adds capability and grant or deny access to capability.
 	 *
 	 * @since 2.0.0
 	 *
@@ -688,7 +710,7 @@
 	}
 
 	/**
-	 * Remove capability from user.
+	 * Removes capability from user.
 	 *
 	 * @since 2.0.0
 	 *
@@ -705,7 +727,7 @@
 	}
 
 	/**
-	 * Remove all of the capabilities of the user.
+	 * Removes all of the capabilities of the user.
 	 *
 	 * @since 2.1.0
 	 *
@@ -802,7 +824,7 @@
 	}
 
 	/**
-	 * Convert numeric level to level capability name.
+	 * Converts numeric level to level capability name.
 	 *
 	 * Prepends 'level_' to level number.
 	 *
@@ -816,7 +838,7 @@
 	}
 
 	/**
-	 * Set the site to operate on. Defaults to the current site.
+	 * Sets the site to operate on. Defaults to the current site.
 	 *
 	 * @since 3.0.0
 	 * @deprecated 4.9.0 Use WP_User::for_site()