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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * REST API Customer Downloads controller
  4. *
  5. * Handles requests to the /customers/<customer_id>/downloads endpoint.
  6. *
  7. * @author WooThemes
  8. * @category API
  9. * @package WooCommerce/API
  10. * @since 3.0.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * REST API Customers controller class.
  17. *
  18. * @package WooCommerce/API
  19. * @extends WC_REST_Controller
  20. */
  21. class WC_REST_Customer_Downloads_V1_Controller extends WC_REST_Controller {
  22. /**
  23. * Endpoint namespace.
  24. *
  25. * @var string
  26. */
  27. protected $namespace = 'wc/v1';
  28. /**
  29. * Route base.
  30. *
  31. * @var string
  32. */
  33. protected $rest_base = 'customers/(?P<customer_id>[\d]+)/downloads';
  34. /**
  35. * Register the routes for customers.
  36. */
  37. public function register_routes() {
  38. register_rest_route( $this->namespace, '/' . $this->rest_base, array(
  39. 'args' => array(
  40. 'customer_id' => array(
  41. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  42. 'type' => 'integer',
  43. ),
  44. ),
  45. array(
  46. 'methods' => WP_REST_Server::READABLE,
  47. 'callback' => array( $this, 'get_items' ),
  48. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  49. 'args' => $this->get_collection_params(),
  50. ),
  51. 'schema' => array( $this, 'get_public_item_schema' ),
  52. ) );
  53. }
  54. /**
  55. * Check whether a given request has permission to read customers.
  56. *
  57. * @param WP_REST_Request $request Full details about the request.
  58. * @return WP_Error|boolean
  59. */
  60. public function get_items_permissions_check( $request ) {
  61. $customer = get_user_by( 'id', (int) $request['customer_id'] );
  62. if ( ! $customer ) {
  63. return new WP_Error( 'woocommerce_rest_customer_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
  64. }
  65. if ( ! wc_rest_check_user_permissions( 'read', $customer->get_id() ) ) {
  66. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  67. }
  68. return true;
  69. }
  70. /**
  71. * Get all customer downloads.
  72. *
  73. * @param WP_REST_Request $request
  74. * @return array
  75. */
  76. public function get_items( $request ) {
  77. $downloads = wc_get_customer_available_downloads( (int) $request['customer_id'] );
  78. $data = array();
  79. foreach ( $downloads as $download_data ) {
  80. $download = $this->prepare_item_for_response( (object) $download_data, $request );
  81. $download = $this->prepare_response_for_collection( $download );
  82. $data[] = $download;
  83. }
  84. return rest_ensure_response( $data );
  85. }
  86. /**
  87. * Prepare a single download output for response.
  88. *
  89. * @param stdObject $download Download object.
  90. * @param WP_REST_Request $request Request object.
  91. * @return WP_REST_Response $response Response data.
  92. */
  93. public function prepare_item_for_response( $download, $request ) {
  94. $data = (array) $download;
  95. $data['access_expires'] = $data['access_expires'] ? wc_rest_prepare_date_response( $data['access_expires'] ) : 'never';
  96. $data['downloads_remaining'] = '' === $data['downloads_remaining'] ? 'unlimited' : $data['downloads_remaining'];
  97. // Remove "product_name" since it's new in 3.0.
  98. unset( $data['product_name'] );
  99. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  100. $data = $this->add_additional_fields_to_object( $data, $request );
  101. $data = $this->filter_response_by_context( $data, $context );
  102. // Wrap the data in a response object.
  103. $response = rest_ensure_response( $data );
  104. $response->add_links( $this->prepare_links( $download, $request ) );
  105. /**
  106. * Filter customer download data returned from the REST API.
  107. *
  108. * @param WP_REST_Response $response The response object.
  109. * @param stdObject $download Download object used to create response.
  110. * @param WP_REST_Request $request Request object.
  111. */
  112. return apply_filters( 'woocommerce_rest_prepare_customer_download', $response, $download, $request );
  113. }
  114. /**
  115. * Prepare links for the request.
  116. *
  117. * @param stdClass $download Download object.
  118. * @param WP_REST_Request $request Request object.
  119. * @return array Links for the given customer download.
  120. */
  121. protected function prepare_links( $download, $request ) {
  122. $base = str_replace( '(?P<customer_id>[\d]+)', $request['customer_id'], $this->rest_base );
  123. $links = array(
  124. 'collection' => array(
  125. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
  126. ),
  127. 'product' => array(
  128. 'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $download->product_id ) ),
  129. ),
  130. 'order' => array(
  131. 'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $download->order_id ) ),
  132. ),
  133. );
  134. return $links;
  135. }
  136. /**
  137. * Get the Customer Download's schema, conforming to JSON Schema.
  138. *
  139. * @return array
  140. */
  141. public function get_item_schema() {
  142. $schema = array(
  143. '$schema' => 'http://json-schema.org/draft-04/schema#',
  144. 'title' => 'customer_download',
  145. 'type' => 'object',
  146. 'properties' => array(
  147. 'download_url' => array(
  148. 'description' => __( 'Download file URL.', 'woocommerce' ),
  149. 'type' => 'string',
  150. 'context' => array( 'view' ),
  151. 'readonly' => true,
  152. ),
  153. 'download_id' => array(
  154. 'description' => __( 'Download ID (MD5).', 'woocommerce' ),
  155. 'type' => 'string',
  156. 'context' => array( 'view' ),
  157. 'readonly' => true,
  158. ),
  159. 'product_id' => array(
  160. 'description' => __( 'Downloadable product ID.', 'woocommerce' ),
  161. 'type' => 'integer',
  162. 'context' => array( 'view' ),
  163. 'readonly' => true,
  164. ),
  165. 'download_name' => array(
  166. 'description' => __( 'Downloadable file name.', 'woocommerce' ),
  167. 'type' => 'string',
  168. 'context' => array( 'view' ),
  169. 'readonly' => true,
  170. ),
  171. 'order_id' => array(
  172. 'description' => __( 'Order ID.', 'woocommerce' ),
  173. 'type' => 'integer',
  174. 'context' => array( 'view' ),
  175. 'readonly' => true,
  176. ),
  177. 'order_key' => array(
  178. 'description' => __( 'Order key.', 'woocommerce' ),
  179. 'type' => 'string',
  180. 'context' => array( 'view' ),
  181. 'readonly' => true,
  182. ),
  183. 'downloads_remaining' => array(
  184. 'description' => __( 'Number of downloads remaining.', 'woocommerce' ),
  185. 'type' => 'string',
  186. 'context' => array( 'view' ),
  187. 'readonly' => true,
  188. ),
  189. 'access_expires' => array(
  190. 'description' => __( "The date when download access expires, in the site's timezone.", 'woocommerce' ),
  191. 'type' => 'string',
  192. 'context' => array( 'view' ),
  193. 'readonly' => true,
  194. ),
  195. 'file' => array(
  196. 'description' => __( 'File details.', 'woocommerce' ),
  197. 'type' => 'object',
  198. 'context' => array( 'view' ),
  199. 'readonly' => true,
  200. 'properties' => array(
  201. 'name' => array(
  202. 'description' => __( 'File name.', 'woocommerce' ),
  203. 'type' => 'string',
  204. 'context' => array( 'view' ),
  205. 'readonly' => true,
  206. ),
  207. 'file' => array(
  208. 'description' => __( 'File URL.', 'woocommerce' ),
  209. 'type' => 'string',
  210. 'context' => array( 'view' ),
  211. 'readonly' => true,
  212. ),
  213. ),
  214. ),
  215. ),
  216. );
  217. return $this->add_additional_fields_schema( $schema );
  218. }
  219. /**
  220. * Get the query params for collections.
  221. *
  222. * @return array
  223. */
  224. public function get_collection_params() {
  225. return array(
  226. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  227. );
  228. }
  229. }