Make WordPress Core

Ticket #43392: 43392.9.diff

File 43392.9.diff, 6.1 KB (added by TimothyBlynJacobs, 5 years ago)
  • src/wp-includes/meta.php

    diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php
    index 3966a23469..2b24a76c37 100644
    a b function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = 
    11151115 *              to support an array of data to attach to registered meta keys}. Previous arguments for
    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.
    11201121 * @param string $meta_key       Meta key to register.
    11211122 * @param array  $args {
    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, add_post_meta, and delete_post_meta capability checks.
     1133 *     @type bool|array $show_in_rest      Whether data associated with this meta key can be considered public and
     1134 *                                         should be accessible via the REST API. A custom post type must also declare
     1135 *                                         support for custom fields for registered meta to be accessible via REST. Optionally,
     1136 *                                         an array with a 'schema' or 'prepare_callback' keys.
    11351137 * }
    11361138 * @param string|array $deprecated Deprecated. Use `$args` instead.
    11371139 *
    function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { 
    11881190        $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
    11891191        $args = wp_parse_args( $args, $defaults );
    11901192
     1193        if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
     1194                if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
     1195                        _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' );
     1196
     1197                        return false;
     1198                }
     1199        }
     1200
    11911201        $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
    11921202
    11931203        // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
  • tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    diff --git a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
    index 1db1798fd6..2007e77ef2 100644
    a b class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    22042204                $this->assertEquals( array( 'project' => 'WordCamp' ), $data['meta']['object'] );
    22052205        }
    22062206
     2207        /**
     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
    22072267        /**
    22082268         * Internal function used to disable an insert query which
    22092269         * will trigger a wpdb error for testing purposes.