diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index f434ef3b5c..abb5e6e8e9 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -210,8 +210,8 @@ function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_ // Compare existing value to new value if no prev value given and the key exists only once. if ( empty( $prev_value ) ) { - $old_value = get_metadata( $meta_type, $object_id, $meta_key ); - if ( count( $old_value ) == 1 ) { + $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key ); + if ( is_countable( $old_value ) && count( $old_value ) === 1 ) { if ( $old_value[0] === $meta_value ) { return false; } @@ -485,7 +485,7 @@ function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $ * If the meta field does not exist, an empty string is returned if `$single` is true, or an empty array if it's false. * If there's a problem with the parameters passed to the function, boolean `false` is returned. * - * @since 2.9.0 + * @since 5.5.0 * * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', * or any other object type with an associated meta table. @@ -496,7 +496,7 @@ function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $ * This parameter has no effect if meta_key is not specified. Default false. * @return mixed The metadata value or array of values. See description above for further details. */ -function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { +function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) { if ( ! $meta_type || ! is_numeric( $object_id ) ) { return false; } @@ -553,11 +553,74 @@ function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) } } - if ( $single ) { - return ''; - } else { - return array(); + return null; +} + +/** + * Retrieves raw metadata for the specified object. + * + * @since 2.9.0 + * @uses get_metadata_raw() + * + * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', + * or any other object type with an associated meta table. + * @param int $object_id ID of the object metadata is for. + * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for + * the specified object. Default empty. + * @param bool $single Optional. If true, return only the first value of the specified meta_key. + * This parameter has no effect if meta_key is not specified. Default false. + * @return mixed Single metadata value, or array of values + */ +function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { + $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); + if ( ! is_null( $value ) ) { + return $value; } + + return get_metadata_default( $meta_type, $meta_key, $single, $object_id ); +} + +/** + * Retrieve metadata data default for the specified object. + * + * @since 5.5.0 + * + * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). + * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for + * the specified object. + * @param bool $single Optional, default is false. + * If true, return only the first value of the specified meta_key. + * This parameter has no effect if meta_key is not specified. + * @param int $object_id Optional, default is 0. + * ID of the object metadata is for + * @return mixed Single metadata value, or array of values + */ +function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) { + if ( $single ) { + $value = ''; + } else { + $value = array(); + } + + /** + * Filter the default value a specified object. + * + * @since 5.5.0 + * + * @param array|string $value The value should return - a single metadata value, + * or an array of values. + * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). + * @param string $meta_key Meta key. + * @param bool $single Whether to return only the first value of the specified $meta_key. + * @param int $object_id Object ID. + */ + $value = apply_filters( "default_{$meta_type}_metadata", $value, $meta_type, $meta_key, $single, $object_id ); + + if ( ! $single && ! wp_is_numeric_array( $value ) ) { + $value = array( $value ); + } + + return $value; } /** @@ -1163,6 +1226,10 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. * @type string $description A description of the data attached to this meta key. * @type bool $single Whether the meta key has one value per object, or an array of values per object. + * @type mixed $default The default value returned from {@see get_metadata()} if no value has been set yet. + * When using a non-single meta key, the default value is for the first entry. In other + * words, when calling {@see get_metadata()} with `$single` set to `false`, the default + * value given here will be wrapped in an array. * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. * @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. @@ -1188,6 +1255,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { 'object_subtype' => '', 'type' => 'string', 'description' => '', + 'default' => '', 'single' => false, 'sanitize_callback' => null, 'auth_callback' => null, @@ -1225,6 +1293,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { * @param string $meta_key Meta key. */ $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); + unset( $defaults['default'] ); $args = wp_parse_args( $args, $defaults ); // Require an item schema when registering array meta. @@ -1264,6 +1333,24 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { } } + if ( array_key_exists( 'default', $args ) ) { + $schema = $args; + if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) { + $schema = array_merge( $schema, $args['show_in_rest']['schema'] ); + } + + $check = rest_validate_value_from_schema( $args['default'], $schema ); + if ( is_wp_error( $check ) ) { + _doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' ); + + return false; + } + + if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) { + add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); + } + } + // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { unset( $args['object_subtype'] ); @@ -1496,3 +1583,64 @@ function get_object_subtype( $object_type, $object_id ) { */ return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); } + +/** + * Filter into default_{$object_type}_metadata and add in default value. + * + * @since 5.5.0 + * + * @param mixed $value Current value passed to filter. + * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). + + * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for + * the specified object. + * @param bool $single Optional, default is false. + * If true, return only the first value of the specified meta_key. + * This parameter has no effect if meta_key is not specified. + * @param int $object_id ID of the object metadata is for + * + * @return mixed Single metadata default, or array of defaults + */ +function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) { + global $wp_meta_keys; + + if ( wp_installing() ) { + return $value; + } + + if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) { + return $value; + } + + $defaults = array(); + foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) { + foreach ( $meta_data as $_meta_key => $args ) { + if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) { + $defaults[ $sub_type ] = $args; + } + } + } + + if ( ! $defaults ) { + return $value; + } + + // If this meta type does not have sub types, then the default is keyed as an empty string. + if ( isset( $defaults[''] ) ) { + $metadata = $defaults['']; + } else { + $sub_type = get_object_subtype( $meta_type, $object_id ); + if ( ! isset( $defaults[ $sub_type ] ) ) { + return $value; + } + $metadata = $defaults[ $sub_type ]; + } + + if ( $single ) { + $value = $metadata['default']; + } else { + $value = array( $metadata['default'] ); + } + + return $value; +} diff --git a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php index f709afa85a..a00b92a1ef 100644 --- a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php +++ b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php @@ -79,7 +79,6 @@ abstract class WP_REST_Meta_Fields { foreach ( $fields as $meta_key => $args ) { $name = $args['name']; $all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false ); - if ( $args['single'] ) { if ( empty( $all_values ) ) { $value = $args['schema']['default']; diff --git a/tests/phpunit/tests/meta/registerMeta.php b/tests/phpunit/tests/meta/registerMeta.php index 334a2320d0..d54c24daaa 100644 --- a/tests/phpunit/tests/meta/registerMeta.php +++ b/tests/phpunit/tests/meta/registerMeta.php @@ -504,6 +504,73 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase { $this->assertSame( 'even', $subtype_for_4 ); } + /** + * @ticket 43941 + * @dataProvider data_get_default_data + */ + public function test_get_default_value( $args, $single, $expected ) { + + $object_type = 'post'; + $meta_key = 'registered_key1'; + register_meta( + $object_type, + $meta_key, + $args + ); + + $object_property_name = $object_type . '_id'; + $object_id = self::$$object_property_name; + $default_value = get_metadata_default( $object_type, $meta_key, $single, $object_id ); + $this->assertSame( $default_value, $expected ); + + // Check for default value. + $value = get_metadata( $object_type, $object_id, $meta_key, $single ); + $this->assertSame( $value, $expected ); + + // Set value to check default is not being returned by mistake. + $meta_value = 'dibble'; + update_metadata( $object_type, $object_id, $meta_key, $meta_value ); + $value = get_metadata( $object_type, $object_id, $meta_key, true ); + $this->assertSame( $value, $meta_value ); + + // Delete meta, make sure the default is returned. + delete_metadata( $object_type, $object_id, $meta_key ); + $value = get_metadata( $object_type, $object_id, $meta_key, $single ); + $this->assertSame( $value, $expected ); + + // Set other meta key, to make sure other keys are not effects. + $meta_value = 'hibble'; + $meta_key = 'unregistered_key1'; + $value = get_metadata( $object_type, $object_id, $meta_key, true ); + $this->assertSame( $value, '' ); + update_metadata( $object_type, $object_id, $meta_key, $meta_value ); + $value = get_metadata( $object_type, $object_id, $meta_key, true ); + $this->assertSame( $value, $meta_value ); + + } + + /** + * @ticket 43941 + * @dataProvider data_get_invalid_default_data + */ + public function test_get_invalid_default_value( $args, $single, $expected ) { + $this->setExpectedIncorrectUsage( 'register_meta' ); + $object_type = 'post'; + $meta_key = 'registered_key1'; + $register = register_meta( + $object_type, + $meta_key, + $args + ); + + $this->assertFalse( $register ); + + $object_property_name = $object_type . '_id'; + $object_id = self::$$object_property_name; + $default_value = get_metadata_default( $object_type, $meta_key, $single, $object_id ); + $this->assertSame( $default_value, $expected ); + } + public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) { if ( 1 === ( $object_id % 2 ) ) { return 'odd'; @@ -512,6 +579,501 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase { return 'even'; } + public function data_get_default_data() { + return array( + 'single string key with single ask ' => array( + array( + 'single' => true, + 'default' => 'wibble', + ), + true, + 'wibble', + ), + 'single string key with multiple ask' => array( + array( + 'single' => true, + 'default' => 'wibble', + ), + false, + array( 'wibble' ), + ), + 'multiple string key with single ask' => array( + array( + 'single' => false, + 'default' => 'wibble', + ), + true, + 'wibble', + ), + 'multiple string key with multiple ask' => array( + array( + 'single' => false, + 'default' => 'wibble', + ), + false, + array( 'wibble' ), + ), + 'single array key with multiple ask' => array( + array( + 'single' => true, + 'type' => 'array', + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'default' => array( 'wibble' ), + ), + false, + array( array( 'wibble' ) ), + ), + 'single string key with single ask for sub type' => array( + array( + 'single' => true, + 'object_subtype' => 'page', + 'default' => 'wibble', + ), + true, + 'wibble', + ), + 'single string key with multiple ask for sub type' => array( + array( + 'single' => true, + 'object_subtype' => 'page', + 'default' => 'wibble', + ), + false, + array( 'wibble' ), + ), + 'single array key with multiple ask for sub type' => array( + array( + 'single' => true, + 'object_subtype' => 'page', + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'default' => array( 'wibble' ), + ), + false, + array( array( 'wibble' ) ), + ), + + // types + 'single object key with single ask' => array( + array( + 'single' => true, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'default' => array( 'wibble' => 'dibble' ), + ), + true, + array( 'wibble' => 'dibble' ), + ), + 'single object key with multiple ask' => array( + array( + 'single' => true, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'default' => array( 'wibble' => 'dibble' ), + ), + false, + array( array( 'wibble' => 'dibble' ) ), + ), + 'multiple object key with single ask' => array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'single' => false, + 'default' => array( 'wibble' => 'dibble' ), + ), + true, + array( 'wibble' => 'dibble' ), + ), + 'multiple object key with multiple ask' => array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'single' => false, + 'default' => array( 'wibble' => 'dibble' ), + ), + false, + array( array( 'wibble' => 'dibble' ) ), + ), + 'single array key with multiple ask part two' => array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => true, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + false, + array( array( 'dibble' ) ), + ), + 'multiple array with multiple ask' => array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => false, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + false, + array( array( 'dibble' ) ), + ), + 'single array with single ask' => array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => true, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + true, + array( 'dibble' ), + ), + + 'multiple array with single ask' => array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => false, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + true, + array( 'dibble' ), + ), + + 'single boolean with single ask' => array( + array( + 'single' => true, + 'type' => 'boolean', + 'default' => true, + ), + true, + true, + ), + 'multiple boolean with single ask' => array( + array( + 'single' => false, + 'type' => 'boolean', + 'default' => true, + ), + true, + true, + ), + 'single boolean with multiple ask' => array( + array( + 'single' => true, + 'type' => 'boolean', + 'default' => true, + ), + false, + array( true ), + ), + 'multiple boolean with multiple ask' => array( + array( + 'single' => false, + 'type' => 'boolean', + 'default' => true, + ), + false, + array( true ), + ), + + 'single integer with single ask' => array( + array( + 'single' => true, + 'type' => 'integer', + 'default' => 123, + ), + true, + 123, + ), + 'multiple integer with single ask' => array( + array( + 'single' => false, + 'type' => 'integer', + 'default' => 123, + ), + true, + 123, + ), + 'single integer with multiple ask' => array( + array( + 'single' => true, + 'type' => 'integer', + 'default' => 123, + ), + false, + array( 123 ), + ), + 'multiple integer with multiple ask' => array( + array( + 'single' => false, + 'type' => 'integer', + 'default' => 123, + ), + false, + array( 123 ), + ), + 'single array of objects with multiple ask' => array( + array( + 'type' => 'array', + 'single' => true, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'object', + 'properties' => array( + 'name' => array( + 'type' => 'string', + ), + ), + ), + ), + ), + 'default' => array( + array( + 'name' => 'Kirk', + ), + ), + ), + false, + array( + array( + array( + 'name' => 'Kirk', + ), + ), + ), + ), + ); + } + + public function data_get_invalid_default_data() { + return array( + array( + array( + 'single' => true, + 'type' => 'boolean', + 'default' => 123, + ), + true, + '', + ), + array( + array( + 'single' => false, + 'type' => 'boolean', + 'default' => 123, + ), + true, + '', + ), + array( + array( + 'single' => true, + 'type' => 'boolean', + 'default' => 123, + ), + false, + array(), + ), + array( + array( + 'single' => false, + 'type' => 'boolean', + 'default' => 123, + ), + false, + array(), + ), + + array( + array( + 'single' => true, + 'type' => 'integer', + 'default' => 'wibble', + ), + true, + '', + ), + array( + array( + 'single' => false, + 'type' => 'integer', + 'default' => 'wibble', + ), + true, + '', + ), + array( + array( + 'single' => true, + 'type' => 'integer', + 'default' => 'wibble', + ), + false, + array(), + ), + array( + array( + 'single' => false, + 'type' => 'integer', + 'default' => 'wibble', + ), + false, + array(), + ), + array( + array( + 'single' => false, + 'type' => 'integer', + 'default' => array( 123, 'wibble' ), + ), + false, + array(), + ), + array( + array( + 'single' => false, + 'type' => 'integer', + 'default' => array( 123, array() ), + ), + false, + array(), + ), + array( + array( + 'single' => false, + 'type' => 'array', + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'default' => array( array( 123, 456 ), array( 'string' ) ), + ), + false, + array(), + ), + array( + array( + 'single' => true, + 'type' => 'array', + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'default' => array( array( 123, 456 ), array( 'string' ) ), + ), + true, + '', + ), + array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'my_prop' => array( + 'type' => 'string', + ), + 'my_required_prop' => array( + 'type' => 'string', + ), + ), + 'required' => array( 'my_required_prop' ), + ), + ), + 'type' => 'object', + 'single' => true, + 'default' => array( 'my_prop' => 'hibble' ), + ), + true, + '', + ), + ); + } + public function data_get_types_and_subtypes() { return array( array( 'post', 'page' ), 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 452e34bb1d..0fa6fdd3af 100644 --- a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php +++ b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php @@ -234,6 +234,17 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { ) ); + register_post_meta( + 'post', + 'with_default', + array( + 'type' => 'string', + 'single' => true, + 'show_in_rest' => true, + 'default' => 'Goodnight Moon', + ) + ); + /** @var WP_REST_Server $wp_rest_server */ global $wp_rest_server; $wp_rest_server = new Spy_REST_Server; @@ -2785,6 +2796,206 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { $this->assertNotFalse( get_metadata_by_mid( 'post', $mid2 ) ); } + /** + * @ticket 43941 + * @dataProvider data_get_default_data + */ + public function test_get_default_value( $args, $expected ) { + $object_type = 'post'; + $meta_key = 'registered_key1'; + $registered = register_meta( + $object_type, + $meta_key, + $args + ); + + $this->assertTrue( $registered ); + + // Check for default value. + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertEquals( 200, $response->get_status() ); + + $data = $response->get_data(); + $this->assertArrayHasKey( 'meta', $data ); + + $meta = (array) $data['meta']; + $this->assertArrayHasKey( $meta_key, $meta ); + $this->assertEquals( $expected, $meta[ $meta_key ] ); + } + + public function data_get_default_data() { + return array( + array( + array( + 'show_in_rest' => true, + 'single' => true, + 'default' => 'wibble', + ), + 'wibble', + ), + array( + array( + 'show_in_rest' => true, + 'single' => false, + 'default' => 'wibble', + ), + array( 'wibble' ), + ), + array( + array( + 'show_in_rest' => true, + 'single' => true, + 'object_subtype' => 'post', + 'default' => 'wibble', + ), + 'wibble', + ), + array( + array( + 'show_in_rest' => true, + 'single' => false, + 'object_subtype' => 'post', + 'default' => 'wibble', + ), + array( 'wibble' ), + ), + array( + array( + 'single' => true, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'default' => array( 'wibble' => 'dibble' ), + ), + array( 'wibble' => 'dibble' ), + ), + array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'single' => false, + 'default' => array( 'wibble' => 'dibble' ), + ), + array( array( 'wibble' => 'dibble' ) ), + ), + + array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => true, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + array( 'dibble' ), + ), + array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => false, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + array( array( 'dibble' ) ), + ), + 'array of objects' => array( + array( + 'type' => 'array', + 'single' => true, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'object', + 'properties' => array( + 'name' => array( + 'type' => 'string', + ), + ), + ), + ), + ), + 'default' => array( + array( + 'name' => 'Kirk', + ), + ), + ), + array( + array( + 'name' => 'Kirk', + ), + ), + ), + ); + } + + /** + * @ticket 43941 + */ + public function test_set_default_in_schema() { + register_post_meta( + 'post', + 'greeting', + array( + 'type' => 'string', + 'single' => true, + 'show_in_rest' => array( + 'schema' => array( + 'default' => 'Hello World', + ), + ), + ) + ); + + $response = rest_do_request( '/wp/v2/posts/' . self::$post_id ); + $this->assertEquals( 'Hello World', $response->get_data()['meta']['greeting'] ); + } + + /** + * @ticket 43941 + */ + public function test_default_is_added_to_schema() { + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); + $response = rest_do_request( $request ); + + $schema = $response->get_data()['schema']['properties']['meta']['properties']['with_default']; + $this->assertArrayHasKey( 'default', $schema ); + $this->assertEquals( 'Goodnight Moon', $schema['default'] ); + } + /** * Internal function used to disable an insert query which * will trigger a wpdb error for testing purposes. diff --git a/tests/phpunit/tests/rest-api/rest-term-meta-fields.php b/tests/phpunit/tests/rest-api/rest-term-meta-fields.php index 51e392edc8..e266589468 100644 --- a/tests/phpunit/tests/rest-api/rest-term-meta-fields.php +++ b/tests/phpunit/tests/rest-api/rest-term-meta-fields.php @@ -1255,6 +1255,36 @@ class WP_Test_REST_Term_Meta_Fields extends WP_Test_REST_TestCase { return $data; } + /** + * @ticket 43941 + */ + public function test_get_default_value() { + $meta_key = 'registered_key1'; + register_term_meta( + 'category', + $meta_key, + array( + 'single' => true, + 'type' => 'string', + 'default' => 'Goodbye', + 'show_in_rest' => true, + ) + ); + + // Check for default value. + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertEquals( 200, $response->get_status() ); + + $data = $response->get_data(); + $this->assertArrayHasKey( 'meta', $data ); + + $meta = (array) $data['meta']; + $this->assertArrayHasKey( $meta_key, $meta ); + $this->assertSame( 'Goodbye', $meta[ $meta_key ] ); + } + /** * Internal function used to disable an insert query which * will trigger a wpdb error for testing purposes. diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index db39ae16f6..6132813268 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -2890,6 +2890,125 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); } + /** + * @ticket 43941 + * @dataProvider data_get_default_data + */ + public function test_get_default_value( $args, $expected ) { + wp_set_current_user( self::$user ); + + $object_type = 'user'; + $meta_key = 'registered_key1'; + register_meta( + $object_type, + $meta_key, + $args + ); + + // Check for default value. + $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertEquals( 200, $response->get_status() ); + + $data = $response->get_data(); + $this->assertArrayHasKey( 'meta', $data ); + + $meta = (array) $data['meta']; + $this->assertArrayHasKey( $meta_key, $meta ); + $this->assertEquals( $expected, $meta[ $meta_key ] ); + } + + public function data_get_default_data() { + return array( + array( + array( + 'show_in_rest' => true, + 'single' => true, + 'default' => 'wibble', + ), + 'wibble', + ), + array( + array( + 'show_in_rest' => true, + 'single' => false, + 'default' => 'wibble', + ), + array( 'wibble' ), + ), + array( + array( + 'single' => true, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'default' => array( 'wibble' => 'dibble' ), + ), + array( 'wibble' => 'dibble' ), + ), + array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'wibble' => array( + 'type' => 'string', + ), + ), + ), + ), + 'type' => 'object', + 'single' => false, + 'default' => array( 'wibble' => 'dibble' ), + ), + array( array( 'wibble' => 'dibble' ) ), + ), + + array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => true, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + array( 'dibble' ), + ), + array( + array( + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'single' => false, + 'type' => 'array', + 'default' => array( 'dibble' ), + ), + array( array( 'dibble' ) ), + ), + ); + } + public function additional_field_get_callback( $object ) { return get_user_meta( $object['id'], 'my_custom_int', true ); }