WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/19/2020 10:54:03 PM (7 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.

File:
1 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 ) ) {
Note: See TracChangeset for help on using the changeset viewer.