Skip to content

Commit

Permalink
[Experimental] Add $request to additional fields validation filter (w…
Browse files Browse the repository at this point in the history
…oocommerce#43789)

* Add $request to additional fields filter

* Add changelog entry

* Add data to additional message
  • Loading branch information
opr authored Jan 20, 2024
1 parent e995ee3 commit 7dabf2b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const getErrorDetails = (
message: decodeEntities(
additionalError.message
),
data,
},
];
if ( typeof additionalError.data !== 'undefined' ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Significance: patch
Type: dev
Comment: Skipping changelog because this is behind a feature flag.
19 changes: 10 additions & 9 deletions plugins/woocommerce/src/Blocks/Domain/Services/CheckoutFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,27 +476,28 @@ public function get_field_location( $field_key ) {
/**
* Validate an additional field against any custom validation rules. The result should be a WP_Error or true.
*
* @param string $key The key of the field.
* @param mixed $field_value The value of the field.
* @param array $field_schema The schema of the field.
* @param string $key The key of the field.
* @param mixed $field_value The value of the field.
* @param array $field_schema The schema of the field.
* @param \WP_REST_Request $request The current API Request.
*
* @since 8.6.0
*/
public function validate_field( $key, $field_value, $field_schema ) {
public function validate_field( $key, $field_value, $field_schema, $request ) {

$error = new \WP_Error();
try {
/**
* Filter the result of validating an additional field.
*
* @param \WP_Error $error A WP_Error that extensions may add errors to.
* @param mixed $field_value The value of the field.
* @param array $field_schema The schema of the field.
* @param string $key The key of the field.
* @param \WP_Error $error A WP_Error that extensions may add errors to.
* @param mixed $field_value The value of the field.
* @param array $field_schema The schema of the field.
* @param \WP_REST_Request $request The current API Request.
*
* @since 8.6.0
*/
$filtered_result = apply_filters( 'woocommerce_blocks_validate_additional_field_' . $key, $error, $field_value, $field_schema, $key );
$filtered_result = apply_filters( 'woocommerce_blocks_validate_additional_field_' . $key, $error, $field_value, $field_schema, $request );

if ( $error !== $filtered_result ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function validate_callback( $address, $request, $param ) {
// Check if a field is in the list of additional fields then validate the value against the custom validation rules defined for it.
// Skip additional validation if the schema validation failed.
if ( true === $result && in_array( $key, $additional_keys, true ) ) {
$result = $this->additional_fields_controller->validate_field( $key, $address[ $key ], $properties[ $key ] );
$result = $this->additional_fields_controller->validate_field( $key, $address[ $key ], $properties[ $key ], $request );
}

if ( is_wp_error( $result ) && $result->has_errors() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function validate_additional_fields( $fields, $request ) {

// Only allow custom validation on fields that pass the schema validation.
if ( true === $result ) {
$result = $this->additional_fields_controller->validate_field( $key, $field_value, $properties[ $key ] );
$result = $this->additional_fields_controller->validate_field( $key, $field_value, $properties[ $key ], $request );
}

if ( is_wp_error( $result ) && $result->has_errors() ) {
Expand Down

0 comments on commit 7dabf2b

Please sign in to comment.