WC_REST_Customer_Downloads_V1_Controller

REST API Customers controller class.

Defined (1)

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

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