/wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 364 lines · 299 code · 12 blank · 53 comment · 3 complexity · d1e58407abd2522f17a750bf0e699f37 MD5 · raw file

  1. <?php
  2. /**
  3. * REST API Customers controller
  4. *
  5. * Handles requests to the /customers endpoint.
  6. *
  7. * @package WooCommerce\RestApi
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Customers controller class.
  13. *
  14. * @package WooCommerce\RestApi
  15. * @extends WC_REST_Customers_V1_Controller
  16. */
  17. class WC_REST_Customers_V2_Controller extends WC_REST_Customers_V1_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Get formatted item data.
  26. *
  27. * @since 3.0.0
  28. * @param WC_Data $object WC_Data instance.
  29. * @return array
  30. */
  31. protected function get_formatted_item_data( $object ) {
  32. $data = $object->get_data();
  33. $format_date = array( 'date_created', 'date_modified' );
  34. // Format date values.
  35. foreach ( $format_date as $key ) {
  36. $datetime = 'date_created' === $key ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $data[ $key ]->getTimestamp() ) ) : $data[ $key ];
  37. $data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
  38. $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
  39. }
  40. return array(
  41. 'id' => $object->get_id(),
  42. 'date_created' => $data['date_created'],
  43. 'date_created_gmt' => $data['date_created_gmt'],
  44. 'date_modified' => $data['date_modified'],
  45. 'date_modified_gmt' => $data['date_modified_gmt'],
  46. 'email' => $data['email'],
  47. 'first_name' => $data['first_name'],
  48. 'last_name' => $data['last_name'],
  49. 'role' => $data['role'],
  50. 'username' => $data['username'],
  51. 'billing' => $data['billing'],
  52. 'shipping' => $data['shipping'],
  53. 'is_paying_customer' => $data['is_paying_customer'],
  54. 'orders_count' => $object->get_order_count(),
  55. 'total_spent' => $object->get_total_spent(),
  56. 'avatar_url' => $object->get_avatar_url(),
  57. 'meta_data' => $data['meta_data'],
  58. );
  59. }
  60. /**
  61. * Prepare a single customer output for response.
  62. *
  63. * @param WP_User $user_data User object.
  64. * @param WP_REST_Request $request Request object.
  65. * @return WP_REST_Response $response Response data.
  66. */
  67. public function prepare_item_for_response( $user_data, $request ) {
  68. $customer = new WC_Customer( $user_data->ID );
  69. $data = $this->get_formatted_item_data( $customer );
  70. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  71. $data = $this->add_additional_fields_to_object( $data, $request );
  72. $data = $this->filter_response_by_context( $data, $context );
  73. $response = rest_ensure_response( $data );
  74. $response->add_links( $this->prepare_links( $user_data ) );
  75. /**
  76. * Filter customer data returned from the REST API.
  77. *
  78. * @param WP_REST_Response $response The response object.
  79. * @param WP_User $user_data User object used to create response.
  80. * @param WP_REST_Request $request Request object.
  81. */
  82. return apply_filters( 'woocommerce_rest_prepare_customer', $response, $user_data, $request );
  83. }
  84. /**
  85. * Update customer meta fields.
  86. *
  87. * @param WC_Customer $customer Customer data.
  88. * @param WP_REST_Request $request Request data.
  89. */
  90. protected function update_customer_meta_fields( $customer, $request ) {
  91. parent::update_customer_meta_fields( $customer, $request );
  92. // Meta data.
  93. if ( isset( $request['meta_data'] ) ) {
  94. if ( is_array( $request['meta_data'] ) ) {
  95. foreach ( $request['meta_data'] as $meta ) {
  96. $customer->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Get the Customer's schema, conforming to JSON Schema.
  103. *
  104. * @return array
  105. */
  106. public function get_item_schema() {
  107. $schema = array(
  108. '$schema' => 'http://json-schema.org/draft-04/schema#',
  109. 'title' => 'customer',
  110. 'type' => 'object',
  111. 'properties' => array(
  112. 'id' => array(
  113. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  114. 'type' => 'integer',
  115. 'context' => array( 'view', 'edit' ),
  116. 'readonly' => true,
  117. ),
  118. 'date_created' => array(
  119. 'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ),
  120. 'type' => 'date-time',
  121. 'context' => array( 'view', 'edit' ),
  122. 'readonly' => true,
  123. ),
  124. 'date_created_gmt' => array(
  125. 'description' => __( 'The date the customer was created, as GMT.', 'woocommerce' ),
  126. 'type' => 'date-time',
  127. 'context' => array( 'view', 'edit' ),
  128. 'readonly' => true,
  129. ),
  130. 'date_modified' => array(
  131. 'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ),
  132. 'type' => 'date-time',
  133. 'context' => array( 'view', 'edit' ),
  134. 'readonly' => true,
  135. ),
  136. 'date_modified_gmt' => array(
  137. 'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce' ),
  138. 'type' => 'date-time',
  139. 'context' => array( 'view', 'edit' ),
  140. 'readonly' => true,
  141. ),
  142. 'email' => array(
  143. 'description' => __( 'The email address for the customer.', 'woocommerce' ),
  144. 'type' => 'string',
  145. 'format' => 'email',
  146. 'context' => array( 'view', 'edit' ),
  147. ),
  148. 'first_name' => array(
  149. 'description' => __( 'Customer first name.', 'woocommerce' ),
  150. 'type' => 'string',
  151. 'context' => array( 'view', 'edit' ),
  152. 'arg_options' => array(
  153. 'sanitize_callback' => 'sanitize_text_field',
  154. ),
  155. ),
  156. 'last_name' => array(
  157. 'description' => __( 'Customer last name.', 'woocommerce' ),
  158. 'type' => 'string',
  159. 'context' => array( 'view', 'edit' ),
  160. 'arg_options' => array(
  161. 'sanitize_callback' => 'sanitize_text_field',
  162. ),
  163. ),
  164. 'role' => array(
  165. 'description' => __( 'Customer role.', 'woocommerce' ),
  166. 'type' => 'string',
  167. 'context' => array( 'view', 'edit' ),
  168. 'readonly' => true,
  169. ),
  170. 'username' => array(
  171. 'description' => __( 'Customer login name.', 'woocommerce' ),
  172. 'type' => 'string',
  173. 'context' => array( 'view', 'edit' ),
  174. 'arg_options' => array(
  175. 'sanitize_callback' => 'sanitize_user',
  176. ),
  177. ),
  178. 'password' => array(
  179. 'description' => __( 'Customer password.', 'woocommerce' ),
  180. 'type' => 'string',
  181. 'context' => array( 'edit' ),
  182. ),
  183. 'billing' => array(
  184. 'description' => __( 'List of billing address data.', 'woocommerce' ),
  185. 'type' => 'object',
  186. 'context' => array( 'view', 'edit' ),
  187. 'properties' => array(
  188. 'first_name' => array(
  189. 'description' => __( 'First name.', 'woocommerce' ),
  190. 'type' => 'string',
  191. 'context' => array( 'view', 'edit' ),
  192. ),
  193. 'last_name' => array(
  194. 'description' => __( 'Last name.', 'woocommerce' ),
  195. 'type' => 'string',
  196. 'context' => array( 'view', 'edit' ),
  197. ),
  198. 'company' => array(
  199. 'description' => __( 'Company name.', 'woocommerce' ),
  200. 'type' => 'string',
  201. 'context' => array( 'view', 'edit' ),
  202. ),
  203. 'address_1' => array(
  204. 'description' => __( 'Address line 1', 'woocommerce' ),
  205. 'type' => 'string',
  206. 'context' => array( 'view', 'edit' ),
  207. ),
  208. 'address_2' => array(
  209. 'description' => __( 'Address line 2', 'woocommerce' ),
  210. 'type' => 'string',
  211. 'context' => array( 'view', 'edit' ),
  212. ),
  213. 'city' => array(
  214. 'description' => __( 'City name.', 'woocommerce' ),
  215. 'type' => 'string',
  216. 'context' => array( 'view', 'edit' ),
  217. ),
  218. 'state' => array(
  219. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
  220. 'type' => 'string',
  221. 'context' => array( 'view', 'edit' ),
  222. ),
  223. 'postcode' => array(
  224. 'description' => __( 'Postal code.', 'woocommerce' ),
  225. 'type' => 'string',
  226. 'context' => array( 'view', 'edit' ),
  227. ),
  228. 'country' => array(
  229. 'description' => __( 'ISO code of the country.', 'woocommerce' ),
  230. 'type' => 'string',
  231. 'context' => array( 'view', 'edit' ),
  232. ),
  233. 'email' => array(
  234. 'description' => __( 'Email address.', 'woocommerce' ),
  235. 'type' => 'string',
  236. 'format' => 'email',
  237. 'context' => array( 'view', 'edit' ),
  238. ),
  239. 'phone' => array(
  240. 'description' => __( 'Phone number.', 'woocommerce' ),
  241. 'type' => 'string',
  242. 'context' => array( 'view', 'edit' ),
  243. ),
  244. ),
  245. ),
  246. 'shipping' => array(
  247. 'description' => __( 'List of shipping address data.', 'woocommerce' ),
  248. 'type' => 'object',
  249. 'context' => array( 'view', 'edit' ),
  250. 'properties' => array(
  251. 'first_name' => array(
  252. 'description' => __( 'First name.', 'woocommerce' ),
  253. 'type' => 'string',
  254. 'context' => array( 'view', 'edit' ),
  255. ),
  256. 'last_name' => array(
  257. 'description' => __( 'Last name.', 'woocommerce' ),
  258. 'type' => 'string',
  259. 'context' => array( 'view', 'edit' ),
  260. ),
  261. 'company' => array(
  262. 'description' => __( 'Company name.', 'woocommerce' ),
  263. 'type' => 'string',
  264. 'context' => array( 'view', 'edit' ),
  265. ),
  266. 'address_1' => array(
  267. 'description' => __( 'Address line 1', 'woocommerce' ),
  268. 'type' => 'string',
  269. 'context' => array( 'view', 'edit' ),
  270. ),
  271. 'address_2' => array(
  272. 'description' => __( 'Address line 2', 'woocommerce' ),
  273. 'type' => 'string',
  274. 'context' => array( 'view', 'edit' ),
  275. ),
  276. 'city' => array(
  277. 'description' => __( 'City name.', 'woocommerce' ),
  278. 'type' => 'string',
  279. 'context' => array( 'view', 'edit' ),
  280. ),
  281. 'state' => array(
  282. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
  283. 'type' => 'string',
  284. 'context' => array( 'view', 'edit' ),
  285. ),
  286. 'postcode' => array(
  287. 'description' => __( 'Postal code.', 'woocommerce' ),
  288. 'type' => 'string',
  289. 'context' => array( 'view', 'edit' ),
  290. ),
  291. 'country' => array(
  292. 'description' => __( 'ISO code of the country.', 'woocommerce' ),
  293. 'type' => 'string',
  294. 'context' => array( 'view', 'edit' ),
  295. ),
  296. ),
  297. ),
  298. 'is_paying_customer' => array(
  299. 'description' => __( 'Is the customer a paying customer?', 'woocommerce' ),
  300. 'type' => 'bool',
  301. 'context' => array( 'view', 'edit' ),
  302. 'readonly' => true,
  303. ),
  304. 'orders_count' => array(
  305. 'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ),
  306. 'type' => 'integer',
  307. 'context' => array( 'view', 'edit' ),
  308. 'readonly' => true,
  309. ),
  310. 'total_spent' => array(
  311. 'description' => __( 'Total amount spent.', 'woocommerce' ),
  312. 'type' => 'string',
  313. 'context' => array( 'view', 'edit' ),
  314. 'readonly' => true,
  315. ),
  316. 'avatar_url' => array(
  317. 'description' => __( 'Avatar URL.', 'woocommerce' ),
  318. 'type' => 'string',
  319. 'context' => array( 'view', 'edit' ),
  320. 'readonly' => true,
  321. ),
  322. 'meta_data' => array(
  323. 'description' => __( 'Meta data.', 'woocommerce' ),
  324. 'type' => 'array',
  325. 'context' => array( 'view', 'edit' ),
  326. 'items' => array(
  327. 'type' => 'object',
  328. 'properties' => array(
  329. 'id' => array(
  330. 'description' => __( 'Meta ID.', 'woocommerce' ),
  331. 'type' => 'integer',
  332. 'context' => array( 'view', 'edit' ),
  333. 'readonly' => true,
  334. ),
  335. 'key' => array(
  336. 'description' => __( 'Meta key.', 'woocommerce' ),
  337. 'type' => 'string',
  338. 'context' => array( 'view', 'edit' ),
  339. ),
  340. 'value' => array(
  341. 'description' => __( 'Meta value.', 'woocommerce' ),
  342. 'type' => 'mixed',
  343. 'context' => array( 'view', 'edit' ),
  344. ),
  345. ),
  346. ),
  347. ),
  348. ),
  349. );
  350. return $this->add_additional_fields_schema( $schema );
  351. }
  352. }