Skip to content

Abilities API: Add the core/user-create, core/user-update and core/user-delete abilities - #16074

Open
jorgefilipecosta wants to merge 16 commits into
WordPress:trunkfrom
jorgefilipecosta:add/user-write-abilities
Open

jorgefilipecosta wants to merge 16 commits into
WordPress:trunkfrom
jorgefilipecosta:add/user-write-abilities

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #10775, with the shape from WordPress/abilities#22.

This PR adds the core/user-create, core/user-update and core/user-delete abilities to the user category:

  • core/user-create takes { data, fields } and returns the new user. data.username and data.email are required. The password is generated and never returned: the new user gets the email with the link to set one.
  • core/user-update takes { where: { id }, data, fields }, changes only the attributes in data and returns the user. An empty roles list removes every role of the user.
  • core/user-delete takes { where: { id }, reassign, permanent } and returns { outcome }: deleted, or removed on multisite, where the user leaves the current site and the account stays in the network. reassign is required: the ID of the user who takes over the content, or null. permanent is required too: true on a single site, where the delete cannot be undone, and false on multisite.
  • data accepts email, name, first_name, last_name, nickname, slug, description, url, locale and roles, plus username on create.
  • Create requires create_users, update a logged-in user and delete delete_users (remove_users on multisite). Each call then checks the capabilities of the user for the target user and for the roles it sets.
  • A user cannot delete themselves, or remove themselves on multisite.
  • The output is the core/users-query item, with the same fields and the same checks per field.
  • core/users-query is registered as before: its fields and item schemas moved to private helpers that the new abilities share. In its tests, the helper that registers it now unregisters the four user abilities, and the run route tests get the cases of the three new abilities.

Creating a user:

const user = await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/user-create/run',
	method: 'POST',
	data: {
		input: {
			data: { username: 'anasilva', email: 'ana@example.org', name: 'Ana Silva', roles: [ 'author' ] },
		},
	},
} );
{
	"id": 2,
	"name": "Ana Silva",
	"link": "http://example.org/author/anasilva/",
	"slug": "anasilva",
	"avatar_urls": {
		"24": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=24&d=mm&r=g",
		"48": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=48&d=mm&r=g",
		"96": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=96&d=mm&r=g"
	}
}

Changing the role and the description, and asking for some fields:

await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/user-update/run',
	method: 'POST',
	data: {
		input: {
			where: { id: user.id },
			data: { roles: [ 'editor' ], description: 'Writes the release notes.' },
			fields: [ 'name', 'email', 'roles', 'description' ],
		},
	},
} );
{ "id": 2, "name": "Ana Silva", "description": "Writes the release notes.", "email": "ana@example.org", "roles": [ "editor" ] }

Deleting the user and giving their content to user 1 (use the ID of a user of your site). With reassign: null the content is deleted with the user. On multisite, send permanent: false:

await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/user-delete/run',
	method: 'POST',
	data: { input: { where: { id: user.id }, reassign: 1, permanent: true } },
} );
{ "outcome": "deleted" }

Trac ticket: https://core.trac.wordpress.org/ticket/66720

Testing

Verify unit tests are passing:

npm run test:php -- --group 66720

Or run the examples above in the editor console as an administrator.

AI disclosure: the change and tests were drafted with AI assistance (Claude Code).

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jorgefilipecosta, gziolo.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

