Skip to content

Forms: Include HTML id in Feedback classes #44760

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

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from

Conversation

edanzer
Copy link
Contributor

@edanzer edanzer commented Aug 12, 2025

Proposed changes:

Note: The diff for this is a bit easier to read if you check the remove whitespace option.

What's is added? This PR adds logic to the Feedback and Feedback_Field classes to preserve and handle the html id for each field.

  • The Feedback Class
    • $field_id is passed in when instantiating Feedback_Field, when relevant.
    • There are two new methods: get_field_by_id() and get_field_value_by_id()
  • The Feedback_Field Class
    • Now has a $form_field_id property, which is set in the constructor.
    • Has has a simple getter, get_form_field_id() to fetch the id.
  • Tests are added or updated for both classes.

Why? The new methods are not used in production code yet, but would be used by the MailPoet integration class. We want to be get first and last name when adding subscribers to MailPoet lists, and one of several methods we want to give users is the ability to set those filed by using ids like 'first_name' and 'last_name'. These changes allow us to do something like get_field_value_by_id( 'first-name' ) and confirm a non-null value is returned.

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

None.

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

No.

Testing instructions:

  • Review new properties and methods and confirm they make sense.
  • Review code to look for any possible regressions or backwards compat issues.
  • Run test and confirm they all pass. Naviage to to projects/packages/forms and run composer test-php.

The new property and methods are not used yet, so there's no way to test them in production use.

@edanzer edanzer self-assigned this Aug 12, 2025
@edanzer edanzer added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Review This PR is ready for review. [Pri] Normal [Package] Forms labels Aug 12, 2025
Copy link
Contributor

github-actions bot commented Aug 12, 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), and enable the add/forms-feedback-field-id branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/forms-feedback-field-id

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 [Feature] Contact Form [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress labels Aug 12, 2025
Copy link
Contributor

github-actions bot commented Aug 12, 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: September 2, 2025
    • Code freeze: September 1, 2025

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

Copy link

jp-launch-control bot commented Aug 12, 2025

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/packages/forms/src/contact-form/class-feedback.php 437/446 (97.98%) -0.17% 1 ❤️‍🩹
projects/packages/forms/src/contact-form/class-feedback-field.php 77/80 (96.25%) 0.20% 0 💚

Full summary · PHP report · JS report

@edanzer edanzer force-pushed the add/forms-feedback-field-id branch from ff892d6 to 28920e2 Compare August 13, 2025 05:33
@edanzer edanzer requested a review from enejb August 13, 2025 05:33
@edanzer edanzer marked this pull request as ready for review August 13, 2025 05:33
'title' => 'Test Form',
'description' => 'This is a test form.',
),
"[contact-field label='Name' type='name' required='1'/][contact-field label='Email' type='email' required='1'/][contact-field label='Message' type='textarea' required='1'/]"
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to check - [contact-field label='Name' type='name' required='1' id="foo" /]
And

that

$this->assertEquals( 'John Doe', $response->get_field_value_by_id( 'foo' ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is apparently not that simple. As written above, this will not pass. Our testing code (if not our production code) seems to be set up in a way where id=foo will be converted to something like gXXX-ID-foo. And then there's some kind of disconnect in the data. I'm having a hard time figuring out how to fix/resolve it in a non-artificial way.


$field = $response->get_field_by_id( $email_id );
$this->assertInstanceOf( Feedback_Field::class, $field );
$this->assertEquals( $email_id, $field->get_form_field_id() );
Copy link
Member

Choose a reason for hiding this comment

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

It would also be good to check that saving worked as expected and that the field_id would also be there after we save the response.

So

$saved_id = $response->save();
$saved_response = Feedback::get( $saved_id );
$this->assertEquals( '[email protected]', $saved_response->get_field_value_by_id( $email_id ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added!

Copy link
Member

Choose a reason for hiding this comment

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

nice.

$this->value = $value;
$this->type = $type;
$this->meta = $meta;
$this->form_field_id = is_string( $form_field_id ) && $form_field_id !== '' ? $form_field_id : null;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to be able to tell is form_field_id is null or '' ?
I think it would simplify things to always return a string?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, let's try not to polymorph the variable types. I'm fine with empty string and keep it a string all the way.

@edanzer edanzer force-pushed the add/forms-feedback-field-id branch from 28920e2 to f9229c3 Compare August 15, 2025 16:40
@@ -266,7 +289,8 @@ public static function from_serialized( $data ) {
$data['label'],
$data['value'],
$data['type'] ?? 'basic',
$data['meta'] ?? array()
$data['meta'] ?? array(),
$data['form_field_id'] ?? null
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the comment above, this should default to '', if we decide to go with it.

}
foreach ( $this->fields as $field ) {
$form_field_id = $field->get_form_field_id();
if ( is_string( $form_field_id ) && $form_field_id !== '' && $form_field_id === $id ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, given the null vs string discussion above, this comparison should be simpler, you wouldn't need to check if is_string, you'd only check for equality :)

Copy link
Contributor

@CGastrell CGastrell Aug 19, 2025

Choose a reason for hiding this comment

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

In fact, if $this->fields is always an array, you could even just return array_find( $this->fields, function( $a ) { $a->get_form_field_id() === $id ); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Contact Form [Package] Forms [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Pri] Normal [Status] In Progress [Status] Needs Review This PR is ready for review. [Tests] Includes Tests [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.

3 participants