Make WordPress Core

Opened 7 weeks ago

Last modified 5 weeks ago

#54572 reviewing enhancement

Add a function for updating the existing role instead of removing, then adding one

Reported by: maksimkuzmin Owned by: audrasjb
Milestone: 6.0 Priority: normal
Severity: normal Version:
Component: Role/Capability Keywords: has-patch has-unit-tests
Focuses: performance Cc:

Description

While I was working on and developing multiple themes and plugins, I often needed to update the existing role after using add_role function.

Say, I already have a role picture_admin. I might want to alter its display name, or capabilities as an array, or both at once. What I'm forced to do now is this (not taking peculiar ways of editing wp_options table in account):

<?php

remove_role( 'picture_admin' );
add_role( 'picture_admin', $display_name, $capabilities );

This code has multiple issues. First, I'll have to perform double query to MySQL database, which affects performance and doesn't feel right to me. Second, I won't be able to alter just one field as easily with one function, I need to carry out the old variable for either display name or capabilities every time I call this statement.

Instead, I propose having a neat way of easily updating roles via the following statement:

<?php

update_role( $role_name, 'New Display Name', $caps );
// or
update_role( $role_name, null, $caps );
// or
update_role( $role_name, 'New Display Name' );

Which also creates a role if it doesn't exist already.

Attachments (1)

54572.diff (5.7 KB) - added by maksimkuzmin 7 weeks ago.
Provides the proposed functionality as well as tests.

Download all attachments as: .zip

Change History (8)

@maksimkuzmin
7 weeks ago

Provides the proposed functionality as well as tests.

#1 @sabernhardt
7 weeks ago

  • Component changed from General to Users
  • Keywords has-patch has-unit-tests added
  • Version trunk deleted

#2 @sabernhardt
7 weeks ago

  • Component changed from Users to Role/Capability

This ticket was mentioned in Slack in #core by maksimkuzmin. View the logs.


5 weeks ago

This ticket was mentioned in Slack in #core by sabernhardt. View the logs.


5 weeks ago

This ticket was mentioned in Slack in #core by sergey. View the logs.


5 weeks ago

#6 @SergeyBiryukov
5 weeks ago

  • Milestone changed from Awaiting Review to 6.0

Hi there, welcome to WordPress Trac! Thanks for the ticket and the patch.

The idea makes sense to me at a glance, and it's great that unit tests are also included. Moving to 6.0 for review.

Last edited 5 weeks ago by SergeyBiryukov (previous) (diff)

#7 @audrasjb
5 weeks ago

  • Owner set to audrasjb
  • Status changed from new to reviewing
Note: See TracTickets for help on using tickets.