Skip to content

Commit 7a88d86

Browse files
authored
Merge pull request #55 from dimadin/patch-1
Use WordPress HTTP API instead of cURL
2 parents afca542 + 8938c42 commit 7a88d86

File tree

1 file changed

+30
-58
lines changed

1 file changed

+30
-58
lines changed

classes/api_calls.php

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function login( $email, $password ) {
7070

7171
$this->auth_token = '';
7272
$url = 'users/login';
73-
$login_call = $this->get_curl( $url, $args );
73+
$login_call = $this->request( $url, $args );
7474

7575
// Credential error checking.
7676
if (
@@ -141,7 +141,7 @@ private function get_auth_token() {
141141
*/
142142
public function self() {
143143
$url = 'users/me';
144-
$login_call = $this->get_curl( $url, [], 'get' );
144+
$login_call = $this->request( $url, [], 'get' );
145145

146146
unset( $login_call['auth_token'] );
147147

@@ -158,7 +158,7 @@ public function transactions_page( $page = 1 ) {
158158
$url = 'users/me/transactions';
159159
$args = [ 'page' => $page ];
160160

161-
$transactions = $this->get_curl( $url, $args, 'get' );
161+
$transactions = $this->request( $url, $args, 'get' );
162162

163163
return $transactions;
164164
}
@@ -190,7 +190,7 @@ public function tasks_page( $filter = 'preferred', $page = 1 ) {
190190
return [];
191191
}
192192

193-
$tasks = $this->get_curl( $url, $args, 'get' );
193+
$tasks = $this->request( $url, $args, 'get' );
194194

195195
return $tasks;
196196
}
@@ -201,84 +201,56 @@ public function tasks_page( $filter = 'preferred', $page = 1 ) {
201201
* @param string $url API endpoint.
202202
* @param array $args Additional URL params or post data.
203203
* @param string $method Request method [GET|POST].
204-
* @param string $headers Optional Curl headers.
204+
* @param array $headers Optional HTTP headers.
205205
* @return array
206206
*/
207-
private function get_curl( $url, $args = [], $method = 'post', $headers = '' ) {
208-
$res = false;
207+
private function request( $url, $args = [], $method = 'POST', $headers = [] ) {
208+
$response_body = false;
209209

210-
try {
211-
set_time_limit( 300 );
210+
set_time_limit( 300 );
212211

213-
$ch = curl_init();
214-
$method = strtolower( $method );
212+
$method = strtoupper( $method );
213+
$request_args = [ 'method' => $method ];
214+
$url = 'https://api.codeable.io/' . ltrim( $url, '/' );
215215

216-
if ( false === $ch ) {
217-
throw new Exception( 'Failed to initialize cURL' );
218-
}
219-
220-
$url = 'https://api.codeable.io/' . ltrim( $url, '/' );
221-
222-
if ( 'get' === $method ) {
223-
if ( ! empty( $args ) ) {
224-
$url = $url . '?' . http_build_query( $args );
225-
}
216+
if ( ! empty( $args ) ) {
217+
if ( 'GET' === $method ) {
218+
$url = add_query_arg( $args, $url );
226219
} else {
227-
curl_setopt( $ch, CURLOPT_POSTFIELDS, $args );
228-
}
229-
230-
// Setup request to send json via POST.
231-
curl_setopt( $ch, CURLOPT_URL, $url );
232-
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
233-
234-
if ( ! $headers || ! is_array( $headers ) ) {
235-
$headers = [];
220+
$request_args['body'] = $args;
236221
}
237-
if ( $this->auth_token_known() ) {
238-
$headers[] = 'Authorization: Bearer ' . $this->auth_token;
239-
}
240-
241-
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
242-
243-
// Send request.
244-
$content = curl_exec( $ch );
222+
}
245223

246-
if ( false === $content ) {
247-
echo '<pre>' . print_r( $url, true ) . '</pre>';
224+
$request_args['headers'] = $headers;
248225

249-
echo '<pre>';
250-
print_r( curl_error( $ch ) );
251-
echo '</pre>';
226+
if ( $this->auth_token_known() ) {
227+
$request_args['headers']['Authorization'] = 'Bearer ' . $this->auth_token;
228+
}
252229

253-
echo '<pre>';
254-
print_r( curl_errno( $ch ) );
255-
echo '</pre>';
230+
$response = wp_remote_request( $url, $request_args );
256231

257-
die;
258-
}
259-
curl_close( $ch );
260-
261-
$res = json_decode( $content, true );
262-
} catch ( Exception $e ) {
232+
if ( is_wp_error( $response ) ) {
263233
trigger_error(
264234
sprintf(
265-
'cURL failed with error #%d: %s',
266-
$e->getCode(), $e->getMessage()
235+
'Request failed with error %1$s: %2$s',
236+
$response->get_error_code(), $response->get_error_message()
267237
),
268238
E_USER_ERROR
269239
);
240+
return false;
270241
}
271242

272-
if ( is_array( $res ) && ! empty( $res['errors'] ) ) {
273-
if ( false !== array_search( 'Invalid login credentials', $res['errors'], true ) ) {
243+
$response_body = json_decode( $response['body'], true );
244+
245+
if ( is_array( $response_body ) && ! empty( $response_body['errors'] ) ) {
246+
if ( false !== array_search( 'Invalid login credentials', $response_body['errors'], true ) ) {
274247
// The auth_token expired or login failed: Clear the token!
275248
// Next time the user visits the settings page, they need to login again.
276249
codeable_api_logout();
277250
return false;
278251
}
279252
}
280253

281-
return $res;
254+
return $response_body;
282255
}
283-
284256
}

0 commit comments

Comments
 (0)