Skip to content

Commit 88dd341

Browse files
enhance(api versioning): ported api versioning changes from phpclassic/php-shopify
1 parent cded408 commit 88dd341

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/ShopifyResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function __construct($config, $id = null, $parentResourceUrl = '')
121121
$this->config = $config;
122122
$this->id = $id;
123123

124-
$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['AdminUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
124+
$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['ApiUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
125125

126126
if (isset($config['AccessToken'])) {
127127
$this->httpHeaders['X-Shopify-Access-Token'] = $config['AccessToken'];

lib/ShopifySDK.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ class ShopifySDK
142142
*/
143143
public static $timeAllowedForEachApiCall = .5;
144144

145+
/**
146+
* @var string Default Shopify API version
147+
*/
148+
public static $defaultApiVersion = '2022-07';
149+
145150
/**
146151
* Shop / API configurations
147152
*
@@ -223,8 +228,7 @@ class ShopifySDK
223228
public function __construct($config = array())
224229
{
225230
if(!empty($config)) {
226-
$this->config = $config;
227-
$this->setAdminUrl();
231+
ShopifySDK::config($config);
228232
}
229233
}
230234

@@ -285,6 +289,13 @@ public function __call($resourceName, $arguments)
285289
*/
286290
public function config($config)
287291
{
292+
/**
293+
* Reset config to it's initial values
294+
*/
295+
$this->config = array(
296+
'ApiVersion' => self::$defaultApiVersion,
297+
);
298+
288299
foreach ($config as $key => $value) {
289300
$this->config[$key] = $value;
290301
}
@@ -313,6 +324,7 @@ public function setAdminUrl()
313324

314325
//Remove https:// and trailing slash (if provided)
315326
$shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
327+
$apiVersion = $this->config['ApiVersion'];
316328

317329
if(isset($this->config['ApiKey']) && isset($this->config['Password'])) {
318330
$apiKey = $this->config['ApiKey'];
@@ -323,6 +335,7 @@ public function setAdminUrl()
323335
}
324336

325337
$this->config['AdminUrl'] = $adminUrl;
338+
$this->config['ApiUrl'] = $adminUrl . "api/$apiVersion/";
326339

327340
return $adminUrl;
328341
}
@@ -336,6 +349,15 @@ public function getAdminUrl() {
336349
return $this->config['AdminUrl'];
337350
}
338351

352+
/**
353+
* Get the api url of the configured shop
354+
*
355+
* @return string
356+
*/
357+
public function getApiUrl() {
358+
return $this->config['ApiUrl'];
359+
}
360+
339361
/**
340362
* Maintain maximum 2 calls per second to the API
341363
*

0 commit comments

Comments
 (0)