REST API: Support meta registration for specific object subtypes.

Introduce an `object_subtype` argument to the args array for `register_meta()` which can be used to limit meta registration to a single subtype (e.g. a custom post type or taxonomy, vs all posts or taxonomies).

Introduce `register_post_meta()` and `register_term_meta()` wrapper methods for `register_meta` to provide a convenient interface for the common case of registering meta for a specific taxonomy or post type. These methods work the way plugin developers have often expected `register_meta` to function, and should be used in place of direct `register_meta` where possible.

Props flixos90, tharsheblows, spacedmonkey.
Fixes #38323.


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


git-svn-id: http://core.svn.wordpress.org/trunk@43206 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
K. Adam White 2018-06-28 02:43:21 +00:00
parent 0db068da94
commit a89c86c711
10 changed files with 376 additions and 141 deletions

View File

@ -281,45 +281,9 @@ function map_meta_cap( $cap, $user_id ) {
list( $_, $object_type, $_ ) = explode( '_', $cap );
$object_id = (int) $args[0];
switch ( $object_type ) {
case 'post':
$post = get_post( $object_id );
if ( ! $post ) {
break;
}
$object_subtype = get_object_subtype( $object_type, $object_id );
$sub_type = get_post_type( $post );
break;
case 'comment':
$comment = get_comment( $object_id );
if ( ! $comment ) {
break;
}
$sub_type = empty( $comment->comment_type ) ? 'comment' : $comment->comment_type;
break;
case 'term':
$term = get_term( $object_id );
if ( ! $term instanceof WP_Term ) {
break;
}
$sub_type = $term->taxonomy;
break;
case 'user':
$user = get_user_by( 'id', $object_id );
if ( ! $user ) {
break;
}
$sub_type = 'user';
break;
}
if ( empty( $sub_type ) ) {
if ( empty( $object_subtype ) ) {
$caps[] = 'do_not_allow';
break;
}
@ -328,55 +292,79 @@ function map_meta_cap( $cap, $user_id ) {
$meta_key = isset( $args[1] ) ? $args[1] : false;
$has_filter = has_filter( "auth_{$object_type}_meta_{$meta_key}" ) || has_filter( "auth_{$object_type}_{$sub_type}_meta_{$meta_key}" );
if ( $meta_key && $has_filter ) {
if ( $meta_key ) {
$allowed = ! is_protected_meta( $meta_key, $object_type );
/**
* Filters whether the user is allowed to edit meta for specific object types.
*
* Return true to have the mapped meta caps from `edit_{$object_type}` apply.
*
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
*
* @since 3.3.0 As `auth_post_meta_{$meta_key}`.
* @since 4.6.0
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", false, $meta_key, $object_id, $user_id, $cap, $caps );
if ( ! empty( $object_subtype ) && has_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
/**
* Filters whether the user is allowed to edit meta for specific object types/subtypes.
*
* Return true to have the mapped meta caps from `edit_{$object_type}` apply.
*
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
* The dynamic portion of the hook name, `$sub_type` refers to the object subtype being filtered.
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
*
* @since 4.6.0 As `auth_post_{$post_type}_meta_{$meta_key}`.
* @since 4.7.0
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters( "auth_{$object_type}_{$sub_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
/**
* Filters whether the user is allowed to edit a specific meta key of a specific object type and subtype.
*
* The dynamic portions of the hook name, `$object_type`, `$meta_key`,
* and `$object_subtype`, refer to the metadata object type (comment, post, term or user),
* the meta key value, and the object subtype respectively.
*
* @since 5.0.0
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
} else {
/**
* Filters whether the user is allowed to edit a specific meta key of a specific object type.
*
* Return true to have the mapped meta caps from `edit_{$object_type}` apply.
*
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
*
* @since 3.3.0 As `auth_post_meta_{$meta_key}`.
* @since 4.6.0
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
}
if ( ! empty( $object_subtype ) ) {
/**
* Filters whether the user is allowed to edit meta for specific object types/subtypes.
*
* Return true to have the mapped meta caps from `edit_{$object_type}` apply.
*
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
* The dynamic portion of the hook name, `$object_subtype` refers to the object subtype being filtered.
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
*
* @since 4.6.0 As `auth_post_{$post_type}_meta_{$meta_key}`.
* @since 4.7.0
* @deprecated 5.0.0 Use `auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}`
*
* @param bool $allowed Whether the user can add the object meta. Default false.
* @param string $meta_key The meta key.
* @param int $object_id Object ID.
* @param int $user_id User ID.
* @param string $cap Capability name.
* @param string[] $caps Array of the user's capabilities.
*/
$allowed = apply_filters_deprecated( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", array( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ), '5.0.0', "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" );
}
if ( ! $allowed ) {
$caps[] = $cap;
}
} elseif ( $meta_key && is_protected_meta( $meta_key, $object_type ) ) {
$caps[] = $cap;
}
break;
case 'edit_comment':

View File

@ -44,12 +44,14 @@ function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique =
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
$column = sanitize_key( $meta_type . '_id' );
// expected_slashed ($meta_key)
$meta_key = wp_unslash( $meta_key );
$meta_value = wp_unslash( $meta_value );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
/**
* Filters whether to add metadata of a specific type.
@ -165,6 +167,8 @@ function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
$column = sanitize_key( $meta_type . '_id' );
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
@ -173,7 +177,7 @@ function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_
$meta_key = wp_unslash( $meta_key );
$passed_value = $meta_value;
$meta_value = wp_unslash( $meta_value );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
/**
* Filters whether to update metadata of a specific type.
@ -666,9 +670,11 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key =
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
// Sanitize the meta
$_meta_value = $meta_value;
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
$meta_value = maybe_serialize( $meta_value );
// Format the data query arguments.
@ -968,6 +974,7 @@ function is_protected_meta( $meta_key, $meta_type = null ) {
* Sanitize meta value.
*
* @since 3.1.3
* @since 5.0.0 The `$object_subtype` parameter was added.
*
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value to sanitize.
@ -975,7 +982,26 @@ function is_protected_meta( $meta_key, $meta_type = null ) {
*
* @return mixed Sanitized $meta_value.
*/
function sanitize_meta( $meta_key, $meta_value, $object_type ) {
function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
/**
* Filters the sanitization of a specific meta key of a specific meta type and subtype.
*
* The dynamic portions of the hook name, `$object_type`, `$meta_key`,
* and `$object_subtype`, refer to the metadata object type (comment, post, term or user),
* the meta key value, and the object subtype respectively.
*
* @since 5.0.0
*
* @param mixed $meta_value Meta value to sanitize.
* @param string $meta_key Meta key.
* @param string $object_type Object type.
* @param string $object_subtype Object subtype.
*/
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
}
/**
* Filters the sanitization of a specific meta key of a specific meta type.
*
@ -995,16 +1021,26 @@ function sanitize_meta( $meta_key, $meta_value, $object_type ) {
/**
* Registers a meta key.
*
* It is recommended to register meta keys for a specific combination of object type and object subtype. If passing
* an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly
* overridden in case a more specific meta key of the same name exists for the same object type and a subtype.
*
* If an object type does not support any subtypes, such as users or comments, you should commonly call this function
* without passing a subtype.
*
* @since 3.3.0
* @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified
* to support an array of data to attach to registered meta keys}. Previous arguments for
* `$sanitize_callback` and `$auth_callback` have been folded into this array.
* @since 5.0.0 The `$object_subtype` argument was added to the arguments array.
*
* @param string $object_type Type of object this meta is registered to.
* @param string $meta_key Meta key to register.
* @param array $args {
* Data used to describe the meta key when registered.
*
* @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty,
* the meta key will be registered on the entire object type. Default empty.
* @type string $type The type of data associated with this meta key.
* Valid values are 'string', 'boolean', 'integer', and 'number'.
* @type string $description A description of the data attached to this meta key.
@ -1027,6 +1063,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
}
$defaults = array(
'object_subtype' => '',
'type' => 'string',
'description' => '',
'single' => false,
@ -1067,6 +1104,8 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
$args = wp_parse_args( $args, $defaults );
$object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
// If `auth_callback` is not provided, fall back to `is_protected_meta()`.
if ( empty( $args['auth_callback'] ) ) {
if ( is_protected_meta( $meta_key, $object_type ) ) {
@ -1078,16 +1117,26 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
// Back-compat: old sanitize and auth callbacks are applied to all of an object type.
if ( is_callable( $args['sanitize_callback'] ) ) {
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
if ( ! empty( $object_subtype ) ) {
add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
} else {
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
}
}
if ( is_callable( $args['auth_callback'] ) ) {
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
if ( ! empty( $object_subtype ) ) {
add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
} else {
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
}
}
// 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 ) {
$wp_meta_keys[ $object_type ][ $meta_key ] = $args;
unset( $args['object_subtype'] );
$wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
return true;
}
@ -1099,59 +1148,63 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
* Checks if a meta key is registered.
*
* @since 4.6.0
* @since 5.0.0 The `$object_subtype` parameter was added.
*
* @param string $object_type The type of object.
* @param string $meta_key The meta key.
* @param string $object_subtype Optional. The subtype of the object type.
*
* @return bool True if the meta key is registered to the object type. False if not.
* @return bool True if the meta key is registered to the object type and, if provided,
* the object subtype. False if not.
*/
function registered_meta_key_exists( $object_type, $meta_key ) {
global $wp_meta_keys;
function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
if ( ! is_array( $wp_meta_keys ) ) {
return false;
}
if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
return false;
}
if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) {
return true;
}
return false;
return isset( $meta_keys[ $meta_key ] );
}
/**
* Unregisters a meta key from the list of registered keys.
*
* @since 4.6.0
* @since 5.0.0 The `$object_subtype` parameter was added.
*
* @param string $object_type The type of object.
* @param string $meta_key The meta key.
* @param string $object_type The type of object.
* @param string $meta_key The meta key.
* @param string $object_subtype Optional. The subtype of the object type.
* @return bool True if successful. False if the meta key was not registered.
*/
function unregister_meta_key( $object_type, $meta_key ) {
function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
global $wp_meta_keys;
if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
return false;
}
$args = $wp_meta_keys[ $object_type ][ $meta_key ];
$args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
if ( ! empty( $object_subtype ) ) {
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] );
} else {
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
}
}
if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
if ( ! empty( $object_subtype ) ) {
remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] );
} else {
remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
}
}
unset( $wp_meta_keys[ $object_type ][ $meta_key ] );
unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
// Do some clean up
if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
}
if ( empty( $wp_meta_keys[ $object_type ] ) ) {
unset( $wp_meta_keys[ $object_type ] );
}
@ -1163,23 +1216,28 @@ function unregister_meta_key( $object_type, $meta_key ) {
* Retrieves a list of registered meta keys for an object type.
*
* @since 4.6.0
* @since 5.0.0 The `$object_subtype` parameter was added.
*
* @param string $object_type The type of object. Post, comment, user, term.
* @param string $object_type The type of object. Post, comment, user, term.
* @param string $object_subtype Optional. The subtype of the object type.
* @return array List of registered meta keys.
*/
function get_registered_meta_keys( $object_type ) {
function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
global $wp_meta_keys;
if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) ) {
if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
return array();
}
return $wp_meta_keys[ $object_type ];
return $wp_meta_keys[ $object_type ][ $object_subtype ];
}
/**
* Retrieves registered metadata for a specified object.
*
* The results include both meta that is registered specifically for the
* object's subtype and meta that is registered for the entire object type.
*
* @since 4.6.0
*
* @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
@ -1187,14 +1245,21 @@ function get_registered_meta_keys( $object_type ) {
* @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
* metadata for the specified object.
* @return mixed A single value or array of values for a key if specified. An array of all registered keys
* and values for an object ID if not.
* and values for an object ID if not. False if a given $meta_key is not registered.
*/
function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
$object_subtype = get_object_subtype( $object_type, $object_id );
if ( ! empty( $meta_key ) ) {
if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
$object_subtype = '';
}
if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
return false;
}
$meta_keys = get_registered_meta_keys( $object_type );
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
$meta_key_data = $meta_keys[ $meta_key ];
$data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
@ -1203,18 +1268,16 @@ function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
}
$data = get_metadata( $object_type, $object_id );
$meta_keys = get_registered_meta_keys( $object_type );
$registered_data = array();
// Someday, array_filter()
foreach ( $meta_keys as $k => $v ) {
if ( isset( $data[ $k ] ) ) {
$registered_data[ $k ] = $data[ $k ];
}
if ( ! $data ) {
return array();
}
return $registered_data;
$meta_keys = get_registered_meta_keys( $object_type );
if ( ! empty( $object_subtype ) ) {
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
}
return array_intersect_key( $data, $meta_keys );
}
/**
@ -1223,22 +1286,77 @@ function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
* to be explicitly turned off is a warranty seal of sorts.
*
* @access private
* @since 4.6.0
* @since 4.6.0
*
* @param array $args Arguments from `register_meta()`.
* @param array $default_args Default arguments for `register_meta()`.
* @param array $args Arguments from `register_meta()`.
* @param array $default_args Default arguments for `register_meta()`.
*
* @return array Filtered arguments.
*/
function _wp_register_meta_args_whitelist( $args, $default_args ) {
$whitelist = array_keys( $default_args );
return array_intersect_key( $args, $default_args );
}
// In an anonymous function world, this would be better as an array_filter()
foreach ( $args as $key => $value ) {
if ( ! in_array( $key, $whitelist ) ) {
unset( $args[ $key ] );
}
/**
* Returns the object subtype for a given object ID of a specific type.
*
* @since 5.0.0
*
* @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
* @param int $object_id ID of the object to retrieve its subtype.
* @return string The object subtype or an empty string if unspecified subtype.
*/
function get_object_subtype( $object_type, $object_id ) {
$object_id = (int) $object_id;
$object_subtype = '';
switch ( $object_type ) {
case 'post':
$post_type = get_post_type( $object_id );
if ( ! empty( $post_type ) ) {
$object_subtype = $post_type;
}
break;
case 'term':
$term = get_term( $object_id );
if ( ! $term instanceof WP_Term ) {
break;
}
$object_subtype = $term->taxonomy;
break;
case 'comment':
$comment = get_comment( $object_id );
if ( ! $comment ) {
break;
}
$object_subtype = 'comment';
break;
case 'user':
$user = get_user_by( 'id', $object_id );
if ( ! $user ) {
break;
}
$object_subtype = 'user';
break;
}
return $args;
/**
* Filters the object subtype identifier for a non standard object type.
*
* The dynamic portion of the hook, `$object_type`, refers to the object
* type (post, comment, term, or user).
*
* @since 5.0.0
*
* @param string $object_subtype Empty string to override.
* @param int $object_id ID of the object to get the subtype for.
*/
return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
}

View File

@ -1989,6 +1989,39 @@ function delete_post_meta_by_key( $post_meta_key ) {
return $deleted;
}
/**
* Registers a meta key for posts.
*
* @since 5.0.0
*
* @param string $post_type Post type to register a meta key for. Pass an empty string
* to register the meta key across all existing post types.
* @param string $meta_key The meta key to register.
* @param array $args Data used to describe the meta key when registered. See
* {@see register_meta()} for a list of supported arguments.
* @return bool True if the meta key was successfully registered, false if not.
*/
function register_post_meta( $post_type, $meta_key, array $args ) {
$args['object_subtype'] = $post_type;
return register_meta( 'post', $meta_key, $args );
}
/**
* Unregisters a meta key for posts.
*
* @since 5.0.0
*
* @param string $post_type Post type the meta key is currently registered for. Pass
* an empty string if the meta key is registered across all
* existing post types.
* @param string $meta_key The meta key to unregister.
* @return bool True on success, false if the meta key was not previously registered.
*/
function unregister_post_meta( $post_type, $meta_key ) {
return unregister_meta_key( 'post', $meta_key, $post_type );
}
/**
* Retrieve post meta fields, based on post ID.
*

View File

@ -27,6 +27,17 @@ class WP_REST_Comment_Meta_Fields extends WP_REST_Meta_Fields {
return 'comment';
}
/**
* Retrieves the object meta subtype.
*
* @since 5.0.0
*
* @return string 'comment' There are no subtypes.
*/
protected function get_meta_subtype() {
return 'comment';
}
/**
* Retrieves the type for register_rest_field() in the context of comments.
*

View File

@ -24,6 +24,17 @@ abstract class WP_REST_Meta_Fields {
*/
abstract protected function get_meta_type();
/**
* Retrieves the object meta subtype.
*
* @since 5.0.0
*
* @return string Subtype for the meta type, or empty string if no specific subtype.
*/
protected function get_meta_subtype() {
return '';
}
/**
* Retrieves the object type for register_rest_field().
*
@ -340,7 +351,15 @@ abstract class WP_REST_Meta_Fields {
protected function get_registered_fields() {
$registered = array();
foreach ( get_registered_meta_keys( $this->get_meta_type() ) as $name => $args ) {
$meta_type = $this->get_meta_type();
$meta_subtype = $this->get_meta_subtype();
$meta_keys = get_registered_meta_keys( $meta_type );
if ( ! empty( $meta_subtype ) ) {
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $meta_type, $meta_subtype ) );
}
foreach ( $meta_keys as $name => $args ) {
if ( empty( $args['show_in_rest'] ) ) {
continue;
}

View File

@ -46,6 +46,17 @@ class WP_REST_Post_Meta_Fields extends WP_REST_Meta_Fields {
return 'post';
}
/**
* Retrieves the object meta subtype.
*
* @since 5.0.0
*
* @return string Subtype for the meta type, or empty string if no specific subtype.
*/
protected function get_meta_subtype() {
return $this->post_type;
}
/**
* Retrieves the type for register_rest_field().
*

View File

@ -46,6 +46,17 @@ class WP_REST_Term_Meta_Fields extends WP_REST_Meta_Fields {
return 'term';
}
/**
* Retrieves the object meta subtype.
*
* @since 5.0.0
*
* @return string Subtype for the meta type, or empty string if no specific subtype.
*/
protected function get_meta_subtype() {
return $this->taxonomy;
}
/**
* Retrieves the type for register_rest_field().
*

View File

@ -27,6 +27,17 @@ class WP_REST_User_Meta_Fields extends WP_REST_Meta_Fields {
return 'user';
}
/**
* Retrieves the object meta subtype.
*
* @since 5.0.0
*
* @return string 'user' There are no subtypes.
*/
protected function get_meta_subtype() {
return 'user';
}
/**
* Retrieves the type for register_rest_field().
*

View File

@ -1322,6 +1322,39 @@ function has_term_meta( $term_id ) {
return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, term_id FROM $wpdb->termmeta WHERE term_id = %d ORDER BY meta_key,meta_id", $term_id ), ARRAY_A );
}
/**
* Registers a meta key for terms.
*
* @since 5.0.0
*
* @param string $taxonomy Taxonomy to register a meta key for. Pass an empty string
* to register the meta key across all existing taxonomies.
* @param string $meta_key The meta key to register.
* @param array $args Data used to describe the meta key when registered. See
* {@see register_meta()} for a list of supported arguments.
* @return bool True if the meta key was successfully registered, false if not.
*/
function register_term_meta( $taxonomy, $meta_key, array $args ) {
$args['object_subtype'] = $taxonomy;
return register_meta( 'term', $meta_key, $args );
}
/**
* Unregisters a meta key for terms.
*
* @since 5.0.0
*
* @param string $taxonomy Taxonomy the meta key is currently registered for. Pass
* an empty string if the meta key is registered across all
* existing taxonomies.
* @param string $meta_key The meta key to unregister.
* @return bool True on success, false if the meta key was not previously registered.
*/
function unregister_term_meta( $taxonomy, $meta_key ) {
return unregister_meta_key( 'term', $meta_key, $taxonomy );
}
/**
* Determines whether a term exists.
*

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-43377';
$wp_version = '5.0-alpha-43378';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.