Repository navigation
Abilities API: Add the core/content-create, core/content-update and core/content-delete abilities - #16028
Abilities API: Add the core/content-create, core/content-update and core/content-delete abilities#16028jorgefilipecosta wants to merge 37 commits into
core/content-create, core/content-update and core/content-delete abilities#16028Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Unlinked AccountsThe 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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
6dbc2bc to
c2efdbd
Compare
|
What is wrong please help |
|
Why not help until done error |
|
Ok what is my alternative stop to using online tell |
| /* | ||
| * 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. | ||
| */ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| $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 ]; | ||
| } | ||
| } |
There was a problem hiding this comment.
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?
| // 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.' ) ); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I opened https://core.trac.wordpress.org/ticket/66844 to track it, with a first version in #16288 that the write abilities can use.
| private function get_content_write_properties( array $statuses ): array { | ||
| return array( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed, we will add terms and featured_media as follow-ups on this ticket once the terms and media abilities land.
There was a problem hiding this comment.
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!
| '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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Update now accepts the stored slug unchanged, also the empty slug of a draft, and the minLength was removed 👍
| /* | ||
| * 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. | ||
| */ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thank you, the comment now mentions trashed posts too.
| /* | ||
| * 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 ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| // 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 ) | ||
| ); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
…t author check comment.
… 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.
7523c27 to
c3fe995
Compare
gziolo
left a comment
There was a problem hiding this comment.
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.
| * @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 { |
There was a problem hiding this comment.
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:
test_update_matrix()here: one test for the posts a role may edit, and one for the posts it may not edit.test_delete_matrix(): one test each for a permanent delete, a trash, trashing a trashed post, and a refused call.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
- Today each test has an
ifwith an earlyreturn, so one method checks two to four different results. The handbook asks for tests that are as small and as specific as possible. - The names can then say what is expected, for example
test_role_is_refused_for_a_post_it_may_not_edit. The nametest_update_matrixdoes not say it. - 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 { |
There was a problem hiding this comment.
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.
Follow-up to #12195, with the shape from WordPress/abilities#22.
This PR adds the
core/content-create,core/content-updateandcore/content-deleteabilities to thecontentcategory, for the post types that setshow_in_abilities:core/content-createtakes{ data, fields }and returns the new post.data.typeis required and the status defaults todraft.core/content-updatetakes{ where: { type, id }, data, fields }, changes only the attributes indataand returns the post.core/content-deletetakes{ where: { type, id }, permanent }and returns{ outcome }:trashed, ordeletedwhenpermanentistrue.dataacceptstitle,content,excerpt,status,slug,date,authorandparent.core/content-querynow acceptstrashinwhere.status, so a trashed post can be found to restore it with an update or to delete it permanently. Trashed posts are only returned whentrashis requested by name.Creating a draft:
{ "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:
{ "id": 4, "status": "publish", "slug": "launch-notes", "title": { "raw": "Launch notes", "rendered": "Launch notes" } }Moving it to the Trash. With
permanent: truein the input the outcome isdeleted:{ "outcome": "trashed" }Follow-ups
core/content-updateand add a test for trash, restore through an update, and trash again, also checking that the comments come back. Discussion: Abilities API: Add thecore/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)core/content-query. #16287 lands, socore/content-queryreturns hooked blocks incontent.raw, callupdate_ignored_hooked_blocks_postmeta()when create and update write content (after the insert on create). Trac ticket: https://core.trac.wordpress.org/ticket/66847. Discussion: Abilities API: Add thecore/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)readonlymarker for all write abilities, e.g.show_in_abilities => array( 'readonly' => true )for post types, in Abilities API: Allow marking ashow_in_abilitiesobject as read only #16288 (https://core.trac.wordpress.org/ticket/66844). Discussion: Abilities API: Add thecore/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)terms: https://core.trac.wordpress.org/ticket/66904, PR Abilities API: Add terms to the content create and update abilities #16388 (on top of Abilities API: Add thecore/terms-queryability #16102). Discussion: Abilities API: Add thecore/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)featured_media: https://core.trac.wordpress.org/ticket/66905, PR Abilities API: Add the featured image to the content create and update abilities #16389 (on top of Abilities API: Add thecore/media-queryability #16138). Discussion: Abilities API: Add thecore/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)core/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)core/content-create,core/content-updateandcore/content-delete, and maybecore/content-query: what each call can do, which permissions it needs, what happens to attributes an update leaves out, and that delete moves to the Trash by default with permanent deletion as an option. Discussion: Abilities API: Add thecore/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)destructiveon update: once https://core.trac.wordpress.org/ticket/66801 is addressed, setdestructivetotrueforcore/content-update, since agents treat any change that is not additive as destructive. Discussion: Abilities API: Add thecore/content-create,core/content-updateandcore/content-deleteabilities #16028 (comment)Trac ticket: https://core.trac.wordpress.org/ticket/66706
Testing
Verify unit tests are passing:
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.