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:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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().
|
||||
*
|
||||
|
||||
@@ -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().
|
||||
*
|
||||
|
||||
@@ -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().
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user