Skip to content

Commit

Permalink
fix: Audio block media selection handling to validate allowed file types
Browse files Browse the repository at this point in the history
  • Loading branch information
dhananjaykuber committed Jan 22, 2025
1 parent 5b7f7ec commit 1d8b322
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/block-editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ export function MediaPlaceholder( {
setSrc( value?.src ?? '' );
}, [ value?.src ] );

const handleMediaSelection = ( selectedMedia ) => {
if ( Array.isArray( selectedMedia ) ) {
const validMedia = selectedMedia.filter( ( media ) => {
const mediaType = media.type?.split( '/' )[ 0 ];
return (
allowedTypes?.includes( mediaType ) ||
allowedTypes?.includes( media.type )
);
} );

if ( validMedia.length !== selectedMedia.length ) {
onError( __( 'Some files are not allowed.' ) );

if ( validMedia.length === 0 ) {
return;
}
}

onSelect( validMedia );
} else {
const mediaType = selectedMedia.type?.split( '/' )[ 0 ];
if (
! allowedTypes?.includes( mediaType ) ||
! allowedTypes?.includes( selectedMedia.type )
) {
onError( __( 'File type not allowed.' ) );
return;
}

onSelect( selectedMedia );
}
};

const onlyAllowsImages = () => {
if ( ! allowedTypes || allowedTypes.length === 0 ) {
return false;
Expand Down Expand Up @@ -451,7 +484,7 @@ export function MediaPlaceholder( {
addToGallery={ addToGallery }
gallery={ multiple && onlyAllowsImages() }
multiple={ multiple }
onSelect={ onSelect }
onSelect={ handleMediaSelection }
allowedTypes={ allowedTypes }
mode="browse"
value={
Expand Down

0 comments on commit 1d8b322

Please sign in to comment.