Skip to content

Commit bd33053

Browse files
committed
Functions for autoset and get AdminUrl
1 parent 5b40ea3 commit bd33053

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

lib/AuthHelper.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@
1212

1313
class AuthHelper
1414
{
15-
/**
16-
* Return the admin url, based on a given shop url
17-
*
18-
* @param string $shopUrl
19-
* @param string $apiKey
20-
* @param string $apiPassword
21-
* @return string
22-
*/
23-
public static function getAdminUrl($shopUrl, $apiKey = null, $apiPassword = null)
24-
{
25-
//Remove https:// and trailing slash (if provided)
26-
$shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
27-
28-
if($apiKey && $apiPassword) {
29-
$adminUrl = "https://$apiKey:$apiPassword@$shopUrl/admin/";
30-
} else {
31-
$adminUrl = "https://$shopUrl/admin/";
32-
}
33-
return $adminUrl;
34-
}
35-
3615
/**
3716
* Get the url of the current page
3817
*
@@ -130,7 +109,7 @@ public static function createAuthRequest($scopes, $redirectUrl = null)
130109
if (is_array($scopes)) {
131110
$scopes = join(',', $scopes);
132111
}
133-
$authUrl = $config['ApiUrl'] . 'oauth/authorize?client_id=' . $config['ApiKey'] . '&redirect_uri=' . $redirectUrl . "&scope=$scopes";
112+
$authUrl = $config['AdminUrl'] . 'oauth/authorize?client_id=' . $config['ApiKey'] . '&redirect_uri=' . $redirectUrl . "&scope=$scopes";
134113

135114
header("Location: $authUrl");
136115
}
@@ -158,7 +137,7 @@ public static function getAccessToken()
158137
'code' => $_GET['code'],
159138
);
160139

161-
$response = HttpRequestJson::post($config['ApiUrl'] . 'oauth/access_token', $data);
140+
$response = HttpRequestJson::post($config['AdminUrl'] . 'oauth/access_token', $data);
162141

163142
return isset($response['access_token']) ? $response['access_token'] : null;
164143
}

lib/ShopifyAPI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function __construct($id = null, $parentResourceUrl = '')
112112

113113
$config = ShopifyClient::$config;
114114

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

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

lib/ShopifyClient.php

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ShopifyClient
7070
*
7171
* @var array
7272
*/
73-
public static $config;
73+
public static $config = array();
7474

7575
/**
7676
* List of available resources which can be called from this client
@@ -137,15 +137,12 @@ class ShopifyClient
137137
*
138138
* @return void
139139
*/
140-
public function __construct($config)
140+
public function __construct($config = array())
141141
{
142-
if (isset($config['ApiKey']) && isset($config['Password'])) {
143-
$config['ApiUrl'] = AuthHelper::getAdminUrl($config['ShopUrl'], $config['ApiKey'], $config['Password']);
144-
} else {
145-
$config['ApiUrl'] = AuthHelper::getAdminUrl($config['ShopUrl']);
142+
if(!empty($config)) {
143+
ShopifyClient::$config = $config;
144+
ShopifyClient::setAdminUrl();
146145
}
147-
148-
ShopifyClient::$config = $config;
149146
}
150147

151148
/**
@@ -199,11 +196,55 @@ public function __call($resourceName, $arguments)
199196
/**
200197
* Configure the SDK client
201198
*
202-
* @param $config
199+
* @param array $config
200+
*
203201
* @return ShopifyClient
204202
*/
205203
public static function config($config)
206204
{
207-
return new ShopifyClient($config);
205+
foreach ($config as $key => $value) {
206+
self::$config[$key] = $value;
207+
}
208+
209+
//Re-set the admin url if shop url is changed
210+
if(isset($config['ShopUrl'])) {
211+
self::setAdminUrl();
212+
}
213+
214+
return new ShopifyClient;
215+
}
216+
217+
/**
218+
* Set the admin url, based on the configured shop url
219+
*
220+
* @return string
221+
*/
222+
public static function setAdminUrl()
223+
{
224+
$shopUrl = self::$config['ShopUrl'];
225+
226+
//Remove https:// and trailing slash (if provided)
227+
$shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
228+
229+
if(isset(self::$config['ApiKey']) && isset(self::$config['Password'])) {
230+
$apiKey = self::$config['ApiKey'];
231+
$apiPassword = self::$config['Password'];
232+
$adminUrl = "https://$apiKey:$apiPassword@$shopUrl/admin/";
233+
} else {
234+
$adminUrl = "https://$shopUrl/admin/";
235+
}
236+
237+
self::$config['AdminUrl'] = $adminUrl;
238+
239+
return $adminUrl;
240+
}
241+
242+
/**
243+
* Get the admin url of the configured shop
244+
*
245+
* @return string
246+
*/
247+
public static function getAdminUrl() {
248+
return self::$config['AdminUrl'];
208249
}
209250
}

0 commit comments

Comments
 (0)