mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 12:50:28 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@48402 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -2890,6 +2890,125 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
$this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 43941
|
||||
* @dataProvider data_get_default_data
|
||||
*/
|
||||
public function test_get_default_value( $args, $expected ) {
|
||||
wp_set_current_user( self::$user );
|
||||
|
||||
$object_type = 'user';
|
||||
$meta_key = 'registered_key1';
|
||||
register_meta(
|
||||
$object_type,
|
||||
$meta_key,
|
||||
$args
|
||||
);
|
||||
|
||||
// Check for default value.
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'meta', $data );
|
||||
|
||||
$meta = (array) $data['meta'];
|
||||
$this->assertArrayHasKey( $meta_key, $meta );
|
||||
$this->assertEquals( $expected, $meta[ $meta_key ] );
|
||||
}
|
||||
|
||||
public function data_get_default_data() {
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'show_in_rest' => true,
|
||||
'single' => true,
|
||||
'default' => 'wibble',
|
||||
),
|
||||
'wibble',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'show_in_rest' => true,
|
||||
'single' => false,
|
||||
'default' => 'wibble',
|
||||
),
|
||||
array( 'wibble' ),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'single' => true,
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'wibble' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'object',
|
||||
'default' => array( 'wibble' => 'dibble' ),
|
||||
),
|
||||
array( 'wibble' => 'dibble' ),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'wibble' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'object',
|
||||
'single' => false,
|
||||
'default' => array( 'wibble' => 'dibble' ),
|
||||
),
|
||||
array( array( 'wibble' => 'dibble' ) ),
|
||||
),
|
||||
|
||||
array(
|
||||
array(
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
),
|
||||
'single' => true,
|
||||
'type' => 'array',
|
||||
'default' => array( 'dibble' ),
|
||||
),
|
||||
array( 'dibble' ),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
),
|
||||
'single' => false,
|
||||
'type' => 'array',
|
||||
'default' => array( 'dibble' ),
|
||||
),
|
||||
array( array( 'dibble' ) ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function additional_field_get_callback( $object ) {
|
||||
return get_user_meta( $object['id'], 'my_custom_int', true );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user