Repository navigation
Media: Attach unattached media to the current post on save/publish #81977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c3fc8ef
b88773e
d543210
bc0f9e4
070d495
3221e58
69b5305
6151ca8
22f014d
5c31ef1
c975fbb
0296c7b
ef6ad2b
e9bc282
279044e
e9d913f
c8a35f5
18ac648
07fb062
19c89ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import { | |
| getNotificationArgumentsForSaveFail, | ||
| getNotificationArgumentsForTrashFail, | ||
| } from './utils/notice-builder'; | ||
| import attachMediaInPost from './utils/attach-media-in-post'; | ||
| import { unlock } from '../lock-unlock'; | ||
| import { setCanvasWidth } from './private-actions'; | ||
| import { getCanvasWidthByDeviceType } from '../utils/device-type'; | ||
|
|
@@ -199,6 +200,10 @@ export const savePost = | |
| dispatch.editPost( { content }, { undoIgnore: true } ); | ||
|
|
||
| const previousRecord = select.getCurrentPost(); | ||
| // Snapshotted alongside the content above, so that attaching media once | ||
| // the save finishes works from what was saved for the post itself. | ||
| const savedBlocks = select.getEditorBlocks(); | ||
|
|
||
| let edits = { | ||
| id: previousRecord.id, | ||
| ...registry | ||
|
|
@@ -277,6 +282,28 @@ export const savePost = | |
| } | ||
| dispatch( { type: 'REQUEST_POST_UPDATE_FINISH', options } ); | ||
|
|
||
| // Attach any images in the post that aren't attached to a post yet. Not | ||
| // awaited: it shouldn't hold up the editor showing "Saved", and it | ||
| // handles its own errors. | ||
| // | ||
| // `isDeletingPost` is what catches trashing. `trashPost` deletes the post | ||
| // and then calls this, and the record still says what it did before the | ||
| // delete, so checking the status alone would miss it. | ||
| if ( | ||
| ! error && | ||
| ! options.isAutosave && | ||
| ! options.isPreview && | ||
| ! select.isDeletingPost() && | ||
| previousRecord.status !== 'trash' && | ||
| select.getEditorSettings().autoAttachMediaEnabled | ||
| ) { | ||
| attachMediaInPost( registry, { | ||
| id: previousRecord.id, | ||
| type: previousRecord.type, | ||
| blocks: savedBlocks, | ||
| } ); | ||
| } | ||
|
Comment on lines
+293
to
+305
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is all in Kapture.2026-08-28.at.17.28.40.mp4Not sure if this is a big deal TBH Is there a way to run
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, great catch! I'll dig in further next week. Cheers!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks again for flagging. I looked into this a little further, but I think getting this running in the other multi-entity panels winds up adding a bit more complexity than it's worth. IMO I think this behaviour is roughly comparable to anything else that will get hooked on I think that's probably good scope for now, and we can revisit / look at expanding it further if we get feedback or requests for it. My hunch is that we likely won't as this is largely a feature to cover a parity gap between the classic and block editors, and we're kind of doing it silently, or as silently as we can. Let me know if you think it winds up being important!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sense, thanks for diving in. Scoping it to savePost makes sense the way you explain it. For me also, the change in context - saving from the other entity - helps to provide a physical gate anyway. |
||
|
|
||
| if ( error ) { | ||
| const args = getNotificationArgumentsForSaveFail( { | ||
| post: previousRecord, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another question - this attaches images after a save is kicked off. What happens if, says, I'm the Flash, and I add another image while the save request is still happening?
Would it hurt to hoist these checks further up, possibly also
select.getEditorBlocks()so you're getting the saved post's blocks, not whatever else has been added to the screen?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, bit of an edge case, but good to be thorough here!
In terms of where we place the checks (this big
ifblock), I chose this location because it occurs after all the hooked filters run (i.e. pre save, unstable save post, save post action, etc). If any of these stages have errored, then we want to skip this side-effect of attaching media.But, we can place the
select.getEditorBlocks()right up to the top so that we take a snapshot of the blocks early in the cycle. I've pushed that in 19c89ea, which I think gives us the best of both worlds (early snapshot, while still only attaching media late in the cycle).