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
[Experiment] Additional field extensible sanitisation and validation handling #44463
[Experiment] Additional field extensible sanitisation and validation handling #44463
Conversation
This reverts commit 872c8f4.
plugins/woocommerce/src/Blocks/Domain/Services/CheckoutFields.php
Outdated
Show resolved
Hide resolved
plugins/woocommerce/src/Blocks/Domain/Services/CheckoutFields.php
Outdated
Show resolved
Hide resolved
Test Results SummaryCommit SHA: 94d2f73
To view the full API test report, click here. To view the full E2E test report, click here. To view all test reports, visit the WooCommerce Test Reports Dashboard. |
@@ -426,10 +427,9 @@ private function update_customer_from_request( \WP_REST_Request $request ) { | |||
$shipping_address_values = $request['shipping_address'] ?? $request['billing_address']; | |||
|
|||
foreach ( $shipping_address_values as $key => $value ) { | |||
if ( is_callable( [ $customer, "set_shipping_$key" ] ) ) { | |||
$customer->{"set_shipping_$key"}( $value ); | |||
} elseif ( 'phone' === $key ) { |
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.
Removed on purpose; core has a set_shipping_phone method now.
Hi @nielslange, Apart from reviewing the code changes, please make sure to review the testing instructions as well. You can follow this guide to find out what good testing instructions should look like: |
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.
Tested this out and seems to be working fine - I did look into the code and couldn't spot anything that should be actioned on this PR (left you a question on Slack for a follow up)
I will revisit on Monday and try some more edge cases too.
One other thing to mention is the order of arguments on the new hooks you added. Their args go $value
first then $key
second. I'd suggest making the key first and value second, but that's just my personal opinion, I am more used to saying "key/value" so that's what seems more natural to me.
If you decide not to change it then maybe consider updating the args of validate_field
to match the order in the hook, just for consistency and to make it a little easier for future us to reason with.
Good work!
Usually it depends if its the value thats the focus of the function/hook to what comes first. If its a hook, and we're filtering value, value must come first. Unless you spotted more places? |
Yeah I'm fine with either I just think |
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.
Thanks for working on this, @mikejolley. I've successfully tested the PR. While checking the code and the code snippet, I noticed that the validation hooks might have inconsistent names.
In the code snippet, I see the following two hooks:
woocommerce_blocks_validate_additional_field
woocommerce_blocks_validate_address_fields
I noticed that woocommerce_blocks_validate_additional_field
validates both the custom fields in the "Contact information" section and the one in the "Additional order information" section.
Furthermore, I noticed that woocommerce_blocks_validate_additional_field
is using the singular ([...]_field
), while woocommerce_blocks_validate_address_fields
is using the plural ([...]_fields
).
I wonder if we should stick to either singular or plural for consistency reasons. I also wonder if we should have individual validation hooks (e.g. woocommerce_blocks_validate_contact_fields
, woocommerce_blocks_validate_address_fields
and woocommerce_blocks_validate_additional_fields
) or one general validation hook (e.g. woocommerce_blocks_validate_custom_fields
).
As this PR works as expected, I'm approving it. As for the validation hooks, I'll leave this up to you and the extensibility squad to decide if it's worth renaming them, introducing one general validation hook or creating individual hooks for each section. Or simply keep the validation as it is. I might have missed earlier discussions about that.
To clarify
The difference is how many fields each hook is responsible for validating, so I think the plural/singular use here is fine. |
* | ||
* @since 8.7.0 | ||
*/ | ||
do_action( 'woocommerce_blocks_validate_' . $location . '_fields', $errors, $fields, $group ); |
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.
When the additional
area is validated, the hook is:
woocommerce_blocks_validate_additional_fields
which may be easily confused with woocommerce_blocks_validate_additional_field
. Do you think this is an issue? Should we consider renaming one or the other? Maybe adding location
to the location hook would work? woocommerce_blocks_validate_location_additional_fields
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 in b424149
Good now @opr ? Ill get tests passing again. |
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.
Working nicely now, thanks!
We should ensure sanitization is also tested, here's the example code I used:
add_action(
'woocommerce_blocks_sanitize_additional_field',
function( $value, $key ) {
if ( 'plugin-namespace/gov-id' === $key || 'plugin-namespace/confirm-gov-id' === $key ) {
return str_replace( ' ', '', $value );
}
return $value;
},
10,
2
);
feel free to add that to the instructions if you like.
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.
Thanks Mike, this is working great. My errors are being caught, checkout can continue 🙌🏼
Submission Review Guidelines:
Changes proposed in this Pull Request:
Introduces new hooks for extensions to sanitise and validate registered checkout fields.
Closes #44019
How to test the changes in this Pull Request:
Developers should use this snippet this add some custom fields. Gov ID and email have validation methods.
Changelog entry
Significance
Type
Message
Comment