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