Skip to content

Commit

Permalink
Inserter: Bail early when a user has no permission to upload media (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Apr 23, 2024
1 parent 73a17b0 commit 32e319d
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,31 @@ export function MediaPreview( { media, onClick, category } ) {
);
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );
const mediaUpload = useSelect(
( select ) => select( blockEditorStore ).getSettings().mediaUpload,
[]
);
const { getSettings } = useSelect( blockEditorStore );

const onMediaInsert = useCallback(
( previewBlock ) => {
// Prevent multiple uploads when we're in the process of inserting.
if ( isInserting ) {
return;
}

const settings = getSettings();
const clonedBlock = cloneBlock( previewBlock );
const { id, url, caption } = clonedBlock.attributes;

// User has no permission to upload media.
if ( ! id && ! settings.mediaUpload ) {
setShowExternalUploadModal( true );
return;
}

// Media item already exists in library, so just insert it.
if ( !! id ) {
onClick( clonedBlock );
return;
}

setIsInserting( true );
// Media item does not exist in library, so try to upload it.
// Fist fetch the image data. This may fail if the image host
Expand All @@ -154,7 +162,7 @@ export function MediaPreview( { media, onClick, category } ) {
.fetch( url )
.then( ( response ) => response.blob() )
.then( ( blob ) => {
mediaUpload( {
settings.mediaUpload( {
filesList: [ blob ],
additionalData: { caption },
onFileChange( [ img ] ) {
Expand Down Expand Up @@ -189,10 +197,10 @@ export function MediaPreview( { media, onClick, category } ) {
},
[
isInserting,
getSettings,
onClick,
mediaUpload,
createErrorNotice,
createSuccessNotice,
createErrorNotice,
]
);

Expand Down

1 comment on commit 32e319d

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 32e319d.
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/8798308769
📝 Reported issues:

Please sign in to comment.