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.



git-svn-id: https://develop.svn.wordpress.org/trunk@43378 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2018-06-21 21:06:50 +00:00
parent cdd9910f01
commit a830dbcab3
14 changed files with 1042 additions and 164 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,11 +292,32 @@ 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 );
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.
* 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.
*
@ -349,7 +334,10 @@ function map_meta_cap( $cap, $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 );
$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.
@ -357,11 +345,12 @@ function map_meta_cap( $cap, $user_id ) {
* 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, `$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.
@ -370,13 +359,12 @@ function map_meta_cap( $cap, $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 );
$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'] ) ) {
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'] ) ) {
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_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'] ) ) {
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'] ) ) {
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_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 );
if ( ! $data ) {
return array();
}
$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 ( ! empty( $object_subtype ) ) {
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
}
return $registered_data;
return array_intersect_key( $data, $meta_keys );
}
/**
@ -1231,14 +1294,69 @@ function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
* @return array Filtered arguments.
*/
function _wp_register_meta_args_whitelist( $args, $default_args ) {
$whitelist = array_keys( $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 ] );
}
}
return $args;
return array_intersect_key( $args, $default_args );
}
/**
* 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;
}
/**
* 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

@ -3,10 +3,24 @@
* @group meta
*/
class Tests_Meta_Register_Meta extends WP_UnitTestCase {
protected static $post_id;
protected static $term_id;
protected static $comment_id;
protected static $user_id;
public static function wpSetUpBeforeClass( $factory ) {
self::$post_id = $factory->post->create();
self::$post_id = $factory->post->create( array( 'post_type' => 'page' ) );
self::$term_id = $factory->term->create( array( 'taxonomy' => 'category' ) );
self::$comment_id = $factory->comment->create();
self::$user_id = $factory->user->create();
}
public static function wpTearDownAfterClass() {
wp_delete_post( self::$post_id, true );
wp_delete_term( self::$term_id, 'category' );
wp_delete_comment( self::$comment_id, true );
self::delete_user( self::$user_id );
}
public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) {
@ -74,6 +88,7 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
$expected = array(
'post' => array(
'' => array(
'flight_number' => array(
'type' => 'string',
'description' => '',
@ -83,9 +98,10 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
'show_in_rest' => false,
),
),
),
);
$this->assertEquals( $actual, $expected );
$this->assertEquals( $expected, $actual );
}
public function test_register_meta_with_term_object_type_populates_wp_meta_keys() {
@ -96,6 +112,7 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
$expected = array(
'term' => array(
'' => array(
'category_icon' => array(
'type' => 'string',
'description' => '',
@ -105,9 +122,10 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
'show_in_rest' => false,
),
),
),
);
$this->assertEquals( $actual, $expected );
$this->assertEquals( $expected, $actual );
}
public function test_register_meta_with_deprecated_sanitize_callback_does_not_populate_wp_meta_keys() {
@ -148,6 +166,7 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
$expected = array(
'post' => array(
'' => array(
'flight_number' => array(
'type' => 'string',
'description' => '',
@ -157,6 +176,7 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
'show_in_rest' => false,
),
),
),
);
$this->assertEquals( $actual, $expected );
}
@ -301,4 +321,190 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
$this->assertEmpty( $meta );
}
/**
* @ticket 38323
* @dataProvider data_get_types_and_subtypes
*/
public function test_register_meta_with_subtype_populates_wp_meta_keys( $type, $subtype ) {
global $wp_meta_keys;
register_meta( $type, 'flight_number', array( 'object_subtype' => $subtype ) );
$expected = array(
$type => array(
$subtype => array(
'flight_number' => array(
'type' => 'string',
'description' => '',
'single' => false,
'sanitize_callback' => null,
'auth_callback' => '__return_true',
'show_in_rest' => false,
),
),
),
);
$actual = $wp_meta_keys;
// Reset global so subsequent data tests do not get polluted.
$wp_meta_keys = array();
$this->assertEquals( $expected, $actual );
}
/**
* @ticket 38323
* @dataProvider data_get_types_and_subtypes
*/
public function test_unregister_meta_with_subtype_unpopulates_wp_meta_keys( $type, $subtype ) {
global $wp_meta_keys;
register_meta( $type, 'flight_number', array( 'object_subtype' => $subtype ) );
unregister_meta_key( $type, 'flight_number', $subtype );
$actual = $wp_meta_keys;
// Reset global so subsequent data tests do not get polluted.
$wp_meta_keys = array();
$this->assertEmpty( $actual );
}
/**
* @ticket 38323
* @dataProvider data_get_types_and_subtypes
*/
public function test_unregister_meta_without_subtype_keeps_subtype_meta_key( $type, $subtype ) {
global $wp_meta_keys;
register_meta( $type, 'flight_number', array( 'object_subtype' => $subtype ) );
// Unregister meta key without subtype.
unregister_meta_key( $type, 'flight_number' );
$expected = array(
$type => array(
$subtype => array(
'flight_number' => array(
'type' => 'string',
'description' => '',
'single' => false,
'sanitize_callback' => null,
'auth_callback' => '__return_true',
'show_in_rest' => false,
),
),
),
);
$actual = $wp_meta_keys;
// Reset global so subsequent data tests do not get polluted.
$wp_meta_keys = array();
$this->assertEquals( $expected, $actual );
}
/**
* @ticket 38323
* @dataProvider data_get_types_and_subtypes
*/
public function test_get_registered_meta_keys_with_subtype( $type, $subtype ) {
register_meta( $type, 'registered_key1', array( 'object_subtype' => $subtype ) );
register_meta( $type, 'registered_key2', array( 'object_subtype' => $subtype ) );
$meta_keys = get_registered_meta_keys( $type, $subtype );
$this->assertArrayHasKey( 'registered_key1', $meta_keys );
$this->assertArrayHasKey( 'registered_key2', $meta_keys );
$this->assertEmpty( get_registered_meta_keys( $type ) );
}
/**
* @ticket 38323
* @dataProvider data_get_types_and_subtypes
*/
public function test_get_registered_metadata_with_subtype( $type, $subtype ) {
register_meta( $type, 'registered_key1', array() );
// This will override the above registration for objects of $subtype.
register_meta( $type, 'registered_key1', array(
'object_subtype' => $subtype,
'single' => true,
) );
// For testing with $single => false.
register_meta( $type, 'registered_key2', array(
'object_subtype' => $subtype,
) );
// Register another meta key for a different subtype.
register_meta( $type, 'registered_key3', array(
'object_subtype' => 'other_subtype',
) );
$object_property_name = $type . '_id';
$object_id = self::$$object_property_name;
add_metadata( $type, $object_id, 'registered_key1', 'value1' );
add_metadata( $type, $object_id, 'registered_key2', 'value2' );
add_metadata( $type, $object_id, 'registered_key3', 'value3' );
$meta = get_registered_metadata( $type, $object_id );
$key1 = get_registered_metadata( $type, $object_id, 'registered_key1' );
$key2 = get_registered_metadata( $type, $object_id, 'registered_key2' );
$key3 = get_registered_metadata( $type, $object_id, 'registered_key3' );
$this->assertSame( array( 'registered_key1', 'registered_key2' ), array_keys( $meta ) );
$this->assertSame( 'value1', $meta['registered_key1'][0] );
$this->assertSame( 'value2', $meta['registered_key2'][0] );
$this->assertSame( 'value1', $key1 );
$this->assertSame( array( 'value2' ), $key2 );
$this->assertFalse( $key3 );
}
/**
* @ticket 38323
* @dataProvider data_get_types_and_subtypes
*/
public function test_get_object_subtype( $type, $expected_subtype ) {
$object_property_name = $type . '_id';
$object_id = self::$$object_property_name;
$this->assertSame( $expected_subtype, get_object_subtype( $type, $object_id ) );
}
/**
* @ticket 38323
*/
public function test_get_object_subtype_custom() {
add_filter( 'get_object_subtype_customtype', array( $this, 'filter_get_object_subtype_for_customtype' ), 10, 2 );
$subtype_for_3 = get_object_subtype( 'customtype', 3 );
$subtype_for_4 = get_object_subtype( 'customtype', 4 );
$this->assertSame( 'odd', $subtype_for_3 );
$this->assertSame( 'even', $subtype_for_4 );
}
public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) {
if ( $object_id % 2 === 1 ) {
return 'odd';
}
return 'even';
}
public function data_get_types_and_subtypes() {
return array(
array( 'post', 'page' ),
array( 'term', 'category' ),
array( 'comment', 'comment' ),
array( 'user', 'user' ),
);
}
}

View File

@ -6,6 +6,12 @@
*/
class Tests_Post_Meta extends WP_UnitTestCase {
private $last_register_meta_call = array(
'object_type' => '',
'meta_key' => '',
'args' => array(),
);
protected static $author;
protected static $post_id;
protected static $post_id_2;
@ -238,4 +244,65 @@ class Tests_Post_Meta extends WP_UnitTestCase {
$this->assertEquals( $funky_meta, get_post_meta( self::$post_id, 'test_funky_post_meta', true ) );
}
/**
* @ticket 38323
* @dataProvider data_register_post_meta
*/
public function test_register_post_meta( $post_type, $meta_key, $args ) {
add_filter( 'register_meta_args', array( $this, 'filter_register_meta_args_set_last_register_meta_call' ), 10, 4 );
register_post_meta( $post_type, $meta_key, $args );
$args['object_subtype'] = $post_type;
// Reset global so subsequent data tests do not get polluted.
$GLOBALS['wp_meta_keys'] = array();
$this->assertEquals( 'post', $this->last_register_meta_call['object_type'] );
$this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
$this->assertEquals( $args, $this->last_register_meta_call['args'] );
}
public function data_register_post_meta() {
return array(
array( 'post', 'registered_key1', array( 'single' => true ) ),
array( 'page', 'registered_key2', array() ),
array( '', 'registered_key3', array( 'sanitize_callback' => 'absint' ) ),
);
}
public function filter_register_meta_args_set_last_register_meta_call( $args, $defaults, $object_type, $meta_key ) {
$this->last_register_meta_call['object_type'] = $object_type;
$this->last_register_meta_call['meta_key'] = $meta_key;
$this->last_register_meta_call['args'] = $args;
return $args;
}
/**
* @ticket 38323
* @dataProvider data_unregister_post_meta
*/
public function test_unregister_post_meta( $post_type, $meta_key ) {
global $wp_meta_keys;
register_post_meta( $post_type, $meta_key, array() );
unregister_post_meta( $post_type, $meta_key );
$actual = $wp_meta_keys;
// Reset global so subsequent data tests do not get polluted.
$wp_meta_keys = array();
$this->assertEmpty( $actual );
}
public function data_unregister_post_meta() {
return array(
array( 'post', 'registered_key1' ),
array( 'page', 'registered_key2' ),
array( '', 'registered_key3' ),
);
}
}

View File

@ -12,15 +12,25 @@
class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
protected static $wp_meta_keys_saved;
protected static $post_id;
protected static $cpt_post_id;
public static function wpSetUpBeforeClass( $factory ) {
register_post_type( 'cpt', array(
'show_in_rest' => true,
'supports' => array( 'custom-fields' ),
) );
self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
self::$post_id = $factory->post->create();
self::$cpt_post_id = $factory->post->create( array( 'post_type' => 'cpt' ) );
}
public static function wpTearDownAfterClass() {
$GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved;
wp_delete_post( self::$post_id, true );
wp_delete_post( self::$cpt_post_id, true );
unregister_post_type( 'cpt' );
}
public function setUp() {
@ -120,6 +130,28 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
)
);
register_post_type( 'cpt', array(
'show_in_rest' => true,
'supports' => array( 'custom-fields' ),
) );
register_post_meta( 'cpt', 'test_cpt_single', array(
'show_in_rest' => true,
'single' => true,
) );
register_post_meta( 'cpt', 'test_cpt_multi', array(
'show_in_rest' => true,
'single' => false,
) );
// Register 'test_single' on subtype to override for bad auth.
register_post_meta( 'cpt', 'test_single', array(
'show_in_rest' => true,
'single' => true,
'auth_callback' => '__return_false',
) );
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = new Spy_REST_Server;
@ -1047,6 +1079,126 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
$this->assertArrayNotHasKey( 'test_no_type', $meta_schema );
}
/**
* @ticket 38323
* @dataProvider data_get_subtype_meta_value
*/
public function test_get_subtype_meta_value( $post_type, $meta_key, $single, $in_post_type ) {
$post_id = self::$post_id;
$endpoint = 'posts';
if ( 'cpt' === $post_type ) {
$post_id = self::$cpt_post_id;
$endpoint = 'cpt';
}
$meta_value = 'testvalue';
add_post_meta( $post_id, $meta_key, $meta_value );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/%s/%d', $endpoint, $post_id ) );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$this->assertInternalType( 'array', $data['meta'] );
if ( $in_post_type ) {
$expected_value = $meta_value;
if ( ! $single ) {
$expected_value = array( $expected_value );
}
$this->assertArrayHasKey( $meta_key, $data['meta'] );
$this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
} else {
$this->assertArrayNotHasKey( $meta_key, $data['meta'] );
}
}
public function data_get_subtype_meta_value() {
return array(
array( 'cpt', 'test_cpt_single', true, true ),
array( 'cpt', 'test_cpt_multi', false, true ),
array( 'cpt', 'test_single', true, true ),
array( 'cpt', 'test_multi', false, true ),
array( 'post', 'test_cpt_single', true, false ),
array( 'post', 'test_cpt_multi', false, false ),
array( 'post', 'test_single', true, true ),
array( 'post', 'test_multi', false, true ),
);
}
/**
* @ticket 38323
* @dataProvider data_set_subtype_meta_value
*/
public function test_set_subtype_meta_value( $post_type, $meta_key, $single, $in_post_type, $can_write ) {
$post_id = self::$post_id;
$endpoint = 'posts';
if ( 'cpt' === $post_type ) {
$post_id = self::$cpt_post_id;
$endpoint = 'cpt';
}
$meta_value = 'value_to_set';
$this->grant_write_permission();
$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/%s/%d', $endpoint, $post_id ) );
$request->set_body_params( array(
'meta' => array(
$meta_key => $meta_value,
),
) );
$response = rest_get_server()->dispatch( $request );
if ( ! $can_write ) {
$this->assertEquals( 403, $response->get_status() );
$this->assertEmpty( get_post_meta( $post_id, $meta_key, $single ) );
return;
}
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$this->assertInternalType( 'array', $data['meta'] );
if ( $in_post_type ) {
$expected_value = $meta_value;
if ( ! $single ) {
$expected_value = array( $expected_value );
}
$this->assertEquals( $expected_value, get_post_meta( $post_id, $meta_key, $single ) );
$this->assertArrayHasKey( $meta_key, $data['meta'] );
$this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
} else {
$this->assertEmpty( get_post_meta( $post_id, $meta_key, $single ) );
$this->assertArrayNotHasKey( $meta_key, $data['meta'] );
}
}
public function data_set_subtype_meta_value() {
$data = $this->data_get_subtype_meta_value();
foreach ( $data as $index => $dataset ) {
$can_write = true;
// This combination is not writable because of an auth callback of '__return_false'.
if ( 'cpt' === $dataset[0] && 'test_single' === $dataset[1] ) {
$can_write = false;
}
$data[ $index ][] = $can_write;
}
return $data;
}
/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.

View File

@ -12,15 +12,24 @@
class WP_Test_REST_Term_Meta_Fields extends WP_Test_REST_TestCase {
protected static $wp_meta_keys_saved;
protected static $category_id;
protected static $customtax_term_id;
public static function wpSetUpBeforeClass( $factory ) {
register_taxonomy( 'customtax', 'post', array(
'show_in_rest' => true,
) );
self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
self::$category_id = $factory->category->create();
self::$customtax_term_id = $factory->term->create( array( 'taxonomy' => 'customtax' ) );
}
public static function wpTearDownAfterClass() {
$GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved;
wp_delete_term( self::$category_id, 'category' );
wp_delete_term( self::$customtax_term_id, 'customtax' );
unregister_taxonomy( 'customtax' );
}
public function setUp() {
@ -120,6 +129,27 @@ class WP_Test_REST_Term_Meta_Fields extends WP_Test_REST_TestCase {
)
);
register_taxonomy( 'customtax', 'post', array(
'show_in_rest' => true,
) );
register_term_meta( 'customtax', 'test_customtax_single', array(
'show_in_rest' => true,
'single' => true,
) );
register_term_meta( 'customtax', 'test_customtax_multi', array(
'show_in_rest' => true,
'single' => false,
) );
// Register 'test_single' on subtype to override for bad auth.
register_term_meta( 'customtax', 'test_single', array(
'show_in_rest' => true,
'single' => true,
'auth_callback' => '__return_false',
) );
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = new Spy_REST_Server;
@ -1047,6 +1077,126 @@ class WP_Test_REST_Term_Meta_Fields extends WP_Test_REST_TestCase {
$this->assertArrayNotHasKey( 'test_no_type', $meta_schema );
}
/**
* @ticket 38323
* @dataProvider data_get_subtype_meta_value
*/
public function test_get_subtype_meta_value( $taxonomy, $meta_key, $single, $in_taxonomy ) {
$term_id = self::$category_id;
$endpoint = 'categories';
if ( 'customtax' === $taxonomy ) {
$term_id = self::$customtax_term_id;
$endpoint = 'customtax';
}
$meta_value = 'testvalue';
add_term_meta( $term_id, $meta_key, $meta_value );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/%s/%d', $endpoint, $term_id ) );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$this->assertInternalType( 'array', $data['meta'] );
if ( $in_taxonomy ) {
$expected_value = $meta_value;
if ( ! $single ) {
$expected_value = array( $expected_value );
}
$this->assertArrayHasKey( $meta_key, $data['meta'] );
$this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
} else {
$this->assertArrayNotHasKey( $meta_key, $data['meta'] );
}
}
public function data_get_subtype_meta_value() {
return array(
array( 'customtax', 'test_customtax_single', true, true ),
array( 'customtax', 'test_customtax_multi', false, true ),
array( 'customtax', 'test_single', true, true ),
array( 'customtax', 'test_multi', false, true ),
array( 'category', 'test_customtax_single', true, false ),
array( 'category', 'test_customtax_multi', false, false ),
array( 'category', 'test_single', true, true ),
array( 'category', 'test_multi', false, true ),
);
}
/**
* @ticket 38323
* @dataProvider data_set_subtype_meta_value
*/
public function test_set_subtype_meta_value( $taxonomy, $meta_key, $single, $in_taxonomy, $can_write ) {
$term_id = self::$category_id;
$endpoint = 'categories';
if ( 'customtax' === $taxonomy ) {
$term_id = self::$customtax_term_id;
$endpoint = 'customtax';
}
$meta_value = 'value_to_set';
$this->grant_write_permission();
$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/%s/%d', $endpoint, $term_id ) );
$request->set_body_params( array(
'meta' => array(
$meta_key => $meta_value,
),
) );
$response = rest_get_server()->dispatch( $request );
if ( ! $can_write ) {
$this->assertEquals( 403, $response->get_status() );
$this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) );
return;
}
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$this->assertInternalType( 'array', $data['meta'] );
if ( $in_taxonomy ) {
$expected_value = $meta_value;
if ( ! $single ) {
$expected_value = array( $expected_value );
}
$this->assertEquals( $expected_value, get_term_meta( $term_id, $meta_key, $single ) );
$this->assertArrayHasKey( $meta_key, $data['meta'] );
$this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
} else {
$this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) );
$this->assertArrayNotHasKey( $meta_key, $data['meta'] );
}
}
public function data_set_subtype_meta_value() {
$data = $this->data_get_subtype_meta_value();
foreach ( $data as $index => $dataset ) {
$can_write = true;
// This combination is not writable because of an auth callback of '__return_false'.
if ( 'customtax' === $dataset[0] && 'test_single' === $dataset[1] ) {
$can_write = false;
}
$data[ $index ][] = $can_write;
}
return $data;
}
/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.

View File

@ -6,6 +6,13 @@
* @ticket 10142
*/
class Tests_Term_Meta extends WP_UnitTestCase {
private $last_register_meta_call = array(
'object_type' => '',
'meta_key' => '',
'args' => array(),
);
public function setUp() {
parent::setUp();
register_taxonomy( 'wptests_tax', 'post' );
@ -463,4 +470,65 @@ class Tests_Term_Meta extends WP_UnitTestCase {
public static function set_cache_results( $q ) {
$q->set( 'cache_results', true );
}
/**
* @ticket 38323
* @dataProvider data_register_term_meta
*/
public function test_register_term_meta( $taxonomy, $meta_key, $args ) {
add_filter( 'register_meta_args', array( $this, 'filter_register_meta_args_set_last_register_meta_call' ), 10, 4 );
register_term_meta( $taxonomy, $meta_key, $args );
$args['object_subtype'] = $taxonomy;
// Reset global so subsequent data tests do not get polluted.
$GLOBALS['wp_meta_keys'] = array();
$this->assertEquals( 'term', $this->last_register_meta_call['object_type'] );
$this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
$this->assertEquals( $args, $this->last_register_meta_call['args'] );
}
public function data_register_term_meta() {
return array(
array( 'wptests_tax', 'registered_key1', array( 'single' => true ) ),
array( 'category', 'registered_key2', array() ),
array( '', 'registered_key3', array( 'sanitize_callback' => 'absint' ) ),
);
}
public function filter_register_meta_args_set_last_register_meta_call( $args, $defaults, $object_type, $meta_key ) {
$this->last_register_meta_call['object_type'] = $object_type;
$this->last_register_meta_call['meta_key'] = $meta_key;
$this->last_register_meta_call['args'] = $args;
return $args;
}
/**
* @ticket 38323
* @dataProvider data_unregister_term_meta
*/
public function test_unregister_term_meta( $taxonomy, $meta_key ) {
global $wp_meta_keys;
register_term_meta( $taxonomy, $meta_key, array() );
unregister_term_meta( $taxonomy, $meta_key );
$actual = $wp_meta_keys;
// Reset global so subsequent data tests do not get polluted.
$wp_meta_keys = array();
$this->assertEmpty( $actual );
}
public function data_unregister_term_meta() {
return array(
array( 'wptests_tax', 'registered_key1' ),
array( 'category', 'registered_key2' ),
array( '', 'registered_key3' ),
);
}
}