Repository navigation
Abilities API: Add the core/media-query, core/media-upload, core/media-update and core/media-delete abilities - #16138
jorgefilipecosta wants to merge 7 commits into
Conversation
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. |
|
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:
|
…/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.
916a153 to
953ff6a
Compare
`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.
|
Known bug: for a PDF, the |
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>
euvoor
left a comment
There was a problem hiding this comment.
@jorgefilipecosta I tried the query-only split Grzegorz suggested on 8ac3b4e. With upload, update, delete and the download helper removed, all 99 media-query tests passed on single-site and multisite.
I'd keep the attachment show_in_abilities flag and the matching exclusion in core/content-query together in the read PR. One easy piece to miss is that format_media() calls the title, caption and description renderers by name. The query needs all three.
I also tried listing one post's three JPEGs, two per page, with a fourth JPEG left unattached. With distinct upload dates, both the ability and /wp/v2/media returned the same IDs on both pages and a total of three. When all three dates matched, the ability used descending ID to break the tie. REST returned the same images in a different order in my test. I'd keep the ability's tie-breaker for predictable pagination.
Shape from WordPress/abilities#22. Same approach as #16028.
Trac ticket: https://core.trac.wordpress.org/ticket/66719
This PR adds the
core/media-query,core/media-upload,core/media-updateandcore/media-deleteabilities to thecontentcategory, for attachments:core/media-querytakes{ where, fields, pagination }and returns{ items, pagination }with the totals.ids,slugs,status,media_type,mime_type,postandauthorfilter the attachments.core/media-uploadtakes{ data, fields }and returns the new attachment.data.fileis required:{ url }for an image to download, or{ base64, filename }for a file of any type the user may upload.core/media-updatetakes{ where: { id }, data, fields }, changes only the attributes indataand returns the attachment.postattaches it to a post, andpost: 0detaches it.core/media-deletetakes{ where: { id }, permanent }and returns{ outcome }. Withpermanent: truethe attachment and its files are deleted. Without it the attachment goes to the trash, and on a site that does not setMEDIA_TRASHthe answer is amedia_trash_not_supportederror.dataacceptstitle,caption,description,alt_text,slug,date,authorandpost.Outside the new
WP_Abilities_Mediaclass:wp_register_core_abilities()loads the class and registers the abilities.Uploading an image from a URL:
{ "id": 8, "status": "inherit", "date": { "site": "2026-11-20T18:01:55+00:00", "gmt": "2026-11-20T18:01:55+00:00" }, "slug": "wordpress-logotype", "title": { "rendered": "WordPress logotype" }, "media_type": "image", "mime_type": "image/png", "source_url": "http://localhost:8951/wp-content/uploads/2026/11/WordPress-logotype-standard.png" }Changing its slug and caption, and asking for some fields:
{ "id": 8, "slug": "logotype", "title": { "raw": "WordPress logotype", "rendered": "WordPress logotype" }, "caption": { "raw": "The logotype on a light background.", "rendered": "<p>The logotype on a light background.</p>\n" }, "alt_text": "The WordPress logotype." }Querying the images, two per page:
{ "items": [ { "id": 10, "title": { "rendered": "WordPress simplified logotype" }, "mime_type": "image/png", "media_details": { "width": 500, "height": 500 } }, { "id": 9, "title": { "rendered": "WordPress logotype with the mark" }, "mime_type": "image/png", "media_details": { "width": 1000, "height": 1000 } } ], "pagination": { "total": 3, "total_pages": 2 } }Deleting it permanently. Without
permanent: truethe answer on this site is amedia_trash_not_supportederror:{ "outcome": "deleted" }Trac ticket: https://core.trac.wordpress.org/ticket/66719
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).