API / Minn\Rest / MediaController

MediaController final readonly class

Minn\Rest\MediaController · public/minn/src/Minn/Rest/MediaController.php · 294 lines

wp/v2/media: list, single, upload on both transports (multipart field "file", or a raw body with Content-Disposition), field edits, and force delete with the files.

Used by 1

Constants and properties

privatereadonly Minn\Content\Posts $posts

Promoted from the constructor.

privatereadonly Minn\Media\Writer $library

Promoted from the constructor.

privatereadonly Minn\Rest\MediaObject $object

Promoted from the constructor.

privatereadonly Minn\Rest\Caller $caller

Promoted from the constructor.

privatereadonly Minn\Rest\PostsController $reads

Promoted from the constructor.

Public

new__construct(Minn\Content\Posts $posts, Minn\Media\Writer $library, Minn\Rest\MediaObject $object, Minn\Rest\Caller $caller, Minn\Rest\PostsController $reads)#
methodlist(Minn\Http\Request $request, string $base): Minn\Http\ResponseGET /wp/v2/{base:media} (public)#

The media list: the post lists' own path for attachments (probe rest-media-lists), each item in the media shape.

methodsingle(Minn\Http\Request $request, string $id): Minn\Http\ResponseGET /wp/v2/media/{id:[\d]+} (public; attachment {id} must exist; edit context: cap edit_post on {id})#

One attachment.

methodcreate(Minn\Http\Request $request): Minn\Http\ResponsePOST /wp/v2/media (cap upload_files)#

Uploads a file and creates its attachment.

methodupdate(Minn\Http\Request $request, string $id): Minn\Http\ResponsePOST /wp/v2/media/{id:[\d]+} (cap edit_post on {id}; attachment {id} must exist)PUT /wp/v2/media/{id:[\d]+} (cap edit_post on {id}; attachment {id} must exist)PATCH /wp/v2/media/{id:[\d]+} (cap edit_post on {id}; attachment {id} must exist)#

The editable fields the app uses.

methoddelete(Minn\Http\Request $request, string $id): Minn\Http\ResponseDELETE /wp/v2/media/{id:[\d]+} (cap delete_post on {id}; attachment {id} must exist)#

Attachments go to the trash only when MEDIA_TRASH (or rest_attachment_trashable) says so; force removes the row, its meta, and its files.

Internals 7

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privaterestDate(string $value, string $param): string#
privatestoreWithPlugins(Minn\Media\Upload $upload, Minn\Http\Request $request, Closure $refused): array#

With plugins loaded, the file goes through the runtime's wp_handle_sideload (a raw body) or wp_handle_upload (a form field), as the reference's REST upload sends it: their prefilters, the allowed types (upload_mimes), the content check, the name, the move and wp_handle_upload, where SVG and security plugins work. A refusal is the engine's REST error with the runtime's words.

$refused\Closure(string): RestError
@return array{0: string, 1: string} the path relative to the uploads folder, and the type
privateinsertedWithPlugins(string $relative, string $type, Minn\Media\Upload $upload, Minn\Http\Request $request): Minn\Http\Response#

With plugins loaded, the attachment saved as the reference's controller saves it (probe rest-media-save): the photo's own title and caption (wp_read_image_metadata) unless the request names them, the prepared attachment through rest_pre_insert_attachment, the file's name as the title when none is left, then wp_insert_attachment, the REST actions (the alt text between them), wp_after_insert_post, and the sizes.

privatepreparedAttachment(stdClass $attachment, array $params, Minn\Http\Request $request): array#

rest_block_hooks_post_types, then rest_pre_insert_attachment, over the prepared attachment, then the caption and description the request sends.

$paramsarray<string, mixed>
@return array<string, mixed>
privatefinishedWithPlugins(int $id, array $params, Minn\Http\Request $request, ?Minn\Content\PostRecord $before): void#

After rest_insert_attachment: the alt text, rest_after_insert_attachment and wp_after_insert_post, in the reference's order.

$paramsarray<string, mixed>
privateparams(Minn\Http\Request $request): array#

The fields a media request sends, wherever it sends them: the query, a form, or a JSON body.

@return array<string, mixed>
privateattachment(int $id): Minn\Content\PostRecord#