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 #43941.


Built from https://develop.svn.wordpress.org/trunk@48402


git-svn-id: http://core.svn.wordpress.org/trunk@48171 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
TimothyBlynJacobs 2020-07-07 20:47:05 +00:00
parent 500508884e
commit 13b10697bf
3 changed files with 157 additions and 10 deletions

View File

@ -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. // Compare existing value to new value if no prev value given and the key exists only once.
if ( empty( $prev_value ) ) { if ( empty( $prev_value ) ) {
$old_value = get_metadata( $meta_type, $object_id, $meta_key ); $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
if ( count( $old_value ) == 1 ) { if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
if ( $old_value[0] === $meta_value ) { if ( $old_value[0] === $meta_value ) {
return false; 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 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. * 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', * @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. * 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. * 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. * @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 ) ) { if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false; return false;
} }
@ -553,11 +553,74 @@ function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false )
} }
} }
if ( $single ) { return null;
return ''; }
} else {
return array(); /**
* 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'. * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
* @type string $description A description of the data attached to this meta key. * @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 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 $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, * @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. * 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' => '', 'object_subtype' => '',
'type' => 'string', 'type' => 'string',
'description' => '', 'description' => '',
'default' => '',
'single' => false, 'single' => false,
'sanitize_callback' => null, 'sanitize_callback' => null,
'auth_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. * @param string $meta_key Meta key.
*/ */
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
unset( $defaults['default'] );
$args = wp_parse_args( $args, $defaults ); $args = wp_parse_args( $args, $defaults );
// Require an item schema when registering array meta. // 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. // 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 ) { if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
unset( $args['object_subtype'] ); 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 ); 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;
}

View File

@ -79,7 +79,6 @@ abstract class WP_REST_Meta_Fields {
foreach ( $fields as $meta_key => $args ) { foreach ( $fields as $meta_key => $args ) {
$name = $args['name']; $name = $args['name'];
$all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false ); $all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false );
if ( $args['single'] ) { if ( $args['single'] ) {
if ( empty( $all_values ) ) { if ( empty( $all_values ) ) {
$value = $args['schema']['default']; $value = $args['schema']['default'];

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.5-alpha-48401'; $wp_version = '5.5-alpha-48402';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.