…re/user-delete` abilities.

Add three abilities to the `user` category:

- `core/user-create` creates a user from `data` and returns it. The password is generated and never returned: the new user receives the email with the link to set one.
- `core/user-update` updates the given attributes of the user matched by `where` and returns it.
- `core/user-delete` deletes the user matched by `where`, or on multisite removes the user from the current site. `reassign` is required: the ID of the user who takes over the content, or false.

The attributes that can be written are the email, name, first name, last name, nickname, slug, description, URL, locale and roles, plus the username on create. Each call checks the capabilities of the current user for the target user and for the roles it sets.

The `fields` and item schemas of `core/users-query` move to private helpers that the new abilities share. That ability is registered as before.
Replace the `@ticket` placeholder in the tests of `core/user-create`, `core/user-update` and `core/user-delete` with 66720.

See #66720.
…er write abilities.

`core/user-create` and `core/user-update` passed the slug to the save as it was sent, so a character such as a slash was removed instead of becoming a hyphen. The slug is now cleaned with `sanitize_title()` first, and the checks and the save both start from that cleaned value: `Team/Desk` is saved as `team-desk`.

A slug that another user already has now returns `user_exists`, as a taken username or email does, so a caller can tell it from a slug that is empty after cleaning.

The description of the `reassign` input of `core/user-delete` now says what happens to the content when no user takes it over.

See #66720.
…ite abilities.

`core/user-create` and `core/user-update` refuse a URL that has nothing left after sanitizing, such as one with a scheme that is not allowed. The error said the value must be at least 1 character long, although a value was sent. It now has the existing "Invalid URL." message, so the caller knows what to fix.

The comments of the attribute checks now say that the `pre_user_email`, `pre_user_display_name`, `pre_user_nicename` and `pre_user_url` filters run in the check and again in the save.

See #66720.
The output schema now lists `deleted` and `removed`, the two values the ability returns, so that clients can check the response.

See #66720.
…delete`.

`reassign` took a user ID or `false`, and `true` passed the schema and was refused by the execute callback. It now takes a user ID or `null`, so the schema refuses `true` and `false` itself, and `reassign` is a "type or `null`" input like the other ones in the core abilities.

The value stays required: `null` has to be sent to reassign nothing, and a missing value is still refused.

See #66720.
Users have no Trash, so on a single site the delete cannot be undone, and on multisite the user is only removed from the current site. The caller now has to state which one is asked for:

- On a single site `permanent` must be `true`. `false` returns `user_trash_not_supported`.
- On multisite `permanent` must be `false`. `true` returns `user_delete_not_supported`, as the account is not deleted there.

The input has the same shape in both site modes, and a caller who asks for a final delete never gets a removal instead.

See #66720.
An empty `roles` list now removes every role of the user: the account and the content stay, and the user stays a member of the site with no role. Leaving `roles` out still keeps the roles, and `core/user-create` still needs at least one role when `roles` is sent.

Nobody can remove all of their own roles, network administrators included. That call returns `user_cannot_assign_role` with the message of the Users screen.

