Repository navigation
Abilities API: Add the featured image to the content create and update abilities - #16389
jorgefilipecosta wants to merge 48 commits into
Conversation
…/media-update` and `core/media-delete` abilities. Adds four abilities for attachments. `core/media-query` returns a page of attachments with the totals, filtered by IDs, slugs, status, media type, MIME type, parent post or author. `core/media-upload` stores a file, sent as base64 data or as the URL of an image to download, and creates its attachment. `core/media-update` changes the attributes of an attachment, and `core/media-delete` trashes it or deletes it permanently together with its files. The upload and update abilities return the attachment in the same form as the query. The abilities require a logged-in user, and each call checks the capabilities of the user for the attachment and for the values it sets. See #66719.
`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 media ability 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 on multisite. The test is now the last one of its class, so fewer posts are created after it. See #66719.
…mplify their tests. The media abilities now check `show_in_abilities` on the attachment post type and on the parent post type, and the attachment post type opts in. `core/content-query` no longer exposes attachments, which are covered by the media abilities. The download of an image from a URL moves to the private `_wp_download_image_from_url()` function, so that the attachments REST controller can use it later. Checks that repeated the input schema are removed, the schemas are built once, and the item schema lists the values of `media_type` and of the deletion `outcome`. The tests share their providers and the gate tests of the update and delete abilities, keep the upload-only helpers in the upload test class, and no longer depend on the month or on files left in the temporary folder. See #66719.
The mime_type filter now accepts any full MIME type and matches it exactly, and media_type matches every type of its group, so neither depends on the types the current user may upload. A failed upload no longer removes the stored file. See #66719.
The `mime_type` filter checks its values with a schema pattern, and a refused file type is found before the upload handling runs instead of by its error message. The author visibility check leaves attachments out, as their status never counts as published. The attribute tests shared by upload and update move into a trait, and the tests that restated the whole schemas are removed. See #66719. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The file size check takes the size as an integer and inlines the network space check, the output schema of the media abilities is built in one method, and the content abilities read their exposed post types from a single helper that leaves out attachments. See #66719. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `media_type` output is `image` or `file`, while the `media_type` filter takes the part of the MIME type before the slash. Both schema descriptions now say so and point to `mime_type` for the exact type. See #66719. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…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.
…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.
…table order." This reverts commit 04bd5c4.
`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.
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.
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.
… 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.
The content abilities no longer expose attachments, so the delete test that exposed them for a post without Trash support is removed.
…tent abilities. Adds `featured_media` to the content create and update input and to the content output, for post types that support thumbnails. It takes the ID of an image the current user can read, and 0 removes the featured image.
- Return `source_url` as null when WordPress has no URL for the file. - Return dates at the Unix epoch instead of null. - Give the full size of a PDF the MIME type of its preview image. - Report the size of the original file for a scaled image, the file `filename` names. - Cover private attachments the current user can read when `ids` or `slugs` is given without a status. - Explain in the schema that the totals can count attachments the current user cannot read, and that `where.post` matches the stored parent ID. - Test that the parent's `show_in_abilities` only matters when the current user cannot read the attachment.
The output `media_type` is now the part of the stored MIME type before the slash, in lowercase, the value the `media_type` filter matches, instead of `image` or `file`. A returned value can be passed back to the filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…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.
…a-query`. With both `media_type` and `mime_type`, an attachment now has to match both, as with every other filter, instead of either. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…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.
- Walk the parent chain of an attachment only through posts that inherit their status, the only posts read through their parent. - Use the image title, caption and alt text from the image metadata only when they are strings. - Leave the status, author and parent of an uploaded attachment to `wp_insert_post()`. - Query attachments with `perm` set to `readable` for every caller. - Reuse the existing message for a URL that does not point to an image, and say how to delete an attachment that cannot be trashed. - Simplify the exposed post type lookup of the content abilities and the file details of the media output. - Remove the trash tests that never ran, and assert the default `MEDIA_TRASH` value in the tests that rely on it. - Make the author, date, slug, status and parent tests able to fail, and tidy up test names, providers and docblocks.
…t author check comment.
…author check comment.
…the post type tests.
|
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: 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. |
Part of WordPress/abilities#17.
Depends on #16028 and #16138, which are merged in. The new code is the commit "Abilities API: Read and write the featured image of a post in the content abilities." and the conflict fixes in the merges after it.
This PR adds
featured_mediatocore/content-create,core/content-updateand the content output, as described in the shape at WordPress/abilities#22:data.featured_mediatakes the ID of an image the user can read, and0removes the featured image.thumbnail, also when the theme does not support post thumbnails.wp_after_insert_postfires, so the callbacks of that action see it.Trac ticket: https://core.trac.wordpress.org/ticket/66905
Testing Instructions
Verify the unit tests pass:
As an administrator, I created a post with a featured image and then removed it (use an image ID from your site):
AI disclosure: the change and tests were drafted with AI assistance (Claude Code).