| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailerLite\Includes\Shared\Api; |
| 4 |
|
| 5 |
class MailerLiteClient |
| 6 |
{ |
| 7 |
|
| 8 |
private $url; |
| 9 |
private $headers; |
| 10 |
private $timeout = 90; |
| 11 |
|
| 12 |
/** |
| 13 |
* Client constructor |
| 14 |
* |
| 15 |
* @access public |
| 16 |
* @return void |
| 17 |
* @since 1.6.0 |
| 18 |
*/ |
| 19 |
public function __construct($url, $headers) |
| 20 |
{ |
| 21 |
|
| 22 |
$this->url = $url; |
| 23 |
$this->headers = $headers; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Client for GET requests |
| 28 |
* |
| 29 |
* @access public |
| 30 |
* @since 1.6.0 |
| 31 |
*/ |
| 32 |
public function remote_get($endpoint, $args = []) |
| 33 |
{ |
| 34 |
|
| 35 |
$args['body'] = $args; |
| 36 |
$args['headers'] = $this->headers; |
| 37 |
$args['timeout'] = $this->timeout; |
| 38 |
$args['user-agent'] = $this->userAgent(); |
| 39 |
|
| 40 |
return wp_remote_get($this->url . $endpoint, $args); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Client for POST requests |
| 45 |
* |
| 46 |
* @access public |
| 47 |
* @since 1.6.0 |
| 48 |
*/ |
| 49 |
public function remote_post($endpoint, $args = []) |
| 50 |
{ |
| 51 |
|
| 52 |
$params = []; |
| 53 |
$params['headers'] = $this->headers; |
| 54 |
$params['body'] = json_encode($args); |
| 55 |
$params['timeout'] = $this->timeout; |
| 56 |
$params['user-agent'] = $this->userAgent(); |
| 57 |
|
| 58 |
return wp_remote_post($this->url . $endpoint, $params); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Client for PUT requests |
| 63 |
* |
| 64 |
* @access public |
| 65 |
* @since 1.6.0 |
| 66 |
*/ |
| 67 |
public function remote_put($endpoint, $args = []) |
| 68 |
{ |
| 69 |
|
| 70 |
$params = []; |
| 71 |
$params['method'] = 'PUT'; |
| 72 |
$params['headers'] = $this->headers; |
| 73 |
$params['body'] = json_encode($args); |
| 74 |
$params['timeout'] = $this->timeout; |
| 75 |
$params['user-agent'] = $this->userAgent(); |
| 76 |
|
| 77 |
return wp_remote_post($this->url . $endpoint, $params); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Client for DELETE requests |
| 82 |
* |
| 83 |
* @access public |
| 84 |
* @since 1.6.0 |
| 85 |
*/ |
| 86 |
public function remote_delete($endpoint, $args = []) |
| 87 |
{ |
| 88 |
|
| 89 |
$params = []; |
| 90 |
$params['method'] = 'DELETE'; |
| 91 |
$params['headers'] = $this->headers; |
| 92 |
$params['body'] = json_encode($args); |
| 93 |
$params['timeout'] = $this->timeout; |
| 94 |
$params['user-agent'] = $this->userAgent(); |
| 95 |
|
| 96 |
return wp_remote_post($this->url . $endpoint, $params); |
| 97 |
} |
| 98 |
|
| 99 |
private function userAgent() |
| 100 |
{ |
| 101 |
global $wp_version; |
| 102 |
|
| 103 |
return 'MailerLite WooCommerce/' . WOO_MAILERLITE_VER . ' (WP/' . $wp_version . ' WOO/' . get_option('woocommerce_version', |
| 104 |
-1) . ')'; |
| 105 |
} |
| 106 |
} |