WC_REST_Customers_V1_Controller

REST API Customers controller class.

Defined (1)

The class is defined in the following location(s).

/includes/api/v1/class-wc-rest-customers-controller.php  
  1. class WC_REST_Customers_V1_Controller extends WC_REST_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v1'; 
  8.  
  9. /** 
  10. * Route base. 
  11. * @var string 
  12. */ 
  13. protected $rest_base = 'customers'; 
  14.  
  15. /** 
  16. * Register the routes for customers. 
  17. */ 
  18. public function register_routes() { 
  19. register_rest_route( $this->namespace, '/' . $this->rest_base, array( 
  20. array( 
  21. 'methods' => WP_REST_Server::READABLE,  
  22. 'callback' => array( $this, 'get_items' ),  
  23. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  24. 'args' => $this->get_collection_params(),  
  25. ),  
  26. array( 
  27. 'methods' => WP_REST_Server::CREATABLE,  
  28. 'callback' => array( $this, 'create_item' ),  
  29. 'permission_callback' => array( $this, 'create_item_permissions_check' ),  
  30. 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( 
  31. 'email' => array( 
  32. 'required' => true,  
  33. 'type' => 'string',  
  34. 'description' => __( 'New user email address.', 'woocommerce' ),  
  35. ),  
  36. 'username' => array( 
  37. 'required' => 'no' === get_option( 'woocommerce_registration_generate_username', 'yes' ),  
  38. 'description' => __( 'New user username.', 'woocommerce' ),  
  39. 'type' => 'string',  
  40. ),  
  41. 'password' => array( 
  42. 'required' => 'no' === get_option( 'woocommerce_registration_generate_password', 'no' ),  
  43. 'description' => __( 'New user password.', 'woocommerce' ),  
  44. 'type' => 'string',  
  45. ),  
  46. ) ),  
  47. ),  
  48. 'schema' => array( $this, 'get_public_item_schema' ),  
  49. ) ); 
  50.  
  51. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 
  52. 'args' => array( 
  53. 'id' => array( 
  54. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  55. 'type' => 'integer',  
  56. ),  
  57. ),  
  58. array( 
  59. 'methods' => WP_REST_Server::READABLE,  
  60. 'callback' => array( $this, 'get_item' ),  
  61. 'permission_callback' => array( $this, 'get_item_permissions_check' ),  
  62. 'args' => array( 
  63. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  64. ),  
  65. ),  
  66. array( 
  67. 'methods' => WP_REST_Server::EDITABLE,  
  68. 'callback' => array( $this, 'update_item' ),  
  69. 'permission_callback' => array( $this, 'update_item_permissions_check' ),  
  70. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  71. ),  
  72. array( 
  73. 'methods' => WP_REST_Server::DELETABLE,  
  74. 'callback' => array( $this, 'delete_item' ),  
  75. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),  
  76. 'args' => array( 
  77. 'force' => array( 
  78. 'default' => false,  
  79. 'type' => 'boolean',  
  80. 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),  
  81. ),  
  82. 'reassign' => array( 
  83. 'default' => 0,  
  84. 'type' => 'integer',  
  85. 'description' => __( 'ID to reassign posts to.', 'woocommerce' ),  
  86. ),  
  87. ),  
  88. ),  
  89. 'schema' => array( $this, 'get_public_item_schema' ),  
  90. ) ); 
  91.  
  92. register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( 
  93. array( 
  94. 'methods' => WP_REST_Server::EDITABLE,  
  95. 'callback' => array( $this, 'batch_items' ),  
  96. 'permission_callback' => array( $this, 'batch_items_permissions_check' ),  
  97. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  98. ),  
  99. 'schema' => array( $this, 'get_public_batch_schema' ),  
  100. ) ); 
  101.  
  102. /** 
  103. * Check whether a given request has permission to read customers. 
  104. * @param WP_REST_Request $request Full details about the request. 
  105. * @return WP_Error|boolean 
  106. */ 
  107. public function get_items_permissions_check( $request ) { 
  108. if ( ! wc_rest_check_user_permissions( 'read' ) ) { 
  109. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  110.  
  111. return true; 
  112.  
  113. /** 
  114. * Check if a given request has access create customers. 
  115. * @param WP_REST_Request $request Full details about the request. 
  116. * @return boolean 
  117. */ 
  118. public function create_item_permissions_check( $request ) { 
  119. if ( ! wc_rest_check_user_permissions( 'create' ) ) { 
  120. return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  121.  
  122. return true; 
  123.  
  124. /** 
  125. * Check if a given request has access to read a customer. 
  126. * @param WP_REST_Request $request Full details about the request. 
  127. * @return WP_Error|boolean 
  128. */ 
  129. public function get_item_permissions_check( $request ) { 
  130. $id = (int) $request['id']; 
  131.  
  132. if ( ! wc_rest_check_user_permissions( 'read', $id ) ) { 
  133. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  134.  
  135. return true; 
  136.  
  137. /** 
  138. * Check if a given request has access update a customer. 
  139. * @param WP_REST_Request $request Full details about the request. 
  140. * @return boolean 
  141. */ 
  142. public function update_item_permissions_check( $request ) { 
  143. $id = (int) $request['id']; 
  144.  
  145. if ( ! wc_rest_check_user_permissions( 'edit', $id ) ) { 
  146. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  147.  
  148. return true; 
  149.  
  150. /** 
  151. * Check if a given request has access delete a customer. 
  152. * @param WP_REST_Request $request Full details about the request. 
  153. * @return boolean 
  154. */ 
  155. public function delete_item_permissions_check( $request ) { 
  156. $id = (int) $request['id']; 
  157.  
  158. if ( ! wc_rest_check_user_permissions( 'delete', $id ) ) { 
  159. return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  160.  
  161. return true; 
  162.  
  163. /** 
  164. * Check if a given request has access batch create, update and delete items. 
  165. * @param WP_REST_Request $request Full details about the request. 
  166. * @return boolean 
  167. */ 
  168. public function batch_items_permissions_check( $request ) { 
  169. if ( ! wc_rest_check_user_permissions( 'batch' ) ) { 
  170. return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  171.  
  172. return true; 
  173.  
  174. /** 
  175. * Get all customers. 
  176. * @param WP_REST_Request $request Full details about the request. 
  177. * @return WP_Error|WP_REST_Response 
  178. */ 
  179. public function get_items( $request ) { 
  180. $prepared_args = array(); 
  181. $prepared_args['exclude'] = $request['exclude']; 
  182. $prepared_args['include'] = $request['include']; 
  183. $prepared_args['order'] = $request['order']; 
  184. $prepared_args['number'] = $request['per_page']; 
  185. if ( ! empty( $request['offset'] ) ) { 
  186. $prepared_args['offset'] = $request['offset']; 
  187. } else { 
  188. $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; 
  189. $orderby_possibles = array( 
  190. 'id' => 'ID',  
  191. 'include' => 'include',  
  192. 'name' => 'display_name',  
  193. 'registered_date' => 'registered',  
  194. ); 
  195. $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ]; 
  196. $prepared_args['search'] = $request['search']; 
  197.  
  198. if ( '' !== $prepared_args['search'] ) { 
  199. $prepared_args['search'] = '*' . $prepared_args['search'] . '*'; 
  200.  
  201. // Filter by email. 
  202. if ( ! empty( $request['email'] ) ) { 
  203. $prepared_args['search'] = $request['email']; 
  204. $prepared_args['search_columns'] = array( 'user_email' ); 
  205.  
  206. // Filter by role. 
  207. if ( 'all' !== $request['role'] ) { 
  208. $prepared_args['role'] = $request['role']; 
  209.  
  210. /** 
  211. * Filter arguments, before passing to WP_User_Query, when querying users via the REST API. 
  212. * @see https://developer.wordpress.org/reference/classes/wp_user_query/ 
  213. * @param array $prepared_args Array of arguments for WP_User_Query. 
  214. * @param WP_REST_Request $request The current request. 
  215. */ 
  216. $prepared_args = apply_filters( 'woocommerce_rest_customer_query', $prepared_args, $request ); 
  217.  
  218. $query = new WP_User_Query( $prepared_args ); 
  219.  
  220. $users = array(); 
  221. foreach ( $query->results as $user ) { 
  222. $data = $this->prepare_item_for_response( $user, $request ); 
  223. $users[] = $this->prepare_response_for_collection( $data ); 
  224.  
  225. $response = rest_ensure_response( $users ); 
  226.  
  227. // Store pagation values for headers then unset for count query. 
  228. $per_page = (int) $prepared_args['number']; 
  229. $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); 
  230.  
  231. $prepared_args['fields'] = 'ID'; 
  232.  
  233. $total_users = $query->get_total(); 
  234. if ( $total_users < 1 ) { 
  235. // Out-of-bounds, run the query again without LIMIT for total count. 
  236. unset( $prepared_args['number'] ); 
  237. unset( $prepared_args['offset'] ); 
  238. $count_query = new WP_User_Query( $prepared_args ); 
  239. $total_users = $count_query->get_total(); 
  240. $response->header( 'X-WP-Total', (int) $total_users ); 
  241. $max_pages = ceil( $total_users / $per_page ); 
  242. $response->header( 'X-WP-TotalPages', (int) $max_pages ); 
  243.  
  244. $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) ); 
  245. if ( $page > 1 ) { 
  246. $prev_page = $page - 1; 
  247. if ( $prev_page > $max_pages ) { 
  248. $prev_page = $max_pages; 
  249. $prev_link = add_query_arg( 'page', $prev_page, $base ); 
  250. $response->link_header( 'prev', $prev_link ); 
  251. if ( $max_pages > $page ) { 
  252. $next_page = $page + 1; 
  253. $next_link = add_query_arg( 'page', $next_page, $base ); 
  254. $response->link_header( 'next', $next_link ); 
  255.  
  256. return $response; 
  257.  
  258. /** 
  259. * Create a single customer. 
  260. * @param WP_REST_Request $request Full details about the request. 
  261. * @return WP_Error|WP_REST_Response 
  262. */ 
  263. public function create_item( $request ) { 
  264. try { 
  265. if ( ! empty( $request['id'] ) ) { 
  266. throw new WC_REST_Exception( 'woocommerce_rest_customer_exists', __( 'Cannot create existing resource.', 'woocommerce' ), 400 ); 
  267.  
  268. // Sets the username. 
  269. $request['username'] = ! empty( $request['username'] ) ? $request['username'] : ''; 
  270.  
  271. // Sets the password. 
  272. $request['password'] = ! empty( $request['password'] ) ? $request['password'] : ''; 
  273.  
  274. // Create customer. 
  275. $customer = new WC_Customer; 
  276. $customer->set_username( $request['username'] ); 
  277. $customer->set_password( $request['password'] ); 
  278. $customer->set_email( $request['email'] ); 
  279. $customer->save(); 
  280.  
  281. if ( ! $customer->get_id() ) { 
  282. throw new WC_REST_Exception( 'woocommerce_rest_cannot_create', __( 'This resource cannot be created.', 'woocommerce' ), 400 ); 
  283.  
  284. $this->update_customer_meta_fields( $customer, $request ); 
  285. $customer->save(); 
  286.  
  287. $user_data = get_userdata( $customer->get_id() ); 
  288. $this->update_additional_fields_for_object( $user_data, $request ); 
  289.  
  290. /** 
  291. * Fires after a customer is created or updated via the REST API. 
  292. * @param WP_User $user_data Data used to create the customer. 
  293. * @param WP_REST_Request $request Request object. 
  294. * @param boolean $creating True when creating customer, false when updating customer. 
  295. */ 
  296. do_action( 'woocommerce_rest_insert_customer', $user_data, $request, true ); 
  297.  
  298. $request->set_param( 'context', 'edit' ); 
  299. $response = $this->prepare_item_for_response( $user_data, $request ); 
  300. $response = rest_ensure_response( $response ); 
  301. $response->set_status( 201 ); 
  302. $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id() ) ) ); 
  303.  
  304. return $response; 
  305. } catch ( Exception $e ) { 
  306. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  307.  
  308. /** 
  309. * Get a single customer. 
  310. * @param WP_REST_Request $request Full details about the request. 
  311. * @return WP_Error|WP_REST_Response 
  312. */ 
  313. public function get_item( $request ) { 
  314. $id = (int) $request['id']; 
  315. $user_data = get_userdata( $id ); 
  316.  
  317. if ( empty( $id ) || empty( $user_data->ID ) ) { 
  318. return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) ); 
  319.  
  320. $customer = $this->prepare_item_for_response( $user_data, $request ); 
  321. $response = rest_ensure_response( $customer ); 
  322.  
  323. return $response; 
  324.  
  325. /** 
  326. * Update a single user. 
  327. * @param WP_REST_Request $request Full details about the request. 
  328. * @return WP_Error|WP_REST_Response 
  329. */ 
  330. public function update_item( $request ) { 
  331. try { 
  332. $id = (int) $request['id']; 
  333. $customer = new WC_Customer( $id ); 
  334.  
  335. if ( ! $customer->get_id() ) { 
  336. throw new WC_REST_Exception( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), 400 ); 
  337.  
  338. if ( ! empty( $request['email'] ) && email_exists( $request['email'] ) && $request['email'] !== $customer->get_email() ) { 
  339. throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), 400 ); 
  340.  
  341. if ( ! empty( $request['username'] ) && $request['username'] !== $customer->get_username() ) { 
  342. throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), 400 ); 
  343.  
  344. // Customer email. 
  345. if ( isset( $request['email'] ) ) { 
  346. $customer->set_email( sanitize_email( $request['email'] ) ); 
  347.  
  348. // Customer password. 
  349. if ( isset( $request['password'] ) ) { 
  350. $customer->set_password( wc_clean( $request['password'] ) ); 
  351.  
  352. $this->update_customer_meta_fields( $customer, $request ); 
  353. $customer->save(); 
  354.  
  355. $user_data = get_userdata( $customer->get_id() ); 
  356. $this->update_additional_fields_for_object( $user_data, $request ); 
  357.  
  358. if ( ! is_user_member_of_blog( $user_data->ID ) ) { 
  359. $user_data->add_role( 'customer' ); 
  360.  
  361. /** 
  362. * Fires after a customer is created or updated via the REST API. 
  363. * @param WP_User $customer Data used to create the customer. 
  364. * @param WP_REST_Request $request Request object. 
  365. * @param boolean $creating True when creating customer, false when updating customer. 
  366. */ 
  367. do_action( 'woocommerce_rest_insert_customer', $user_data, $request, false ); 
  368.  
  369. $request->set_param( 'context', 'edit' ); 
  370. $response = $this->prepare_item_for_response( $user_data, $request ); 
  371. $response = rest_ensure_response( $response ); 
  372. return $response; 
  373. } catch ( Exception $e ) { 
  374. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  375.  
  376. /** 
  377. * Delete a single customer. 
  378. * @param WP_REST_Request $request Full details about the request. 
  379. * @return WP_Error|WP_REST_Response 
  380. */ 
  381. public function delete_item( $request ) { 
  382. $id = (int) $request['id']; 
  383. $reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null; 
  384. $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 
  385.  
  386. // We don't support trashing for this type, error out. 
  387. if ( ! $force ) { 
  388. return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Customers do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) ); 
  389.  
  390. $user_data = get_userdata( $id ); 
  391. if ( ! $user_data ) { 
  392. return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) ); 
  393.  
  394. if ( ! empty( $reassign ) ) { 
  395. if ( $reassign === $id || ! get_userdata( $reassign ) ) { 
  396. return new WP_Error( 'woocommerce_rest_customer_invalid_reassign', __( 'Invalid resource id for reassignment.', 'woocommerce' ), array( 'status' => 400 ) ); 
  397.  
  398. $request->set_param( 'context', 'edit' ); 
  399. $response = $this->prepare_item_for_response( $user_data, $request ); 
  400.  
  401. /** Include admin customer functions to get access to wp_delete_user() */ 
  402. require_once ABSPATH . 'wp-admin/includes/user.php'; 
  403.  
  404. $customer = new WC_Customer( $id ); 
  405.  
  406. if ( ! is_null( $reassign ) ) { 
  407. $result = $customer->delete_and_reassign( $reassign ); 
  408. } else { 
  409. $result = $customer->delete(); 
  410.  
  411. if ( ! $result ) { 
  412. return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) ); 
  413.  
  414. /** 
  415. * Fires after a customer is deleted via the REST API. 
  416. * @param WP_User $user_data User data. 
  417. * @param WP_REST_Response $response The response returned from the API. 
  418. * @param WP_REST_Request $request The request sent to the API. 
  419. */ 
  420. do_action( 'woocommerce_rest_delete_customer', $user_data, $response, $request ); 
  421.  
  422. return $response; 
  423.  
  424. /** 
  425. * Prepare a single customer output for response. 
  426. * @param WP_User $user_data User object. 
  427. * @param WP_REST_Request $request Request object. 
  428. * @return WP_REST_Response $response Response data. 
  429. */ 
  430. public function prepare_item_for_response( $user_data, $request ) { 
  431. $customer = new WC_Customer( $user_data->ID ); 
  432. $_data = $customer->get_data(); 
  433. $last_order = wc_get_customer_last_order( $customer->get_id() ); 
  434. $format_date = array( 'date_created', 'date_modified' ); 
  435.  
  436. // Format date values. 
  437. foreach ( $format_date as $key ) { 
  438. $_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( $_data[ $key ] ) : null; // v1 API used UTC. 
  439.  
  440. $data = array( 
  441. 'id' => $_data['id'],  
  442. 'date_created' => $_data['date_created'],  
  443. 'date_modified' => $_data['date_modified'],  
  444. 'email' => $_data['email'],  
  445. 'first_name' => $_data['first_name'],  
  446. 'last_name' => $_data['last_name'],  
  447. 'username' => $_data['username'],  
  448. 'last_order' => array( 
  449. 'id' => is_object( $last_order ) ? $last_order->get_id() : null,  
  450. 'date' => is_object( $last_order ) ? wc_rest_prepare_date_response( $last_order->get_date_created() ) : null, // v1 API used UTC. 
  451. ),  
  452. 'orders_count' => $customer->get_order_count(),  
  453. 'total_spent' => $customer->get_total_spent(),  
  454. 'avatar_url' => $customer->get_avatar_url(),  
  455. 'billing' => $_data['billing'],  
  456. 'shipping' => $_data['shipping'],  
  457. ); 
  458.  
  459. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  460. $data = $this->add_additional_fields_to_object( $data, $request ); 
  461. $data = $this->filter_response_by_context( $data, $context ); 
  462. $response = rest_ensure_response( $data ); 
  463. $response->add_links( $this->prepare_links( $user_data ) ); 
  464.  
  465. /** 
  466. * Filter customer data returned from the REST API. 
  467. * @param WP_REST_Response $response The response object. 
  468. * @param WP_User $user_data User object used to create response. 
  469. * @param WP_REST_Request $request Request object. 
  470. */ 
  471. return apply_filters( 'woocommerce_rest_prepare_customer', $response, $user_data, $request ); 
  472.  
  473. /** 
  474. * Update customer meta fields. 
  475. * @param WC_Customer $customer 
  476. * @param WP_REST_Request $request 
  477. */ 
  478. protected function update_customer_meta_fields( $customer, $request ) { 
  479. $schema = $this->get_item_schema(); 
  480.  
  481. // Customer first name. 
  482. if ( isset( $request['first_name'] ) ) { 
  483. $customer->set_first_name( wc_clean( $request['first_name'] ) ); 
  484.  
  485. // Customer last name. 
  486. if ( isset( $request['last_name'] ) ) { 
  487. $customer->set_last_name( wc_clean( $request['last_name'] ) ); 
  488.  
  489. // Customer billing address. 
  490. if ( isset( $request['billing'] ) ) { 
  491. foreach ( array_keys( $schema['properties']['billing']['properties'] ) as $field ) { 
  492. if ( isset( $request['billing'][ $field ] ) && is_callable( array( $customer, "set_billing_{$field}" ) ) ) { 
  493. $customer->{"set_billing_{$field}"}( $request['billing'][ $field ] ); 
  494.  
  495. // Customer shipping address. 
  496. if ( isset( $request['shipping'] ) ) { 
  497. foreach ( array_keys( $schema['properties']['shipping']['properties'] ) as $field ) { 
  498. if ( isset( $request['shipping'][ $field ] ) && is_callable( array( $customer, "set_shipping_{$field}" ) ) ) { 
  499. $customer->{"set_shipping_{$field}"}( $request['shipping'][ $field ] ); 
  500.  
  501. /** 
  502. * Prepare links for the request. 
  503. * @param WP_User $customer Customer object. 
  504. * @return array Links for the given customer. 
  505. */ 
  506. protected function prepare_links( $customer ) { 
  507. $links = array( 
  508. 'self' => array( 
  509. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->ID ) ),  
  510. ),  
  511. 'collection' => array( 
  512. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),  
  513. ),  
  514. ); 
  515.  
  516. return $links; 
  517.  
  518. /** 
  519. * Get the Customer's schema, conforming to JSON Schema. 
  520. * @return array 
  521. */ 
  522. public function get_item_schema() { 
  523. $schema = array( 
  524. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  525. 'title' => 'customer',  
  526. 'type' => 'object',  
  527. 'properties' => array( 
  528. 'id' => array( 
  529. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  530. 'type' => 'integer',  
  531. 'context' => array( 'view', 'edit' ),  
  532. 'readonly' => true,  
  533. ),  
  534. 'date_created' => array( 
  535. 'description' => __( 'The date the customer was created, as GMT.', 'woocommerce' ),  
  536. 'type' => 'date-time',  
  537. 'context' => array( 'view', 'edit' ),  
  538. 'readonly' => true,  
  539. ),  
  540. 'date_modified' => array( 
  541. 'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce' ),  
  542. 'type' => 'date-time',  
  543. 'context' => array( 'view', 'edit' ),  
  544. 'readonly' => true,  
  545. ),  
  546. 'email' => array( 
  547. 'description' => __( 'The email address for the customer.', 'woocommerce' ),  
  548. 'type' => 'string',  
  549. 'format' => 'email',  
  550. 'context' => array( 'view', 'edit' ),  
  551. ),  
  552. 'first_name' => array( 
  553. 'description' => __( 'Customer first name.', 'woocommerce' ),  
  554. 'type' => 'string',  
  555. 'context' => array( 'view', 'edit' ),  
  556. 'arg_options' => array( 
  557. 'sanitize_callback' => 'sanitize_text_field',  
  558. ),  
  559. ),  
  560. 'last_name' => array( 
  561. 'description' => __( 'Customer last name.', 'woocommerce' ),  
  562. 'type' => 'string',  
  563. 'context' => array( 'view', 'edit' ),  
  564. 'arg_options' => array( 
  565. 'sanitize_callback' => 'sanitize_text_field',  
  566. ),  
  567. ),  
  568. 'username' => array( 
  569. 'description' => __( 'Customer login name.', 'woocommerce' ),  
  570. 'type' => 'string',  
  571. 'context' => array( 'view', 'edit' ),  
  572. 'arg_options' => array( 
  573. 'sanitize_callback' => 'sanitize_user',  
  574. ),  
  575. ),  
  576. 'password' => array( 
  577. 'description' => __( 'Customer password.', 'woocommerce' ),  
  578. 'type' => 'string',  
  579. 'context' => array( 'edit' ),  
  580. ),  
  581. 'last_order' => array( 
  582. 'description' => __( 'Last order data.', 'woocommerce' ),  
  583. 'type' => 'object',  
  584. 'context' => array( 'view', 'edit' ),  
  585. 'readonly' => true,  
  586. 'properties' => array( 
  587. 'id' => array( 
  588. 'description' => __( 'Last order ID.', 'woocommerce' ),  
  589. 'type' => 'integer',  
  590. 'context' => array( 'view', 'edit' ),  
  591. 'readonly' => true,  
  592. ),  
  593. 'date' => array( 
  594. 'description' => __( 'The date of the customer last order, as GMT.', 'woocommerce' ),  
  595. 'type' => 'date-time',  
  596. 'context' => array( 'view', 'edit' ),  
  597. 'readonly' => true,  
  598. ),  
  599. ),  
  600. ),  
  601. 'orders_count' => array( 
  602. 'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ),  
  603. 'type' => 'integer',  
  604. 'context' => array( 'view', 'edit' ),  
  605. 'readonly' => true,  
  606. ),  
  607. 'total_spent' => array( 
  608. 'description' => __( 'Total amount spent.', 'woocommerce' ),  
  609. 'type' => 'string',  
  610. 'context' => array( 'view', 'edit' ),  
  611. 'readonly' => true,  
  612. ),  
  613. 'avatar_url' => array( 
  614. 'description' => __( 'Avatar URL.', 'woocommerce' ),  
  615. 'type' => 'string',  
  616. 'context' => array( 'view', 'edit' ),  
  617. 'readonly' => true,  
  618. ),  
  619. 'billing' => array( 
  620. 'description' => __( 'List of billing address data.', 'woocommerce' ),  
  621. 'type' => 'object',  
  622. 'context' => array( 'view', 'edit' ),  
  623. 'properties' => array( 
  624. 'first_name' => array( 
  625. 'description' => __( 'First name.', 'woocommerce' ),  
  626. 'type' => 'string',  
  627. 'context' => array( 'view', 'edit' ),  
  628. ),  
  629. 'last_name' => array( 
  630. 'description' => __( 'Last name.', 'woocommerce' ),  
  631. 'type' => 'string',  
  632. 'context' => array( 'view', 'edit' ),  
  633. ),  
  634. 'company' => array( 
  635. 'description' => __( 'Company name.', 'woocommerce' ),  
  636. 'type' => 'string',  
  637. 'context' => array( 'view', 'edit' ),  
  638. ),  
  639. 'address_1' => array( 
  640. 'description' => __( 'Address line 1.', 'woocommerce' ),  
  641. 'type' => 'string',  
  642. 'context' => array( 'view', 'edit' ),  
  643. ),  
  644. 'address_2' => array( 
  645. 'description' => __( 'Address line 2.', 'woocommerce' ),  
  646. 'type' => 'string',  
  647. 'context' => array( 'view', 'edit' ),  
  648. ),  
  649. 'city' => array( 
  650. 'description' => __( 'City name.', 'woocommerce' ),  
  651. 'type' => 'string',  
  652. 'context' => array( 'view', 'edit' ),  
  653. ),  
  654. 'state' => array( 
  655. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),  
  656. 'type' => 'string',  
  657. 'context' => array( 'view', 'edit' ),  
  658. ),  
  659. 'postcode' => array( 
  660. 'description' => __( 'Postal code.', 'woocommerce' ),  
  661. 'type' => 'string',  
  662. 'context' => array( 'view', 'edit' ),  
  663. ),  
  664. 'country' => array( 
  665. 'description' => __( 'ISO code of the country.', 'woocommerce' ),  
  666. 'type' => 'string',  
  667. 'context' => array( 'view', 'edit' ),  
  668. ),  
  669. 'email' => array( 
  670. 'description' => __( 'Email address.', 'woocommerce' ),  
  671. 'type' => 'string',  
  672. 'format' => 'email',  
  673. 'context' => array( 'view', 'edit' ),  
  674. ),  
  675. 'phone' => array( 
  676. 'description' => __( 'Phone number.', 'woocommerce' ),  
  677. 'type' => 'string',  
  678. 'context' => array( 'view', 'edit' ),  
  679. ),  
  680. ),  
  681. ),  
  682. 'shipping' => array( 
  683. 'description' => __( 'List of shipping address data.', 'woocommerce' ),  
  684. 'type' => 'object',  
  685. 'context' => array( 'view', 'edit' ),  
  686. 'properties' => array( 
  687. 'first_name' => array( 
  688. 'description' => __( 'First name.', 'woocommerce' ),  
  689. 'type' => 'string',  
  690. 'context' => array( 'view', 'edit' ),  
  691. ),  
  692. 'last_name' => array( 
  693. 'description' => __( 'Last name.', 'woocommerce' ),  
  694. 'type' => 'string',  
  695. 'context' => array( 'view', 'edit' ),  
  696. ),  
  697. 'company' => array( 
  698. 'description' => __( 'Company name.', 'woocommerce' ),  
  699. 'type' => 'string',  
  700. 'context' => array( 'view', 'edit' ),  
  701. ),  
  702. 'address_1' => array( 
  703. 'description' => __( 'Address line 1.', 'woocommerce' ),  
  704. 'type' => 'string',  
  705. 'context' => array( 'view', 'edit' ),  
  706. ),  
  707. 'address_2' => array( 
  708. 'description' => __( 'Address line 2.', 'woocommerce' ),  
  709. 'type' => 'string',  
  710. 'context' => array( 'view', 'edit' ),  
  711. ),  
  712. 'city' => array( 
  713. 'description' => __( 'City name.', 'woocommerce' ),  
  714. 'type' => 'string',  
  715. 'context' => array( 'view', 'edit' ),  
  716. ),  
  717. 'state' => array( 
  718. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),  
  719. 'type' => 'string',  
  720. 'context' => array( 'view', 'edit' ),  
  721. ),  
  722. 'postcode' => array( 
  723. 'description' => __( 'Postal code.', 'woocommerce' ),  
  724. 'type' => 'string',  
  725. 'context' => array( 'view', 'edit' ),  
  726. ),  
  727. 'country' => array( 
  728. 'description' => __( 'ISO code of the country.', 'woocommerce' ),  
  729. 'type' => 'string',  
  730. 'context' => array( 'view', 'edit' ),  
  731. ),  
  732. ),  
  733. ),  
  734. ),  
  735. ); 
  736.  
  737. return $this->add_additional_fields_schema( $schema ); 
  738.  
  739. /** 
  740. * Get role names. 
  741. * @return array 
  742. */ 
  743. protected function get_role_names() { 
  744. global $wp_roles; 
  745.  
  746. return array_keys( $wp_roles->role_names ); 
  747.  
  748. /** 
  749. * Get the query params for collections. 
  750. * @return array 
  751. */ 
  752. public function get_collection_params() { 
  753. $params = parent::get_collection_params(); 
  754.  
  755. $params['context']['default'] = 'view'; 
  756.  
  757. $params['exclude'] = array( 
  758. 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),  
  759. 'type' => 'array',  
  760. 'items' => array( 
  761. 'type' => 'integer',  
  762. ),  
  763. 'default' => array(),  
  764. 'sanitize_callback' => 'wp_parse_id_list',  
  765. ); 
  766. $params['include'] = array( 
  767. 'description' => __( 'Limit result set to specific IDs.', 'woocommerce' ),  
  768. 'type' => 'array',  
  769. 'items' => array( 
  770. 'type' => 'integer',  
  771. ),  
  772. 'default' => array(),  
  773. 'sanitize_callback' => 'wp_parse_id_list',  
  774. ); 
  775. $params['offset'] = array( 
  776. 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),  
  777. 'type' => 'integer',  
  778. 'sanitize_callback' => 'absint',  
  779. 'validate_callback' => 'rest_validate_request_arg',  
  780. ); 
  781. $params['order'] = array( 
  782. 'default' => 'asc',  
  783. 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),  
  784. 'enum' => array( 'asc', 'desc' ),  
  785. 'sanitize_callback' => 'sanitize_key',  
  786. 'type' => 'string',  
  787. 'validate_callback' => 'rest_validate_request_arg',  
  788. ); 
  789. $params['orderby'] = array( 
  790. 'default' => 'name',  
  791. 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),  
  792. 'enum' => array( 
  793. 'id',  
  794. 'include',  
  795. 'name',  
  796. 'registered_date',  
  797. ),  
  798. 'sanitize_callback' => 'sanitize_key',  
  799. 'type' => 'string',  
  800. 'validate_callback' => 'rest_validate_request_arg',  
  801. ); 
  802. $params['email'] = array( 
  803. 'description' => __( 'Limit result set to resources with a specific email.', 'woocommerce' ),  
  804. 'type' => 'string',  
  805. 'format' => 'email',  
  806. 'validate_callback' => 'rest_validate_request_arg',  
  807. ); 
  808. $params['role'] = array( 
  809. 'description' => __( 'Limit result set to resources with a specific role.', 'woocommerce' ),  
  810. 'type' => 'string',  
  811. 'default' => 'customer',  
  812. 'enum' => array_merge( array( 'all' ), $this->get_role_names() ),  
  813. 'validate_callback' => 'rest_validate_request_arg',  
  814. ); 
  815. return $params;