Merge pull request #13555 from woocommerce/fix-13526-rest-api
Update date usage in REST API v1 and v2
This commit is contained in:
commit
d2bd1a422c
|
@ -29,6 +29,45 @@ class WC_REST_Customers_Controller extends WC_REST_Customers_V1_Controller {
|
|||
*/
|
||||
protected $namespace = 'wc/v2';
|
||||
|
||||
/**
|
||||
* Get formatted item data.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Data $object WC_Data instance.
|
||||
* @return array
|
||||
*/
|
||||
protected function get_formatted_item_data( $object ) {
|
||||
$data = $object->get_data();
|
||||
$format_date = array( 'date_created', 'date_modified' );
|
||||
|
||||
// Format date values.
|
||||
foreach ( $format_date as $key ) {
|
||||
$datetime = $data[ $key ];
|
||||
$data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
|
||||
$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
|
||||
}
|
||||
|
||||
return array(
|
||||
'id' => $object->get_id(),
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => $data['email'],
|
||||
'first_name' => $data['first_name'],
|
||||
'last_name' => $data['last_name'],
|
||||
'role' => $data['role'],
|
||||
'username' => $data['username'],
|
||||
'billing' => $data['billing'],
|
||||
'shipping' => $data['shipping'],
|
||||
'is_paying_customer' => $data['is_paying_customer'],
|
||||
'orders_count' => $object->get_order_count(),
|
||||
'total_spent' => $object->get_total_spent(),
|
||||
'avatar_url' => $object->get_avatar_url(),
|
||||
'meta_data' => $data['meta_data'],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single customer output for response.
|
||||
*
|
||||
|
@ -38,28 +77,11 @@ class WC_REST_Customers_Controller extends WC_REST_Customers_V1_Controller {
|
|||
*/
|
||||
public function prepare_item_for_response( $user_data, $request ) {
|
||||
$customer = new WC_Customer( $user_data->ID );
|
||||
$data = $customer->get_data();
|
||||
$format_date = array( 'date_created', 'date_modified' );
|
||||
$meta_data = $data['meta_data'];
|
||||
unset( $data['meta_data'] );
|
||||
|
||||
// Format date values.
|
||||
foreach ( $format_date as $key ) {
|
||||
$data[ $key ] = $data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $data[ $key ] ) ) ) : null;
|
||||
}
|
||||
|
||||
// Additional non-crud data.
|
||||
$data['orders_count'] = $customer->get_order_count();
|
||||
$data['total_spent'] = $customer->get_total_spent();
|
||||
$data['avatar_url'] = $customer->get_avatar_url();
|
||||
|
||||
// Includes meta_data as last item.
|
||||
$data['meta_data'] = $meta_data;
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
$response = rest_ensure_response( $data );
|
||||
$data = $this->get_formatted_item_data( $customer );
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( $this->prepare_links( $user_data ) );
|
||||
|
||||
/**
|
||||
|
@ -114,12 +136,24 @@ class WC_REST_Customers_Controller extends WC_REST_Customers_V1_Controller {
|
|||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_created_gmt' => array(
|
||||
'description' => __( 'The date the order was created, as GMT.', 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified_gmt' => array(
|
||||
'description' => __( 'The date the order was last modified, as GMT.', 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'email' => array(
|
||||
'description' => __( 'The email address for the customer.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
|
|
|
@ -520,7 +520,7 @@ class WC_REST_Customers_V1_Controller extends WC_REST_Controller {
|
|||
|
||||
// Format date values.
|
||||
foreach ( $format_date as $key ) {
|
||||
$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $_data[ $key ] ) ) ) : null;
|
||||
$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( $_data[ $key ] ) : null; // v1 API used UTC.
|
||||
}
|
||||
|
||||
$data = array(
|
||||
|
@ -533,7 +533,7 @@ class WC_REST_Customers_V1_Controller extends WC_REST_Controller {
|
|||
'username' => $_data['username'],
|
||||
'last_order' => array(
|
||||
'id' => is_object( $last_order ) ? $last_order->get_id() : null,
|
||||
'date' => is_object( $last_order ) ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $last_order->get_date_created() ) ) ) : null,
|
||||
'date' => is_object( $last_order ) ? wc_rest_prepare_date_response( $last_order->get_date_created() ) : null, // v1 API used UTC.
|
||||
),
|
||||
'orders_count' => $customer->get_order_count(),
|
||||
'total_spent' => $customer->get_total_spent(),
|
||||
|
@ -633,13 +633,13 @@ class WC_REST_Customers_V1_Controller extends WC_REST_Controller {
|
|||
'readonly' => true,
|
||||
),
|
||||
'date_created' => array(
|
||||
'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ),
|
||||
'description' => __( 'The date the customer was created, as GMT.', 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'date_modified' => array(
|
||||
'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ),
|
||||
'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
|
@ -692,7 +692,7 @@ class WC_REST_Customers_V1_Controller extends WC_REST_Controller {
|
|||
'readonly' => true,
|
||||
),
|
||||
'date' => array(
|
||||
'description' => __( 'UTC DateTime of the customer last order.', 'woocommerce' ),
|
||||
'description' => __( 'The date of the customer last order, as GMT.', 'woocommerce' ),
|
||||
'type' => 'date-time',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
|
|
|
@ -51,15 +51,17 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 2, count( $customers ) );
|
||||
|
||||
$this->assertContains( array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'date_created' => wc_rest_prepare_date_response( date( 'Y-m-d H:i:s', $customer_1->get_date_created() ) ),
|
||||
'date_modified' => wc_rest_prepare_date_response( date( 'Y-m-d H:i:s', $customer_1->get_date_modified() ) ),
|
||||
'email' => 'test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'testcustomer',
|
||||
'billing' => array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'date_created' => wc_rest_prepare_date_response( $customer_1->get_date_created(), false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_created() ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $customer_1->get_date_modified(), false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_modified() ),
|
||||
'email' => 'test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'testcustomer',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -72,7 +74,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -84,11 +86,11 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $customer_1->get_avatar_url(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $customer_1->get_avatar_url(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/customers/' . $customer_1->get_id() . '' ),
|
||||
|
@ -134,15 +136,17 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test',
|
||||
'billing' => array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -155,7 +159,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -167,10 +171,10 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'country' => '',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
|
||||
// Test extra data
|
||||
|
@ -195,15 +199,17 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test2',
|
||||
'billing' => array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test2',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -216,7 +222,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -228,10 +234,10 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
|
||||
// Test without required field
|
||||
|
@ -276,12 +282,14 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'email' => 'get_customer_test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'billing' => array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'get_customer_test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -294,7 +302,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
|
@ -306,13 +314,13 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'get_customer_test',
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
'meta_data' => array(),
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'get_customer_test',
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
}
|
||||
|
||||
|
@ -491,10 +499,12 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 16, count( $properties ) );
|
||||
$this->assertEquals( 18, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'email', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties );
|
||||
$this->assertArrayHasKey( 'last_name', $properties );
|
||||
|
|
Loading…
Reference in New Issue