Skip to content

Commit 08997e1

Browse files
committed
1.2 release
Added support for the situations if Envato API is down, plus added detailed error message handler.
1 parent 5a9c341 commit 08997e1

File tree

7 files changed

+277
-220
lines changed

7 files changed

+277
-220
lines changed

Libraries/EnvatoAPIManager.php

Lines changed: 97 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
class EnvatoAPIManager
1616
{
1717
protected $debugMode = 0;
18-
protected $debugMessages = array();
19-
protected $okayMessages = array();
20-
protected $errorMessages = array();
18+
protected $savedOkayMessages = array();
19+
protected $savedErrorMessages = array();
2120
protected $username = '';
2221
protected $apiKey = '';
2322
protected $personalToken = '';
@@ -42,26 +41,14 @@ public function inDebug()
4241
return ($this->debugMode >= 1 ? TRUE : FALSE);
4342
}
4443

45-
public function flushMessages()
44+
public function getSavedOkayMessages()
4645
{
47-
$this->debugMessages = array();
48-
$this->okayMessages = array();
49-
$this->errorMessages = array();
46+
return $this->savedOkayMessages;
5047
}
5148

52-
public function getDebugMessages()
49+
public function getSavedErrorMessages()
5350
{
54-
return $this->debugMessages;
55-
}
56-
57-
public function getOkayMessages()
58-
{
59-
return $this->okayMessages;
60-
}
61-
62-
public function getErrorMessages()
63-
{
64-
return $this->errorMessages;
51+
return $this->savedErrorMessages;
6552
}
6653

6754

@@ -94,16 +81,6 @@ private function normalize($string)
9481
/* Methods, that are using Envato Edge API */
9582
/* -------------------------------------------------------------------------------------- */
9683

97-
/**
98-
* @uses EnvatoEdgeAPI
99-
* @param $paramPurchaseCode
100-
* @return bool
101-
*/
102-
public function isValidLicense($paramPurchaseCode)
103-
{
104-
return $this->getLicenseDetails($paramPurchaseCode) !== FALSE ? TRUE : FALSE;
105-
}
106-
10784
/**
10885
* @uses EnvatoEdgeAPI
10986
* @param string $paramPurchaseCode
@@ -121,14 +98,18 @@ public function getLicenseDetails($paramPurchaseCode)
12198
} else if($validPurchaseCode != '' && !isset($this->cachedLicenses[$validPurchaseCode]))
12299
{
123100
// Call Edge API - this is a quicker path, and does not require personal token
124-
$objAPI = new EnvatoEdgeAPI($this->username, $this->apiKey);
125-
$licenseDetails = $objAPI->getLicenseDetails($paramPurchaseCode);
101+
$objEnvatoAPI = new EnvatoEdgeAPI($this->username, $this->apiKey);
102+
$licenseDetails = $objEnvatoAPI->getLicenseDetails($paramPurchaseCode);
126103

127104
if($licenseDetails != FALSE)
128105
{
129106
// Add to cache, but only if it is not there yet
130107
$this->cachedLicenses[$validPurchaseCode] = $licenseDetails;
131108
}
109+
110+
// Save okay/error messages
111+
$this->savedOkayMessages = $objEnvatoAPI->getErrorMessages();
112+
$this->savedErrorMessages = $objEnvatoAPI->getErrorMessages();
132113
}
133114

134115
return $licenseDetails;
@@ -142,6 +123,7 @@ public function getLicenseDetails($paramPurchaseCode)
142123
/**
143124
* Get any Envato user details
144125
* @note Username here, of course, can be different from
126+
* @uses EnvatoMarketAPI
145127
* @param $paramUsername
146128
* @return array|false
147129
*/
@@ -161,27 +143,13 @@ public function getUserDetails($paramUsername)
161143

162144
// Get user details for specified username
163145
$userDetails = $objEnvatoAPI->getUser($validUsername);
164-
}
165-
166-
return $userDetails;
167-
}
168146

169-
public function getLicensesByItemId($paramEnvatoItemId)
170-
{
171-
// Load license codes
172-
$this->getPurchasedItemsWithDetails('wordpress-plugins');
173-
$this->getPurchasedItemsWithDetails('wordpress-themes');
174-
175-
$licenses = array();
176-
foreach($this->cachedLicenses AS $purchaseCode => $licenseDetails)
177-
{
178-
if(isset($licenseDetails['envato_item_id']) && $licenseDetails['envato_item_id'] == $paramEnvatoItemId)
179-
{
180-
$licenses[] = $licenseDetails;
181-
}
147+
// Save okay/error messages
148+
$this->savedOkayMessages = $objEnvatoAPI->getErrorMessages();
149+
$this->savedErrorMessages = $objEnvatoAPI->getErrorMessages();
182150
}
183151

184-
return $licenses;
152+
return $userDetails;
185153
}
186154

