Make WordPress Core


Ignore:
Timestamp:
09/19/2019 02:35:47 PM (5 years ago)
Author:
kadamwhite
Message:

REST API: Issue warning if array meta is registered without item schema.

The purpose of meta registration is to assert that the meta key will contain a predictable value conforming to a schema, so the schema is therefore considered to be required.

Props TimothyBlynJacobs, grapplerulrich.
Fixes #43392.

File:
1 edited

Legend:

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

    r45915 r46186  
    11161116 *              `$sanitize_callback` and `$auth_callback` have been folded into this array.
    11171117 * @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
     1118 * @since 5.3.0 Valid meta types expanded to include "array" and "object".
    11181119 *
    11191120 * @param string $object_type    Type of object this meta is registered to.
     
    11221123 *     Data used to describe the meta key when registered.
    11231124 *
    1124  *     @type string $object_subtype    A subtype; e.g. if the object type is "post", the post type. If left empty,
    1125  *                                     the meta key will be registered on the entire object type. Default empty.
    1126  *     @type string $type              The type of data associated with this meta key.
    1127  *                                     Valid values are 'string', 'boolean', 'integer', and 'number'.
    1128  *     @type string $description       A description of the data attached to this meta key.
    1129  *     @type bool   $single            Whether the meta key has one value per object, or an array of values per object.
    1130  *     @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
    1131  *     @type string $auth_callback     Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
    1132  *     @type bool   $show_in_rest      Whether data associated with this meta key can be considered public and
    1133  *                                     should be accessible via the REST API. A custom post type must also declare
    1134  *                                     support for custom fields for registered meta to be accessible via REST.
     1125 *     @type string     $object_subtype    A subtype; e.g. if the object type is "post", the post type. If left empty,
     1126 *                                         the meta key will be registered on the entire object type. Default empty.
     1127 *     @type string     $type              The type of data associated with this meta key.
     1128 *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
     1129 *     @type string     $description       A description of the data attached to this meta key.
     1130 *     @type bool       $single            Whether the meta key has one value per object, or an array of values per object.
     1131 *     @type string     $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
     1132 *     @type string     $auth_callback     Optional. A function or method to call when performing edit_post_meta,
     1133 *                                         add_post_meta, and delete_post_meta capability checks.
     1134 *     @type bool|array $show_in_rest      Whether data associated with this meta key can be considered public and
     1135 *                                         should be accessible via the REST API. A custom post type must also declare
     1136 *                                         support for custom fields for registered meta to be accessible via REST.
     1137 *                                         When registering complex meta values this argument may optionally be an
     1138 *                                         array with 'schema' or 'prepare_callback' keys instead of a boolean.
    11351139 * }
    11361140 * @param string|array $deprecated Deprecated. Use `$args` instead.
     
    11891193    $args = wp_parse_args( $args, $defaults );
    11901194
     1195    // Require an item schema when registering array meta.
     1196    if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
     1197        if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
     1198            _doing_it_wrong( __FUNCTION__, __( 'When registering an "array" meta type to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.3.0' );
     1199
     1200            return false;
     1201        }
     1202    }
     1203
    11911204    $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
    11921205
Note: See TracChangeset for help on using the changeset viewer.