-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass-envato-api.php
322 lines (260 loc) · 9.11 KB
/
class-envato-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* Envato API
*/
if ( ! class_exists( 'Envato_API' ) ) {
class Envato_API {
/**
* The buyer's Username
*
* @var string
*
* @access public
* @since 1.0
*/
public $user_name;
/**
* The buyer's API Key
*
* @var string
*
* @access public
* @since 1.0
*/
public $personal_token;
/**
* The default API URL
*
* @var string
*
* @access private
* @since 1.0
*/
protected $public_url = 'https://api.envato.com/v3/';
/**
* Error messages
*
* @var array
*
* @access public
* @since 1.0
*/
public $errors = array( 'errors' => '' );
/**
* Class contructor method
*
* @param string The buyer's Username
* @param string The buyer's API Key can be accessed on the marketplaces via My Account -> My Settings -> API Key
* @return void Sets error messages if any.
*
* @access public
* @since 1.0
*/
public function __construct( $user_name = '', $personal_token = '' ) {
if ( $user_name == '' ) {
$this->set_error( 'user_name', __( 'Please enter your Envato Marketplace Username.', 'envato-wordpress-toolkit' ) );
}
if ( $personal_token == '' ) {
$this->set_error( 'personal_token', __( 'Please enter your Envato Marketplace Personal Token.', 'envato-wordpress-toolkit' ) );
}
$this->user_name = $user_name;
$this->personal_token = $personal_token;
}
/**
* Retrieve the details for a specific marketplace item.
*
* @param string $item_id The id of the item you need information for.
* @return object Details for the given item.
*
* @access public
* @since 1.0
* @updated 1.3
*/
public function item_details( $item_id, $allow_cache = true, $timeout = 300 ) {
$url = $this->public_url . '/market/catalog/item?id=' . $item_id;
/* set transient ID for later */
$transient = substr( md5( 'item_' . $item_id ), 0, 16 );
if ( $allow_cache ) {
$cache_results = $this->set_cache( $transient, $url, $timeout );
$results = $cache_results;
} else {
$results = $this->remote_request( $url );
}
if ( isset( $results->error ) ) {
$this->set_error( 'error_item_' . $item_id, $results->error );
}
if ( $errors = $this->api_errors() ) {
$this->clear_cache( $transient );
return $errors;
}
if ( isset( $results ) ) {
return $results;
}
return false;
}
/**
* Retrieve the prices for a specific marketplace item.
*
* @param string $item_id The id of the item you need information for.
* @return object Details for the given item.
*
* @access public
*/
public function item_prices( $item_id, $allow_cache = true, $timeout = 300 ) {
$url = str_replace('/v3/', sprintf('/v1/market/item-prices:%s.json', $item_id), $this->public_url);
/* set transient ID for later */
$transient = substr( md5( 'prices_' . $item_id ), 0, 16 );
if ( $allow_cache ) {
$cache_results = $this->set_cache( $transient, $url, $timeout );
$results = $cache_results;
} else {
$results = $this->remote_request( $url );
}
if ( isset( $results->error ) ) {
$this->set_error( 'error_prices_' . $item_id, $results->error );
}
if ( $errors = $this->api_errors() ) {
$this->clear_cache( $transient );
return $errors;
}
if ( isset( $results ) ) {
return $results;
}
return false;
}
/**
* Set cache with the Transients API.
*
* @link http://codex.wordpress.org/Transients_API
*
* @param string Transient ID.
* @param string The URL of the API request.
* @param int Set transient timeout. Default 300 seconds (5 minutes).
* @return mixed
*
* @access public
* @since 1.3
*/
public function set_cache( $transient = '', $url = '', $timeout = 300 ) {
if ( $transient == '' || $url == '' ) {
return false;
}
/* keep the code below cleaner */
$transient = $this->validate_transient( $transient );
$transient_timeout = '_transient_timeout_' . $transient;
/* set original cache before we destroy it */
$old_cache = get_option( $transient_timeout ) < time() ? get_option( $transient ) : '';
/* look for a cached result and return if exists */
if ( false !== $results = get_transient( $transient ) ) {
return $results;
}
/* create the cache and allow filtering before it's saved */
if ( $results = apply_filters( 'envato_api_set_cache', $this->remote_request( $url ), $transient ) ) {
set_transient( $transient, $results, $timeout );
return $results;
}
return false;
}
/**
* Clear cache with the Transients API.
*
* @link http://codex.wordpress.org/Transients_API
*
* @param string Transient ID.
* @return void
*
* @access public
* @since 1.3
*/
public function clear_cache( $transient = '' ) {
delete_transient( $transient );
}
/**
* Helper function to validate transient ID's.
*
* @param string The transient ID.
* @return string Returns a DB safe transient ID.
*
* @access public
* @since 1.3
*/
public function validate_transient( $id = '' ) {
return preg_replace( '/[^A-Za-z0-9\_\-]/i', '', str_replace( ':', '_', $id ) );
}
/**
* Helper function to set error messages.
*
* @param string The error array id.
* @param string The error message.
* @return void
*
* @access public
* @since 1.0
*/
public function set_error( $id, $error ) {
$this->errors['errors'][$id] = $error;
}
/**
* Helper function to return the set errors.
*
* @return array The errors array.
*
* @access public
* @since 1.0
*/
public function api_errors() {
if ( ! empty( $this->errors['errors'] ) ) {
return $this->errors['errors'];
}
}
/**
* Helper function to query the marketplace API via wp_remote_request.
*
* @param string The url to access.
* @return object The results of the wp_remote_request request.
*
* @access private
* @since 1.0
*/
protected function remote_request( $url ) {
if ( empty( $url ) ) {
return false;
}
$args = array(
'headers' => array( 'Accept-Encoding' => '', 'Authorization' => sprintf('Bearer %s', $this->personal_token) ),
'timeout' => 30,
'user-agent' => 'Toolkit/1.7.3',
);
$request = wp_safe_remote_request( $url, $args );
if ( is_wp_error( $request ) ) {
echo $request->get_error_message();
return false;
}
$data = json_decode( $request['body'] );
if ( $request['response']['code'] == 200 ) {
return $data;
} else {
$this->set_error( 'http_code', $request['response']['code'] );
}
if ( isset( $data->error ) ) {
$this->set_error( 'api_error', $data->error );
}
return false;
}
/**
* Helper function to print arrays to the screen ofr testing.
*
* @param array The array to print out
* @return string
*
* @access public
* @since 1.0
*/
public function pretty_print( $array ) {
echo '<pre>';
print_r( $array );
echo '</pre>';
}
}
}
/* End of file class-envato-api.php */