-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Media Utils: Restrict file uploads with multiple
prop in uploadMedia
and mediaUpload
#69175
base: trunk
Are you sure you want to change the base?
Media Utils: Restrict file uploads with multiple
prop in uploadMedia
and mediaUpload
#69175
Conversation
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
1 similar comment
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
}: UploadMediaArgs ) { | ||
if ( ! multiple && filesList.length > 1 ) { |
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.
Let's add a unit test for this accordingly in packages/media-utils/src/utils/test/upload-media.ts
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.
The change here is unnecessary, let's revert that.
new UploadError( { | ||
code: 'GENERAL', | ||
message: __( 'Only one file can be used here.' ), | ||
} ) |
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.
Just use a regular Error
if you don't want to pass a file.
What?
Closes #68629
This PR adds a
multiple
prop to theuploadMedia
andmediaUpload
functions. The prop defaults toTrue
. When set toFalse
, any attempts to drop or select multiple images result in an error message returned to the user, and the process aborts completely due to an early return.Why?
This PR uses a generalized approach to implement prop-based restrictions on file uploads instead of implementing separate logic in the consumers. Some consumer implementations include:
gutenberg/packages/block-editor/src/components/background-image-control/index.js
Line 316 in 0c42f22
gutenberg/packages/block-library/src/site-logo/edit.js
Line 509 in 0c42f22
How?
The
multiple
prop, which accepts a boolean value, has been introduced in theuploadMedia
andmediaUpload
functions. Whenmultiple
is set tofalse
and multiple images are selected, the process is aborted early, triggering theonError
callback.Testing Instructions
1.1. Site logo block
1.2. Featured image block
1.3. Featured image panel in the document sidebar
1.4. Media & text block
1.5. Cover block
Screencast