-
Notifications
You must be signed in to change notification settings - Fork 825
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
base: trunk
Are you sure you want to change the base?
Conversation
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
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. |
Code Coverage SummaryCoverage changed in 2 files.
|
ff892d6
to
28920e2
Compare
'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'/]" |
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.
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' ) );
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.
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() ); |
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.
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 ) );
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.
Added!
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.
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; |
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 need to be able to tell is form_field_id
is null
or ''
?
I think it would simplify things to always return a string?
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.
Agree, let's try not to polymorph the variable types. I'm fine with empty string and keep it a string all the way.
28920e2
to
f9229c3
Compare
@@ -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 |
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.
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 ) { |
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.
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 :)
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.
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 ); }
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.
get_field_by_id()
andget_field_value_by_id()
get_form_field_id()
to fetch the id.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:
Jetpack product discussion
None.
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
composer test-php
.The new property and methods are not used yet, so there's no way to test them in production use.