REST API: Warn if registering array settings without an items schema.

The documentation for register_setting has also been clarified to include all of the supported types and the flexibility of the show_in_rest parameter.

Fixes #42875.
Props perrywagle, dshanske, kadamwhite.


git-svn-id: https://develop.svn.wordpress.org/trunk@47325 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs 2020-02-19 22:54:03 +00:00
parent 5f26580730
commit 727faabe98
2 changed files with 75 additions and 6 deletions

View File

@ -2104,12 +2104,14 @@ function register_initial_settings() {
* @param array $args {
* Data used to describe the setting when registered.
*
* @type string $type The type of data associated with this setting.
* Valid values are 'string', 'boolean', 'integer', and 'number'.
* @type string $description A description of the data attached to this setting.
* @type callable $sanitize_callback A callback function that sanitizes the option's value.
* @type bool $show_in_rest Whether data associated with this setting should be included in the REST API.
* @type mixed $default Default value when calling `get_option()`.
* @type string $type The type of data associated with this setting.
* Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
* @type string $description A description of the data attached to this setting.
* @type callable $sanitize_callback A callback function that sanitizes the option's value.
* @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API.
* When registering complex settings, this argument may optionally be an
* array with a 'schema' key.
* @type mixed $default Default value when calling `get_option()`.
* }
*/
function register_setting( $option_group, $option_name, $args = array() ) {
@ -2143,6 +2145,11 @@ function register_setting( $option_group, $option_name, $args = array() ) {
$args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
$args = wp_parse_args( $args, $defaults );
// Require an item schema when registering settings with an array type.
if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) {
_doing_it_wrong( __FUNCTION__, __( 'When registering an "array" setting to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.4.0' );
}
if ( ! is_array( $wp_registered_settings ) ) {
$wp_registered_settings = array();
}

View File

@ -38,6 +38,14 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
$this->endpoint = new WP_REST_Settings_Controller();
}
public function tearDown() {
parent::tearDown();
if ( isset( get_registered_settings()['mycustomarraysetting'] ) ) {
unregister_setting( 'somegroup', 'mycustomarraysetting' );
}
}
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/settings', $routes );
@ -649,4 +657,58 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
public function test_get_item_schema() {
}
/**
* @ticket 42875
*/
public function test_register_setting_issues_doing_it_wrong_when_show_in_rest_is_true() {
$this->setExpectedIncorrectUsage( 'register_setting' );
register_setting(
'somegroup',
'mycustomarraysetting',
array(
'type' => 'array',
'show_in_rest' => true,
)
);
}
/**
* @ticket 42875
*/
public function test_register_setting_issues_doing_it_wrong_when_show_in_rest_omits_schema() {
$this->setExpectedIncorrectUsage( 'register_setting' );
register_setting(
'somegroup',
'mycustomarraysetting',
array(
'type' => 'array',
'show_in_rest' => array(
'prepare_callback' => 'rest_sanitize_value_from_schema',
),
)
);
}
/**
* @ticket 42875
*/
public function test_register_setting_issues_doing_it_wrong_when_show_in_rest_omits_schema_items() {
$this->setExpectedIncorrectUsage( 'register_setting' );
register_setting(
'somegroup',
'mycustomarraysetting',
array(
'type' => 'array',
'show_in_rest' => array(
'schema' => array(
'default' => array( 'Hi!' ),
),
),
)
);
}
}