From 0078019409c6628af23d6f193daf0abe91f55103 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 27 Dec 2023 16:54:23 +1100 Subject: [PATCH 1/2] Always send a numerical post ID. Templates and template parts use string ids comprising "theme + // + template name". --- packages/editor/src/utils/media-upload/index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/utils/media-upload/index.js b/packages/editor/src/utils/media-upload/index.js index 3edd4fec51d4be..d44a6e06198ea1 100644 --- a/packages/editor/src/utils/media-upload/index.js +++ b/packages/editor/src/utils/media-upload/index.js @@ -31,17 +31,25 @@ 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(); + let currentPostId = + typeof currentPost?.id === 'number' ? currentPost.id : 0; + + // Templates and template parts' numerical ID is stored in `wp_id`. + if ( ! currentPostId && typeof currentPost?.wp_id === 'number' ) { + currentPostId = currentPost.wp_id; + } + uploadMedia( { allowedTypes, filesList, onFileChange, additionalData: { - ...( ! isNaN( currentPostId ) ? { post: currentPostId } : {} ), + post: currentPostId, ...additionalData, }, maxUploadFileSize, From f44f8fec6e83217c5e3c5e385fb4fb11c82f00a8 Mon Sep 17 00:00:00 2001 From: ramon Date: Thu, 28 Dec 2023 13:00:55 +1100 Subject: [PATCH 2/2] Don't assign a post at all if id is not a number or wp_id (for templates) doesn't exist. --- packages/editor/src/utils/media-upload/index.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/editor/src/utils/media-upload/index.js b/packages/editor/src/utils/media-upload/index.js index d44a6e06198ea1..f1f60b7c1e99a0 100644 --- a/packages/editor/src/utils/media-upload/index.js +++ b/packages/editor/src/utils/media-upload/index.js @@ -36,20 +36,19 @@ export default function mediaUpload( { maxUploadFileSize = maxUploadFileSize || getEditorSettings().maxUploadFileSize; const currentPost = getCurrentPost(); - let currentPostId = - typeof currentPost?.id === 'number' ? currentPost.id : 0; - // Templates and template parts' numerical ID is stored in `wp_id`. - if ( ! currentPostId && typeof currentPost?.wp_id === 'number' ) { - currentPostId = currentPost.wp_id; - } + const currentPostId = + typeof currentPost?.id === 'number' + ? currentPost.id + : currentPost?.wp_id; + const postData = currentPostId ? { post: currentPostId } : {}; uploadMedia( { allowedTypes, filesList, onFileChange, additionalData: { - post: currentPostId, + ...postData, ...additionalData, }, maxUploadFileSize,