woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-...

365 lines
13 KiB
PHP
Raw Normal View History

<?php
/**
* REST API Customers controller
*
* Handles requests to the /customers endpoint.
*
2019-06-21 09:40:39 +00:00
* @package Automattic/WooCommerce/RestApi
* @since 2.6.0
*/
defined( 'ABSPATH' ) || exit;
/**
* REST API Customers controller class.
*
2019-06-21 09:40:39 +00:00
* @package Automattic/WooCommerce/RestApi
* @extends WC_REST_Customers_V1_Controller
*/
class WC_REST_Customers_V2_Controller extends WC_REST_Customers_V1_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v2';
/**
* Get formatted item data.
*
* @since 3.0.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 = 'date_created' === $key ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $data[ $key ]->getTimestamp() ) ) : $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.
*
* @param WP_User $user_data User object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $user_data, $request ) {
$customer = new WC_Customer( $user_data->ID );
$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 ) );
/**
* Filter customer data returned from the REST API.
*
* @param WP_REST_Response $response The response object.
* @param WP_User $user_data User object used to create response.
* @param WP_REST_Request $request Request object.
*/
return apply_filters( 'woocommerce_rest_prepare_customer', $response, $user_data, $request );
}
/**
* Update customer meta fields.
*
* @param WC_Customer $customer Customer data.
* @param WP_REST_Request $request Request data.
*/
protected function update_customer_meta_fields( $customer, $request ) {
parent::update_customer_meta_fields( $customer, $request );
// Meta data.
if ( isset( $request['meta_data'] ) ) {
if ( is_array( $request['meta_data'] ) ) {
foreach ( $request['meta_data'] as $meta ) {
$customer->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
}
}
}
}
/**
* Get the Customer's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'customer',
'type' => 'object',
'properties' => array(
'id' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Unique identifier for the resource.', 'woocommerce-rest-api' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created' => array(
2019-06-24 12:01:21 +00:00
'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce-rest-api' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created_gmt' => array(
2019-06-26 14:26:58 +00:00
'description' => __( 'The date the customer was created, as GMT.', 'woocommerce-rest-api' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_modified' => array(
2019-06-24 12:01:21 +00:00
'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce-rest-api' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_modified_gmt' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce-rest-api' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'email' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'The email address for the customer.', 'woocommerce-rest-api' ),
'type' => 'string',
'format' => 'email',
'context' => array( 'view', 'edit' ),
),
'first_name' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Customer first name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'last_name' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Customer last name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'role' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Customer role.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'username' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Customer login name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_user',
),
),
'password' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Customer password.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'billing' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'List of billing address data.', 'woocommerce-rest-api' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'first_name' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'First name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'last_name' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Last name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'company' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Company name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'address_1' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Address line 1', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'address_2' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Address line 2', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'city' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'City name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'state' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'postcode' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Postal code.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'country' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'ISO code of the country.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'email' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Email address.', 'woocommerce-rest-api' ),
'type' => 'string',
'format' => 'email',
'context' => array( 'view', 'edit' ),
),
'phone' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Phone number.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
),
),
'shipping' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'List of shipping address data.', 'woocommerce-rest-api' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'first_name' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'First name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'last_name' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Last name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'company' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Company name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'address_1' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Address line 1', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'address_2' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Address line 2', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'city' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'City name.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'state' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'postcode' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Postal code.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'country' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'ISO code of the country.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
),
),
'is_paying_customer' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Is the customer a paying customer?', 'woocommerce-rest-api' ),
'type' => 'bool',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'orders_count' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Quantity of orders made by the customer.', 'woocommerce-rest-api' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'total_spent' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Total amount spent.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'avatar_url' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Avatar URL.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'meta_data' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Meta data.', 'woocommerce-rest-api' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Meta ID.', 'woocommerce-rest-api' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'key' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Meta key.', 'woocommerce-rest-api' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'value' => array(
2019-06-24 12:01:21 +00:00
'description' => __( 'Meta value.', 'woocommerce-rest-api' ),
2020-08-05 16:14:27 +00:00
'type' => array( 'string', 'null' ),
'context' => array( 'view', 'edit' ),
),
),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}