| @@ -1,108 +1,55 @@ | ||
| 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 | -} | |
| 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 | +} | |