Make WordPress Core


Ignore:
Timestamp:
09/19/2022 08:47:22 PM (2 years ago)
Author:
davidbaumwald
Message:

Role/Capability: Add a new update_role function.

Until now, changing a user's role involved deleting a user's role then re-adding. This change creates a new update_role function and associated method in WP_Roles to consolidate this process.

This commit also introduces new unit tests around update_role and adds additional "unhappy path" tests for roles and capabilities in general.

Props maksimkuzmin, peterwilsoncc, NomNom99, costdev, SergeyBiryukov.
Fixes #54572.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/capabilities.php

    r53436 r54213  
    10321032
    10331033/**
     1034 * Updates an existing role. Creates a new role if it doesn't exist.
     1035 *
     1036 * Modifies the display name and/or capabilities for an existing role.
     1037 * If the role does not exist then a new role is created.
     1038 *
     1039 * The capabilities are defined in the following format: `array( 'read' => true )`.
     1040 * To explicitly deny the role a capability, set the value for that capability to false.
     1041 *
     1042 * @since 6.1.0
     1043 *
     1044 * @param string      $role         Role name.
     1045 * @param string|null $display_name Optional. Role display name. If null, the display name
     1046 *                                  is not modified. Default null.
     1047 * @param bool[]|null $capabilities Optional. List of capabilities keyed by the capability name,
     1048 *                                  e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`.
     1049 *                                  If null, don't alter capabilities for the existing role and make
     1050 *                                  empty capabilities for the new one. Default null.
     1051 * @return WP_Role|void WP_Role object, if the role is updated.
     1052 */
     1053function update_role( $role, $display_name = null, $capabilities = null ) {
     1054    return wp_roles()->update_role( $role, $display_name, $capabilities );
     1055}
     1056
     1057/**
    10341058 * Removes a role, if it exists.
    10351059 *
Note: See TracChangeset for help on using the changeset viewer.