-
Notifications
You must be signed in to change notification settings - Fork 368
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
Add filters for file uploads (prepare and pre upload), update PHPDoc #747
Conversation
IMO this is a really important filter that is missing as I could use the |
Also updated PHPDoc as return value is going to be |
@@ -723,6 +723,10 @@ function job_manager_upload_file( $file, $args = array() ) { | |||
$allowed_mime_types = $args['allowed_mime_types']; | |||
} | |||
|
|||
if( is_wp_error( $file = apply_filters( 'job_manager_upload_file_pre_upload', $file, $args, $allowed_mime_types ) ) ) { |
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.
Incredibly picky, but can you add the space after the if
?
I'm not crazy about the readability of the line, but not a blocker for me.
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.
I would say pull out the whole $file
var.
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.
@spencerfinnell nah this way by adding one filter we add two use case scenarios ... one for modifying the $file
data before being uploaded, and the other for returning a WP_Error
to prevent upload and return that error
Noted very nit-picky code standard fix, but otherwise, 👍 from me. |
@kraftbj Would you prefer something like this instead? /**
* Filter file configuration before upload
*
* This filter can be used to modify the file arguments before being uploaded, or return a WP_Error
* object to prevent the file from being uploaded, and return the error.
*
* @since 1.25.2
*
* @param array $file Array of $_FILE data to upload.
* @param array $args Optional file arguments
* @param array $allowed_mime_types Array of allowed mime types from field config or defaults
*/
$file = apply_filters( 'job_manager_upload_file_pre_upload', $file, $args, $allowed_mime_types );
if ( is_wp_error( $file ) ) {
return $file;
} |
I used the method in the PR to kind of mimic this: // Validate required
if ( is_wp_error( ( $return = $this->validate_fields( $values ) ) ) ) {
throw new Exception( $return->get_error_message() );
} |
Yeah, that docblock is good. Commit it and I'll merge this in. |
@kraftbj okay updated |
This PR adds two new filters in the file upload handling process.
The first filter
job_manager_prepare_uploaded_files
is added to the return value from the core functionjob_manager_prepare_uploaded_files
(this I added so it's available if needed later)The second filter and the most important one is the
job_manager_upload_file_pre_upload
filter added before the corewp_handle_upload
function is called.This allows us to check the file for certain conditions (file size, dimensions, etc), or anything else that may be required before actually uploading the file, and return our own
WP_Error
that would then be output on the frontend (in error notice, or message box for Ajax uploads).The most important part of this is that it prevents the file from actually being uploaded to the server, as the only method I can use right now is in the validation process and return an error, but by that point the file has already been uploaded to the server.