2013-11-04 06:36:31 +00:00
< ? php
/**
* WooCommerce API Customers Class
*
* Handles requests to the / customers endpoint
*
2015-01-05 18:06:52 +00:00
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.2
2013-11-04 06:36:31 +00:00
*/
2014-09-20 19:24:20 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-11-04 06:36:31 +00:00
2013-11-09 21:20:23 +00:00
class WC_API_Customers extends WC_API_Resource {
2013-11-04 06:36:31 +00:00
/** @var string $base the route base */
protected $base = '/customers' ;
2013-11-10 23:28:58 +00:00
/** @var string $created_at_min for date filtering */
private $created_at_min = null ;
/** @var string $created_at_max for date filtering */
private $created_at_max = null ;
2013-11-04 06:36:31 +00:00
/**
* Setup class , overridden to provide customer data to order response
*
* @ since 2.1
2013-11-06 06:54:19 +00:00
* @ param WC_API_Server $server
2013-11-04 06:36:31 +00:00
* @ return WC_API_Customers
*/
2013-11-06 06:54:19 +00:00
public function __construct ( WC_API_Server $server ) {
2013-11-04 06:36:31 +00:00
parent :: __construct ( $server );
// add customer data to order responses
2013-11-11 00:29:36 +00:00
add_filter ( 'woocommerce_api_order_response' , array ( $this , 'add_customer_data' ), 10 , 2 );
2013-11-10 23:28:58 +00:00
// modify WP_User_Query to support created_at date filtering
add_action ( 'pre_user_query' , array ( $this , 'modify_user_query' ) );
2013-11-04 06:36:31 +00:00
}
/**
* Register the routes for this class
*
2013-11-23 20:01:53 +00:00
* GET / customers
2013-11-04 06:36:31 +00:00
* GET / customers / count
2013-11-23 20:01:53 +00:00
* GET / customers /< id >
2013-11-04 06:36:31 +00:00
* GET / customers /< id >/ orders
*
2014-03-05 01:48:54 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ param array $routes
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function register_routes ( $routes ) {
2013-11-04 06:36:31 +00:00
2014-03-04 00:13:44 +00:00
# GET/POST /customers
2013-11-04 06:36:31 +00:00
$routes [ $this -> base ] = array (
2014-03-05 01:48:54 +00:00
array ( array ( $this , 'get_customers' ), WC_API_SERVER :: READABLE ),
array ( array ( $this , 'create_customer' ), WC_API_SERVER :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
2013-11-04 06:36:31 +00:00
);
# GET /customers/count
$routes [ $this -> base . '/count' ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_customers_count' ), WC_API_SERVER :: READABLE ),
2013-11-04 06:36:31 +00:00
);
2014-03-04 01:16:54 +00:00
# GET/PUT/DELETE /customers/<id>
2013-11-04 06:36:31 +00:00
$routes [ $this -> base . '/(?P<id>\d+)' ] = array (
2014-03-05 01:48:54 +00:00
array ( array ( $this , 'get_customer' ), WC_API_SERVER :: READABLE ),
array ( array ( $this , 'edit_customer' ), WC_API_SERVER :: EDITABLE | WC_API_SERVER :: ACCEPT_DATA ),
2014-03-04 01:16:54 +00:00
array ( array ( $this , 'delete_customer' ), WC_API_SERVER :: DELETABLE ),
2013-11-04 06:36:31 +00:00
);
2014-08-30 19:50:40 +00:00
# GET /customers/email/<email>
2014-02-17 17:13:01 +00:00
$routes [ $this -> base . '/email/(?P<email>.+)' ] = array (
2014-03-05 01:48:54 +00:00
array ( array ( $this , 'get_customer_by_email' ), WC_API_SERVER :: READABLE ),
2014-02-17 13:19:11 +00:00
);
2013-11-04 06:36:31 +00:00
# GET /customers/<id>/orders
$routes [ $this -> base . '/(?P<id>\d+)/orders' ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_customer_orders' ), WC_API_SERVER :: READABLE ),
2013-11-04 06:36:31 +00:00
);
2014-05-28 17:06:46 +00:00
# GET /customers/<id>/downloads
$routes [ $this -> base . '/(?P<id>\d+)/downloads' ] = array (
array ( array ( $this , 'get_customer_downloads' ), WC_API_SERVER :: READABLE ),
);
2015-05-07 16:09:59 +00:00
# POST|PUT /customers/bulk
$routes [ $this -> base . '/bulk' ] = array (
array ( array ( $this , 'bulk' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
);
2013-11-04 06:36:31 +00:00
return $routes ;
}
/**
* Get all customers
*
2014-03-05 17:24:47 +00:00
* @ since 2.1
2013-11-04 06:36:31 +00:00
* @ param array $fields
2013-11-11 00:29:36 +00:00
* @ param array $filter
2013-11-19 02:06:45 +00:00
* @ param int $page
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-19 02:06:45 +00:00
public function get_customers ( $fields = null , $filter = array (), $page = 1 ) {
$filter [ 'page' ] = $page ;
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
$query = $this -> query_customers ( $filter );
2013-11-04 06:36:31 +00:00
$customers = array ();
2014-03-05 01:48:54 +00:00
foreach ( $query -> get_results () as $user_id ) {
2013-11-04 06:36:31 +00:00
2014-03-05 01:48:54 +00:00
if ( ! $this -> is_readable ( $user_id ) ) {
2013-11-11 00:29:36 +00:00
continue ;
2014-03-05 01:48:54 +00:00
}
2013-11-11 00:29:36 +00:00
2013-11-22 08:41:32 +00:00
$customers [] = current ( $this -> get_customer ( $user_id , $fields ) );
2013-11-04 06:36:31 +00:00
}
2013-11-19 02:06:45 +00:00
$this -> server -> add_pagination_headers ( $query );
2013-11-11 00:29:36 +00:00
2013-11-04 06:36:31 +00:00
return array ( 'customers' => $customers );
}
/**
* Get the customer for the given ID
*
2014-03-05 17:24:47 +00:00
* @ since 2.1
2013-11-04 06:36:31 +00:00
* @ param int $id the customer ID
2015-02-03 14:17:49 +00:00
* @ param array $fields
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function get_customer ( $id , $fields = null ) {
2013-11-04 06:36:31 +00:00
global $wpdb ;
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'customer' , 'read' );
2013-11-04 06:36:31 +00:00
2014-03-05 01:48:54 +00:00
if ( is_wp_error ( $id ) ) {
2013-11-11 00:29:36 +00:00
return $id ;
2014-03-05 01:48:54 +00:00
}
2013-11-04 06:36:31 +00:00
2014-02-17 14:39:56 +00:00
$customer = new WP_User ( $id );
2013-11-04 06:36:31 +00:00
2015-01-05 18:06:52 +00:00
// Get info about user's last order
2013-11-18 21:47:38 +00:00
$last_order = $wpdb -> get_row ( " SELECT id, post_date_gmt
2013-11-04 06:36:31 +00:00
FROM $wpdb -> posts AS posts
LEFT JOIN { $wpdb -> postmeta } AS meta on posts . ID = meta . post_id
WHERE meta . meta_key = '_customer_user'
AND meta . meta_value = { $customer -> ID }
AND posts . post_type = 'shop_order'
2014-05-30 15:34:59 +00:00
AND posts . post_status IN ( '" . implode( "' , '", array_keys( wc_get_order_statuses() ) ) . "' )
2015-01-12 17:09:42 +00:00
ORDER BY posts . ID DESC
2013-11-04 06:36:31 +00:00
" );
2016-06-10 15:14:54 +00:00
$roles = array_values ( $customer -> roles );
2013-11-04 06:36:31 +00:00
$customer_data = array (
'id' => $customer -> ID ,
2013-11-18 21:47:38 +00:00
'created_at' => $this -> server -> format_datetime ( $customer -> user_registered ),
2016-02-10 11:56:13 +00:00
'last_update' => $this -> server -> format_datetime ( get_user_meta ( $customer -> ID , 'last_update' , true ) ),
2013-11-04 06:36:31 +00:00
'email' => $customer -> user_email ,
'first_name' => $customer -> first_name ,
'last_name' => $customer -> last_name ,
'username' => $customer -> user_login ,
2016-06-10 15:14:54 +00:00
'role' => $roles [ 0 ],
2013-11-04 06:36:31 +00:00
'last_order_id' => is_object ( $last_order ) ? $last_order -> id : null ,
2013-11-18 21:47:38 +00:00
'last_order_date' => is_object ( $last_order ) ? $this -> server -> format_datetime ( $last_order -> post_date_gmt ) : null ,
2015-01-12 17:09:42 +00:00
'orders_count' => wc_get_customer_order_count ( $customer -> ID ),
'total_spent' => wc_format_decimal ( wc_get_customer_total_spent ( $customer -> ID ), 2 ),
2013-11-06 06:54:19 +00:00
'avatar_url' => $this -> get_avatar_url ( $customer -> customer_email ),
2013-11-04 06:36:31 +00:00
'billing_address' => array (
'first_name' => $customer -> billing_first_name ,
'last_name' => $customer -> billing_last_name ,
'company' => $customer -> billing_company ,
'address_1' => $customer -> billing_address_1 ,
'address_2' => $customer -> billing_address_2 ,
'city' => $customer -> billing_city ,
'state' => $customer -> billing_state ,
'postcode' => $customer -> billing_postcode ,
'country' => $customer -> billing_country ,
'email' => $customer -> billing_email ,
'phone' => $customer -> billing_phone ,
),
'shipping_address' => array (
'first_name' => $customer -> shipping_first_name ,
'last_name' => $customer -> shipping_last_name ,
'company' => $customer -> shipping_company ,
'address_1' => $customer -> shipping_address_1 ,
'address_2' => $customer -> shipping_address_2 ,
'city' => $customer -> shipping_city ,
'state' => $customer -> shipping_state ,
'postcode' => $customer -> shipping_postcode ,
'country' => $customer -> shipping_country ,
),
);
2013-11-22 08:41:32 +00:00
return array ( 'customer' => apply_filters ( 'woocommerce_api_customer_response' , $customer_data , $customer , $fields , $this -> server ) );
2013-11-04 06:36:31 +00:00
}
2014-02-17 14:39:56 +00:00
/**
* Get the customer for the given email
*
2014-03-05 17:24:47 +00:00
* @ since 2.1
2014-02-17 14:39:56 +00:00
* @ param string $email the customer email
2015-02-03 14:44:53 +00:00
* @ param array $fields
2014-02-17 14:39:56 +00:00
* @ return array
*/
2014-08-30 19:50:40 +00:00
public function get_customer_by_email ( $email , $fields = null ) {
2015-01-05 18:06:52 +00:00
try {
if ( is_email ( $email ) ) {
$customer = get_user_by ( 'email' , $email );
if ( ! is_object ( $customer ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_customer_email' , __ ( 'Invalid customer Email' , 'woocommerce' ), 404 );
}
} else {
throw new WC_API_Exception ( 'woocommerce_api_invalid_customer_email' , __ ( 'Invalid customer Email' , 'woocommerce' ), 404 );
2014-02-17 14:39:56 +00:00
}
2015-01-05 18:06:52 +00:00
return $this -> get_customer ( $customer -> ID , $fields );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2014-02-17 14:39:56 +00:00
}
2013-11-04 06:36:31 +00:00
/**
* Get the total number of customers
*
2014-03-05 17:24:47 +00:00
* @ since 2.1
2013-11-11 00:29:36 +00:00
* @ param array $filter
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function get_customers_count ( $filter = array () ) {
2015-01-05 18:06:52 +00:00
try {
if ( ! current_user_can ( 'list_users' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_customers_count' , __ ( 'You do not have permission to read the customers count' , 'woocommerce' ), 401 );
}
2013-11-04 06:36:31 +00:00
2015-01-05 18:06:52 +00:00
$query = $this -> query_customers ( $filter );
2013-11-11 00:29:36 +00:00
2015-10-01 08:54:30 +00:00
return array ( 'count' => $query -> get_total () );
2015-01-05 18:06:52 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2014-03-05 01:48:54 +00:00
}
2013-11-04 06:36:31 +00:00
}
2014-08-30 19:50:40 +00:00
/**
* Get customer billing address fields .
*
* @ since 2.2
* @ return array
*/
protected function get_customer_billing_address () {
$billing_address = apply_filters ( 'woocommerce_api_customer_billing_address' , array (
'first_name' ,
'last_name' ,
'company' ,
'address_1' ,
'address_2' ,
'city' ,
'state' ,
'postcode' ,
'country' ,
'email' ,
'phone' ,
) );
return $billing_address ;
}
/**
* Get customer shipping address fields .
*
* @ since 2.2
* @ return array
*/
protected function get_customer_shipping_address () {
$shipping_address = apply_filters ( 'woocommerce_api_customer_shipping_address' , array (
'first_name' ,
'last_name' ,
'company' ,
'address_1' ,
'address_2' ,
'city' ,
'state' ,
'postcode' ,
'country' ,
) );
return $shipping_address ;
}
2014-03-04 02:01:07 +00:00
/**
* Add / Update customer data .
*
* @ since 2.2
* @ param int $id the customer ID
* @ param array $data
*/
protected function update_customer_data ( $id , $data ) {
// Customer first name.
if ( isset ( $data [ 'first_name' ] ) ) {
2014-03-04 05:48:17 +00:00
update_user_meta ( $id , 'first_name' , wc_clean ( $data [ 'first_name' ] ) );
2014-03-04 02:01:07 +00:00
}
// Customer last name.
if ( isset ( $data [ 'last_name' ] ) ) {
2014-03-04 05:48:17 +00:00
update_user_meta ( $id , 'last_name' , wc_clean ( $data [ 'last_name' ] ) );
2014-03-04 02:01:07 +00:00
}
// Customer billing address.
if ( isset ( $data [ 'billing_address' ] ) ) {
foreach ( $this -> get_customer_billing_address () as $address ) {
if ( isset ( $data [ 'billing_address' ][ $address ] ) ) {
2014-03-04 05:48:17 +00:00
update_user_meta ( $id , 'billing_' . $address , wc_clean ( $data [ 'billing_address' ][ $address ] ) );
2014-03-04 02:01:07 +00:00
}
}
}
// Customer shipping address.
if ( isset ( $data [ 'shipping_address' ] ) ) {
foreach ( $this -> get_customer_shipping_address () as $address ) {
if ( isset ( $data [ 'shipping_address' ][ $address ] ) ) {
2014-03-04 05:48:17 +00:00
update_user_meta ( $id , 'shipping_' . $address , wc_clean ( $data [ 'shipping_address' ][ $address ] ) );
2014-03-04 02:01:07 +00:00
}
}
}
do_action ( 'woocommerce_api_update_customer_data' , $id , $data );
}
2013-11-04 06:36:31 +00:00
/**
* Create a customer
*
2014-03-04 00:04:40 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ param array $data
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function create_customer ( $data ) {
2015-01-05 18:06:52 +00:00
try {
2015-04-13 16:06:19 +00:00
if ( ! isset ( $data [ 'customer' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_customer_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'customer' ), 400 );
}
$data = $data [ 'customer' ];
2013-11-11 00:29:36 +00:00
2015-01-05 18:06:52 +00:00
// Checks with can create new users.
if ( ! current_user_can ( 'create_users' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_customer' , __ ( 'You do not have permission to create this customer' , 'woocommerce' ), 401 );
}
2014-03-04 00:04:40 +00:00
2015-01-05 18:06:52 +00:00
$data = apply_filters ( 'woocommerce_api_create_customer_data' , $data , $this );
2014-08-30 19:50:40 +00:00
2015-01-05 18:06:52 +00:00
// Checks with the email is missing.
if ( ! isset ( $data [ 'email' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_customer_email' , sprintf ( __ ( 'Missing parameter %s' , 'woocommerce' ), 'email' ), 400 );
}
2014-03-04 00:04:40 +00:00
2015-01-05 18:06:52 +00:00
// Sets the username.
2015-06-10 13:56:25 +00:00
$data [ 'username' ] = ! empty ( $data [ 'username' ] ) ? $data [ 'username' ] : '' ;
2014-03-04 00:04:40 +00:00
2015-01-05 18:06:52 +00:00
// Sets the password.
2015-06-10 13:56:25 +00:00
$data [ 'password' ] = ! empty ( $data [ 'password' ] ) ? $data [ 'password' ] : '' ;
2014-03-04 00:04:40 +00:00
2015-01-05 18:06:52 +00:00
// Attempts to create the new customer
$id = wc_create_new_customer ( $data [ 'email' ], $data [ 'username' ], $data [ 'password' ] );
2014-03-04 00:04:40 +00:00
2015-01-05 18:06:52 +00:00
// Checks for an error in the customer creation.
if ( is_wp_error ( $id ) ) {
2015-03-17 23:20:35 +00:00
throw new WC_API_Exception ( $id -> get_error_code (), $id -> get_error_message (), 400 );
2015-01-05 18:06:52 +00:00
}
2014-03-04 00:04:40 +00:00
2015-01-05 18:06:52 +00:00
// Added customer data.
$this -> update_customer_data ( $id , $data );
2013-11-04 06:36:31 +00:00
2015-01-05 18:06:52 +00:00
do_action ( 'woocommerce_api_create_customer' , $id , $data );
2014-03-04 01:09:05 +00:00
2015-01-05 18:06:52 +00:00
$this -> server -> send_status ( 201 );
2014-03-06 07:28:42 +00:00
2015-01-05 18:06:52 +00:00
return $this -> get_customer ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2013-11-04 06:36:31 +00:00
}
/**
* Edit a customer
*
2014-03-04 01:07:28 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ param int $id the customer ID
* @ param array $data
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function edit_customer ( $id , $data ) {
2015-04-13 16:06:19 +00:00
try {
if ( ! isset ( $data [ 'customer' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_customer_data' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'customer' ), 400 );
}
2013-11-11 00:29:36 +00:00
2015-04-13 16:06:19 +00:00
$data = $data [ 'customer' ];
2014-08-30 19:50:40 +00:00
2015-04-13 16:06:19 +00:00
// Validate the customer ID.
$id = $this -> validate_request ( $id , 'customer' , 'edit' );
2013-11-11 00:29:36 +00:00
2015-04-13 16:06:19 +00:00
// Return the validate error.
if ( is_wp_error ( $id ) ) {
throw new WC_API_Exception ( $id -> get_error_code (), $id -> get_error_message (), 400 );
}
2014-03-04 01:07:28 +00:00
2015-04-13 16:06:19 +00:00
$data = apply_filters ( 'woocommerce_api_edit_customer_data' , $data , $this );
2014-08-30 19:50:40 +00:00
2015-04-13 16:06:19 +00:00
// Customer email.
if ( isset ( $data [ 'email' ] ) ) {
wp_update_user ( array ( 'ID' => $id , 'user_email' => sanitize_email ( $data [ 'email' ] ) ) );
}
2014-03-04 01:07:28 +00:00
2015-04-13 16:06:19 +00:00
// Customer password.
if ( isset ( $data [ 'password' ] ) ) {
wp_update_user ( array ( 'ID' => $id , 'user_pass' => wc_clean ( $data [ 'password' ] ) ) );
}
2014-03-04 01:07:28 +00:00
2015-04-13 16:06:19 +00:00
// Update customer data.
$this -> update_customer_data ( $id , $data );
2013-11-04 06:36:31 +00:00
2015-04-13 16:06:19 +00:00
do_action ( 'woocommerce_api_edit_customer' , $id , $data );
2014-03-04 01:09:50 +00:00
2015-04-13 16:06:19 +00:00
return $this -> get_customer ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2013-11-04 06:36:31 +00:00
}
/**
* Delete a customer
*
2014-03-04 01:16:54 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ param int $id the customer ID
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function delete_customer ( $id ) {
2014-03-04 01:16:54 +00:00
// Validate the customer ID.
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'customer' , 'delete' );
2014-03-04 01:16:54 +00:00
// Return the validate error.
if ( is_wp_error ( $id ) ) {
2014-03-05 02:42:36 +00:00
return $id ;
2014-03-04 01:16:54 +00:00
}
2013-11-04 06:36:31 +00:00
2014-08-30 19:50:40 +00:00
do_action ( 'woocommerce_api_delete_customer' , $id , $this );
2013-11-11 00:29:36 +00:00
return $this -> delete ( $id , 'customer' );
2013-11-04 06:36:31 +00:00
}
/**
* Get the orders for a customer
*
2014-03-05 17:24:47 +00:00
* @ since 2.1
2013-11-04 06:36:31 +00:00
* @ param int $id the customer ID
2013-11-11 00:29:36 +00:00
* @ param string $fields fields to include in response
2015-09-21 22:11:54 +00:00
* @ param array $filter filters
2013-11-04 06:36:31 +00:00
* @ return array
*/
2015-09-21 22:11:54 +00:00
public function get_customer_orders ( $id , $fields = null , $filter = array () ) {
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'customer' , 'read' );
2013-11-04 06:36:31 +00:00
2014-03-05 01:48:54 +00:00
if ( is_wp_error ( $id ) ) {
2013-11-11 00:29:36 +00:00
return $id ;
2014-03-05 01:48:54 +00:00
}
2013-11-04 06:36:31 +00:00
2015-09-21 22:11:54 +00:00
$filter [ 'customer_id' ] = $id ;
$orders = WC () -> api -> WC_API_Orders -> get_orders ( $fields , $filter , null , - 1 );
2013-11-04 06:36:31 +00:00
2015-09-21 22:11:54 +00:00
return $orders ;
2013-11-04 06:36:31 +00:00
}
2014-05-28 17:06:46 +00:00
/**
* Get the available downloads for a customer
*
* @ since 2.2
* @ param int $id the customer ID
* @ param string $fields fields to include in response
* @ return array
*/
public function get_customer_downloads ( $id , $fields = null ) {
$id = $this -> validate_request ( $id , 'customer' , 'read' );
if ( is_wp_error ( $id ) ) {
return $id ;
}
2015-01-21 12:38:33 +00:00
$downloads = array ();
$_downloads = wc_get_customer_available_downloads ( $id );
2014-05-28 17:06:46 +00:00
2015-01-21 12:38:33 +00:00
foreach ( $_downloads as $key => $download ) {
$downloads [ $key ] = $download ;
$downloads [ $key ][ 'access_expires' ] = $this -> server -> format_datetime ( $downloads [ $key ][ 'access_expires' ] );
2014-05-28 17:06:46 +00:00
}
return array ( 'downloads' => apply_filters ( 'woocommerce_api_customer_downloads_response' , $downloads , $id , $fields , $this -> server ) );
}
2013-11-04 06:36:31 +00:00
/**
* Helper method to get customer user objects
*
2013-11-19 02:06:45 +00:00
* Note that WP_User_Query does not have built - in pagination so limit & offset are used to provide limited
* pagination support
2015-04-13 16:06:19 +00:00
*
2015-02-07 06:08:48 +00:00
* The filter for role can only be a single role in a string .
2013-11-19 02:06:45 +00:00
*
2015-02-04 11:59:25 +00:00
* @ since 2.3
2013-11-04 06:36:31 +00:00
* @ param array $args request arguments for filtering query
2014-02-07 17:39:40 +00:00
* @ return WP_User_Query
2013-11-04 06:36:31 +00:00
*/
2013-11-11 00:29:36 +00:00
private function query_customers ( $args = array () ) {
2013-11-04 06:36:31 +00:00
2013-11-19 02:06:45 +00:00
// default users per page
$users_per_page = get_option ( 'posts_per_page' );
2015-01-05 18:06:52 +00:00
// Set base query arguments
2013-11-04 06:36:31 +00:00
$query_args = array (
'fields' => 'ID' ,
'role' => 'customer' ,
'orderby' => 'registered' ,
2013-11-19 02:06:45 +00:00
'number' => $users_per_page ,
2013-11-04 06:36:31 +00:00
);
2015-02-04 12:31:14 +00:00
// Custom Role
2015-02-04 11:59:25 +00:00
if ( ! empty ( $args [ 'role' ] ) ) {
2015-02-04 12:31:14 +00:00
$query_args [ 'role' ] = $args [ 'role' ];
2015-09-30 16:10:43 +00:00
2015-09-30 16:12:12 +00:00
// Show users on all roles
2015-09-30 16:10:43 +00:00
if ( 'all' === $query_args [ 'role' ] ) {
unset ( $query_args [ 'role' ] );
}
2015-02-04 11:59:25 +00:00
}
2015-01-05 18:06:52 +00:00
// Search
2013-11-19 02:06:45 +00:00
if ( ! empty ( $args [ 'q' ] ) ) {
2013-11-04 06:36:31 +00:00
$query_args [ 'search' ] = $args [ 'q' ];
2013-11-19 02:06:45 +00:00
}
2013-11-04 06:36:31 +00:00
2015-01-05 18:06:52 +00:00
// Limit number of users returned
2013-11-19 02:06:45 +00:00
if ( ! empty ( $args [ 'limit' ] ) ) {
2014-10-15 17:43:13 +00:00
if ( $args [ 'limit' ] == - 1 ) {
2014-10-14 22:31:56 +00:00
unset ( $query_args [ 'number' ] );
} else {
2014-10-15 17:43:13 +00:00
$query_args [ 'number' ] = absint ( $args [ 'limit' ] );
$users_per_page = absint ( $args [ 'limit' ] );
2014-10-14 22:31:56 +00:00
}
2015-01-05 14:36:50 +00:00
} else {
$args [ 'limit' ] = $query_args [ 'number' ];
2013-11-19 02:06:45 +00:00
}
2013-11-04 06:36:31 +00:00
2015-01-05 18:06:52 +00:00
// Page
2013-11-22 08:41:32 +00:00
$page = ( isset ( $args [ 'page' ] ) ) ? absint ( $args [ 'page' ] ) : 1 ;
2013-11-04 06:36:31 +00:00
2015-01-05 18:06:52 +00:00
// Offset
2013-11-19 02:06:45 +00:00
if ( ! empty ( $args [ 'offset' ] ) ) {
$query_args [ 'offset' ] = absint ( $args [ 'offset' ] );
} else {
$query_args [ 'offset' ] = $users_per_page * ( $page - 1 );
}
2015-01-05 18:06:52 +00:00
// Created date
2013-11-19 02:06:45 +00:00
if ( ! empty ( $args [ 'created_at_min' ] ) ) {
2013-11-18 21:47:38 +00:00
$this -> created_at_min = $this -> server -> parse_datetime ( $args [ 'created_at_min' ] );
2013-11-19 02:06:45 +00:00
}
2013-11-10 23:28:58 +00:00
2013-11-19 02:06:45 +00:00
if ( ! empty ( $args [ 'created_at_max' ] ) ) {
2013-11-18 21:47:38 +00:00
$this -> created_at_max = $this -> server -> parse_datetime ( $args [ 'created_at_max' ] );
2013-11-19 02:06:45 +00:00
}
2015-01-05 18:06:52 +00:00
// Order (ASC or DESC, ASC by default)
2014-07-28 23:38:21 +00:00
if ( ! empty ( $args [ 'order' ] ) ) {
$query_args [ 'order' ] = $args [ 'order' ];
}
2014-07-28 23:40:28 +00:00
2015-01-05 18:06:52 +00:00
// Orderby
2014-07-28 23:40:28 +00:00
if ( ! empty ( $args [ 'orderby' ] ) ) {
$query_args [ 'orderby' ] = $args [ 'orderby' ];
2015-01-05 18:06:52 +00:00
// Allow sorting by meta value
2014-07-28 23:49:56 +00:00
if ( ! empty ( $args [ 'orderby_meta_key' ] ) ) {
2014-07-28 23:40:28 +00:00
$query_args [ 'meta_key' ] = $args [ 'orderby_meta_key' ];
}
}
2013-11-19 02:06:45 +00:00
$query = new WP_User_Query ( $query_args );
2013-11-10 23:28:58 +00:00
2015-01-05 18:06:52 +00:00
// Helper members for pagination headers
2014-10-15 17:43:13 +00:00
$query -> total_pages = ( $args [ 'limit' ] == - 1 ) ? 1 : ceil ( $query -> get_total () / $users_per_page );
2013-11-19 02:06:45 +00:00
$query -> page = $page ;
2013-11-04 06:36:31 +00:00
2013-11-19 02:06:45 +00:00
return $query ;
2013-11-04 06:36:31 +00:00
}
/**
* Add customer data to orders
*
* @ since 2.1
* @ param $order_data
* @ param $order
2013-11-11 00:29:36 +00:00
* @ return array
2013-11-04 06:36:31 +00:00
*/
2013-11-11 00:29:36 +00:00
public function add_customer_data ( $order_data , $order ) {
2013-11-04 06:36:31 +00:00
if ( 0 == $order -> customer_user ) {
2014-01-23 21:38:08 +00:00
// add customer data from order
$order_data [ 'customer' ] = array (
'id' => 0 ,
'email' => $order -> billing_email ,
'first_name' => $order -> billing_first_name ,
'last_name' => $order -> billing_last_name ,
'billing_address' => array (
'first_name' => $order -> billing_first_name ,
'last_name' => $order -> billing_last_name ,
'company' => $order -> billing_company ,
'address_1' => $order -> billing_address_1 ,
'address_2' => $order -> billing_address_2 ,
'city' => $order -> billing_city ,
'state' => $order -> billing_state ,
'postcode' => $order -> billing_postcode ,
'country' => $order -> billing_country ,
'email' => $order -> billing_email ,
'phone' => $order -> billing_phone ,
),
'shipping_address' => array (
'first_name' => $order -> shipping_first_name ,
'last_name' => $order -> shipping_last_name ,
'company' => $order -> shipping_company ,
'address_1' => $order -> shipping_address_1 ,
'address_2' => $order -> shipping_address_2 ,
'city' => $order -> shipping_city ,
'state' => $order -> shipping_state ,
'postcode' => $order -> shipping_postcode ,
'country' => $order -> shipping_country ,
),
);
2013-11-04 06:36:31 +00:00
} else {
2013-11-22 08:41:32 +00:00
$order_data [ 'customer' ] = current ( $this -> get_customer ( $order -> customer_user ) );
2013-11-04 06:36:31 +00:00
}
return $order_data ;
}
2013-11-10 23:28:58 +00:00
/**
* Modify the WP_User_Query to support filtering on the date the customer was created
*
2014-03-05 17:24:47 +00:00
* @ since 2.1
2013-11-10 23:28:58 +00:00
* @ param WP_User_Query $query
*/
public function modify_user_query ( $query ) {
2014-03-05 01:48:54 +00:00
if ( $this -> created_at_min ) {
2015-01-12 17:01:12 +00:00
$query -> query_where .= sprintf ( " AND user_registered >= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%H:%%i:%%s' ) " , esc_sql ( $this -> created_at_min ) );
2014-03-05 01:48:54 +00:00
}
2013-11-10 23:28:58 +00:00
2014-03-05 01:48:54 +00:00
if ( $this -> created_at_max ) {
2015-01-12 17:01:12 +00:00
$query -> query_where .= sprintf ( " AND user_registered <= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%H:%%i:%%s' ) " , esc_sql ( $this -> created_at_max ) );
2014-03-05 01:48:54 +00:00
}
2013-11-10 23:28:58 +00:00
}
2013-11-06 06:54:19 +00:00
/**
* Wrapper for @ see get_avatar () which doesn ' t simply return
* the URL so we need to pluck it from the HTML img tag
*
2014-05-26 10:48:21 +00:00
* Kudos to https :// github . com / WP - API / WP - API for offering a better solution
*
2013-11-11 00:29:36 +00:00
* @ since 2.1
2013-11-06 06:54:19 +00:00
* @ param string $email the customer ' s email
* @ return string the URL to the customer ' s avatar
*/
private function get_avatar_url ( $email ) {
2014-05-26 10:48:21 +00:00
$avatar_html = get_avatar ( $email );
2013-11-06 06:54:19 +00:00
2014-05-26 10:48:21 +00:00
// Get the URL of the avatar from the provided HTML
preg_match ( '/src=["|\'](.+)[\&|"|\']/U' , $avatar_html , $matches );
2013-11-06 06:54:19 +00:00
2014-05-26 10:48:21 +00:00
if ( isset ( $matches [ 1 ] ) && ! empty ( $matches [ 1 ] ) ) {
return esc_url_raw ( $matches [ 1 ] );
}
2013-11-06 06:54:19 +00:00
2014-05-26 10:51:03 +00:00
return null ;
2013-11-06 06:54:19 +00:00
}
2013-11-11 00:29:36 +00:00
/**
* Validate the request by checking :
*
* 1 ) the ID is a valid integer
* 2 ) the ID returns a valid WP_User
* 3 ) the current user has the proper permissions
*
2014-03-05 17:24:47 +00:00
* @ since 2.1
2013-11-11 00:29:36 +00:00
* @ see WC_API_Resource :: validate_request ()
2014-09-07 23:37:55 +00:00
* @ param integer $id the customer ID
2013-11-11 00:29:36 +00:00
* @ param string $type the request type , unused because this method overrides the parent class
* @ param string $context the context of the request , either `read` , `edit` or `delete`
* @ return int | WP_Error valid user ID or WP_Error if any of the checks fails
*/
protected function validate_request ( $id , $type , $context ) {
2015-01-05 18:06:52 +00:00
try {
$id = absint ( $id );
2014-02-17 14:44:08 +00:00
2015-01-05 18:06:52 +00:00
// validate ID
if ( empty ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_customer_id' , __ ( 'Invalid customer ID' , 'woocommerce' ), 404 );
}
2013-11-11 00:29:36 +00:00
2015-01-05 18:06:52 +00:00
// non-existent IDs return a valid WP_User object with the user ID = 0
$customer = new WP_User ( $id );
2013-11-11 00:29:36 +00:00
2015-01-05 18:06:52 +00:00
if ( 0 === $customer -> ID ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_customer' , __ ( 'Invalid customer' , 'woocommerce' ), 404 );
}
2013-11-11 00:29:36 +00:00
2015-01-05 18:06:52 +00:00
// validate permissions
switch ( $context ) {
case 'read' :
if ( ! current_user_can ( 'list_users' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_customer' , __ ( 'You do not have permission to read this customer' , 'woocommerce' ), 401 );
}
break ;
case 'edit' :
if ( ! current_user_can ( 'edit_users' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_edit_customer' , __ ( 'You do not have permission to edit this customer' , 'woocommerce' ), 401 );
}
break ;
case 'delete' :
if ( ! current_user_can ( 'delete_users' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_delete_customer' , __ ( 'You do not have permission to delete this customer' , 'woocommerce' ), 401 );
}
break ;
}
2013-11-11 00:29:36 +00:00
2015-01-05 18:06:52 +00:00
return $id ;
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2013-11-11 00:29:36 +00:00
}
}
/**
* Check if the current user can read users
*
* @ since 2.1
* @ see WC_API_Resource :: is_readable ()
* @ param int | WP_Post $post unused
* @ return bool true if the current user can read users , false otherwise
*/
protected function is_readable ( $post ) {
return current_user_can ( 'list_users' );
}
2015-05-07 16:09:59 +00:00
/**
* Bulk update or insert customers
* Accepts an array with customers in the formats supported by
* WC_API_Customers -> create_customer () and WC_API_Customers -> edit_customer ()
*
* @ since 2.4 . 0
* @ param array $data
* @ return array
*/
public function bulk ( $data ) {
try {
if ( ! isset ( $data [ 'customers' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_customers_data' , sprintf ( __ ( 'No %1$s data specified to create/edit %1$s' , 'woocommerce' ), 'customers' ), 400 );
}
$data = $data [ 'customers' ];
$limit = apply_filters ( 'woocommerce_api_bulk_limit' , 100 , 'customers' );
// Limit bulk operation
if ( count ( $data ) > $limit ) {
throw new WC_API_Exception ( 'woocommerce_api_customers_request_entity_too_large' , sprintf ( __ ( 'Unable to accept more than %s items for this request' , 'woocommerce' ), $limit ), 413 );
}
$customers = array ();
foreach ( $data as $_customer ) {
$customer_id = 0 ;
// Try to get the customer ID
if ( isset ( $_customer [ 'id' ] ) ) {
$customer_id = intval ( $_customer [ 'id' ] );
}
// Customer exists / edit customer
if ( $customer_id ) {
$edit = $this -> edit_customer ( $customer_id , array ( 'customer' => $_customer ) );
if ( is_wp_error ( $edit ) ) {
$customers [] = array (
'id' => $customer_id ,
'error' => array ( 'code' => $edit -> get_error_code (), 'message' => $edit -> get_error_message () )
);
} else {
$customers [] = $edit [ 'customer' ];
}
}
// Customer don't exists / create customer
else {
$new = $this -> create_customer ( array ( 'customer' => $_customer ) );
if ( is_wp_error ( $new ) ) {
$customers [] = array (
'id' => $customer_id ,
'error' => array ( 'code' => $new -> get_error_code (), 'message' => $new -> get_error_message () )
);
} else {
$customers [] = $new [ 'customer' ];
}
}
}
return array ( 'customers' => apply_filters ( 'woocommerce_api_customers_bulk_response' , $customers , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2013-11-04 06:36:31 +00:00
}