REST API: Add support for "integer" type for meta and options
Previously Settings only supported "number" which meant it was possible to push floats to things like posts_per_page. This means now developers can also specify `type => ineger` in meta nad settings resgration. Props flixos90. Fixes #38393. git-svn-id: https://develop.svn.wordpress.org/trunk@39058 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
fb3c57a399
commit
89ce913169
@ -1781,7 +1781,7 @@ function register_initial_settings() {
|
||||
|
||||
register_setting( 'general', 'start_of_week', array(
|
||||
'show_in_rest' => true,
|
||||
'type' => 'number',
|
||||
'type' => 'integer',
|
||||
'description' => __( 'A day number of the week that the week should start on.' ),
|
||||
) );
|
||||
|
||||
@ -1803,7 +1803,7 @@ function register_initial_settings() {
|
||||
|
||||
register_setting( 'writing', 'default_category', array(
|
||||
'show_in_rest' => true,
|
||||
'type' => 'number',
|
||||
'type' => 'integer',
|
||||
'description' => __( 'Default category.' ),
|
||||
) );
|
||||
|
||||
@ -1815,7 +1815,7 @@ function register_initial_settings() {
|
||||
|
||||
register_setting( 'reading', 'posts_per_page', array(
|
||||
'show_in_rest' => true,
|
||||
'type' => 'number',
|
||||
'type' => 'integer',
|
||||
'description' => __( 'Blog pages show at most.' ),
|
||||
'default' => 10,
|
||||
) );
|
||||
|
@ -132,6 +132,8 @@ class WP_REST_Settings_Controller extends WP_REST_Controller {
|
||||
switch ( $schema['type'] ) {
|
||||
case 'string':
|
||||
return (string) $value;
|
||||
case 'integer':
|
||||
return (int) $value;
|
||||
case 'number':
|
||||
return (float) $value;
|
||||
case 'boolean':
|
||||
@ -258,7 +260,7 @@ class WP_REST_Settings_Controller extends WP_REST_Controller {
|
||||
* Whitelist the supported types for settings, as we don't want invalid types
|
||||
* to be updated with arbitrary values that we can't do decent sanitizing for.
|
||||
*/
|
||||
if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'string', 'boolean' ), true ) ) {
|
||||
if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'integer', 'string', 'boolean' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -404,6 +404,9 @@ abstract class WP_REST_Meta_Fields {
|
||||
case 'string':
|
||||
$value = (string) $value;
|
||||
break;
|
||||
case 'integer':
|
||||
$value = (int) $value;
|
||||
break;
|
||||
case 'number':
|
||||
$value = (float) $value;
|
||||
break;
|
||||
|
@ -267,6 +267,22 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
}
|
||||
|
||||
public function test_update_item_with_integer() {
|
||||
wp_set_current_user( self::$administrator );
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'posts_per_page', 11 );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
public function test_update_item_with_invalid_float_for_integer() {
|
||||
wp_set_current_user( self::$administrator );
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'posts_per_page', 10.5 );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Setting an item to "null" will essentially restore it to it's default value.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user