WordPress.org

Make WordPress Core

Changeset 47325


Ignore:
Timestamp:
02/19/2020 10:54:03 PM (3 months ago)
Author:
TimothyBlynJacobs
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r47219 r47325  
    21052105 *     Data used to describe the setting when registered.
    21062106 *
    2107  *     @type string   $type              The type of data associated with this setting.
    2108  *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
    2109  *     @type string   $description       A description of the data attached to this setting.
    2110  *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
    2111  *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
    2112  *     @type mixed    $default           Default value when calling `get_option()`.
     2107 *     @type string     $type              The type of data associated with this setting.
     2108 *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
     2109 *     @type string     $description       A description of the data attached to this setting.
     2110 *     @type callable   $sanitize_callback A callback function that sanitizes the option's value.
     2111 *     @type bool|array $show_in_rest      Whether data associated with this setting should be included in the REST API.
     2112 *                                         When registering complex settings, this argument may optionally be an
     2113 *                                         array with a 'schema' key.
     2114 *     @type mixed      $default           Default value when calling `get_option()`.
    21132115 * }
    21142116 */
     
    21432145    $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
    21442146    $args = wp_parse_args( $args, $defaults );
     2147
     2148    // Require an item schema when registering settings with an array type.
     2149    if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) {
     2150        _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' );
     2151    }
    21452152
    21462153    if ( ! is_array( $wp_registered_settings ) ) {
  • trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php

    r47122 r47325  
    3737        parent::setUp();
    3838        $this->endpoint = new WP_REST_Settings_Controller();
     39    }
     40
     41    public function tearDown() {
     42        parent::tearDown();
     43
     44        if ( isset( get_registered_settings()['mycustomarraysetting'] ) ) {
     45            unregister_setting( 'somegroup', 'mycustomarraysetting' );
     46        }
    3947    }
    4048
     
    650658    public function test_get_item_schema() {
    651659    }
     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    }
    652714}
Note: See TracChangeset for help on using the changeset viewer.