WordPress.org

Make WordPress Core

Changeset 46186


Ignore:
Timestamp:
09/19/2019 02:35:47 PM (2 months 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.

Location:
trunk
Files:
2 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
  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r45903 r46186  
    22062206
    22072207    /**
     2208     * @ticket 43392
     2209     */
     2210    public function test_register_meta_issues_doing_it_wrong_when_show_in_rest_is_true() {
     2211        $this->setExpectedIncorrectUsage( 'register_meta' );
     2212
     2213        $registered = register_meta(
     2214            'post',
     2215            'invalid_array',
     2216            array(
     2217                'type'         => 'array',
     2218                'show_in_rest' => true,
     2219            )
     2220        );
     2221
     2222        self::assertFalse( $registered );
     2223    }
     2224
     2225    /**
     2226     * @ticket 43392
     2227     */
     2228    public function test_register_meta_issues_doing_it_wrong_when_show_in_rest_omits_schema() {
     2229        $this->setExpectedIncorrectUsage( 'register_meta' );
     2230
     2231        $registered = register_meta(
     2232            'post',
     2233            'invalid_array',
     2234            array(
     2235                'type'         => 'array',
     2236                'show_in_rest' => array(
     2237                    'prepare_callback' => 'rest_sanitize_value_from_schema',
     2238                ),
     2239            )
     2240        );
     2241
     2242        self::assertFalse( $registered );
     2243    }
     2244
     2245    /**
     2246     * @ticket 43392
     2247     */
     2248    public function test_register_meta_issues_doing_it_wrong_when_show_in_rest_omits_schema_items() {
     2249        $this->setExpectedIncorrectUsage( 'register_meta' );
     2250
     2251        $registered = register_meta(
     2252            'post',
     2253            'invalid_array',
     2254            array(
     2255                'type'         => 'array',
     2256                'show_in_rest' => array(
     2257                    'schema' => array(
     2258                        'default' => array( 'Hi!' ),
     2259                    ),
     2260                ),
     2261            )
     2262        );
     2263
     2264        self::assertFalse( $registered );
     2265    }
     2266
     2267    /**
    22082268     * Internal function used to disable an insert query which
    22092269     * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.