2016-02-17 19:29:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Customers controller
|
|
|
|
*
|
|
|
|
* Handles requests to the /customers endpoint.
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST API Customers controller class.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/API
|
2017-02-09 21:54:16 +00:00
|
|
|
* @extends WC_REST_Customers_V1_Controller
|
2016-02-17 19:29:09 +00:00
|
|
|
*/
|
2017-02-09 21:54:16 +00:00
|
|
|
class WC_REST_Customers_Controller extends WC_REST_Customers_V1_Controller {
|
2016-02-17 19:29:09 +00:00
|
|
|
|
2016-03-07 18:36:17 +00:00
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-02-09 17:06:13 +00:00
|
|
|
protected $namespace = 'wc/v2';
|
2016-03-07 18:36:17 +00:00
|
|
|
|
2016-03-01 23:07:20 +00:00
|
|
|
/**
|
2017-03-10 22:10:34 +00:00
|
|
|
* Get formatted item data.
|
2016-03-01 23:07:20 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-03-10 22:10:34 +00:00
|
|
|
* @param WC_Data $object WC_Data instance.
|
|
|
|
* @return array
|
2016-03-01 23:07:20 +00:00
|
|
|
*/
|
2017-03-10 22:10:34 +00:00
|
|
|
protected function get_formatted_item_data( $object ) {
|
|
|
|
$data = $object->get_data();
|
2016-08-30 16:50:35 +00:00
|
|
|
$format_date = array( 'date_created', 'date_modified' );
|
2016-08-20 17:29:48 +00:00
|
|
|
|
2016-08-30 16:50:35 +00:00
|
|
|
// Format date values.
|
|
|
|
foreach ( $format_date as $key ) {
|
2017-03-10 22:10:34 +00:00
|
|
|
$datetime = $data[ $key ];
|
|
|
|
$data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
|
|
|
|
$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
|
2016-08-30 16:50:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 22:10:34 +00:00
|
|
|
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'],
|
|
|
|
);
|
|
|
|
}
|
2017-02-09 21:54:16 +00:00
|
|
|
|
2017-03-10 22:10:34 +00:00
|
|
|
/**
|
|
|
|
* 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 );
|
2016-08-09 20:51:38 +00:00
|
|
|
$response->add_links( $this->prepare_links( $user_data ) );
|
2016-03-01 23:07:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter customer data returned from the REST API.
|
|
|
|
*
|
2016-08-09 20:51:38 +00:00
|
|
|
* @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.
|
2016-03-01 23:07:20 +00:00
|
|
|
*/
|
2016-08-09 20:51:38 +00:00
|
|
|
return apply_filters( 'woocommerce_rest_prepare_customer', $response, $user_data, $request );
|
2016-03-01 23:07:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 22:14:03 +00:00
|
|
|
/**
|
|
|
|
* Update customer meta fields.
|
|
|
|
*
|
2016-08-09 20:51:38 +00:00
|
|
|
* @param WC_Customer $customer
|
2016-03-02 22:14:03 +00:00
|
|
|
* @param WP_REST_Request $request
|
|
|
|
*/
|
|
|
|
protected function update_customer_meta_fields( $customer, $request ) {
|
2017-02-09 21:54:16 +00:00
|
|
|
parent::update_customer_meta_fields( $customer, $request );
|
2016-03-02 22:14:03 +00:00
|
|
|
|
2017-02-09 21:54:16 +00:00
|
|
|
// Meta data.
|
2016-08-31 14:43:34 +00:00
|
|
|
if ( isset( $request['meta_data'] ) ) {
|
|
|
|
if ( is_array( $request['meta_data'] ) ) {
|
|
|
|
foreach ( $request['meta_data'] as $meta ) {
|
2017-02-16 03:44:45 +00:00
|
|
|
$customer->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
2016-08-31 14:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 23:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 11:30:58 +00:00
|
|
|
* Get the Customer's schema, conforming to JSON Schema.
|
2016-03-01 23:07:20 +00:00
|
|
|
*
|
|
|
|
* @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(
|
|
|
|
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2016-03-30 17:53:46 +00:00
|
|
|
'date_created' => array(
|
2016-03-01 23:07:20 +00:00
|
|
|
'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ),
|
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2017-03-10 22:10:34 +00:00
|
|
|
'date_created_gmt' => array(
|
2017-03-10 22:14:28 +00:00
|
|
|
'description' => __( 'The date the order was created, as GMT.', 'woocommerce' ),
|
2017-03-10 22:10:34 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2016-03-30 17:53:46 +00:00
|
|
|
'date_modified' => array(
|
2016-03-01 23:07:20 +00:00
|
|
|
'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ),
|
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2017-03-10 22:10:34 +00:00
|
|
|
'date_modified_gmt' => array(
|
2017-03-20 16:01:55 +00:00
|
|
|
'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce' ),
|
2017-03-10 22:10:34 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2016-03-01 23:07:20 +00:00
|
|
|
'email' => array(
|
|
|
|
'description' => __( 'The email address for the customer.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'email',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'first_name' => array(
|
|
|
|
'description' => __( 'Customer first name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'last_name' => array(
|
|
|
|
'description' => __( 'Customer last name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
2016-11-25 13:04:21 +00:00
|
|
|
'role' => array(
|
|
|
|
'description' => __( 'Customer role.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2016-03-01 23:07:20 +00:00
|
|
|
'username' => array(
|
|
|
|
'description' => __( 'Customer login name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_user',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'password' => array(
|
|
|
|
'description' => __( 'Customer password.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'edit' ),
|
|
|
|
),
|
2016-06-06 13:37:04 +00:00
|
|
|
'billing' => array(
|
2016-03-01 23:07:20 +00:00
|
|
|
'description' => __( 'List of billing address data.', 'woocommerce' ),
|
2016-12-07 11:36:46 +00:00
|
|
|
'type' => 'object',
|
2016-03-01 23:07:20 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'properties' => array(
|
|
|
|
'first_name' => array(
|
|
|
|
'description' => __( 'First name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'last_name' => array(
|
|
|
|
'description' => __( 'Last name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'company' => array(
|
|
|
|
'description' => __( 'Company name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'address_1' => array(
|
2016-10-11 01:39:13 +00:00
|
|
|
'description' => __( 'Address 1', 'woocommerce' ),
|
2016-03-01 23:07:20 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'address_2' => array(
|
2016-10-11 01:39:13 +00:00
|
|
|
'description' => __( 'Address 2', 'woocommerce' ),
|
2016-03-01 23:07:20 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'city' => array(
|
|
|
|
'description' => __( 'City name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'state' => array(
|
|
|
|
'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'postcode' => array(
|
|
|
|
'description' => __( 'Postal code.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'country' => array(
|
|
|
|
'description' => __( 'ISO code of the country.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'email' => array(
|
|
|
|
'description' => __( 'Email address.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'email',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'phone' => array(
|
|
|
|
'description' => __( 'Phone number.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-06-06 13:37:04 +00:00
|
|
|
'shipping' => array(
|
2016-03-01 23:07:20 +00:00
|
|
|
'description' => __( 'List of shipping address data.', 'woocommerce' ),
|
2016-12-07 11:36:46 +00:00
|
|
|
'type' => 'object',
|
2016-03-01 23:07:20 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'properties' => array(
|
|
|
|
'first_name' => array(
|
|
|
|
'description' => __( 'First name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'last_name' => array(
|
|
|
|
'description' => __( 'Last name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'company' => array(
|
|
|
|
'description' => __( 'Company name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'address_1' => array(
|
2016-10-11 01:39:13 +00:00
|
|
|
'description' => __( 'Address 1', 'woocommerce' ),
|
2016-03-01 23:07:20 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'address_2' => array(
|
2016-10-11 01:39:13 +00:00
|
|
|
'description' => __( 'Address 2', 'woocommerce' ),
|
2016-03-01 23:07:20 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'city' => array(
|
|
|
|
'description' => __( 'City name.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'state' => array(
|
|
|
|
'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'postcode' => array(
|
|
|
|
'description' => __( 'Postal code.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'country' => array(
|
|
|
|
'description' => __( 'ISO code of the country.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-08-31 14:43:34 +00:00
|
|
|
'is_paying_customer' => array(
|
|
|
|
'description' => __( 'Is the customer a paying customer?', 'woocommerce' ),
|
|
|
|
'type' => 'bool',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2017-02-09 21:54:16 +00:00
|
|
|
'orders_count' => array(
|
|
|
|
'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ),
|
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'total_spent' => array(
|
|
|
|
'description' => __( 'Total amount spent.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'avatar_url' => array(
|
|
|
|
'description' => __( 'Avatar URL.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2016-08-31 14:43:34 +00:00
|
|
|
'meta_data' => array(
|
2017-02-15 14:28:45 +00:00
|
|
|
'description' => __( 'Meta data.', 'woocommerce' ),
|
2016-12-07 12:20:56 +00:00
|
|
|
'type' => 'array',
|
2016-08-31 14:43:34 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
2016-12-07 14:24:44 +00:00
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
|
|
|
'description' => __( 'Meta ID.', 'woocommerce' ),
|
2016-12-07 18:13:17 +00:00
|
|
|
'type' => 'integer',
|
2016-12-07 14:24:44 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'key' => array(
|
|
|
|
'description' => __( 'Meta key.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
|
|
|
'value' => array(
|
|
|
|
'description' => __( 'Meta value.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
),
|
2016-08-31 14:43:34 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-03-01 23:07:20 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->add_additional_fields_schema( $schema );
|
2016-02-17 19:29:09 +00:00
|
|
|
}
|
|
|
|
}
|