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