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

https://gitlab.com/campus-academy/krowkaramel · PHP · 312 lines · 270 code · 8 blank · 34 comment · 1 complexity · b8153b7f4e556680751ed5a451a969f5 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_V2_Controller
  16. */
  17. class WC_REST_Customers_Controller extends WC_REST_Customers_V2_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v3';
  24. /**
  25. * Get formatted item data.
  26. *
  27. * @param WC_Data $object WC_Data instance.
  28. *
  29. * @since 3.0.0
  30. * @return array
  31. */
  32. protected function get_formatted_item_data( $object ) {
  33. $data = $object->get_data();
  34. $format_date = array( 'date_created', 'date_modified' );
  35. // Format date values.
  36. foreach ( $format_date as $key ) {
  37. // Date created is stored UTC, date modified is stored WP local time.
  38. $datetime = 'date_created' === $key ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $data[ $key ]->getTimestamp() ) ) : $data[ $key ];
  39. $data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
  40. $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
  41. }
  42. return array(
  43. 'id' => $object->get_id(),
  44. 'date_created' => $data['date_created'],
  45. 'date_created_gmt' => $data['date_created_gmt'],
  46. 'date_modified' => $data['date_modified'],
  47. 'date_modified_gmt' => $data['date_modified_gmt'],
  48. 'email' => $data['email'],
  49. 'first_name' => $data['first_name'],
  50. 'last_name' => $data['last_name'],
  51. 'role' => $data['role'],
  52. 'username' => $data['username'],
  53. 'billing' => $data['billing'],
  54. 'shipping' => $data['shipping'],
  55. 'is_paying_customer' => $data['is_paying_customer'],
  56. 'avatar_url' => $object->get_avatar_url(),
  57. 'meta_data' => $data['meta_data'],
  58. );
  59. }
  60. /**
  61. * Get the Customer's schema, conforming to JSON Schema.
  62. *
  63. * @return array
  64. */
  65. public function get_item_schema() {
  66. $schema = array(
  67. '$schema' => 'http://json-schema.org/draft-04/schema#',
  68. 'title' => 'customer',
  69. 'type' => 'object',
  70. 'properties' => array(
  71. 'id' => array(
  72. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  73. 'type' => 'integer',
  74. 'context' => array( 'view', 'edit' ),
  75. 'readonly' => true,
  76. ),
  77. 'date_created' => array(
  78. 'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ),
  79. 'type' => 'date-time',
  80. 'context' => array( 'view', 'edit' ),
  81. 'readonly' => true,
  82. ),
  83. 'date_created_gmt' => array(
  84. 'description' => __( 'The date the customer was created, as GMT.', 'woocommerce' ),
  85. 'type' => 'date-time',
  86. 'context' => array( 'view', 'edit' ),
  87. 'readonly' => true,
  88. ),
  89. 'date_modified' => array(
  90. 'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ),
  91. 'type' => 'date-time',
  92. 'context' => array( 'view', 'edit' ),
  93. 'readonly' => true,
  94. ),
  95. 'date_modified_gmt' => array(
  96. 'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce' ),
  97. 'type' => 'date-time',
  98. 'context' => array( 'view', 'edit' ),
  99. 'readonly' => true,
  100. ),
  101. 'email' => array(
  102. 'description' => __( 'The email address for the customer.', 'woocommerce' ),
  103. 'type' => 'string',
  104. 'format' => 'email',
  105. 'context' => array( 'view', 'edit' ),
  106. ),
  107. 'first_name' => array(
  108. 'description' => __( 'Customer first name.', 'woocommerce' ),
  109. 'type' => 'string',
  110. 'context' => array( 'view', 'edit' ),
  111. 'arg_options' => array(
  112. 'sanitize_callback' => 'sanitize_text_field',
  113. ),
  114. ),
  115. 'last_name' => array(
  116. 'description' => __( 'Customer last name.', 'woocommerce' ),
  117. 'type' => 'string',
  118. 'context' => array( 'view', 'edit' ),
  119. 'arg_options' => array(
  120. 'sanitize_callback' => 'sanitize_text_field',
  121. ),
  122. ),
  123. 'role' => array(
  124. 'description' => __( 'Customer role.', 'woocommerce' ),
  125. 'type' => 'string',
  126. 'context' => array( 'view', 'edit' ),
  127. 'readonly' => true,
  128. ),
  129. 'username' => array(
  130. 'description' => __( 'Customer login name.', 'woocommerce' ),
  131. 'type' => 'string',
  132. 'context' => array( 'view', 'edit' ),
  133. 'arg_options' => array(
  134. 'sanitize_callback' => 'sanitize_user',
  135. ),
  136. ),
  137. 'password' => array(
  138. 'description' => __( 'Customer password.', 'woocommerce' ),
  139. 'type' => 'string',
  140. 'context' => array( 'edit' ),
  141. ),
  142. 'billing' => array(
  143. 'description' => __( 'List of billing address data.', 'woocommerce' ),
  144. 'type' => 'object',
  145. 'context' => array( 'view', 'edit' ),
  146. 'properties' => array(
  147. 'first_name' => array(
  148. 'description' => __( 'First name.', 'woocommerce' ),
  149. 'type' => 'string',
  150. 'context' => array( 'view', 'edit' ),
  151. ),
  152. 'last_name' => array(
  153. 'description' => __( 'Last name.', 'woocommerce' ),
  154. 'type' => 'string',
  155. 'context' => array( 'view', 'edit' ),
  156. ),
  157. 'company' => array(
  158. 'description' => __( 'Company name.', 'woocommerce' ),
  159. 'type' => 'string',
  160. 'context' => array( 'view', 'edit' ),
  161. ),
  162. 'address_1' => array(
  163. 'description' => __( 'Address line 1', 'woocommerce' ),
  164. 'type' => 'string',
  165. 'context' => array( 'view', 'edit' ),
  166. ),
  167. 'address_2' => array(
  168. 'description' => __( 'Address line 2', 'woocommerce' ),
  169. 'type' => 'string',
  170. 'context' => array( 'view', 'edit' ),
  171. ),
  172. 'city' => array(
  173. 'description' => __( 'City name.', 'woocommerce' ),
  174. 'type' => 'string',
  175. 'context' => array( 'view', 'edit' ),
  176. ),
  177. 'state' => array(
  178. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
  179. 'type' => 'string',
  180. 'context' => array( 'view', 'edit' ),
  181. ),
  182. 'postcode' => array(
  183. 'description' => __( 'Postal code.', 'woocommerce' ),
  184. 'type' => 'string',
  185. 'context' => array( 'view', 'edit' ),
  186. ),
  187. 'country' => array(
  188. 'description' => __( 'ISO code of the country.', 'woocommerce' ),
  189. 'type' => 'string',
  190. 'context' => array( 'view', 'edit' ),
  191. ),
  192. 'email' => array(
  193. 'description' => __( 'Email address.', 'woocommerce' ),
  194. 'type' => 'string',
  195. 'format' => 'email',
  196. 'context' => array( 'view', 'edit' ),
  197. ),
  198. 'phone' => array(
  199. 'description' => __( 'Phone number.', 'woocommerce' ),
  200. 'type' => 'string',
  201. 'context' => array( 'view', 'edit' ),
  202. ),
  203. ),
  204. ),
  205. 'shipping' => array(
  206. 'description' => __( 'List of shipping address data.', 'woocommerce' ),
  207. 'type' => 'object',
  208. 'context' => array( 'view', 'edit' ),
  209. 'properties' => array(
  210. 'first_name' => array(
  211. 'description' => __( 'First name.', 'woocommerce' ),
  212. 'type' => 'string',
  213. 'context' => array( 'view', 'edit' ),
  214. ),
  215. 'last_name' => array(
  216. 'description' => __( 'Last name.', 'woocommerce' ),
  217. 'type' => 'string',
  218. 'context' => array( 'view', 'edit' ),
  219. ),
  220. 'company' => array(
  221. 'description' => __( 'Company name.', 'woocommerce' ),
  222. 'type' => 'string',
  223. 'context' => array( 'view', 'edit' ),
  224. ),
  225. 'address_1' => array(
  226. 'description' => __( 'Address line 1', 'woocommerce' ),
  227. 'type' => 'string',
  228. 'context' => array( 'view', 'edit' ),
  229. ),
  230. 'address_2' => array(
  231. 'description' => __( 'Address line 2', 'woocommerce' ),
  232. 'type' => 'string',
  233. 'context' => array( 'view', 'edit' ),
  234. ),
  235. 'city' => array(
  236. 'description' => __( 'City name.', 'woocommerce' ),
  237. 'type' => 'string',
  238. 'context' => array( 'view', 'edit' ),
  239. ),
  240. 'state' => array(
  241. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),
  242. 'type' => 'string',
  243. 'context' => array( 'view', 'edit' ),
  244. ),
  245. 'postcode' => array(
  246. 'description' => __( 'Postal code.', 'woocommerce' ),
  247. 'type' => 'string',
  248. 'context' => array( 'view', 'edit' ),
  249. ),
  250. 'country' => array(
  251. 'description' => __( 'ISO code of the country.', 'woocommerce' ),
  252. 'type' => 'string',
  253. 'context' => array( 'view', 'edit' ),
  254. ),
  255. 'phone' => array(
  256. 'description' => __( 'Phone number.', 'woocommerce' ),
  257. 'type' => 'string',
  258. 'context' => array( 'view', 'edit' ),
  259. ),
  260. ),
  261. ),
  262. 'is_paying_customer' => array(
  263. 'description' => __( 'Is the customer a paying customer?', 'woocommerce' ),
  264. 'type' => 'bool',
  265. 'context' => array( 'view', 'edit' ),
  266. 'readonly' => true,
  267. ),
  268. 'avatar_url' => array(
  269. 'description' => __( 'Avatar URL.', 'woocommerce' ),
  270. 'type' => 'string',
  271. 'context' => array( 'view', 'edit' ),
  272. 'readonly' => true,
  273. ),
  274. 'meta_data' => array(
  275. 'description' => __( 'Meta data.', 'woocommerce' ),
  276. 'type' => 'array',
  277. 'context' => array( 'view', 'edit' ),
  278. 'items' => array(
  279. 'type' => 'object',
  280. 'properties' => array(
  281. 'id' => array(
  282. 'description' => __( 'Meta ID.', 'woocommerce' ),
  283. 'type' => 'integer',
  284. 'context' => array( 'view', 'edit' ),
  285. 'readonly' => true,
  286. ),
  287. 'key' => array(
  288. 'description' => __( 'Meta key.', 'woocommerce' ),
  289. 'type' => 'string',
  290. 'context' => array( 'view', 'edit' ),
  291. ),
  292. 'value' => array(
  293. 'description' => __( 'Meta value.', 'woocommerce' ),
  294. 'type' => 'mixed',
  295. 'context' => array( 'view', 'edit' ),
  296. ),
  297. ),
  298. ),
  299. ),
  300. ),
  301. );
  302. return $this->add_additional_fields_schema( $schema );
  303. }
  304. }