| 1 |
<?php |
| 2 |
|
| 3 |
namespace SendWP\API; |
| 4 |
|
| 5 |
class Request { |
| 6 |
protected $server_url; |
| 7 |
protected $endpoint; |
| 8 |
protected $client_name; |
| 9 |
protected $client_secret; |
| 10 |
|
| 11 |
/** |
| 12 |
* Create Request |
| 13 |
* |
| 14 |
* @param String $endpoint |
| 15 |
* |
| 16 |
* @return Self |
| 17 |
*/ |
| 18 |
public static function create( $endpoint ) { |
| 19 |
$server_url = sendwp_get_server_url(); |
| 20 |
$client_name = sendwp_get_client_name(); |
| 21 |
$client_secret = sendwp_get_client_secret(); |
| 22 |
|
| 23 |
return new self( $server_url, $endpoint, $client_name, $client_secret ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor |
| 28 |
* |
| 29 |
* @param String $server_url |
| 30 |
* @param String $endpoint |
| 31 |
* @param String $client_name |
| 32 |
* @param String $client_secret |
| 33 |
* |
| 34 |
* @return Void |
| 35 |
*/ |
| 36 |
public function __construct( $server_url, $endpoint, $client_name, $client_secret ) { |
| 37 |
$this->server_url = $server_url; |
| 38 |
|
| 39 |
$this->set_endpoint($endpoint); |
| 40 |
|
| 41 |
$this->client_name = $client_name; |
| 42 |
$this->client_secret = $client_secret; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set the Request Endpoint |
| 47 |
* |
| 48 |
* @param String $target |
| 49 |
* |
| 50 |
* @return Void |
| 51 |
*/ |
| 52 |
public function set_endpoint( $target ) { |
| 53 |
$this->endpoint = 'wp-json/sendwp/' . $target; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Return the request URL |
| 58 |
* |
| 59 |
* @return String |
| 60 |
*/ |
| 61 |
public function request_url() { |
| 62 |
return $this->server_url . $this->endpoint; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Return the post request |
| 67 |
* |
| 68 |
* @param Array $args |
| 69 |
* |
| 70 |
* @return Self |
| 71 |
*/ |
| 72 |
public function post( $args ) { |
| 73 |
return $this->request( 'POST', $args ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Set the request |
| 78 |
* |
| 79 |
* @param String $method |
| 80 |
* @param Array $args |
| 81 |
* |
| 82 |
* @return WP_REMOTE_REQUEST |
| 83 |
*/ |
| 84 |
public function request( $method, $args ) { |
| 85 |
$args['method'] = $method; |
| 86 |
$args['reject_unsafe_urls'] = false; // Whitelist requests to the service. |
| 87 |
$args['headers']['x-sendwp-client-auth'] = $this->get_auth_headers(); |
| 88 |
|
| 89 |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 90 |
$args['headers']['x-forwarded-for'] = $_SERVER['HTTP_CLIENT_IP']; |
| 91 |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 92 |
$args['headers']['x-forwarded-for'] = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 93 |
} else { |
| 94 |
$args['headers']['x-forwarded-for'] = $_SERVER['REMOTE_ADDR']; |
| 95 |
} |
| 96 |
|
| 97 |
return wp_remote_request( $this->request_url(), $args ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the authentication headers |
| 102 |
* |
| 103 |
* @return String |
| 104 |
*/ |
| 105 |
protected function get_auth_headers() { |
| 106 |
return 'Basic ' . base64_encode( $this->client_name . ':' . $this->client_secret ); |
| 107 |
} |
| 108 |
} |
| 109 |
|