187155
public function getPurchasedPluginsWithDetails()
@@ -196,6 +164,7 @@ public function getPurchasedThemesWithDetails()
196164

197165
/**
198166
* Get the list of plugins or themes for specified personal token, that were purchased in Envato Store
167+
* @uses EnvatoMarketAPI
199168
* @param $paramFilterBy - 'wordpress-plugins' or 'wordpress-themes'
200169
* @return array
201170
*/
@@ -214,10 +183,11 @@ private function getPurchasedItemsWithDetails($paramFilterBy)
214183
// Get all purchased plugins of this customer by his token
215184
// NOTE: Username parameter here is optional, we use it only to match the output with Edge API
216185
$itemsAndTheirPurchases = $objEnvatoAPI->getItemsAndTheirPurchases($paramFilterBy, $this->username);
217-
$purchasedItemsWithDetails = $itemsAndTheirPurchases['items'];
186+
$purchasedItemsWithDetails = isset($itemsAndTheirPurchases['items']) ? $itemsAndTheirPurchases['items'] : array();
187+
$purchases = isset($itemsAndTheirPurchases['purchases']) ? $itemsAndTheirPurchases['purchases'] : array();
218188

219189
// Add to items cache if needed
220-
foreach($itemsAndTheirPurchases['items'] AS $envatoItemId => $itemDetails)
190+
foreach($purchasedItemsWithDetails AS $envatoItemId => $itemDetails)
221191
{
222192
if(!isset($this->cachedItems[$envatoItemId]))
223193
{
@@ -227,14 +197,18 @@ private function getPurchasedItemsWithDetails($paramFilterBy)
227197
}
228198

229199
// Add to licenses cache if needed
230-
foreach($itemsAndTheirPurchases['purchases'] AS $purchaseCode => $licenseDetails)
200+
foreach($purchases AS $purchaseCode => $licenseDetails)
231201
{
232202
if(!isset($this->cachedLicenses[$purchaseCode]))
233203
{
234204
// No data exist for this purchase - add it
235205
$this->cachedLicenses[$purchaseCode] = $licenseDetails;
236206
}
237207
}
208+
209+
// Save okay/error messages
210+
$this->savedOkayMessages = $objEnvatoAPI->getErrorMessages();
211+
$this->savedErrorMessages = $objEnvatoAPI->getErrorMessages();
238212
} else
239213
{
240214
// Load from cache
@@ -256,6 +230,7 @@ private function getPurchasedItemsWithDetails($paramFilterBy)
256230
/**
257231
* Get details of single Envato item for specified personal token.
258232
* @note returns item details only if it was purchased (that can be either theme or a plugin)
233+
* @uses EnvatoMarketAPI
259234
* @param int $paramEnvatoItemId
260235
* @return array|FALSE
261236
*/
@@ -277,11 +252,58 @@ public function getItemDetailsIfPurchased($paramEnvatoItemId)
277252

278253
// Add item to cache
279254
$this->cachedItems[$validEnvatoItemId] = $itemDetails;
255+
256+
// Save okay/error messages
257+
$this->savedOkayMessages = $objEnvatoAPI->getErrorMessages();
258+
$this->savedErrorMessages = $objEnvatoAPI->getErrorMessages();
280259
}
281260

282261
return $itemDetails;
283262
}
284263

