Skip to content

Abilities API: Add the core/content-create, core/content-update and core/content-delete abilities - #16028

Open
jorgefilipecosta wants to merge 37 commits into
WordPress:trunkfrom
jorgefilipecosta:add/content-write-abilities
Open

jorgefilipecosta wants to merge 37 commits into
WordPress:trunkfrom
jorgefilipecosta:add/content-write-abilities

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

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

This PR adds the core/content-create, core/content-update and core/content-delete abilities to the content category, for the post types that set show_in_abilities:

  • core/content-create takes { data, fields } and returns the new post. data.type is required and the status defaults to draft.
  • core/content-update takes { where: { type, id }, data, fields }, changes only the attributes in data and returns the post.
  • core/content-delete takes { where: { type, id }, permanent } and returns { outcome }: trashed, or deleted when permanent is true.
  • data accepts title, content, excerpt, status, slug, date, author and parent.
  • The abilities require a logged-in user, and each call checks the capabilities of the user for the post and for the values it sets.
  • core/content-query now accepts trash in where.status, so a trashed post can be found to restore it with an update or to delete it permanently. Trashed posts are only returned when trash is requested by name.

Creating a draft:

const post = await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/content-create/run',
	method: 'POST',
	data: {
		input: {
			data: { type: 'post', title: 'Launch notes', content: '<!-- wp:paragraph --><p>We are live.</p><!-- /wp:paragraph -->' },
		},
	},
} );
{
	"id": 4,
	"type": "post",
	"status": "draft",
	"date": { "site": "2026-11-16T13:51:06+00:00", "gmt": "2026-11-16T13:51:06+00:00" },
	"slug": "",
	"title": { "rendered": "Launch notes" }
}

Publishing it with a slug, and asking for some fields:

await wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/content-update/run',
	method: 'POST',
	data: {
		input: {
			where: { type: 'post', id: post.id },
			data: { status: 'publish', slug: 'launch-notes' },
			fields: [ 'status', 'slug', 'title' ],
		},
	},
} );
{ "id": 4, "status": "publish", "slug": "launch-notes", "title": { "raw": "Launch notes", "rendered": "Launch notes" } }

Moving it to the Trash. With permanent: true in the input the outcome is deleted:

await wp.apiFetch( {
	path: wp.url.addQueryArgs( '/wp-abilities/v1/abilities/core/content-delete/run', {
		input: { where: { type: 'post', id: post.id } },
	} ),
	method: 'DELETE',
} );
{ "outcome": "trashed" }

Follow-ups

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

Testing

Verify unit tests are passing:

npm run test:php -- --group 66706

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) and reviewed by me.

@github-actions

github-actions Bot commented Nov 16, 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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @nesirzekerya11-source.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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.

@nesirzekerya11-source

Copy link
Copy Markdown

What is wrong please help

@nesirzekerya11-source

Copy link
Copy Markdown

Why not help until done error

@nesirzekerya11-source

Copy link
Copy Markdown

Ok what is my alternative stop to using online tell

Comment on lines +909 to +917
/*
* Known issue: a `data.status` sent for a post in the Trash moves it out of the Trash
* through a regular save, not through wp_untrash_post(). The `_wp_trash_meta_status`
* and `_wp_trash_meta_time` meta and the comments trashed with the post are left
* behind. The REST API posts endpoint has the same issue when an update sends a
* status for a trashed post. The cause is in the save path, so the fix belongs there,
* where it covers every caller of wp_update_post().
* See https://core.trac.wordpress.org/ticket/66809.
*/

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 note covers the cause well. One consequence from testing is worth adding. The old trash timestamp stays on the post. When the post is trashed again, the next scheduled cleanup can delete it permanently right away.

With #16214 applied, the same steps worked correctly. Could it land first, with a test for this sequence through the abilities? The design document now also says that comments come back with a restored post.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed, #16214 should land first, once it does I will remove the note and add a test for that sequence through the abilities, also checking the comments come back.

