-
Notifications
You must be signed in to change notification settings - Fork 823
Forms: Add file upload field support #42011
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
Forms: Add file upload field support #42011
Conversation
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: The Jetpack plugin has different release cadences depending on the platform:
If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
||
if ( is_wp_error( $move_result ) ) { | ||
return $move_result; | ||
} | ||
|
||
if ( $move_result === false ) { | ||
// Try native PHP copy as a fallback | ||
if ( copy( $file_path, $destination ) ) { |
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.
probably not necessary
projects/plugins/jetpack/_inc/lib/class-unauth-file-upload-handler.php
Outdated
Show resolved
Hide resolved
projects/packages/forms/src/contact-form/class-contact-form-plugin.php
Outdated
Show resolved
Hide resolved
@@ -1433,6 +1490,11 @@ public function process_submission() { | |||
|
|||
update_post_meta( $post_id, '_feedback_extra_fields', $this->addslashes_deep( $extra_values ) ); | |||
|
|||
// Store file attachments in a dedicated meta field | |||
if ( ! empty( $uploaded_files ) ) { |
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.
Do we really need this?
@@ -719,6 +727,13 @@ public static function escape_and_sanitize_field_value( $value ) { | |||
return ''; | |||
} | |||
|
|||
// Check if this is a JSON-encoded file upload and extract the filename if it is. | |||
$json_decoded = json_decode( $value, true ); | |||
if ( $json_decoded && isset( $json_decoded['name'] ) && isset( $json_decoded['file_id'] ) ) { |
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.
we do this all the time, maybe we should add a helper, or add another field to easily identify it is a file
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.
there's another method that does this already?
234b01c
to
3192f5e
Compare
282de93
to
3b121fd
Compare
516e2eb
to
c99a41e
Compare
3b121fd
to
2a3aeef
Compare
@@ -59,6 +59,7 @@ | |||
"automattic/jetpack-changelogger": "@dev", | |||
"automattic/patchwork-redefine-exit": "@dev", | |||
"automattic/phpunit-select-config": "@dev", | |||
"automattic/wordbless": "^0.5.0", |
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.
If WorDBless is needed (most existing Jetpack tests rely on having an actuall DB-full WordPress install to run against), you should use automattic/jetpack-test-environment
instead. See #41057.
projects/packages/forms/package.json
Outdated
@@ -76,6 +79,7 @@ | |||
"@wordpress/babel-plugin-import-jsx-pragma": "5.19.0", | |||
"@wordpress/browserslist-config": "6.19.0", | |||
"@wordpress/date": "5.19.0", | |||
"@wordpress/scripts": "30.12.0", |
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.
Also please don't bring in @wordpress/scripts
. It brings in different versions of a lot of deps that may conflict with the ones we use, while meanwhile we generally have our own way of doing what it tries to do.
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.
please don't bring in @wordpress/scripts.
@anomiex We are using the wordpress/scripts for the module support. Does the current Jetpack build script have the ability to build modules?
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.
Since wp-scripts build
just calls Webpack with a predefined config, you should be able to do the same thing by setting your webpack.config.js appropriately.
At a quick glance, it looks like you may need to set experiments.outputModule
and output.module
. Maybe also output.chunkFormat = 'module'
, output.environment.module
, and output.library.type = 'module'
if setting output.module
doesn't already change the defaults for those.
...jetpackWebpackConfig.StandardPlugins( { | ||
DependencyExtractionPlugin: false, | ||
I18nLoaderPlugin: false, | ||
I18nCheckPlugin: false, | ||
} ), | ||
new DependencyExtractionWebpackPlugin(), |
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.
Is there something wrong with the way the version of DependencyExtractionWebpackPlugin
returned from jetpackWebpackConfig.StandardPlugins()
is configured that you have to disable it and add your own copy?
It might be better to fix jetpackWebpackConfig.StandardPlugins()
instead.
File upload shipped. Closing this since this was a PR that worked on a proof of concept. |
Fixes #
Works on top of the endpoint PR: #41892
Proposed changes:
Adds support for file upload fields in Jetpack contact forms
Implements file field rendering and handling in the contact form field class
Integrates with existing upload handling for processing file uploads
Adds file verification and secure storage for uploaded files
Implements drag-and-drop functionality for file uploads
Adds CSS styling for the file upload field
Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
Testing instructions:
Set up a local WordPress installation with Jetpack and the Jetpack Forms package
Create a new page or post using the block editor
Add a Contact Form block
Add a new "File" field to the form using the form field controls
Preview or publish the page
Test uploading a file through the form:
Try dragging and dropping a file into the upload area
Try clicking the "Select a file" button and choosing a file
Verify the file uploads successfully (you should see the file name displayed)
Submit the form with the uploaded file
Verify the form submission includes the file attachment
Check that the file is properly stored in the wp-content/uploads/jetpack-forms directory
Additional edge case testing:
Try uploading files of different types (images, documents, etc.)
Try uploading a file larger than the allowed size
Test with multiple file upload fields in the same form