Skip to content

Commit b80ba26

Browse files
REST API: Add support for settings to specify their own additionalProperties.
This switches the Settings Controller to use `rest_default_additional_properties_to_false` and deprecates its own method. Props anna.bansaghi. Fixes #56493. git-svn-id: https://develop.svn.wordpress.org/trunk@54131 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 244a209 commit b80ba26

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ protected function get_registered_options() {
258258
continue;
259259
}
260260

261-
$rest_args['schema'] = $this->set_additional_properties_to_false( $rest_args['schema'] );
261+
$rest_args['schema'] = rest_default_additional_properties_to_false( $rest_args['schema'] );
262262

263263
$rest_options[ $rest_args['name'] ] = $rest_args;
264264
}
@@ -322,31 +322,22 @@ public function sanitize_callback( $value, $request, $param ) {
322322
}
323323

324324
/**
325-
* Recursively add additionalProperties = false to all objects in a schema.
325+
* Recursively add additionalProperties = false to all objects in a schema
326+
* if no additionalProperties setting is specified.
326327
*
327-
* This is need to restrict properties of objects in settings values to only
328+
* This is needed to restrict properties of objects in settings values to only
328329
* registered items, as the REST API will allow additional properties by
329330
* default.
330331
*
331332
* @since 4.9.0
333+
* @deprecated 6.1.0 Use {@see rest_default_additional_properties_to_false()} instead.
332334
*
333335
* @param array $schema The schema array.
334336
* @return array
335337
*/
336338
protected function set_additional_properties_to_false( $schema ) {
337-
switch ( $schema['type'] ) {
338-
case 'object':
339-
foreach ( $schema['properties'] as $key => $child_schema ) {
340-
$schema['properties'][ $key ] = $this->set_additional_properties_to_false( $child_schema );
341-
}
342-
343-
$schema['additionalProperties'] = false;
344-
break;
345-
case 'array':
346-
$schema['items'] = $this->set_additional_properties_to_false( $schema['items'] );
347-
break;
348-
}
339+
_deprecated_function( __METHOD__, '6.1.0', 'rest_default_additional_properties_to_false()' );
349340

350-
return $schema;
341+
return rest_default_additional_properties_to_false( $schema );
351342
}
352343
}

tests/phpunit/tests/rest-api/rest-settings-controller.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,50 @@ public function test_register_setting_issues_doing_it_wrong_when_show_in_rest_om
737737
)
738738
);
739739
}
740+
741+
/**
742+
* @ticket 56493
743+
*/
744+
public function test_register_setting_with_custom_additional_properties_value() {
745+
wp_set_current_user( self::$administrator );
746+
747+
register_setting(
748+
'somegroup',
749+
'mycustomsetting',
750+
array(
751+
'type' => 'object',
752+
'show_in_rest' => array(
753+
'schema' => array(
754+
'type' => 'object',
755+
'properties' => array(
756+
'test1' => array(
757+
'type' => 'string',
758+
),
759+
),
760+
'additionalProperties' => array(
761+
'type' => 'integer',
762+
),
763+
),
764+
),
765+
)
766+
);
767+
768+
$data = array(
769+
'mycustomsetting' => array(
770+
'test1' => 'my-string',
771+
'test2' => '2',
772+
'test3' => 3,
773+
),
774+
);
775+
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
776+
$request->add_header( 'content-type', 'application/json' );
777+
$request->set_body( wp_json_encode( $data ) );
778+
779+
$response = rest_do_request( $request );
780+
781+
$this->assertSame( 200, $response->get_status() );
782+
$this->assertSame( 'my-string', $response->data['mycustomsetting']['test1'] );
783+
$this->assertSame( 2, $response->data['mycustomsetting']['test2'] );
784+
$this->assertSame( 3, $response->data['mycustomsetting']['test3'] );
785+
}
740786
}

0 commit comments

Comments
 (0)