Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#42875 Fix PHP warning when registering array settings with missing items. #144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -2103,12 +2103,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() ) {
Expand Down Expand Up @@ -2142,6 +2144,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();
}
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/tests/rest-api/rest-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function setUp() {
$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 );
Expand Down Expand Up @@ -649,4 +657,58 @@ public function test_prepare_item() {

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!' ),
),
),
)
);
}
}