Skip to content
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: Allow ability to add hidden fields #40860

Draft
wants to merge 15 commits into
base: trunk
Choose a base branch
from

Conversation

coder-karen
Copy link
Contributor

@coder-karen coder-karen commented Jan 6, 2025

Fixes #21241

Proposed changes:

  • This PR includes:
    • The initial foundation added here: Forms: hidden fields #40329 (allows adding hidden fields via the inspector). Modifications to that include:
    • Creating a button to add new fields (allows for cases when the value field is blank)
    • Adding the hidden fields to form HTML if the value is blank for any name / value hidden field pairs, as well as a new set of filter, which allows third party integration for dynamically added values.
    • Warning messaging if duplicate names are spotted (but not preventing this from happening)
    • An initial framework to allow using hidden fields within the Salesforce form variation for the organization ID. An earlier commit included allowing for the actual hidden field, but this is pared back for now so functionality remains as it was (to simplify this initial hidden fields PR).

Additional notes:

  • See https://github.com/Automattic/vulcan/issues/601 for more information on possible next steps in terms of extending hidden fields and, how that can tie in to other functionality.
  • In terms of using hidden fields on more than one form on a post / page, this is currently staying as a 'to do', until form reusability steps related to unique form IDs are in place.

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

#21241
See also https://github.com/Automattic/vulcan/issues/601

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

On a self-hosted or WoA site, apply this PR via the Jetpack Beta plugin or locally. On Simple, with a sandboxed Simple site apply the changes using the command in the generated comment below.

  • Create a new post and add a form block - any form variation will do, but for ease you can use the Contact Form variation.
  • Once the form is added, select the form block and notice in the inspector there is a new section for hidden fields.
  • You should be able to add new name / value pairs with the Add button.
  • If you duplicate a name, you should see a warning message 'on blur' (when clicking elsewhere out of the name field). This should disappear once the duplicate name is changed.
  • If you test using a pre-defined hidden field with the name '[post_id]', this should remove the value field, and replace that with the form ID.

Form response data and exporting data:

  • After submitting a form that includes hidden fields, view the feedback response at wp-admin/edit.php?post_type=feedback and ensure the hidden field data is there.
  • Try exporting the data via CSV, and Google sheets. In both cases, the hidden fields names should be prefixed with '85_', which will place them after the more important form data such as name, email, etc, but before Consent and IP address.

Testing form filters:

  • On self-hosted / WoA:
  • Create two new sets of hidden fields. Name one [post_id] and name another 'Test', with a value 'Demo'.
  • Using a code snippet plugin / functionality plugin, add the following code:
add_filter( 'jetpack_contact_form_modify_hidden_fields', 'custom_modify_hidden_field_both', 10, 3 );

function custom_modify_hidden_field_both( $updated_field, $hidden_field ) {
    if ( isset( $updated_field['value'] ) && $updated_field['value'] === 'Demo' ) {
        $updated_field['name'] = 'Custom Name';
        $updated_field['value'] = 'Custom Value';
    }

    return $updated_field;
}
  • Activate the snippet, then on the front-end of the site fill out the demo form with the added hidden fields.
  • Check the form responses at wp-admin/edit.php?post_type=feedback - you should see one entry for [post_id] with the post id, and you should see one entry with the name 'Custom Name' and value of 'Custom Value'. There should be no entry for 'Test' with the value 'Demo'.
  • Add the following snippet:
add_filter( 'jetpack_contact_form_modify_hidden_field_name', 'custom_modify_hidden_field_name', 10, 3 );

function custom_modify_hidden_field_name( $field_name, $field_value, $hidden_field ) {
    if ( $field_value === '1771' ) { // replace 1771 with the post ID that your form is included in.
        return 'No post ID';
    }

    return $field_name;
}
  • Activate the snippet, then on the front-end of the site fill out the demo form with the added hidden fields.
  • Check the form responses at wp-admin/edit.php?post_type=feedback - you should see one entry for 'No post ID' with the post id, as well as the changed values from the previous values (or the original name / value pair values for those hidden fields if the other snippet is deactivated).

Copy link
Contributor

github-actions bot commented Jan 6, 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 WordPress.com Simple 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, and enable the add/forms-hidden-field-option branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack add/forms-hidden-field-option
    

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 [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/ labels Jan 6, 2025
Copy link
Contributor

github-actions bot commented Jan 6, 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 Team 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 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? 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 semi-continuously (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for February 4, 2025 (scheduled code freeze on undefined).

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

@simison simison requested a review from a team January 10, 2025 17:09
* @param string $form_html The complete form HTML.
* @param array $atts The block attributes.
*/
return apply_filters( 'grunion_modify_rendered_contact_form_html', $form_html, $atts );
Copy link
Member

Choose a reason for hiding this comment

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

I know we've used grunion_ a lot in code (and probably filters) in the past but I thin we could move towards jetpack_ prefix by now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I noticed that later as well, but instead of changing it I've removed this filter entirely and will be replacing with another elsewhere. I'll send you a message when ready (shortly), as the PR isn't good for reviewing just yet (additional items to be removed as well, will cause confusion otherwise.).

Copy link
Member

Choose a reason for hiding this comment

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

Cool 👍 sorry for jumping early onto this, I appreciate it's in draft still :-)


// If there's more than one form block on the page we can't tell
// which one is being submitted, abort.
// TODO: solve it or inform the user about this limitation!
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I'll drop a comment over there with a few thoughts shortly.

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/ [Status] In Progress [Type] Feature Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Forms: hidden input type
3 participants