Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Fix sanitization callback (#5060)
Browse files Browse the repository at this point in the history
* Fix sanitization callback

* Add test case to confirm sanitization callbacks work for nested properties

Co-authored-by: Mike Jolley <mike.jolley@me.com>
  • Loading branch information
senadir and mikejolley authored Nov 5, 2021
1 parent 3393e58 commit 711d2fb
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"wp-phpunit/wp-phpunit": "^5.4",
"woocommerce/woocommerce-sniffs": "0.1.0",
"yoast/phpunit-polyfills": "^1.0",
"johnbillion/wp-hooks-generator": "0.6.1"
"johnbillion/wp-hooks-generator": "0.6.1",
"mockery/mockery": "^1.4"
},
"autoload": {
"psr-4": {
Expand Down
125 changes: 124 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<!-- Exclude paths -->
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>languages/woo-gutenberg-products-block.php</exclude-pattern>

<!-- Configs -->
Expand Down
11 changes: 8 additions & 3 deletions src/StoreApi/Schemas/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ protected function get_recursive_sanitize_callback( $properties ) {
* @return true|\WP_Error
*/
return function ( $values, $request, $param ) use ( $properties ) {
$sanitized_values = [];

foreach ( $properties as $property_key => $property_value ) {
$current_value = isset( $values[ $property_key ] ) ? $values[ $property_key ] : null;

Expand All @@ -188,17 +190,20 @@ protected function get_recursive_sanitize_callback( $properties ) {
$current_value = rest_sanitize_value_from_schema( $current_value, $property_value, $param . ' > ' . $property_key );
}

// If sanitization failed, return the WP_Error object straight away.
if ( is_wp_error( $current_value ) ) {
return $current_value;
}

if ( isset( $property_value['properties'] ) ) {
$sanitize_callback = $this->get_recursive_sanitize_callback( $property_value['properties'] );
return $sanitize_callback( $current_value, $request, $param . ' > ' . $property_key );
$sanitize_callback = $this->get_recursive_sanitize_callback( $property_value['properties'] );
$sanitized_values[ $property_key ] = $sanitize_callback( $current_value, $request, $param . ' > ' . $property_key );
} else {
$sanitized_values[ $property_key ] = $current_value;
}
}

return true;
return $sanitized_values;
};
}

Expand Down
Loading

0 comments on commit 711d2fb

Please sign in to comment.