264+
/**
265+
* @uses EnvatoMarketAPI
266+
* @param $paramEnvatoItemId - required, unless the purchase code is provided
267+
* @param string $paramPurchaseCode - usually we don't need that, but in case if somebody will need
268+
* to download this way, we keep this parameter here
269+
* @return string
270+
*/
271+
public function getDownloadUrlIfPurchased($paramEnvatoItemId = 0, $paramPurchaseCode = '')
272+
{
273+
$downloadURL = '';
274+
$validEnvatoItemId = !is_array($paramEnvatoItemId) ? intval($paramEnvatoItemId) : 0;
275+
276+
if($validEnvatoItemId > 0 && isset($this->cachedDownloadURLs[$validEnvatoItemId]))
277+
{
278+
// Take download url from cache
279+
$downloadURL = $this->cachedDownloadURLs[$validEnvatoItemId];
280+
} else if($this->personalToken != '' && ($validEnvatoItemId > 0 || $paramPurchaseCode != ''))
281+
{
282+
// Call Market API
283+
$objEnvatoAPI = new EnvatoMarketAPI($this->personalToken);
284+
285+
$downloadURL = $objEnvatoAPI->getDownload($validEnvatoItemId, $paramPurchaseCode);
286+
287+
// Add to cache
288+
$this->cachedDownloadURLs[$validEnvatoItemId] = $downloadURL;
289+
290+
// Save okay/error messages
291+
$this->savedOkayMessages = $objEnvatoAPI->getErrorMessages();
292+
$this->savedErrorMessages = $objEnvatoAPI->getErrorMessages();
293+
}
294+
295+
return $downloadURL;
296+
}
297+
298+
/* -------------------------------------------------------------------------------------- */
299+
/* Extended methods */
300+
/* -------------------------------------------------------------------------------------- */
301+
302+
public function isValidLicense($paramPurchaseCode)
303+
{
304+
return $this->getLicenseDetails($paramPurchaseCode) !== FALSE ? TRUE : FALSE;
305+
}
306+
285307
public function getItemName($paramEnvatoItemId)
286308
{
287309
$itemDetails = $this->getItemDetailsIfPurchased($paramEnvatoItemId);
@@ -329,40 +351,29 @@ public function checkPurchaseIsTheme($paramEnvatoItemId)
329351
return $purchaseIsTheme;
330352
}
331353

332-
/**
333-
* @param $paramEnvatoItemId - required, unless the purchase code is provided
334-
* @param string $paramPurchaseCode - usually we don't need that, but in case if somebody will need
335-
* to download this way, we keep this parameter here
336-
* @return string
337-
*/
338-
public function getDownloadUrlIfPurchased($paramEnvatoItemId = 0, $paramPurchaseCode = '')
339-
{
340-
$downloadURL = '';
341-
$validEnvatoItemId = !is_array($paramEnvatoItemId) ? intval($paramEnvatoItemId) : 0;
342354

343-
if($validEnvatoItemId > 0 && isset($this->cachedDownloadURLs[$validEnvatoItemId]))
344-
{
345-
// Take download url from cache
346-
$downloadURL = $this->cachedDownloadURLs[$validEnvatoItemId];
347-
} else if($this->personalToken != '' && ($validEnvatoItemId > 0 || $paramPurchaseCode != ''))
348-
{
349-
// Call Market API
350-
$objEnvatoAPI = new EnvatoMarketAPI($this->personalToken);
355+
/* -------------------------------------------------------------------------------------- */
356+
/* Search methods */
357+
/* -------------------------------------------------------------------------------------- */
351358

352-
$downloadURL = $objEnvatoAPI->getDownload($validEnvatoItemId, $paramPurchaseCode);
359+
public function getLicensesByItemId($paramEnvatoItemId)
360+
{
361+
// Load license codes
362+
$this->getPurchasedItemsWithDetails('wordpress-plugins');
363+
$this->getPurchasedItemsWithDetails('wordpress-themes');
353364

354-
// Add to cache
355-
$this->cachedDownloadURLs[$validEnvatoItemId] = $downloadURL;
365+
$licenses = array();
366+
foreach($this->cachedLicenses AS $purchaseCode => $licenseDetails)
367+
{
368+
if(isset($licenseDetails['envato_item_id']) && $licenseDetails['envato_item_id'] == $paramEnvatoItemId)
369+
{
370+
$licenses[] = $licenseDetails;
371+
}
356372
}
357373

358-
return $downloadURL;
374+
return $licenses;
359375
}
360376

361-
362-
/* -------------------------------------------------------------------------------------- */
363-
/* Search methods */
364-
/* -------------------------------------------------------------------------------------- */
365-
366377
/**
367378
* Get item id by plugin name and plugin author, but only if that plugin was purchased (based on personal token)
368379
* @param string $paramPluginName
@@ -375,10 +386,10 @@ public function getItemIdByPluginAndAuthorIfPurchased($paramPluginName, $paramPl
375386
$purchasedPlugins = $this->getPurchasedItemsWithDetails('wordpress-plugins');
376387
foreach($purchasedPlugins AS $purchasedPlugin)
377388
{
378-
if ($this->normalize($purchasedPlugin['name']) === $this->normalize($paramPluginName) &&
379-
$this->normalize($purchasedPlugin['author']) === $this->normalize($paramPluginAuthor)
389+
if (isset($purchasedPlugin['name']) && $this->normalize($purchasedPlugin['name']) === $this->normalize($paramPluginName) &&
390+
isset($purchasedPlugin['author']) && $this->normalize($purchasedPlugin['author']) === $this->normalize($paramPluginAuthor)
380391
) {
381-
$envatoItemId = $purchasedPlugin['envato_item_id'];
392+
$envatoItemId = isset($purchasedPlugin['envato_item_id']) ? $purchasedPlugin['envato_item_id'] : 0;
382393
}
383394
}
384395

@@ -395,12 +406,12 @@ public function getItemIdByThemeAndAuthorIfPurchased($paramThemeName, $paramThem
395406
{
396407
$envatoItemId = 0;
397408
$purchasedThemes = $this->getPurchasedItemsWithDetails('wordpress-themes');
398-
foreach($purchasedThemes AS $purchaseTheme)
409+
foreach($purchasedThemes AS $purchasedTheme)
399410
{
400-
if ($this->normalize($purchaseTheme['name']) === $this->normalize($paramThemeName) &&
401-
$this->normalize($purchaseTheme['author']) === $this->normalize($paramThemeAuthor)
411+
if (isset($purchasedTheme['name']) && $this->normalize($purchasedTheme['name']) === $this->normalize($paramThemeName) &&
412+
isset($purchasedTheme['author']) && $this->normalize($purchasedTheme['author']) === $this->normalize($paramThemeAuthor)
402413
) {
403-
$envatoItemId = $purchaseTheme['envato_item_id'];
414+
$envatoItemId = isset($purchasedTheme['envato_item_id']) ? $purchasedTheme['envato_item_id']: 0;
404415
}
405416
}
406417

Libraries/EnvatoEdgeAPI.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
if(!class_exists('EnvatoEdgeAPI')):
1313
class EnvatoEdgeAPI
1414
{
15-
//const API_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'; // Original
16-
const API_AGENT = 'EnvatoToolkit/1.0';
15+
const VERSION = '1.2';
16+
const API_AGENT = 'EnvatoToolkit/%s';
1717

1818
protected $debugMode = 0;
1919
protected $debugMessages = array();
@@ -34,7 +34,7 @@ public function __construct($paramUsername, $paramAPIKey)
3434
*/
3535
public function __clone()
3636
{
37-
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'envato-edge-api' ), '1.0.0' );
37+
_doing_it_wrong(__FUNCTION__, esc_html__('Cloning is not allowed.', 'envato-toolkit'), static::VERSION);
3838
}
3939