Comment on lines +1034 to +1044
$text_fields = array(
'title' => 'post_title',
'content' => 'post_content',
'excerpt' => 'post_excerpt',
);

foreach ( $text_fields as $attribute => $field ) {
if ( array_key_exists( $attribute, $data ) ) {
$postarr[ $field ] = $data[ $attribute ];
}
}

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.

An open question about Block Hooks. I am not sure how much we should worry about it.

On read, the two content fields differ. content.rendered includes hooked blocks by design, because the_content inserts them. content.raw is the stored markup without them. The REST posts endpoint adds hooked blocks to content.raw and marks them on the anchor block as already placed.

Block Hooks depends on those markers, and the editor saves them with the content. A client that changes the raw content through the abilities does not have them. In my test with a hooked core/loginout block, I could not remove the hooked block. When I added it by hand, it rendered twice.

Should the abilities follow REST here, or is it enough to document this for now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hi @gziolo, I think we should follow REST, the read side is fixed in #16287, and the write side is tracked in the follow-ups section of the PR description.

Comment on lines 1304 to 1307
// Intentionally strict: `site` and `gmt` that name different times are an error.
if ( $site && $gmt && $site[1] !== $gmt[1] ) {
return $this->invalid_attribute_error( 'data[date]', __( 'The `site` and `gmt` dates must name the same time.' ) );
}

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 check can refuse a date that core/content-query just returned.

WordPress stores the local date and the GMT date separately. A change of the site timezone does not update them. After that, the query returns two dates that name different times, for example:

{ "site": "2026-01-15T12:00:00+01:00", "gmt": "2026-01-15T12:00:00+00:00" }

In my test, I sent such a date back together with a new title. The update failed with content_invalid_attribute, and the title did not change. The REST posts endpoint accepted the same request.

The design document says that site wins when both are sent. Could we follow that here? A test for this round trip would help.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, site now wins when both are sent, and I added a test that sends the queried date back after a timezone change.

$statuses = array_values( get_post_stati( array( 'internal' => false ) ) );

$this->register_content_query( $post_types, $statuses );
$this->register_content_write_abilities( $post_types, $statuses );

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 is the same open question that I raised earlier today for settings in #10892 (comment). Every post type with show_in_abilities can now be created, updated, and deleted. In 7.2 the flag only made a post type readable through core/content-query. A plugin that set it then did not choose write access.

A setting can at least be marked readonly in its schema. A post type has no way to stay read only.

Could show_in_abilities get a way to mark a post type as read only? Today only true exposes a post type, and other values are kept free for later. An array such as array( 'readonly' => true ) could work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hi @gziolo, I agree a way to keep a post type read only makes sense, but this PR is already too huge, so we will add readonly as a follow-up. I added it to the follow-ups section in the PR description.

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.

Feel free to open a follow up ticket to track it. We need a generic solution for all write abilities. It looks like we can figure out something based on readonly marker. At least for start having readonly vs all possible for start should be enough.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I opened https://core.trac.wordpress.org/ticket/66844 to track it, with a first version in #16288 that the write abilities can use.

