Skip to content

Port the media write abilities (wp/media-upload, wp/media-update, wp/media-delete) from WordPress/ai PR #1104 - #7

Merged
nikmclaughlin merged 1 commit into
mainfrom
add/wp-media-writes
Oct 30, 2026
Merged

nikmclaughlin merged 1 commit into
mainfrom
add/wp-media-writes

Conversation

@nikmclaughlin

Copy link
Copy Markdown
Contributor

What?

Adds the three media write abilities to the package: wp/media-upload, wp/media-update, and wp/media-delete — a faithful port of WordPress/ai PR #1104 ("Add core/media-upload, core/media-update, and core/media-delete abilities", author @jorgefilipecosta), incubating under the experimental tier's wp/ prefix (D5). The PR's core/media-* names, gating, and unregister-core-copy wiring are not copied; no aliases exist (D7).

The writes build on the merged wp/media-query field projection (PR #5): written attachments are answered through the same contract, in the edit context, and register under the package-authored media category (core trunk has no media category to copy).

Why?

Agents can read the media library through abilities (wp/media-query) but not write it. This package ports the three writes so consumers get one canonical copy while the upstream proposal settles (plan item B2, decisions D2/D5).

How?

  • The abilities: src/Media/Abstract_Media_Write.php (shared base) + Media_Upload.php / Media_Update.php / Media_Delete.php mirror the PR's logic — labels, descriptions, schemas (upload input is a oneOf of a base64-data branch and a URL branch; delete output is a oneOf of the trashed item and the deleted/previous pair), annotations (open_world: true for upload's URL sideload), error codes, the URL-sideload SSRF guard and download-size cap, the status/author/parent checks that name the refused field in the execute callback (because the Abilities API replaces permission-callback errors with a generic one), and the global-post restoration the upstream PR refactored into a helper — on the package's Abstract_Core_Ability authoring surface, with the default text domain and closures for the registered callbacks. Helpers shared with the package's Media_Query port are extracted verbatim with private relaxed to protected (the content-trio pattern).
  • Naming and tier: manifest entries wp/media-*, tier experimental, core_since: null (flips when core lands them), proposal pointing at the WordPress/ai PR.
  • No polyfill needed: every core primitive the writes rely on (wp_handle_sideload, wp_http_validate_url, MEDIA_TRASH, the media capability mapping) exists on the supported floor (WordPress 6.9+).
  • Tests: MediaUploadTest / MediaUpdateTest / MediaDeleteTest (tests/Abilities/), ported from the PR's integration tests onto the Registrar-based conventions: negative and positive checks on every permission path (upload_files, edit_post, publish floors), base64-data and mocked URL transports, trash/force semantics with the MEDIA_TRASH guard, deleted-item field parity against the live media REST endpoint responses, author reassignment, and hook/globals hygiene.

Use of AI tools

Tool(s): goose (agent)
Model(s): provided via the harness
Used for: Implementation, tests, and docs. Reviewed and edited by the committer.

Testing Instructions

  1. Load the package in a wp_abilities_api_init callback: ( new WordPress\Abilities\Registrar )->register_all();
  2. wp_get_ability( 'wp/media-upload' )->execute( array( 'data' => base64_encode( $bytes ), 'filename' => 'p.jpg' ) ) uploads the file and returns the item in the edit context (raw fields included when requested — the caller already had edit access to upload). A URL payload (array( 'url' => … )) sideloads instead, capped at wp_max_upload_size().
  3. wp_get_ability( 'wp/media-update' )->execute( array( 'id' => $id, 'title' => 'New title' ) ) updates fields (omitted fields keep their values); a status other than the attachment's current one is validated, so non-attachment statuses are rejected while inherit can be kept.
  4. wp_get_ability( 'wp/media-delete' )->execute( array( 'id' => $id ) ) trashes (requires MEDIA_TRASH, else media_trash_not_supported); array( 'id' => $id, 'force' => true ) deletes the attachment and its files permanently and returns deleted/previous.
  5. Permission floors: without upload_files the upload is denied; update/delete require edit_post/delete_post for attachments (a missing item is denied like an unreadable one); private/publish statuses require the publish capability; deleting another author's attachment requires delete_others_posts.
  6. Gates: composer run lint:phpcs && composer run lint:phpstan && composer run verify:manifest && composer run verify:coreify, and the PHPUnit suite on both wp-env environments (trunk + stable, one at a time) pass.

