Make WordPress Core

Ticket #42875: 42875.diff

File 42875.diff, 4.5 KB (added by TimothyBlynJacobs, 4 years ago)
  • src/wp-includes/option.php

    diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php
    index 3620064c17..2d7b751464 100644
    a b function register_initial_settings() { 
    20982098 * @param array  $args {
    20992099 *     Data used to describe the setting when registered.
    21002100 *
    2101  *     @type string   $type              The type of data associated with this setting.
    2102  *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
    2103  *     @type string   $description       A description of the data attached to this setting.
    2104  *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
    2105  *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
    2106  *     @type mixed    $default           Default value when calling `get_option()`.
     2101 *     @type string     $type              The type of data associated with this setting.
     2102 *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
     2103 *     @type string     $description       A description of the data attached to this setting.
     2104 *     @type callable   $sanitize_callback A callback function that sanitizes the option's value.
     2105 *     @type bool|array $show_in_rest      Whether data associated with this setting should be included in the REST API.
     2106 *                                         When registering complex settings, this argument may optionally be an
     2107 *                                         array with a 'schema' key.
     2108 *     @type mixed      $default           Default value when calling `get_option()`.
    21072109 * }
    21082110 */
    21092111function register_setting( $option_group, $option_name, $args = array() ) {
    function register_setting( $option_group, $option_name, $args = array() ) { 
    21372139        $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
    21382140        $args = wp_parse_args( $args, $defaults );
    21392141
     2142        // Require an item schema when registering array meta.
     2143        if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) {
     2144                _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' );
     2145        }
     2146
    21402147        if ( ! is_array( $wp_registered_settings ) ) {
    21412148                $wp_registered_settings = array();
    21422149        }
  • tests/phpunit/tests/rest-api/rest-settings-controller.php

    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 
    3838                $this->endpoint = new WP_REST_Settings_Controller();
    3939        }
    4040
     41        public function tearDown() {
     42                parent::tearDown();
     43                unregister_setting( 'somegroup', 'mycustomarraysetting' );
     44        }
     45
    4146        public function test_register_routes() {
    4247                $routes = rest_get_server()->get_routes();
    4348                $this->assertArrayHasKey( '/wp/v2/settings', $routes );
    class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase 
    649654
    650655        public function test_get_item_schema() {
    651656        }
     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        }
    652711}