Repository navigation
Add core/media-upload, core/media-update, and core/media-delete abilities - #1104
jorgefilipecosta wants to merge 33 commits into
Conversation
✅ WordPress Plugin Check Report
📊 ReportAll checks passed! No errors or warnings found. 🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## add/core-media-query #1104 +/- ##
==========================================================
+ Coverage 82.27% 82.60% +0.32%
- Complexity 3231 3400 +169
==========================================================
Files 131 131
Lines 12875 13515 +640
==========================================================
+ Hits 10593 11164 +571
- Misses 2282 2351 +69
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Tested in the browser console of the post editor (WP 7.1.2, Excerpt Generation and Custom Abilities enabled). Every output below matched the comments. await ( await import( '@wordpress/core-abilities' ) ).ready;
const { executeAbility } = await import( '@wordpress/abilities' );
const postId = wp.data.select( 'core/editor' ).getCurrentPostId();
// A 1x1 PNG, base64 encoded.
const data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQzwAE9QEsxQH1ggAAAABJRU5ErkJggg==';
const uploaded = await executeAbility( 'core/media-upload', {
data,
filename: 'console-demo.png',
title: 'Console demo',
alt_text: 'A one-pixel image',
post: postId,
fields: [ 'id', 'status', 'title_raw', 'alt_text', 'post', 'mime_type' ],
} );
console.log( 'upload', uploaded );
// { id: <new ID>, status: 'inherit', title_raw: 'Console demo', alt_text: 'A one-pixel image', mime_type: 'image/png', post: <this post's ID> }
const updated = await executeAbility( 'core/media-update', {
id: uploaded.id,
caption: { raw: 'Captioned from the console' },
post: 0,
fields: [ 'id', 'title_raw', 'caption_raw', 'post' ],
} );
console.log( 'update', updated );
// { id: <same ID>, title_raw: 'Console demo', caption_raw: 'Captioned from the console', post: null }
const read = await executeAbility( 'core/media-query', {
id: uploaded.id,
fields: [ 'title_raw', 'caption_raw', 'alt_text' ],
} );
console.log( 'read back', read );
// { id: <same ID>, title_raw: 'Console demo', caption_raw: 'Captioned from the console', alt_text: 'A one-pixel image' }
const notTrashed = await executeAbility( 'core/media-delete', { id: uploaded.id } ).catch( ( error ) => error );
console.log( 'delete without force', notTrashed.code );
// 'media_trash_not_supported' (unless the site enables the media trash)
const deleted = await executeAbility( 'core/media-delete', {
id: uploaded.id,
force: true,
fields: [ 'id', 'title_raw' ],
} );
console.log( 'delete with force', deleted );
// { deleted: true, previous: { id: <same ID>, title_raw: 'Console demo' } }
const mixed = await executeAbility( 'core/media-upload', {
data,
filename: 'mixed.png',
url: 'https://example.com/photo.png',
} ).catch( ( error ) => error );
console.log( 'data and url together', mixed.code );
// 'ability_invalid_input': an upload takes either data and a file name, or a URL |
|
@jorgefilipecosta note that I'm holding off on requesting reviews until this comes out of draft status |
|
Closing in favor of WordPress/abilities#7. |
What?
Adds
core/media-upload,core/media-update, andcore/media-delete, abilities that write media library items. Based on #1098.How?
includes/Abilities/Media/Media.phpnext tocore/media-query, behind the Custom Abilities experiment.core/media-uploadtakes a file as base64datawith afilename, plus a title, caption, description, alt text, parentpost, author, status, slug, and date. It can instead take theurlof an image to download, with only a parentpost.core/media-updatetakes anidand the same fields.core/media-deletetakes anidandforce. Withoutforceit moves the item to the trash, which only works when the site enables the media trash; withforceit deletes the item and its files.core/media-query's fields, raw fields included;fieldspicks them. A forced delete returns{ deleted, previous }.Testing Instructions
core/media-uploadwith{ "data": "<base64 PNG>", "filename": "test.png", "title": "Test" }, thencore/media-updatewith the returnedidand a newtitle, thencore/media-deletewith theidand"force": true.npm run test:php -- --filter 'Media|Gated_Abilities'npm run test:e2e -- tests/e2e/specs/abilities/core-media-write.spec.jsChangelog Entry