<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[48402] trunk: REST API, Meta: Introduce support for default metadata values.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/48402">48402</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/48402","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>TimothyBlynJacobs</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-07-07 20:45:55 +0000 (Tue, 07 Jul 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API, Meta: Introduce support for default metadata values.

The `register_meta()` API now officially supports specifying a default metadata value. When `get_metadata()` is called for a meta key that does not yet exist for the object, this default value will be returned instead of an empty string.

A new function is introduced `get_metadata_raw` to retrieve the raw metadata value from the database, without applying the registered default.

Props spacedmonkey, flixos90, rmccue, kadamwhite, mnelson4, johnbillion, chrisvanpatten, TimothyBlynJacobs.
Fixes <a href="https://core.trac.wordpress.org/ticket/43941">#43941</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmetaphp">trunk/src/wp-includes/meta.php</a></li>
<li><a href="#trunksrcwpincludesrestapifieldsclasswprestmetafieldsphp">trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php</a></li>
<li><a href="#trunktestsphpunittestsmetaregisterMetaphp">trunk/tests/phpunit/tests/meta/registerMeta.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpostmetafieldsphp">trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiresttermmetafieldsphp">trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestuserscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-users-controller.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/meta.php    2020-07-07 20:36:41 UTC (rev 48401)
+++ trunk/src/wp-includes/meta.php      2020-07-07 20:45:55 UTC (rev 48402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -210,8 +210,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Compare existing value to new value if no prev value given and the key exists only once.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $prev_value ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $old_value = get_metadata( $meta_type, $object_id, $meta_key );
-               if ( count( $old_value ) == 1 ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
+               if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( $old_value[0] === $meta_value ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return false;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -485,7 +485,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * If the meta field does not exist, an empty string is returned if `$single` is true, or an empty array if it's false.
</span><span class="cx" style="display: block; padding: 0 10px">  * If there's a problem with the parameters passed to the function, boolean `false` is returned.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 2.9.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.5.0
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
</span><span class="cx" style="display: block; padding: 0 10px">  *                          or any other object type with an associated meta table.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -496,7 +496,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                          This parameter has no effect if meta_key is not specified. Default false.
</span><span class="cx" style="display: block; padding: 0 10px">  * @return mixed The metadata value or array of values. See description above for further details.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! $meta_type || ! is_numeric( $object_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -553,11 +553,74 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        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 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( $single ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $value = '';
</ins><span class="cx" style="display: block; padding: 0 10px">         } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $value = array();
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * 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;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1163,6 +1226,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type string     $description       A description of the data attached to this meta key.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type bool       $single            Whether the meta key has one value per object, or an array of values per object.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  *     @type string     $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type string     $auth_callback     Optional. A function or method to call when performing edit_post_meta,
</span><span class="cx" style="display: block; padding: 0 10px">  *                                         add_post_meta, and delete_post_meta capability checks.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1188,6 +1255,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'object_subtype'    => '',
</span><span class="cx" style="display: block; padding: 0 10px">                'type'              => 'string',
</span><span class="cx" style="display: block; padding: 0 10px">                'description'       => '',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'default'           => '',
</ins><span class="cx" style="display: block; padding: 0 10px">                 'single'            => false,
</span><span class="cx" style="display: block; padding: 0 10px">                'sanitize_callback' => null,
</span><span class="cx" style="display: block; padding: 0 10px">                'auth_callback'     => null,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1225,6 +1293,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $meta_key    Meta key.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        unset( $defaults['default'] );
</ins><span class="cx" style="display: block; padding: 0 10px">         $args = wp_parse_args( $args, $defaults );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Require an item schema when registering array meta.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1264,6 +1333,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        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 );
+               }
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
</span><span class="cx" style="display: block; padding: 0 10px">                unset( $args['object_subtype'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1496,3 +1583,64 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * 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;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesrestapifieldsclasswprestmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php       2020-07-07 20:36:41 UTC (rev 48401)
+++ trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php 2020-07-07 20:45:55 UTC (rev 48402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,7 +79,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $fields as $meta_key => $args ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $name       = $args['name'];
</span><span class="cx" style="display: block; padding: 0 10px">                        $all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px">                         if ( $args['single'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( empty( $all_values ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $value = $args['schema']['default'];
</span></span></pre></div>
<a id="trunktestsphpunittestsmetaregisterMetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/meta/registerMeta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/meta/registerMeta.php   2020-07-07 20:36:41 UTC (rev 48401)
+++ trunk/tests/phpunit/tests/meta/registerMeta.php     2020-07-07 20:45:55 UTC (rev 48402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -504,6 +504,73 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 'even', $subtype_for_4 );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @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 );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 1 === ( $object_id % 2 ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return 'odd';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -512,6 +579,501 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return 'even';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        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,
+                               '',
+                       ),
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function data_get_types_and_subtypes() {
</span><span class="cx" style="display: block; padding: 0 10px">                return array(
</span><span class="cx" style="display: block; padding: 0 10px">                        array( 'post', 'page' ),
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestpostmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php      2020-07-07 20:36:41 UTC (rev 48401)
+++ trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php        2020-07-07 20:45:55 UTC (rev 48402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -234,6 +234,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                register_post_meta(
+                       'post',
+                       'with_default',
+                       array(
+                               'type'         => 'string',
+                               'single'       => true,
+                               'show_in_rest' => true,
+                               'default'      => 'Goodnight Moon',
+                       )
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /** @var WP_REST_Server $wp_rest_server */
</span><span class="cx" style="display: block; padding: 0 10px">                global $wp_rest_server;
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_rest_server = new Spy_REST_Server;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2786,6 +2797,206 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @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'] );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Internal function used to disable an insert query which
</span><span class="cx" style="display: block; padding: 0 10px">         * will trigger a wpdb error for testing purposes.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiresttermmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php      2020-07-07 20:36:41 UTC (rev 48401)
+++ trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php        2020-07-07 20:45:55 UTC (rev 48402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1256,6 +1256,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @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 ] );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Internal function used to disable an insert query which
</span><span class="cx" style="display: block; padding: 0 10px">         * will trigger a wpdb error for testing purposes.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestuserscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-users-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-users-controller.php      2020-07-07 20:36:41 UTC (rev 48401)
+++ trunk/tests/phpunit/tests/rest-api/rest-users-controller.php        2020-07-07 20:45:55 UTC (rev 48402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2890,6 +2890,125 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @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' ) ),
+                       ),
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function additional_field_get_callback( $object ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return get_user_meta( $object['id'], 'my_custom_int', true );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre>
</div>
</div>

</body>
</html>