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

@@ -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' ),
);
}
}