Index: src/wp-includes/capabilities.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php
--- a/src/wp-includes/capabilities.php	(revision 52305)
+++ b/src/wp-includes/capabilities.php	(date 1638549784425)
@@ -849,6 +849,26 @@
 	return wp_roles()->add_role( $role, $display_name, $capabilities );
 }
 
+/**
+ * Add role, if it does not exist. Update, if exists.
+ *
+ * @param string $role Role name.
+ * @param string|null $display_name Role display name.
+ * @param bool[]|null $capabilities List of capabilities keyed by the capability name,
+ *                                  e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
+ *                                  If null, don't alter capabilities for the existing role and make
+ *                                  empty capabilities for the new one.
+ *
+ * @return WP_Role|null WP_Role object if role is added, null if already exists.
+ */
+function update_role( $role, $display_name, $capabilities = null ) {
+	if ( empty( $role ) ) {
+		return;
+	}
+
+	return wp_roles()->update_role( $role, $display_name, $capabilities );
+}
+
 /**
  * Remove role, if it exists.
  *
Index: src/wp-includes/class-wp-roles.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/wp-includes/class-wp-roles.php b/src/wp-includes/class-wp-roles.php
--- a/src/wp-includes/class-wp-roles.php	(revision 52305)
+++ b/src/wp-includes/class-wp-roles.php	(date 1638549774027)
@@ -170,6 +170,57 @@
 		return $this->role_objects[ $role ];
 	}
 
+	/**
+	 * Update the existing role. If the role doesn't exist, add a new role.
+	 *
+	 * Updates the list of roles, if the role doesn't already exist.
+	 *
+	 * For the capabilities format, @param string $role Role name.
+	 *
+	 * @param string|null $display_name Role display name.
+	 * @param bool[]|null $capabilities List of capabilities keyed by the capability name,
+	 *                                  e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
+	 *                                  If null, don't alter capabilities for the existing role and make
+	 *                                  empty capabilities for the new one.
+	 *
+	 * @return WP_Role|void             WP_Role object, if role is added.
+	 * @see WP_Roles::add_role()
+	 *
+	 */
+	public function update_role( $role, $display_name = null, $capabilities = null ) {
+		if ( empty( $role ) || ( null === $display_name && null === $capabilities ) ) {
+			return;
+		}
+
+		if ( null === $display_name ) {
+			if ( isset( $this->role_objects[ $role ] ) ) {
+				$display_name = $this->roles[ $role ]['name'];
+			} else {
+				return;
+			}
+		}
+
+		if ( null === $capabilities ) {
+			if ( isset( $this->role_objects[ $role ] ) ) {
+				$capabilities = $this->role_objects[ $role ]->capabilities;
+			} else {
+				$capabilities = array();
+			}
+		}
+
+		if ( isset( $this->roles[ $role ] ) ) {
+			if ( null === $capabilities ) {
+				$capabilities = $this->role_objects[ $role ]->capabilities;
+			}
+
+			unset( $this->role_objects[ $role ] );
+			unset( $this->role_names[ $role ] );
+			unset( $this->roles[ $role ] );
+		}
+
+		return $this->add_role( $role, $display_name, $capabilities );
+	}
+
 	/**
 	 * Remove role by name.
 	 *
Index: tests/phpunit/tests/user/capabilities.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php
--- a/tests/phpunit/tests/user/capabilities.php	(revision 52305)
+++ b/tests/phpunit/tests/user/capabilities.php	(date 1638549500882)
@@ -980,6 +980,57 @@
 		$this->assertFalse( $wp_roles->is_role( $role_name ) );
 	}
 
+	/**
+	 * Update role.
+	 */
+	public function test_update_role() {
+		global $wp_roles;
+
+		// Create role if it does not exist.
+		$role_name     = 'janitor';
+		$expected_caps = array(
+			'edit_posts' => true,
+			'edit_pages' => true,
+			'level_0'    => true,
+			'level_1'    => true,
+			'level_2'    => true,
+		);
+		update_role( $role_name, 'Janitor', $expected_caps );
+		$this->flush_roles();
+		$this->assertTrue( $wp_roles->is_role( $role_name ) );
+
+		// Update both role name and capabilities.
+		$expected_display_name = 'Janitor Manager';
+		$new_expected_caps     = array(
+			'level_3' => true,
+			'level_4' => true,
+		);
+		update_role( $role_name, $expected_display_name, $new_expected_caps );
+		$this->flush_roles();
+
+		$this->assertEquals( $expected_display_name, $wp_roles->roles[ $role_name ]['name'] );
+		$this->assertEquals( $new_expected_caps, $wp_roles->roles[ $role_name ]['capabilities'] );
+
+		// Update role name only.
+		$expected_display_name = 'Janitor Executive';
+		update_role( $role_name, $expected_display_name );
+		$this->flush_roles();
+
+		$this->assertEquals( $expected_display_name, $wp_roles->roles[ $role_name ]['name'] );
+		$this->assertEquals( $new_expected_caps, $wp_roles->roles[ $role_name ]['capabilities'] );
+
+		// Update empty capabilities only (proper null vs empty array handling).
+		update_role( $role_name, null, array() );
+		$this->flush_roles();
+
+		$this->assertEmpty( $wp_roles->roles[ $role_name ]['capabilities'] );
+
+		// Clean up.
+		remove_role( $role_name );
+		$this->flush_roles();
+		$this->assertFalse( $wp_roles->is_role( $role_name ) );
+	}
+
 	/**
 	 * Change the capabilites associated with a role and make sure the change
 	 * is reflected in has_cap().