Changelog entries

Added (CHANGELOG.md): wp/media-upload, wp/media-update, wp/media-delete — tier experimental, faithful ports of the media write abilities from WordPress/ai PR #1104 by @jorgefilipecosta, building on the wp/media-query field projection.

Co-authored-by: jorgefilipecosta jorgefilipecosta@users.noreply.github.com

…Abilities from WordPress/ai PR #1104

## What?

Adds the three media write abilities to the package: a faithful port of
WordPress/ai PR #1104 ("Add core/media-upload, core/media-update, and
core/media-delete abilities", author @jorgefilipecosta), incubating under the
experimental tier's `wp/` prefix on the shared `media` category and field
projection of `wp/media-query` (PR #5). The PR's `core/media-*` registration
names, gating, and unregister-core-copy wiring are not copied; no aliases exist.

## Why?

Agents can read the media library through abilities (`wp/media-query`) but not
write it. This package ports the three writes so consumers get one canonical
copy while the upstream proposal settles.

## How?

- **The abilities:** `src/Media/Abstract_Media_Write.php` (shared base) +
  `Media_Upload.php` / `Media_Update.php` / `Media_Delete.php` mirror the PR's
  logic — labels, descriptions, schemas (upload is a `oneOf` of base64-data and
  URL-branch; delete output is a `oneOf` of the trashed item and the
  `deleted`/`previous` pair), annotations (`open_world: true` on upload),
  error codes, the URL-sideload SSRF guard and download-size cap, the status /
  author / parent checks that name the refused field in the execute callback,
  and the global-post restoration — on the package's `Abstract_Core_Ability`
  authoring surface, with the `default` text domain and closures for the
  registered callbacks. Shared helpers are extracted verbatim from the
  package's `Media_Query` port with `private` relaxed to `protected`.
- **Naming and tier:** manifest entries `wp/media-*`, tier `experimental`,
  `core_since: null` (flips when core lands them), proposal pointing at the
  WordPress/ai PR.
- **No polyfill needed:** every core primitive the writes rely on
  (`wp_handle_sideload`, `wp_http_validate_url`, `MEDIA_TRASH`, the media-cap
  mapping) exists on the supported floor (WordPress 6.9+).
- **Tests:** `MediaUploadTest` / `MediaUpdateTest` / `MediaDeleteTest` —
  Registrar-based, ported from the PR's integration tests: negative and
  positive checks on every permission path (upload_files, edit_post, publish
  floors), wrapped and URL transports, trash/force semantics with the media
  trash guard, gone-read field parity against the live media REST endpoint
  responses, and hook/globals hygiene.

## Use of AI tools

Tool(s): goose (agent)
Model(s): provided via the harness
Used for: Implementation, tests, and docs. Reviewed and edited by the
committer.

## Testing Instructions

1. Load the package in a `wp_abilities_api_init` callback:
   `( new WordPress\Abilities\Registrar )->register_all();`
2. `wp_get_ability( 'wp/media-upload' )->execute( array( 'data' => base64_encode( $bytes ), 'filename' => 'p.jpg' ) )`
   uploads the file and returns the item in the edit context (raw fields
   included when requested — the caller already had edit access to upload).
3. `wp_get_ability( 'wp/media-update' )->execute( array( 'id' => $id, 'title' => 'New title' ) )`
   updates fields (omitted fields keep their values); a status other than the
   attachment's current one is validated, so non-attachment statuses are
   rejected while `inherit` can be kept.
4. `wp_get_ability( 'wp/media-delete' )->execute( array( 'id' => $id ) )`
   trashes (requires `MEDIA_TRASH`, else `media_trash_not_supported`);
   `array( 'id' => $id, 'force' => true )` deletes the attachment and its files
   permanently and returns `deleted`/`previous`.
5. `composer run lint:phpcs && composer run lint:phpstan && composer run verify:manifest`
   and the PHPUnit suite on both wp-env environments (one at a time) pass.

Co-authored-by: jorgefilipecosta <jorgefilipecosta@users.noreply.github.com>
@nikmclaughlin nikmclaughlin self-assigned this Oct 30, 2026
@nikmclaughlin
nikmclaughlin merged commit 8d10aaa into main Oct 30, 2026
12 checks passed
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.

1 participant