| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
if (!class_exists('WPRWPAPI')) : |
| 5 |
class WPRWPAPI { |
| 6 |
public $settings; |
| 7 |
|
| 8 |
public function __construct($settings) { |
| 9 |
$this->settings = $settings; |
| 10 |
} |
| 11 |
|
| 12 |
public function pingbv($method, $body, $public = false) { |
| 13 |
if ($public) { |
| 14 |
$this->do_request($method, $body, $public); |
| 15 |
} else { |
| 16 |
$api_public_key = $this->settings->getOption('bvApiPublic'); |
| 17 |
if (!empty($api_public_key) && (strlen($api_public_key) >= 32)) { |
| 18 |
$this->do_request($method, $body, $api_public_key); |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
public function do_request($method, $body, $pubkey) { |
| 24 |
$account = WPRAccount::find($this->settings, $pubkey); |
| 25 |
if (isset($account)) { |
| 26 |
$url = $account->authenticatedUrl($method); |
| 27 |
$this->http_request($url, $body); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function http_request($url, $body, $headers = array()) { |
| 32 |
$_body = array( |
| 33 |
'method' => 'POST', |
| 34 |
'timeout' => 15, |
| 35 |
'body' => $body |
| 36 |
); |
| 37 |
if (!empty($headers)) { |
| 38 |
$_body['headers'] = $headers; |
| 39 |
} |
| 40 |
return wp_remote_post($url, $_body); |
| 41 |
} |
| 42 |
} |
| 43 |
endif; |