| 1 |
<?php |
| 2 |
|
| 3 |
namespace SendWP\API; |
| 4 |
|
| 5 |
class Request |
| 6 |
{ |
| 7 |
protected $server_url; |
| 8 |
protected $endpoint; |
| 9 |
protected $client_name; |
| 10 |
protected $client_secret; |
| 11 |
|
| 12 |
public static function create( $endpoint ) |
| 13 |
{ |
| 14 |
$server_url = sendwp_get_server_url(); |
| 15 |
$client_name = sendwp_get_client_name(); |
| 16 |
$client_secret = sendwp_get_client_secret(); |
| 17 |
return new self( $server_url, $endpoint, $client_name, $client_secret ); |
| 18 |
} |
| 19 |
|
| 20 |
public function __construct( $server_url, $endpoint, $client_name, $client_secret ) |
| 21 |
{ |
| 22 |
$this->server_url = $server_url; |
| 23 |
$this->set_endpoint( $endpoint ); |
| 24 |
$this->client_name = $client_name; |
| 25 |
$this->client_secret = $client_secret; |
| 26 |
} |
| 27 |
|
| 28 |
public function set_endpoint( $target ) |
| 29 |
{ |
| 30 |
$this->endpoint = 'wp-json/sendwp/' . $target; |
| 31 |
} |
| 32 |
|
| 33 |
public function request_url() |
| 34 |
{ |
| 35 |
return $this->server_url . $this->endpoint; |
| 36 |
} |
| 37 |
|
| 38 |
public function post( $args ) |
| 39 |
{ |
| 40 |
return $this->request( 'POST', $args ); |
| 41 |
} |
| 42 |
|
| 43 |
public function request( $method, $args ) |
| 44 |
{ |
| 45 |
$args[ 'method' ] = $method; |
| 46 |
$args['reject_unsafe_urls'] = false; // Whitelist requests to the service. |
| 47 |
$args[ 'headers' ][ 'x-sendwp-client-auth' ] = $this->get_auth_headers(); |
| 48 |
return wp_remote_request( $this->request_url(), $args ); |
| 49 |
} |
| 50 |
|
| 51 |
protected function get_auth_headers() |
| 52 |
{ |
| 53 |
return 'Basic ' . base64_encode( $this->client_name . ':' . $this->client_secret ); |
| 54 |
} |
| 55 |
} |