See #66720.
…e` error messages.

The two messages that name the `permanent` input now write it the way the other delete abilities do, and the value is read the same way.

See #66720.
On a single site, deleting a user who owns links without reassigning the content calls wp_delete_link(), which is defined in wp-admin/includes/bookmark.php.
…lbacks.

The capability checks of `core/user-update` and `core/user-delete`, and the role checks of `core/user-create` and `core/user-update`, run in the permission callbacks, and the execute callbacks no longer check them again, so the `wp_ability_permission_result` filter decides. `core/user-delete` refuses deleting or removing yourself, as the Users screen does, and the `core/user-create` schema limits `username` to 60 characters.

Also applies the remaining test review fixes: a missing `reassign` joins the unusable values, the REST reassign test keeps the three values only REST shows, and docblocks and names are fixed.
…r in the user write abilities.

The execute callbacks of `core/user-create`, `core/user-update`, and `core/user-delete` now read `where` and `data` through `WP_Abilities_Users::get_input_object()`, like the permission callbacks do, so static analysis knows both are arrays.

See #66720.
The execute callbacks of `core/user-create`, `core/user-update`, and `core/user-delete` pass on the input only when it is an array. The role, username, nickname, and slug checks take only strings, and a role or username that is not a string is refused. The user data schemas are typed as schemas, so `src/wp-includes/abilities` keeps passing the strict PHPStan configuration without baseline entries.

See #66720.
@jorgefilipecosta
jorgefilipecosta force-pushed the add/user-write-abilities branch from dfc2ae1 to fd61e14 Compare December 2, 2026 19:09
*/
'type' => array( 'integer', 'null' ),
'minimum' => 1,
'description' => __( 'The ID of the user who takes over the content of the deleted user, or null when no user takes it over. With null on a single site, the content is deleted with the account: posts and pages go to the Trash where the site has one, and the rest is deleted permanently. On multisite, null leaves the content where it is.' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small wording detail: “the rest is deleted permanently” is broader than what happens. The new test shows that a published pattern stays published when its author is deleted. Could we mention that exception here, as the ability description already does?

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, @jorgefilipecosta!

I applied the latest version locally and checked each of my eight comments again, on a single site and on multisite. All of them are addressed, and I have no further concerns about the behavior.

I am still working through the code quality checks. The PR is large, so this takes some time. I will follow up as soon as I finish.

One small request from that pass so far: could we remove the @since tags from the test files? There are about 150 of them, in the four new files under tests/phpunit/tests/abilities-api/ and in the new tests in wpRestAbilitiesUsersController.php. The testing handbook does not list @since as a test annotation, and @ticket already links every test to this change. Apart from that, the tests follow the handbook closely.

$where = $this->get_input_object( $input, 'where' ) ?? array();
$target = $this->get_target_user( $where['id'] ?? null );
if ( ! $target ) {
return true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part caught my attention. For a missing user, the permission check returns true. So it allows an edit without checking anything, and it relies on execute_user_update() to look up the user again and stop the call.

That is brittle. The permission check should not allow editing in the first place when it has no user to check. This ability has no required_capabilities, so being logged in is then the only check. If the second lookup ever finds a user, the changes are saved without the edit_user, promote_user and can_assign_roles() checks.

Could we return false here, or a WP_Error? Then the call stops at this point, and every write is for a user that was checked.

One note on the result for a wrong ID. With false, the caller gets a permission error and not the 404. A WP_Error keeps the user_not_found code over REST, but execute() reports it with _doing_it_wrong().

$where = $this->get_input_object( rest_sanitize_object( $input ), 'where' ) ?? array();
$target = $this->get_target_user( $where['id'] ?? null );

return ! $target || current_user_can( is_multisite() ? 'remove_user' : 'delete_user', $target->ID );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same pattern is here. The risk is lower, because the caller already needs delete_users or remove_users, and in core the check for a specific user needs the same capability. Could we also return false or a WP_Error for a missing user, for consistency?

'core/user-update',
array(
'label' => __( 'Update User' ),
'description' => __( 'Updates the given attributes of a user and returns the user. Attributes that are left out keep their stored values. Changing roles requires permission to promote the user; changing any other attribute requires permission to edit the user. Requires an authenticated user.' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also say two things that the caller cannot see from the schema? A change of email sends a notice to the previous address. And the username and the password cannot be changed here. core/user-create already describes its emails, so this would match.

'core/user-delete',
array(
'label' => __( 'Delete User' ),
'description' => __( 'Deletes a user. On a single site the account is deleted permanently, since users have no Trash, and `permanent` must be true. On multisite the user is removed from the current site instead, the account stays in the network, and `permanent` must be false. `reassign` is required: the ID of the user who takes over the content of the deleted user, or null to delete the content with the user. With null, posts and pages go to the Trash where the site has one, and other content is deleted permanently (media too, unless the site keeps a Trash for media); posts of a type that is not deleted with its author, such as patterns, stay as they are. On multisite, null leaves the content in place. Requires permission to delete the user, or on multisite to remove the user. Users cannot delete or remove themselves.' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description covers a single site and multisite at once, but a site is only one of them. The caller cannot tell which part applies, for example which value permanent needs. It also repeats the content rules from reassign, and the two texts already differ.

Could we pick the text for the current install, as we already do for required_capabilities? The same would help permanent, reassign and the data description of core/user-create. For a single site it could be:

Deletes a user permanently. Users have no Trash, so this cannot be undone, and permanent must be true. reassign decides what happens to the content of the user. Users cannot delete themselves. Requires permission to delete the user.

The content rules would then live only in the reassign description.

'type' => 'string',
'minLength' => 1,
'maxLength' => 60,
'description' => __( 'Login name for the user.' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Login name" does not tell the caller which names are accepted, and the rules are strict. On a single site, letters, digits, spaces and _ . - @ pass. On multisite, only lowercase letters and digits pass, with at least 4 characters. So jane.doe works on one install and fails on the other.

Could the description state the rule for the current install, and say that the username cannot be changed with core/user-update?

'slug' => array(
'type' => 'string',
'minLength' => 1,
'description' => __( 'An alphanumeric identifier for the user.' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these descriptions come from the REST schema. They name the field, but they do not tell the caller what to send. Three places where a few more words would help:

  • slug: it is the slug in the author archive URL, it must be unique, and on create it defaults to the username.
  • locale: the enum has an empty string, which means the site language.
  • url: an empty string removes the URL. The same works for first_name and description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core abilities Core abilities related tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants