From d84b7167423e937e13a84ab0e5092f8345b4cca5 Mon Sep 17 00:00:00 2001
From: Sagar Tamang <mi5t4n@gmail.com>
Date: Tue, 7 Mar 2023 12:55:20 +0545
Subject: [PATCH] fix - allow user-defined validate and sanitize callbacks in
rest json schema.
---
src/wp-includes/rest-api.php | 8 ++++++
.../tests/rest-api/rest-controller.php | 16 +++++++++++
.../tests/rest-api/rest-test-controller.php | 28 ++++++++++---------
3 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
index 33fcdd5b8a..41a290f11d 100644
a
|
b
|
function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::C |
3272 | 3272 | 'sanitize_callback' => 'rest_sanitize_request_arg', |
3273 | 3273 | ); |
3274 | 3274 | |
| 3275 | if ( isset( $params['validate_callback'] ) && is_callable( $params['validate_callback'] ) ) { |
| 3276 | $endpoint_args[ $field_id ]['validate_callback'] = $params['validate_callback']; |
| 3277 | } |
| 3278 | |
| 3279 | if ( isset( $params['sanitize_callback'] ) && is_callable( $params['sanitize_callback'] ) ) { |
| 3280 | $endpoint_args[ $field_id ]['sanitize_callback'] = $params['sanitize_callback']; |
| 3281 | } |
| 3282 | |
3275 | 3283 | if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) { |
3276 | 3284 | $endpoint_args[ $field_id ]['default'] = $params['default']; |
3277 | 3285 | } |
diff --git a/tests/phpunit/tests/rest-api/rest-controller.php b/tests/phpunit/tests/rest-api/rest-controller.php
index f7da75327a..912f7a7255 100644
a
|
b
|
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
300 | 300 | $this->assertArrayHasKey( 'someobject', $args ); |
301 | 301 | } |
302 | 302 | |
| 303 | public function test_get_endpoint_args_for_item_schema_validate_callback() { |
| 304 | $controller = new WP_REST_Test_Controller(); |
| 305 | $args = rest_get_endpoint_args_for_schema( $controller->get_item_schema() ); |
| 306 | |
| 307 | $this->assertSame( $args['somestring']['validate_callback'], '__return_true' ); |
| 308 | $this->assertSame( $args['sometextfield']['validate_callback'], 'rest_validate_request_arg' ); |
| 309 | } |
| 310 | |
| 311 | public function test_get_endpoint_args_for_item_schema_sanitize_callback() { |
| 312 | $controller = new WP_REST_Test_Controller(); |
| 313 | $args = rest_get_endpoint_args_for_schema( $controller->get_item_schema() ); |
| 314 | |
| 315 | $this->assertSame( $args['someinteger']['sanitize_callback'], 'absint' ); |
| 316 | $this->assertSame( $args['sometextfield']['sanitize_callback'], 'rest_sanitize_request_arg' ); |
| 317 | } |
| 318 | |
303 | 319 | public function test_get_endpoint_args_for_item_schema_description() { |
304 | 320 | $controller = new WP_REST_Test_Controller(); |
305 | 321 | $args = rest_get_endpoint_args_for_schema( $controller->get_item_schema() ); |
diff --git a/tests/phpunit/tests/rest-api/rest-test-controller.php b/tests/phpunit/tests/rest-api/rest-test-controller.php
index 042f37cf8b..8c47eed942 100644
a
|
b
|
class WP_REST_Test_Controller extends WP_REST_Controller { |
35 | 35 | 'type' => 'object', |
36 | 36 | 'properties' => array( |
37 | 37 | 'somestring' => array( |
38 | | 'type' => 'string', |
39 | | 'description' => 'A pretty string.', |
40 | | 'minLength' => 3, |
41 | | 'maxLength' => 3, |
42 | | 'pattern' => '[a-zA-Z]+', |
43 | | 'context' => array( 'view' ), |
| 38 | 'type' => 'string', |
| 39 | 'description' => 'A pretty string.', |
| 40 | 'minLength' => 3, |
| 41 | 'maxLength' => 3, |
| 42 | 'pattern' => '[a-zA-Z]+', |
| 43 | 'context' => array( 'view' ), |
| 44 | 'validate_callback' => '__return_true', |
44 | 45 | ), |
45 | 46 | 'someinteger' => array( |
46 | | 'type' => 'integer', |
47 | | 'multipleOf' => 10, |
48 | | 'minimum' => 100, |
49 | | 'maximum' => 200, |
50 | | 'exclusiveMinimum' => true, |
51 | | 'exclusiveMaximum' => true, |
52 | | 'context' => array( 'view' ), |
| 47 | 'type' => 'integer', |
| 48 | 'multipleOf' => 10, |
| 49 | 'minimum' => 100, |
| 50 | 'maximum' => 200, |
| 51 | 'exclusiveMinimum' => true, |
| 52 | 'exclusiveMaximum' => true, |
| 53 | 'sanitize_callback' => 'absint', |
| 54 | 'context' => array( 'view' ), |
53 | 55 | ), |
54 | 56 | 'someboolean' => array( |
55 | 57 | 'type' => 'boolean', |