Skip to content

Commit

Permalink
Media: send numerical post id when uploading image (#57388)
Browse files Browse the repository at this point in the history
* Always send a numerical post ID. Templates and template parts use string ids comprising "theme + // + template name".

* Don't assign a post at all if id is not a number or wp_id (for templates) doesn't exist.
  • Loading branch information
ramonjd committed Dec 28, 2023
1 parent 31706d5 commit 1fface7
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/editor/src/utils/media-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@ export default function mediaUpload( {
onError = noop,
onFileChange,
} ) {
const { getCurrentPostId, getEditorSettings } = select( editorStore );
const { getCurrentPost, getEditorSettings } = select( editorStore );
const wpAllowedMimeTypes = getEditorSettings().allowedMimeTypes;
maxUploadFileSize =
maxUploadFileSize || getEditorSettings().maxUploadFileSize;
const currentPostId = getCurrentPostId();
const currentPost = getCurrentPost();
// Templates and template parts' numerical ID is stored in `wp_id`.
const currentPostId =
typeof currentPost?.id === 'number'
? currentPost.id
: currentPost?.wp_id;
const postData = currentPostId ? { post: currentPostId } : {};

uploadMedia( {
allowedTypes,
filesList,
onFileChange,
additionalData: {
...( ! isNaN( currentPostId ) ? { post: currentPostId } : {} ),
...postData,
...additionalData,
},
maxUploadFileSize,
Expand Down

1 comment on commit 1fface7

@github-actions
Copy link

@github-actions github-actions bot commented on 1fface7 Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 1fface7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7344477167
📝 Reported issues:

Please sign in to comment.