Skip to content

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

Closed

Conversation

enejb
Copy link
Member

@enejb enejb commented Feb 24, 2025

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:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

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

@enejb enejb changed the base branch from update/file-field-with-api to add/unauth-file-upload-endpoint February 24, 2025 21:04
Copy link
Contributor

github-actions bot commented Feb 24, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • 🔴 Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

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:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: April 1, 2025
    • Code freeze: March 30, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Feb 24, 2025
@lezama lezama changed the title Update/file field endpoint working Forms: Add file upload field support Feb 24, 2025
@lezama lezama self-assigned this Feb 24, 2025
Copy link
Contributor

github-actions bot commented Feb 24, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the update/file-field-endpoint-working branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack update/file-field-endpoint-working
bin/jetpack-downloader test jetpack-mu-wpcom-plugin update/file-field-endpoint-working

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Feb 24, 2025
Copy link

jp-launch-control bot commented Feb 24, 2025

Code Coverage Summary

No summary data is available for parent commit 2a3aeef, so cannot calculate coverage changes. 😴

If that commit is a feature branch rather than a trunk commit, this is expected. Otherwise, this should be updated once coverage for 2a3aeef is available.

Full summary · PHP report · JS report


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 ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

probably not necessary

@@ -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 ) ) {
Copy link
Member Author

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?

@enejb enejb added the [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it label Feb 26, 2025
@@ -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'] ) ) {
Copy link
Contributor

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

Copy link
Contributor

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?

@lezama lezama force-pushed the update/file-field-endpoint-working branch from 234b01c to 3192f5e Compare February 27, 2025 19:38
@enejb enejb force-pushed the add/unauth-file-upload-endpoint branch from 282de93 to 3b121fd Compare March 3, 2025 20:32
@enejb enejb force-pushed the update/file-field-endpoint-working branch from 516e2eb to c99a41e Compare March 3, 2025 20:36
@enejb enejb force-pushed the add/unauth-file-upload-endpoint branch from 3b121fd to 2a3aeef Compare March 18, 2025 23:43
@github-actions github-actions bot added the [Block] Contact Form Form block (also see Contact Form label) label Mar 20, 2025
@@ -59,6 +59,7 @@
"automattic/jetpack-changelogger": "@dev",
"automattic/patchwork-redefine-exit": "@dev",
"automattic/phpunit-select-config": "@dev",
"automattic/wordbless": "^0.5.0",
Copy link
Contributor

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.

@anomiex anomiex mentioned this pull request Mar 20, 2025
3 tasks
@@ -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",
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

@github-actions github-actions bot added the [Tools] Development CLI The tools/cli to assist during JP development. label Mar 21, 2025
Comment on lines +107 to +112
...jetpackWebpackConfig.StandardPlugins( {
DependencyExtractionPlugin: false,
I18nLoaderPlugin: false,
I18nCheckPlugin: false,
} ),
new DependencyExtractionWebpackPlugin(),
Copy link
Contributor

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.

@lezama lezama mentioned this pull request Mar 25, 2025
3 tasks
@enejb
Copy link
Member Author

enejb commented Jun 12, 2025

File upload shipped. Closing this since this was a PR that worked on a proof of concept.

@enejb enejb closed this Jun 12, 2025
@github-actions github-actions bot removed the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Contact Form Form block (also see Contact Form label) [Feature] Contact Form [Package] Forms [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests [Tools] Development CLI The tools/cli to assist during JP development. [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants