diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php
index fbe1eda4a4..316f021ea6 100644
a
|
b
|
function register_initial_settings() { |
2101 | 2101 | * @param array $args { |
2102 | 2102 | * Data used to describe the setting when registered. |
2103 | 2103 | * |
2104 | | * @type string $type The type of data associated with this setting. |
2105 | | * Valid values are 'string', 'boolean', 'integer', and 'number'. |
2106 | | * @type string $description A description of the data attached to this setting. |
2107 | | * @type callable $sanitize_callback A callback function that sanitizes the option's value. |
2108 | | * @type bool $show_in_rest Whether data associated with this setting should be included in the REST API. |
2109 | | * @type mixed $default Default value when calling `get_option()`. |
| 2104 | * @type string $type The type of data associated with this setting. |
| 2105 | * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. |
| 2106 | * @type string $description A description of the data attached to this setting. |
| 2107 | * @type callable $sanitize_callback A callback function that sanitizes the option's value. |
| 2108 | * @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API. |
| 2109 | * When registering complex settings, this argument may optionally be an |
| 2110 | * array with a 'schema' key. |
| 2111 | * @type mixed $default Default value when calling `get_option()`. |
2110 | 2112 | * } |
2111 | 2113 | */ |
2112 | 2114 | function register_setting( $option_group, $option_name, $args = array() ) { |
… |
… |
function register_setting( $option_group, $option_name, $args = array() ) { |
2140 | 2142 | $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name ); |
2141 | 2143 | $args = wp_parse_args( $args, $defaults ); |
2142 | 2144 | |
| 2145 | // Require an item schema when registering settings with an array type. |
| 2146 | if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) { |
| 2147 | _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' ); |
| 2148 | } |
| 2149 | |
2143 | 2150 | if ( ! is_array( $wp_registered_settings ) ) { |
2144 | 2151 | $wp_registered_settings = array(); |
2145 | 2152 | } |
diff --git a/tests/phpunit/tests/rest-api/rest-settings-controller.php b/tests/phpunit/tests/rest-api/rest-settings-controller.php
index 73805b4d38..bc9beb4ad7 100644
a
|
b
|
class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase |
38 | 38 | $this->endpoint = new WP_REST_Settings_Controller(); |
39 | 39 | } |
40 | 40 | |
| 41 | public function tearDown() { |
| 42 | parent::tearDown(); |
| 43 | unregister_setting( 'somegroup', 'mycustomarraysetting' ); |
| 44 | } |
| 45 | |
41 | 46 | public function test_register_routes() { |
42 | 47 | $routes = rest_get_server()->get_routes(); |
43 | 48 | $this->assertArrayHasKey( '/wp/v2/settings', $routes ); |
… |
… |
class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase |
649 | 654 | |
650 | 655 | public function test_get_item_schema() { |
651 | 656 | } |
| 657 | |
| 658 | /** |
| 659 | * @ticket 42875 |
| 660 | */ |
| 661 | public function test_register_setting_issues_doing_it_wrong_when_show_in_rest_is_true() { |
| 662 | $this->setExpectedIncorrectUsage( 'register_setting' ); |
| 663 | |
| 664 | register_setting( |
| 665 | 'somegroup', |
| 666 | 'mycustomarraysetting', |
| 667 | array( |
| 668 | 'type' => 'array', |
| 669 | 'show_in_rest' => true, |
| 670 | ) |
| 671 | ); |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * @ticket 42875 |
| 676 | */ |
| 677 | public function test_register_setting_issues_doing_it_wrong_when_show_in_rest_omits_schema() { |
| 678 | $this->setExpectedIncorrectUsage( 'register_setting' ); |
| 679 | |
| 680 | register_setting( |
| 681 | 'somegroup', |
| 682 | 'mycustomarraysetting', |
| 683 | array( |
| 684 | 'type' => 'array', |
| 685 | 'show_in_rest' => array( |
| 686 | 'prepare_callback' => 'rest_sanitize_value_from_schema', |
| 687 | ), |
| 688 | ) |
| 689 | ); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * @ticket 42875 |
| 694 | */ |
| 695 | public function test_register_setting_issues_doing_it_wrong_when_show_in_rest_omits_schema_items() { |
| 696 | $this->setExpectedIncorrectUsage( 'register_setting' ); |
| 697 | |
| 698 | register_setting( |
| 699 | 'somegroup', |
| 700 | 'mycustomarraysetting', |
| 701 | array( |
| 702 | 'type' => 'array', |
| 703 | 'show_in_rest' => array( |
| 704 | 'schema' => array( |
| 705 | 'default' => array( 'Hi!' ), |
| 706 | ), |
| 707 | ), |
| 708 | ) |
| 709 | ); |
| 710 | } |
652 | 711 | } |