mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
REST API: Support objects in settings schema.
Enables register_setting to accept an object as its schema value, allowing settings to accept non-scalar values through the REST API. This whitelists the added type in the settings controller, and passes properties from argument registration into the validation functions. Props joehoyle. See #38583. git-svn-id: https://develop.svn.wordpress.org/trunk@41758 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -127,6 +127,93 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
|
||||
unregister_setting( 'somegroup', 'mycustomsetting' );
|
||||
}
|
||||
|
||||
public function test_get_item_with_custom_array_setting() {
|
||||
wp_set_current_user( self::$administrator );
|
||||
|
||||
register_setting( 'somegroup', 'mycustomsetting', array(
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'array',
|
||||
) );
|
||||
|
||||
// Array is cast to correct types.
|
||||
update_option( 'mycustomsetting', array( '1', '2' ) );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] );
|
||||
|
||||
// Empty array works as expected.
|
||||
update_option( 'mycustomsetting', array() );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array(), $data['mycustomsetting'] );
|
||||
|
||||
// Invalid value
|
||||
update_option( 'mycustomsetting', array( array( 1 ) ) );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( null, $data['mycustomsetting'] );
|
||||
|
||||
// No option value
|
||||
delete_option( 'mycustomsetting' );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( null, $data['mycustomsetting'] );
|
||||
|
||||
unregister_setting( 'somegroup', 'mycustomsetting' );
|
||||
}
|
||||
|
||||
public function test_get_item_with_custom_object_setting() {
|
||||
wp_set_current_user( self::$administrator );
|
||||
|
||||
register_setting( 'somegroup', 'mycustomsetting', array(
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'object',
|
||||
) );
|
||||
|
||||
// Object is cast to correct types.
|
||||
update_option( 'mycustomsetting', array( 'a' => '1' ) );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
|
||||
|
||||
// Empty array works as expected.
|
||||
update_option( 'mycustomsetting', array() );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array(), $data['mycustomsetting'] );
|
||||
|
||||
// Invalid value
|
||||
update_option( 'mycustomsetting', array( 'a' => 1, 'b' => 2 ) );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
|
||||
|
||||
unregister_setting( 'somegroup', 'mycustomsetting' );
|
||||
}
|
||||
|
||||
public function get_setting_custom_callback( $result, $name, $args ) {
|
||||
switch ( $name ) {
|
||||
case 'mycustomsetting1':
|
||||
@@ -215,6 +302,7 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( null, $data['mycustomsettinginrest'] );
|
||||
unregister_setting( 'somegroup', 'mycustomsetting' );
|
||||
}
|
||||
|
||||
|
||||
@@ -242,6 +330,91 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
|
||||
return false;
|
||||
}
|
||||
|
||||
public function test_update_item_with_array() {
|
||||
register_setting( 'somegroup', 'mycustomsetting', array(
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'array',
|
||||
) );
|
||||
|
||||
// We have to re-register the route, as the args changes based off registered settings.
|
||||
$this->server->override_by_default = true;
|
||||
$this->endpoint->register_routes();
|
||||
wp_set_current_user( self::$administrator );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'mycustomsetting', array( '1', '2' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] );
|
||||
$this->assertEquals( array( 1, 2 ), get_option( 'mycustomsetting' ) );
|
||||
|
||||
// Setting an empty array.
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'mycustomsetting', array() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array(), $data['mycustomsetting'] );
|
||||
$this->assertEquals( array(), get_option( 'mycustomsetting' ) );
|
||||
|
||||
// Setting an invalid array.
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'mycustomsetting', array( 'invalid' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
unregister_setting( 'somegroup', 'mycustomsetting' );
|
||||
}
|
||||
|
||||
public function test_update_item_with_object() {
|
||||
register_setting( 'somegroup', 'mycustomsetting', array(
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'object',
|
||||
) );
|
||||
|
||||
// We have to re-register the route, as the args changes based off registered settings.
|
||||
$this->server->override_by_default = true;
|
||||
$this->endpoint->register_routes();
|
||||
wp_set_current_user( self::$administrator );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'mycustomsetting', array( 'a' => 1 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
|
||||
$this->assertEquals( array( 'a' => 1 ), get_option( 'mycustomsetting' ) );
|
||||
|
||||
// Setting an empty object.
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'mycustomsetting', array() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( array(), $data['mycustomsetting'] );
|
||||
$this->assertEquals( array(), get_option( 'mycustomsetting' ) );
|
||||
|
||||
// Setting an invalid object.
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
|
||||
$request->set_param( 'mycustomsetting', array( 'a' => 'invalid' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
unregister_setting( 'somegroup', 'mycustomsetting' );
|
||||
}
|
||||
|
||||
public function test_update_item_with_filter() {
|
||||
wp_set_current_user( self::$administrator );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user