Skip to content

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

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

jorgefilipecosta wants to merge 24 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" }

Known issue: the write abilities return dates the same way core/content-query does, so a post dated exactly at the Unix epoch (1970-01-01 00:00:00 GMT) comes back with a null date. This is already the case on trunk and is fixed separately in #16219.

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

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

…nd `core/content-delete` abilities.

Add three abilities to the `content` category, for the post types registered with `show_in_abilities`:

- `core/content-create` creates a post from `data` and returns it.
- `core/content-update` updates the given attributes of the post matched by `where` and returns it.
- `core/content-delete` moves the post matched by `where` to the Trash, or deletes it permanently when `permanent` is true.

The attributes that can be written are the title, content, excerpt, status, slug, date, author and parent. Each call checks the capabilities of the current user for the post and for the values it sets.
The execute callbacks always receive input that `WP_Ability::execute()` validated and converted to the schema types, so the type checks they repeated are removed. The permission callbacks keep reading the input defensively, since `WP_Ability::check_permissions()` does not prepare it.

Other changes:

- A `slug` sent for a pending post by a user who cannot publish it is now an error instead of being dropped by the save.
- The invalid attribute error uses the reason as its message.
- A delete that did not happen returns `content_delete_failed`, so `content_cannot_delete` is only the permission error.
- A date at the Unix epoch is returned instead of null.

In the tests, the helpers and data providers shared by the three write abilities move to a base test case, and the tests for input that validation already rejects are removed. New tests cover a post type without Trash support, the slug of a pending post, a date at the Unix epoch, and trashing and permanently deleting the special pages separately.
The floating draft tests used a date in June 2099 in a time zone with daylight saving time. PHP versions before 8.1 apply no daylight saving time after 2037, so the expected GMT date was one hour off on PHP 7.4 and 8.0.
…lete permission callbacks.

The permission callbacks of `core/content-update` and `core/content-delete` now require `edit_post` or `delete_post` on the post that `where` names, the way the `core/content-create` callback already requires `create_posts`. Before, they only required a logged-in user and an exposed post type. A post that does not exist is let through, so that the call still ends as not found.

The execute callbacks keep their own capability check. It applies when the `wp_ability_permission_result` filter grants the permission and when an execute callback is called directly.

In the tests, a post the user cannot edit or delete is now refused by the permission callback. New tests cover the callback for input that was not prepared, for a post the user can edit or delete and for a missing post, and the capability check that remains after a filter grants the permission.
- Add `enum` to the `outcome` of the `core/content-delete` output schema.
- Note in the changelog that the `where.status` filter of `core/content-query` accepts `trash`.
- Say to set `permanent` to true in the message for posts that cannot be trashed.
- Pass the full input path to `invalid_attribute_error()`, as `invalid_filter_error()` takes it.
- Rename `save_post()` to `persist_post()`, so it does not read as the `save_post` hook.
- Read the statuses that can be set directly, instead of removing `trash` from the queryable ones.
- Remove the unused input defaults from the write ability callbacks.
- Align comments and docblocks.

See #66706.
@jorgefilipecosta
jorgefilipecosta force-pushed the add/content-write-abilities branch from 6dbc2bc to c2efdbd Compare November 24, 2026 15:59
…te tests.

Removes an unreachable post type check, a redundant guard and cast, and the separate list of write attributes and queryable statuses, and explains why the write abilities share one class with the query ability.

Moves the fixtures, tests and providers shared by the create, update and delete tests to the base test case, turns duplicate tests into data provider rows, and fixes misleading test names, docblocks and assertions.

See #66706.
…der.

`Tests_Admin_wpPostsListTable::test_search_hierarchical_pages_first_page` expects the child pages of the first top-level page. The child pages of different parents share a title, and the test query orders by `menu_order` and title only, so which of them comes first is up to the database.

On MariaDB and MySQL 5.7 that order depends on the page IDs, which in turn depend on how many posts the earlier tests created. With the tests added on this branch, the test fails on those databases.

The test query now also orders by ID.

See #66706.
Documents that a status sent for a post in the Trash moves it out of the Trash through a regular save, as an update through the REST API posts endpoint does, and corrects which requests the update and delete permission callback lets through.

See #66706.
`test_execute_callback_rejects_ids_with_no_valid_ids` creates a post with the ID 4194304, and every post created after it gets a higher ID. With the content write tests running after it, the pages of `Tests_Admin_wpPostsListTable::test_search_hierarchical_pages_first_page` got IDs for which MariaDB and MySQL 5.7 return pages with the same title in another order, and that test failed.

The test is now the last one of its class, so fewer posts are created after it. The posts list table test is back to its trunk version.

See #66706.
Registers the create, update and delete abilities in one loop that adds the shared category, public flag and `readonly` annotation, passes `where` to the post lookup, leaves the default `draft` status of a create to the save, and returns no status from the status preparation when the sent status is unchanged.

See #66706.
Moves the tests that the update and delete abilities share, such as the `where` lookup, the permission callback and the role matrix, to a base test case for the abilities that write an existing post, and the registration test to the base of the write abilities. Merges tests that differed only in their input into data providers, removes tests that other tests already cover, and checks the error data in a few more tests.

See #66706.
@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

Registers each write ability with an explicit category and meta, uses the
prepared input directly for the author, parent and date, and checks for
data to write with empty(). Links the known Trash issue to its ticket.

Merges duplicated tests into data providers, removes tests that could not
fail, and moves the members used only by the update and delete tests to the
existing post test case.

See #66706, #66809.
The content query returns null for a date at the Unix epoch on trunk too,
so the fix and its test move to a separate change.
Replaces the `MISSING_ID` constant with `REST_TESTS_IMPOSSIBLY_HIGH_NUMBER`
and dates the future fixture posts with `FUTURE_GMT`.

See #66706.
When both `site` and `gmt` dates are sent to the content write abilities, they now have to name the same time, instead of the site date winning. Also removes guards for an empty `data` and an unusable date that input validation already covers, and resets the author visibility memo where the post is saved.

Tests: check the exact invalid attribute messages, share the tests that core/content-create and core/content-update have in common through a trait, and fold or remove duplicated tests and providers.

See #66706.
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.

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