Skip to content

Abilities API: Add core media abilities (query, upload, update, delete). - #16079

Closed
robelsust wants to merge 2 commits into
WordPress:trunkfrom
robelsust:feat/66719-abilities-media
Closed

robelsust wants to merge 2 commits into
WordPress:trunkfrom
robelsust:feat/66719-abilities-media

Conversation

@robelsust

Copy link
Copy Markdown

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

What?

Adds the four core media abilities (core/media-query, core/media-upload, core/media-update, and core/media-delete) in a new WP_Abilities_Media class registered via wp_register_core_abilities().

Why?

Exposes standard media library read, upload, update, and delete operations through the Abilities API in the content category, matching the input/output schemas and error semantics defined in CORE-SHAPE.md (WordPress/abilities#22).

How?

  • Added src/wp-includes/abilities/class-wp-abilities-media.php:
    • core/media-query: Read-only paginated attachment query supporting where (ids, slugs, status, media_type, mime_type, post, author), fields projection, pagination, raw field gating (edit_post), author visibility checks, and media_cannot_filter_by_status (403).
    • core/media-upload: Requires upload_files; supports image URL sideloading (rejecting non-image URLs with media_invalid_file 400) and base64 + filename uploads; validates parent edit_post (media_cannot_attach 403) and author assignment (edit_others_posts).
    • core/media-update: Requires edit_post (media_not_found 404 / media_cannot_edit 403); updates title, caption, description, alt_text, slug, date, author, and post (0 to detach).
    • core/media-delete: Requires delete_post (media_not_found 404 / media_cannot_delete 403); returns media_trash_not_supported (501) when MEDIA_TRASH is off and permanent is not set; trashes or permanently deletes attachments.
  • Updated src/wp-includes/abilities.php to require class-wp-abilities-media.php and call ( new WP_Abilities_Media() )->register().
  • Added unit tests in tests/phpunit/tests/abilities-api/wpRegisterCoreMediaAbilities.php.

Testing Instructions

  1. Run ./vendor/bin/phpunit --group abilities-api (928 tests, 3221 assertions — passed).
  2. Run ./vendor/bin/phpcs src/wp-includes/abilities.php src/wp-includes/abilities/class-wp-abilities-media.php tests/phpunit/tests/abilities-api/wpRegisterCoreMediaAbilities.php (passed).

AI Usage Details

  • Used an AI coding assistant (Antigravity) to implement WP_Abilities_Media against CORE-SHAPE.md, wire it into wp_register_core_abilities(), write the PHPUnit test suite in wpRegisterCoreMediaAbilities.php, and run PHPUnit and PHPCS. All code and test results were reviewed before committing.

Introduce `WP_Abilities_Media` in `wp-includes/abilities/class-wp-abilities-media.php` and register it from `wp_register_core_abilities()` to expose four abilities in the `content` category:
- `core/media-query`: Read-only paginated attachment query with `ids`, `slugs`, `status`, `media_type`, `mime_type`, `post`, and `author` filters, field projection, and row-level permission/visibility checks.
- `core/media-upload`: Creates an attachment from an image URL or base64 payload with filename and optional metadata, enforcing `upload_files` and parent `edit_post` permissions.
- `core/media-update`: Updates attachment metadata, date, author, or parent post (`0` to detach), enforcing `edit_post` and `media_cannot_attach` checks.
- `core/media-delete`: Trashes or permanently deletes an attachment, returning `media_trash_not_supported` (501) when `MEDIA_TRASH` is disabled and `permanent` is not set.

Adds unit tests in `tests/phpunit/tests/abilities-api/wpRegisterCoreMediaAbilities.php`.

Props therssoftware.
Fixes #66719.
@github-actions

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props therssoftware.

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

This comment has been minimized.

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

…n `WP_Abilities_Media`.

Resolves PHPStan `wordpress.hookDocMissing` notice on PR WordPress#16079.

Props therssoftware.
See #66719.
@jorgefilipecosta

Copy link
Copy Markdown
Member

Hi @robelsust, I had a PR in progress on AI plugin for this abilities WordPress/ai#1104, which I'm porting at #16138. #16138 covers more permission cases, and has a wider test coverage I guess we can keep iteration on it. Would appreciate any feedback you may have there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants