| 1 |
<?php |
| 2 |
/** |
| 3 |
* API request class |
| 4 |
* |
| 5 |
* Handles all the API requests to Hub. |
| 6 |
* |
| 7 |
* @link http://wpmudev.com |
| 8 |
* @since 1.0.0 |
| 9 |
* @author Joel James <[email protected]> |
| 10 |
* @package WPMUDEV\Hub\Connector |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace WPMUDEV\Hub\Connector; |
| 14 |
|
| 15 |
defined( 'WPINC' ) || die; |
| 16 |
|
| 17 |
use Exception; |
| 18 |
use WP_Error; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Request. |
| 22 |
*/ |
| 23 |
class Request { |
| 24 |
|
| 25 |
/** |
| 26 |
* Request max timeout. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
private $timeout = 15; |
| 33 |
|
| 34 |
/** |
| 35 |
* Header arguments. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
private $headers = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* POST arguments. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
private $post_args = array(); |
| 51 |
|
| 52 |
/** |
| 53 |
* GET arguments. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* |
| 57 |
* @var array |
| 58 |
*/ |
| 59 |
private $get_args = array(); |
| 60 |
|
| 61 |
/** |
| 62 |
* Request constructor. |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Set request timeout. |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
* |
| 74 |
* @param int $timeout Request timeout (seconds). |
| 75 |
*/ |
| 76 |
public function set_timeout( $timeout ) { |
| 77 |
$this->timeout = $timeout; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Add a new request argument for POST requests. |
| 82 |
* |
| 83 |
* @since 1.0.0 |
| 84 |
* |
| 85 |
* @param string $name Argument name. |
| 86 |
* @param string $value Argument value. |
| 87 |
*/ |
| 88 |
public function add_post_argument( $name, $value ) { |
| 89 |
$this->post_args[ $name ] = $value; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Add a new request argument for GET requests. |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* |
| 97 |
* @param string $name Argument name. |
| 98 |
* @param string $value Argument value. |
| 99 |
*/ |
| 100 |
public function add_get_argument( $name, $value ) { |
| 101 |
$this->get_args[ $name ] = $value; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Add a new request argument for GET requests. |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
* |
| 109 |
* @param string $name Argument name. |
| 110 |
* @param string $value Argument value. |
| 111 |
*/ |
| 112 |
public function add_header_argument( $name, $value ) { |
| 113 |
$this->headers[ $name ] = $value; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Make a POST request. |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @param string $path Endpoint route. |
| 122 |
* @param bool $auth Should attach API key?. |
| 123 |
* @param array $data Data array. |
| 124 |
* |
| 125 |
* @return mixed|WP_Error |
| 126 |
*/ |
| 127 |
public function post( $path, $auth = false, $data = array() ) { |
| 128 |
try { |
| 129 |
return $this->request( $path, $auth, $data ); |
| 130 |
} catch ( Exception $e ) { |
| 131 |
return new WP_Error( $e->getCode(), $e->getMessage() ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Make a POST request. |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* |
| 140 |
* @param string $path Endpoint route. |
| 141 |
* @param bool $auth Should attach API key?. |
| 142 |
* @param array $data Data array. |
| 143 |
* |
| 144 |
* @return mixed|WP_Error |
| 145 |
*/ |
| 146 |
public function delete( $path, $auth = false, $data = array() ) { |
| 147 |
try { |
| 148 |
return $this->request( $path, $auth, $data, 'delete' ); |
| 149 |
} catch ( Exception $e ) { |
| 150 |
return new WP_Error( $e->getCode(), $e->getMessage() ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Make a GET request. |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* |
| 159 |
* @param string $path Endpoint route. |
| 160 |
* @param bool $auth Should attach API key?. |
| 161 |
* @param array $data Data array. |
| 162 |
* |
| 163 |
* @return mixed|WP_Error |
| 164 |
*/ |
| 165 |
public function get( $path, $auth = false, $data = array() ) { |
| 166 |
try { |
| 167 |
return $this->request( $path, $auth, $data, 'get' ); |
| 168 |
} catch ( Exception $e ) { |
| 169 |
return new WP_Error( $e->getCode(), $e->getMessage() ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Add API key for authorization to request. |
| 175 |
* |
| 176 |
* Optionally if hub site id is found, include that too. |
| 177 |
* |
| 178 |
* @since 1.0.0 |
| 179 |
* |
| 180 |
* @param string $url URL of request. |
| 181 |
* @param string $method Request method (post or get). |
| 182 |
* |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
private function set_api_key( $url, $method = 'post' ) { |
| 186 |
$key = API::get()->get_api_key(); |
| 187 |
$site_id = Data::get()->hub_site_id(); |
| 188 |
|
| 189 |
if ( ! empty( $key ) ) { |
| 190 |
if ( in_array( strtolower( $method ), array( 'post', 'delete' ), true ) ) { |
| 191 |
$this->add_post_argument( 'api_key', $key ); |
| 192 |
} else { |
| 193 |
// Set API key if not already set. |
| 194 |
$this->add_header_argument( 'Authorization', 'Basic ' . $key ); |
| 195 |
if ( ! empty( $site_id ) ) { |
| 196 |
$this->add_get_argument( 'site_id', $site_id ); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return $url; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Make an HTTP request using WP. |
| 206 |
* |
| 207 |
* @since 1.0.0 |
| 208 |
* |
| 209 |
* @param string $path API endpoint route. |
| 210 |
* @param bool $auth Should attach API key?. |
| 211 |
* @param array $data Data array. |
| 212 |
* @param string $method Request method. |
| 213 |
* |
| 214 |
* @return array|WP_Error |
| 215 |
*/ |
| 216 |
private function request( $path, $auth = false, $data = array(), $method = 'post' ) { |
| 217 |
$url = API::get()->rest_url( $path ); |
| 218 |
|
| 219 |
if ( $auth ) { |
| 220 |
$url = $this->set_api_key( $url, $method ); |
| 221 |
} |
| 222 |
|
| 223 |
// Add URL params. |
| 224 |
$url = add_query_arg( $this->get_args, $url ); |
| 225 |
|
| 226 |
// Default request options. |
| 227 |
$args = array( |
| 228 |
'user-agent' => 'WPMUDEV Hub Connector Client/' . \WPMUDEV_HUB_CONNECTOR_VERSION . ' (+' . network_site_url() . ')', |
| 229 |
'headers' => $this->headers, |
| 230 |
'sslverify' => defined( '\WPMUDEV_API_SSLVERIFY' ) ? \WPMUDEV_API_SSLVERIFY : false, |
| 231 |
'method' => strtoupper( $method ), |
| 232 |
'timeout' => $this->timeout, |
| 233 |
); |
| 234 |
|
| 235 |
switch ( strtolower( $method ) ) { |
| 236 |
case 'post': |
| 237 |
if ( is_array( $data ) ) { |
| 238 |
$data = array_merge( $data, $this->post_args ); |
| 239 |
} |
| 240 |
|
| 241 |
$args['body'] = $data; |
| 242 |
|
| 243 |
$response = wp_remote_post( $url, $args ); |
| 244 |
break; |
| 245 |
case 'delete': |
| 246 |
if ( is_array( $data ) ) { |
| 247 |
$data = array_merge( $data, $this->post_args ); |
| 248 |
} |
| 249 |
|
| 250 |
$args['body'] = $data; |
| 251 |
$args['method'] = 'DELETE'; |
| 252 |
|
| 253 |
$response = wp_remote_request( $url, $args ); |
| 254 |
break; |
| 255 |
case 'get': |
| 256 |
// If data is set for get request add it to URL. |
| 257 |
$url = add_query_arg( $data, $url ); |
| 258 |
|
| 259 |
$response = wp_remote_get( $url, $args ); |
| 260 |
break; |
| 261 |
default: |
| 262 |
$response = wp_remote_request( $url, $args ); |
| 263 |
break; |
| 264 |
} |
| 265 |
|
| 266 |
return $response; |
| 267 |
} |
| 268 |
} |
| 269 |
|