class-wc-rest-customer-downloads-controller.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * REST API Customer Downloads controller
  4. *
  5. * Handles requests to the /customers/<customer_id>/downloads endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Customers controller class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Customer_Downloads_V1_Controller
  16. */
  17. class WC_REST_Customer_Downloads_Controller extends WC_REST_Customer_Downloads_V1_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Prepare a single download output for response.
  26. *
  27. * @param stdClass $download Download object.
  28. * @param WP_REST_Request $request Request object.
  29. * @return WP_REST_Response $response Response data.
  30. */
  31. public function prepare_item_for_response( $download, $request ) {
  32. $data = array(
  33. 'download_id' => $download->download_id,
  34. 'download_url' => $download->download_url,
  35. 'product_id' => $download->product_id,
  36. 'product_name' => $download->product_name,
  37. 'download_name' => $download->download_name,
  38. 'order_id' => $download->order_id,
  39. 'order_key' => $download->order_key,
  40. 'downloads_remaining' => '' === $download->downloads_remaining ? 'unlimited' : $download->downloads_remaining,
  41. 'access_expires' => $download->access_expires ? wc_rest_prepare_date_response( $download->access_expires ) : 'never',
  42. 'access_expires_gmt' => $download->access_expires ? wc_rest_prepare_date_response( get_gmt_from_date( $download->access_expires ) ) : 'never',
  43. 'file' => $download->file,
  44. );
  45. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  46. $data = $this->add_additional_fields_to_object( $data, $request );
  47. $data = $this->filter_response_by_context( $data, $context );
  48. // Wrap the data in a response object.
  49. $response = rest_ensure_response( $data );
  50. $response->add_links( $this->prepare_links( $download, $request ) );
  51. /**
  52. * Filter customer download data returned from the REST API.
  53. *
  54. * @param WP_REST_Response $response The response object.
  55. * @param stdClass $download Download object used to create response.
  56. * @param WP_REST_Request $request Request object.
  57. */
  58. return apply_filters( 'woocommerce_rest_prepare_customer_download', $response, $download, $request );
  59. }
  60. /**
  61. * Get the Customer Download'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_download',
  69. 'type' => 'object',
  70. 'properties' => array(
  71. 'download_id' => array(
  72. 'description' => __( 'Download ID.', 'woocommerce' ),
  73. 'type' => 'string',
  74. 'context' => array( 'view' ),
  75. 'readonly' => true,
  76. ),
  77. 'download_url' => array(
  78. 'description' => __( 'Download file URL.', 'woocommerce' ),
  79. 'type' => 'string',
  80. 'context' => array( 'view' ),
  81. 'readonly' => true,
  82. ),
  83. 'product_id' => array(
  84. 'description' => __( 'Downloadable product ID.', 'woocommerce' ),
  85. 'type' => 'integer',
  86. 'context' => array( 'view' ),
  87. 'readonly' => true,
  88. ),
  89. 'product_name' => array(
  90. 'description' => __( 'Product name.', 'woocommerce' ),
  91. 'type' => 'string',
  92. 'context' => array( 'view' ),
  93. 'readonly' => true,
  94. ),
  95. 'download_name' => array(
  96. 'description' => __( 'Downloadable file name.', 'woocommerce' ),
  97. 'type' => 'string',
  98. 'context' => array( 'view' ),
  99. 'readonly' => true,
  100. ),
  101. 'order_id' => array(
  102. 'description' => __( 'Order ID.', 'woocommerce' ),
  103. 'type' => 'integer',
  104. 'context' => array( 'view' ),
  105. 'readonly' => true,
  106. ),
  107. 'order_key' => array(
  108. 'description' => __( 'Order key.', 'woocommerce' ),
  109. 'type' => 'string',
  110. 'context' => array( 'view' ),
  111. 'readonly' => true,
  112. ),
  113. 'downloads_remaining' => array(
  114. 'description' => __( 'Number of downloads remaining.', 'woocommerce' ),
  115. 'type' => 'string',
  116. 'context' => array( 'view' ),
  117. 'readonly' => true,
  118. ),
  119. 'access_expires' => array(
  120. 'description' => __( "The date when download access expires, in the site's timezone.", 'woocommerce' ),
  121. 'type' => 'string',
  122. 'context' => array( 'view' ),
  123. 'readonly' => true,
  124. ),
  125. 'access_expires_gmt' => array(
  126. 'description' => __( 'The date when download access expires, as GMT.', 'woocommerce' ),
  127. 'type' => 'string',
  128. 'context' => array( 'view' ),
  129. 'readonly' => true,
  130. ),
  131. 'file' => array(
  132. 'description' => __( 'File details.', 'woocommerce' ),
  133. 'type' => 'object',
  134. 'context' => array( 'view' ),
  135. 'readonly' => true,
  136. 'properties' => array(
  137. 'name' => array(
  138. 'description' => __( 'File name.', 'woocommerce' ),
  139. 'type' => 'string',
  140. 'context' => array( 'view' ),
  141. 'readonly' => true,
  142. ),
  143. 'file' => array(
  144. 'description' => __( 'File URL.', 'woocommerce' ),
  145. 'type' => 'string',
  146. 'context' => array( 'view' ),
  147. 'readonly' => true,
  148. ),
  149. ),
  150. ),
  151. ),
  152. );
  153. return $this->add_additional_fields_schema( $schema );
  154. }
  155. }