Comment on lines +2038 to +2039
private function get_content_write_properties( array $statuses ): array {
return array(

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 design document now adds terms and featured_media to create and update. They are in the input, in the fields list, and in the output. It also says that core/content-query gets both when core/content-create is added.

This PR has neither, and that makes sense to me. Both depend on abilities that are not in Core yet. featured_media takes an ID from the media abilities, and terms takes IDs from core/terms-query.

Two dedicated follow-ups sound like a great fit, one for terms and one for featured_media. I would at least create the two Trac tickets and link them in the tracking issue, WordPress/abilities#17.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed, we will add terms and featured_media as follow-ups on this ticket once the terms and media abilities land.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hi @gziolo, the follow-ups are https://core.trac.wordpress.org/ticket/66904 (terms, PR #16388) and https://core.trac.wordpress.org/ticket/66905 (featured_media, PR #16389). Thank you!

Comment on lines +2058 to +2062
'type' => 'string',
'minLength' => 1,
'description' => __( 'The post slug. It is sanitized as a post slug is, and made unique among the posts of the type that share its parent.' ),
),
'date' => array(

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.

A new draft has an empty slug. Create and query both return slug: "" for it. When a client sends that value back together with another change, minLength refuses the whole update with a 400. The REST posts endpoint accepts the same request and keeps the empty slug.

Could update accept an unchanged empty slug? If not, the description could say that clients must leave it out.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Update now accepts the stored slug unchanged, also the empty slug of a draft, and the minLength was removed 👍

Comment on lines +838 to +844
/*
* Mirror the REST posts controller: totals come from the underlying WP_Query,
* while the row-level checks above may withhold individual returned rows. The
* totals can therefore count posts the current user cannot read, such as another
* author's draft listed in `ids`. This is deliberate: it only reveals that such a
* post exists, as a REST collection request with `include` and `status` does.
*/

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.

A note for documentation purposes, no change is needed. With trash in the status filter, this also applies to trashed posts. The totals can count a trashed post that the caller cannot read.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you, the comment now mentions trashed posts too.

Comment on lines 1431 to +1440
/*
* Mirror the REST posts controller: totals come from the underlying WP_Query,
* while the row-level checks above may withhold individual returned rows. The
* totals can therefore count posts the current user cannot read, such as another
* author's draft listed in `ids`. This is deliberate: it only reveals that such a
* post exists, as a REST collection request with `include` and `status` does.
* Intentionally strict: the saved post passes the same read check as every query
* row. When the current user may no longer read it, for example after moving it to
* a status they cannot read, the write stands but only its ID is returned.
*/
return $this->prepare_output( $posts, $total, $total_pages );
if ( ! $this->check_read_permission( $post ) ) {
return array( 'id' => $post->ID );
}

return $this->format_post( $post, $fields );

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.

An open question about an edge case from testing. It needs a custom post type where the capability to create posts is separate from the capability to edit them, which is rare.

A user who can create but not edit gets the new draft back in this response. That is correct, because an author can read their own draft. But the same user cannot get it again with core/content-query. A query by its ID returns no items, and a query with status: draft returns 403.

REST has a single post route for this case, and it checks only the single post. Abilities only have the query. Should a query by a known ID follow the permission of the single post?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good edge case, I think a query by a known ID should follow the permission of the single post, we will handle this case as a follow-up. I added it to the follow-ups section in the PR description.

Comment on lines +866 to +873
// Checked again after the permission callback, whose result a filter can change.
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
return new WP_Error(
'content_cannot_create',
__( 'Sorry, you are not allowed to create posts as this user.' ),
array( 'status' => 403 )
);
}

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 all permission checks live in the permission callbacks? That is the place for permission rules. I asked for the same on the settings PR in #10892 (comment).

This check repeats the permission callback, and update and delete do the same. I think the three repeated checks can go. A filter can change the permission result, and that is by design. The developer who adds such a filter is responsible for keeping it secure.

The checks that depend on the input could move into the permission callbacks as well. These are the check for a status that needs publish_posts and the check for another author. A caller can then learn about them from check_permissions(), before the ability runs.

A failed permission check returns the generic ability_invalid_permissions error from execute(). So the tests that expect codes such as content_cannot_edit or content_cannot_publish will need an update, and the design document too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hi @gziolo, all the permission checks now live in the permission callbacks, also the status and author checks, the tests now expect the generic error and the design document was updated in WordPress/abilities#22.

@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.

Thank you for all the work on this. The PR is in great shape. Most of all, it follows the design document closely, so the three abilities match the proposed shape. The code is careful, and it comes with many tests.

I left nine inline comments. I am requesting changes because of four of them:

  • Permission checks should live in the permission callbacks.
  • A date that the query returns can fail on update.
  • An empty draft slug cannot be sent back.
  • The restore from the Trash needs #16214 first, with a test.

The other comments do not block the PR. They are open questions about write access for plugins, Block Hooks, and one edge case with capabilities. There is also a note about totals, and a suggestion to track terms and featured_media in two follow-up tickets.

I am happy to look again as soon as there is an update.

… callbacks.

The permission callbacks of `core/content-create` and `core/content-update` now also check the capabilities the sent status and author need, so the execute callbacks no longer check capabilities again.

When both the site and the GMT date are sent, the site date is used again, so the dates that `core/content-query` returns can be sent back, also after the site's timezone changed.

Sending back the stored slug, also the empty slug of a draft, leaves it unchanged.
… permission callbacks.

A missing or unreadable parent and the slug of a pending post that the user cannot publish are now refused by the permission callbacks, the same way as the status and author.
… callback and loop the content write steps.

The update and delete permission callbacks now share one check that reads the requested post once, and `prepare_post_data()` runs its status, author, parent, date and slug steps in one loop, each returning the fields to save.
…data and fix test names and messages.

Moves the output schema, author and raw fields tests and the shared fixtures into the data write trait, moves `assert_invalid_input()` to the write base class, and fixes test names, messages and assertions.
…tatus with the other stored attributes.

Unchanged status, author, parent and slug values are removed in one helper shared by the permission and execute callbacks, instead of separate checks in each step.
…t errors in one helper.

Describes the write data and the post array with PHPStan types, names the error helpers verb first and builds the filter and attribute errors in `get_invalid_input_error()`, replaces `get_changed_data()` with `get_unchanged_attributes()`, returns an error from `prepare_date()` for a date that cannot be read, lets the delete error name the step that failed, makes the slug of an auto-draft unique as the slug of a draft is, and shortens the docblocks.
…to their providers.

Moves more of the tests that create and update share into the data write trait, adds `run_ability_that_writes_nothing()` and `create_post()`, removes the helpers that only forwarded to another helper, turns tests that repeat a provider test into rows of that provider, merges the two delete matrix tests, and covers a create call granted by a filter and a direct call with a date that cannot be read.
…es` are writable.

The `register_post_type()` and `WP_Post_Type::$show_in_abilities` docs now say that the core content abilities can query, create, update, and delete the posts of an exposed post type.
…eturn save errors with their input path.

An empty `slug` sent to `core/content-create` counts as not sent, since a new post has no slug yet. The `empty_content` and `invalid_date` errors of the save are returned as `content_invalid_attribute` with `params`, like the other attribute errors. The known gap note about a status sent for a post in the Trash moves into the `execute_content_update()` docblock, and the `slug` description says that a post that has a slug cannot be given an empty one and that setting the slug of a pending post requires the capability to publish it.
…post types that support authors.

An `author` sent for a post type without author support is now rejected by the execute callbacks with `content_invalid_attribute` for every user, like a `parent` sent for a post type that is not hierarchical. Before, a user who cannot edit the posts of others got the generic permission error when naming another user. Adds a create test row for a user with the author role.
…on demand, and fold duplicated content tests into their providers.

The content query test and the content write test base now extend a new `WP_Abilities_Content_UnitTestCase`, which holds the shared `set_up()` and `tear_down()`. `run_ability()` registers the content abilities unless the test already did, so most tests no longer register them by hand. The create and existing-post tests share one revision fixture, and the REST content tests use the base `snapshot()`.

Tests that repeated another test with one different value are now rows of its provider: the create parent and author field tests, the update custom status tests, the delete special page tests of an administrator and a super admin, and the REST refused create test. Removes the multisite markup test that repeated a `data_filtered_markup()` case, the update schema assertions that the comparison with the create schema covers, and set-up that the shared fixtures already provide.
… only.

`test_invalid_data_fails_input_validation()` and its provider move from the shared write trait to the create test class. The 31 rows stop at input validation, and `test_input_schema()` of the update ability already checks that its attributes have the same schemas as on create, so the rows no longer run a second time for update.
…ontent write tests.

The permission tests that create, update, and delete repeat move into the write test case behind one input hook, the fields schema and `wp_after_insert_post` tests move into the data write trait, and update tests that check a stored value or a new date fold into one test each. Helpers that only the data write trait uses move into it, and an extra ability registration and post cache flush are removed.
…ontent deletes once, and stop creating the aliased post.

The `@since` tags added to the content test files are removed, while the ones already in trunk stay. The delete matrix runs a refused case once, as a trash, since the permission callback does not read `permanent`, and a new test refuses an author who may edit their published post but not delete it. The test of `where.ids` values with no valid ID goes back to its place in the class and no longer creates a post with the aliased ID, which stayed in the post ID counter after the rollback.
…t class now has twice.

Trunk added the same `current_user_can_for_post_type()` helper to the content class, so the write abilities use that one.

@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.

This implementation is fully functional with extensive testing code coverage. The logic closely mirrors how REST API works, but the public API is highly optimized for how abilities are designed. I would personally spent more time next improving descriptions for abilities and individual fields because it is too tightly coupled withe the post and post type concepts that might be confusing with the content management ideas we are trying to promote to make it more friendly for new users.

All non-blocking findings are being tracked, and we can address them individually. Some of them are existing bugs in how REST API works which these efforts surfaced during verification.

This PR is big, I reviewed manually and with the assistance of coding agents to account for the size of logic and tests necessary.

@jorgefilipecosta

Copy link
Copy Markdown
Member Author

Could we remove the @SInCE tags from the test files?

Done, the test files now rely only on @ticket.

* @param string $status The matrix status of the post.
* @param bool $allowed Whether the update is allowed.
*/
public function test_update_matrix( string $role, string $owner, string $status, bool $allowed ): void {

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.

Automated verification raised this as a possible way to improve the tests further. It does not block the PR, and it could also be a follow-up.

The idea

Split each matrix test, so that one test checks one outcome:

  1. test_update_matrix() here: one test for the posts a role may edit, and one for the posts it may not edit.
  2. test_delete_matrix(): one test each for a permanent delete, a trash, trashing a trashed post, and a refused call.
  3. test_create_by_role_and_status(): one test for the statuses a role may set, and one for the refused ones.

The providers can stay small. data_capability_matrix() already holds the flag, so two providers can filter it:

public static function data_posts_a_role_may_not_write(): array {
	return array_filter(
		self::data_capability_matrix(),
		static function ( array $case ): bool {
			return ! $case[3];
		}
	);
}

Why

  1. Today each test has an if with an early return, so one method checks two to four different results. The handbook asks for tests that are as small and as specific as possible.
  2. The names can then say what is expected, for example test_role_is_refused_for_a_post_it_may_not_edit. The name test_update_matrix does not say it.
  3. A failure points at one behavior right away.

The same idea fits the two special page tests, where the privacy policy page takes a different path than the other two pages.

* @group abilities-api
*/
class Tests_Abilities_API_WpRegisterCoreContentAbility extends WP_UnitTestCase {
class Tests_Abilities_API_WpRegisterCoreContentAbility extends WP_Abilities_Content_UnitTestCase {

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.

Another invitation to improve the tests further, as a follow-up and not for this PR.

This file tests only core/content-query. Next to wpRegisterCoreContentCreateAbility.php and the update and delete files, the name now reads as if it covered all content abilities.

A rename to wpRegisterCoreContentQueryAbility.php, with the class Tests_Abilities_API_WpRegisterCoreContentQueryAbility, would match the new files and wpRegisterCoreSettingsGetAbility.php. Nothing else refers to this file or class name. The same applies to wpRegisterCoreUsersAbility.php, which tests core/users-query.

Doing it separately keeps this diff small. If you agree, it could go to the Follow-ups list.

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

Status: Neeeds review

Development

Successfully merging this pull request may close these issues.

3 participants