4040
/**
@@ -43,7 +43,7 @@ public function __clone()
4343
*/
4444
public function __wakeup()
4545
{
46-
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'envato-edge-api' ), '1.0.0' );
46+
_doing_it_wrong(__FUNCTION__, esc_html__('Wake-up is not allowed.', 'envato-toolkit'), static::VERSION);
4747
}
4848

4949
public function inDebug()
@@ -93,7 +93,7 @@ public function getLicenseDetails($paramPurchaseCode)
9393
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
9494

9595
// Set the user agent
96-
curl_setopt($ch, CURLOPT_USERAGENT, static::API_AGENT);
96+
curl_setopt($ch, CURLOPT_USERAGENT, sprintf(static::API_AGENT, static::VERSION));
9797
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // wait for connection in seconds
9898
curl_setopt($ch, CURLOPT_TIMEOUT, 20); // timeout in seconds
9999
// Decode returned JSON
@@ -124,6 +124,10 @@ public function getLicenseDetails($paramPurchaseCode)
124124
if(isset($output['verify-purchase']) && sizeof($output['verify-purchase']) > 0)
125125
{
126126
$licenseDetails = $this->normalizeLicense($output['verify-purchase'], $sanitizedPurchaseCode);
127+
} else
128+
{
129+
$this->errorMessages[] = sprintf(__('\'verify-purchase\' key is empty or not exist', 'envato-toolkit'), esc_url_raw($url));
130+
$this->errorMessages[] = sprintf(__('Failed URL: %s', 'envato-toolkit'), esc_url_raw($url));
127131
}
128132

129133
return $licenseDetails;

0 commit comments

Comments
 (0)