Index: src/wp-includes/meta.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/meta.php	(revision 3d31284c5e658f1fd31b5758b4a18a27778001cb)
+++ src/wp-includes/meta.php	(date 1591267015452)
@@ -205,8 +205,8 @@
 
 	// 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;
 			}
@@ -473,9 +473,9 @@
 }
 
 /**
- * Retrieves metadata for the specified object.
+ * Retrieves raw metadata for the specified object.
  *
- * @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.
@@ -486,7 +486,7 @@
  *                          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 ) {
+function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
 	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
 		return false;
 	}
@@ -543,11 +543,74 @@
 		}
 	}
 
+	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 ) {
-		return '';
+		$value = '';
 	} else {
-		return array();
+		$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;
 }
 
 /**
@@ -1139,6 +1202,7 @@
  *     @type string     $type              The type of data associated with this meta key.
  *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  *     @type string     $description       A description of the data attached to this meta key.
+ *     @type mixed      $default           Default value when calling `get_metadata()`.
  *     @type bool       $single            Whether the meta key has one value per object, or an array of values per object.
  *     @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,
@@ -1165,6 +1229,7 @@
 		'object_subtype'    => '',
 		'type'              => 'string',
 		'description'       => '',
+		'default'           => '',
 		'single'            => false,
 		'sanitize_callback' => null,
 		'auth_callback'     => null,
@@ -1202,6 +1267,7 @@
 	 * @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.
@@ -1241,6 +1307,15 @@
 		}
 	}
 
+	if ( array_key_exists( 'default', $args ) ) {
+		if ( false === $args['single'] && ! wp_is_numeric_array( $args['default'] ) ) {
+			$args['default'] = array( $args['default'] );
+		}
+		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'] );
@@ -1473,3 +1548,67 @@
 	 */
 	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 ( 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 ) {
+		if ( $metadata['single'] ) {
+			$value = $metadata['default'];
+		} else {
+			$value = $metadata['default'][0];
+		}
+	} else {
+		$value = $metadata['default'];
+	}
+
+	return $value;
+}
Index: tests/phpunit/tests/meta/registerMeta.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- tests/phpunit/tests/meta/registerMeta.php	(revision 3d31284c5e658f1fd31b5758b4a18a27778001cb)
+++ tests/phpunit/tests/meta/registerMeta.php	(date 1591266988516)
@@ -504,6 +504,51 @@
 		$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 );
+
+	}
+
 	public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) {
 		if ( 1 === ( $object_id % 2 ) ) {
 			return 'odd';
@@ -512,6 +557,183 @@
 		return 'even';
 	}
 
+	public function data_get_default_data() {
+		return array(
+			array(
+				array(
+					'single'  => true,
+					'default' => 'wibble',
+				),
+				true,
+				'wibble',
+			),
+			array(
+				array(
+					'single'  => true,
+					'default' => 'wibble',
+				),
+				false,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'  => true,
+					'default' => array( 'wibble' ),
+				),
+				true,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'  => true,
+					'default' => array( 'wibble' ),
+				),
+				false,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'  => false,
+					'default' => 'wibble',
+				),
+				true,
+				'wibble',
+			),
+			array(
+				array(
+					'single'  => false,
+					'default' => 'wibble',
+				),
+				false,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'  => false,
+					'default' => array( 'wibble' ),
+				),
+				true,
+				'wibble',
+			),
+			array(
+				array(
+					'single'  => false,
+					'default' => array( 'wibble' ),
+				),
+				false,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'page',
+					'default'        => 'wibble',
+				),
+				true,
+				'wibble',
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'page',
+					'default'        => 'wibble',
+				),
+				false,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'page',
+					'default'        => array( 'wibble' ),
+				),
+				true,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'page',
+					'default'        => array( 'wibble' ),
+				),
+				false,
+				array( 'wibble' ),
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'post',
+					'default'        => 'wibble',
+				),
+				true,
+				'',
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'post',
+					'default'        => 'wibble',
+				),
+				false,
+				array(),
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'post',
+					'default'        => array( 'wibble' ),
+				),
+				true,
+				'',
+			),
+			array(
+				array(
+					'single'         => true,
+					'object_subtype' => 'post',
+					'default'        => array( 'wibble' ),
+				),
+				false,
+				array(),
+			),
+			array(
+				array(
+					'single'  => true,
+					'default' => array( 'wibble' => 'dibble' ),
+				),
+				true,
+				array( 'wibble' => 'dibble' ),
+			),
+			array(
+				array(
+					'single'  => true,
+					'default' => array( 'wibble' => 'dibble' ),
+				),
+				false,
+				array(
+					array( 'wibble' => 'dibble' ),
+				),
+			),
+			array(
+				array(
+					'single'  => false,
+					'default' => array( 'wibble' => 'dibble' ),
+				),
+				true,
+				array( 'wibble' => 'dibble' ),
+			),
+			array(
+				array(
+					'single'  => false,
+					'default' => array( 'wibble' => 'dibble' ),
+				),
+				false,
+				array(
+					array( 'wibble' => 'dibble' ),
+				),
+			),
+		);
+	}
+
 	public function data_get_types_and_subtypes() {
 		return array(
 			array( 'post', 'page' ),
