WC_REST_Customer_Downloads_Controller

REST API Customers controller class.

Defined (1)

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

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