| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Gateways; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Tools; |
| 7 |
use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 8 |
use JFB_Modules\Gateways\Actions_Abstract\Action_Application_Raw_Body_It; |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
abstract class Base_Gateway_Action { |
| 16 |
|
| 17 |
const CODE_OK = 200; |
| 18 |
const CODE_CREATED = 201; |
| 19 |
const CODE_NO_CONTENT = 204; |
| 20 |
|
| 21 |
const SUCCESS_CODES = array( |
| 22 |
self::CODE_OK, |
| 23 |
self::CODE_CREATED, |
| 24 |
self::CODE_NO_CONTENT, |
| 25 |
); |
| 26 |
|
| 27 |
protected $auth; |
| 28 |
protected $method = 'POST'; |
| 29 |
protected $body = array(); |
| 30 |
protected $response; |
| 31 |
protected $response_body; |
| 32 |
protected $response_code; |
| 33 |
protected $response_message; |
| 34 |
protected $response_headers; |
| 35 |
protected $path_parts = array(); |
| 36 |
protected $custom_url = ''; |
| 37 |
|
| 38 |
abstract public function base_url(): string; |
| 39 |
|
| 40 |
public function action_endpoint() { |
| 41 |
return ''; |
| 42 |
} |
| 43 |
|
| 44 |
public function action_headers() { |
| 45 |
return array(); |
| 46 |
} |
| 47 |
|
| 48 |
public function action_body() { |
| 49 |
return array(); |
| 50 |
} |
| 51 |
|
| 52 |
public function action_query_args(): array { |
| 53 |
return array(); |
| 54 |
} |
| 55 |
|
| 56 |
public function set_url( $url ): self { |
| 57 |
$this->custom_url = esc_url_raw( $url ); |
| 58 |
|
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
public function get_raw_url(): string { |
| 63 |
return $this->api_url( $this->action_endpoint(), $this->action_query_args() ); |
| 64 |
} |
| 65 |
|
| 66 |
public function get_url(): string { |
| 67 |
if ( $this->custom_url ) { |
| 68 |
return $this->custom_url; |
| 69 |
} |
| 70 |
|
| 71 |
$url = $this->get_raw_url(); |
| 72 |
|
| 73 |
if ( empty( $this->path_parts ) ) { |
| 74 |
return esc_url_raw( $url ); |
| 75 |
} |
| 76 |
|
| 77 |
return esc_url_raw( |
| 78 |
preg_replace_callback_array( |
| 79 |
$this->get_parts_patterns(), |
| 80 |
$url |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
public function get_parts_patterns(): array { |
| 86 |
$patterns = array(); |
| 87 |
|
| 88 |
foreach ( $this->path_parts as $key => $value ) { |
| 89 |
$patterns[ "#\{($key)\}#" ] = function () use ( $value ) { |
| 90 |
return $value; |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
return $patterns; |
| 95 |
} |
| 96 |
|
| 97 |
public function get_method(): string { |
| 98 |
return $this->method; |
| 99 |
} |
| 100 |
|
| 101 |
public function get_auth() { |
| 102 |
return $this->auth; |
| 103 |
} |
| 104 |
|
| 105 |
public function set_bearer_auth( $token ): self { |
| 106 |
$this->set_auth( "Bearer $token" ); |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
public function set_basic_auth( $token ): self { |
| 112 |
$this->set_auth( "Basic $token" ); |
| 113 |
|
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
public function set_auth( $auth_str ): self { |
| 118 |
$this->auth = $auth_str; |
| 119 |
|
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
public function set_path( array $parts ): self { |
| 124 |
$this->path_parts = array_merge( $this->path_parts, $parts ); |
| 125 |
|
| 126 |
return $this; |
| 127 |
} |
| 128 |
|
| 129 |
public function set_body( $content ): self { |
| 130 |
if ( ! $content ) { |
| 131 |
return $this; |
| 132 |
} |
| 133 |
|
| 134 |
if ( is_string( $content ) ) { |
| 135 |
$this->body = $content; |
| 136 |
} |
| 137 |
|
| 138 |
if ( is_array( $content ) ) { |
| 139 |
$this->body = array_merge( $this->body, $content ); |
| 140 |
} |
| 141 |
|
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
public function get_headers(): array { |
| 146 |
$args = array( |
| 147 |
'Accept-Language' => get_locale(), |
| 148 |
); |
| 149 |
|
| 150 |
if ( $this->get_auth() ) { |
| 151 |
$args['Authorization'] = $this->get_auth(); |
| 152 |
} |
| 153 |
|
| 154 |
return array_merge( $args, $this->action_headers() ); |
| 155 |
} |
| 156 |
|
| 157 |
protected function is_body_ready(): bool { |
| 158 |
if ( $this instanceof Action_Application_Raw_Body_It ) { |
| 159 |
return is_array( $this->body ) && ! empty( $this->body ); |
| 160 |
} |
| 161 |
|
| 162 |
return is_string( $this->body ); |
| 163 |
} |
| 164 |
|
| 165 |
public function get_body() { |
| 166 |
if ( $this->is_body_ready() ) { |
| 167 |
return $this->body; |
| 168 |
} |
| 169 |
|
| 170 |
// phpcs:disable WordPress.PHP.IniSet.Risky |
| 171 |
if ( version_compare( phpversion(), '7.1', '>=' ) ) { |
| 172 |
ini_set( 'precision', 17 ); |
| 173 |
ini_set( 'serialize_precision', - 1 ); |
| 174 |
} |
| 175 |
// phpcs:enable WordPress.PHP.IniSet.Risky |
| 176 |
|
| 177 |
$this->body = $this->to_json( $this->body ); |
| 178 |
|
| 179 |
return $this->body; |
| 180 |
} |
| 181 |
|
| 182 |
protected function to_json( $body ) { |
| 183 |
if ( $this instanceof Action_Application_Raw_Body_It ) { |
| 184 |
return $body; |
| 185 |
} |
| 186 |
|
| 187 |
return wp_unslash( Tools::encode_json( $body ) ); |
| 188 |
} |
| 189 |
|
| 190 |
public function get_request_args(): array { |
| 191 |
$args = array( |
| 192 |
'timeout' => 45, |
| 193 |
'headers' => $this->get_headers(), |
| 194 |
); |
| 195 |
|
| 196 |
if ( 'GET' === $this->get_method() ) { |
| 197 |
return $args; |
| 198 |
} |
| 199 |
|
| 200 |
$args['method'] = $this->get_method(); |
| 201 |
|
| 202 |
if ( ! $this->is_body_ready() ) { |
| 203 |
$this->set_body( $this->action_body() ); |
| 204 |
} |
| 205 |
|
| 206 |
if ( ! empty( $this->body ) ) { |
| 207 |
$args['body'] = $this->get_body(); |
| 208 |
} |
| 209 |
|
| 210 |
return $args; |
| 211 |
} |
| 212 |
|
| 213 |
public function set_method( $method ) { |
| 214 |
$this->method = $method; |
| 215 |
|
| 216 |
return $this; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Return API url |
| 221 |
* |
| 222 |
* @param $endpoint |
| 223 |
* @param $query_args |
| 224 |
* |
| 225 |
* @return string |
| 226 |
*/ |
| 227 |
public function api_url( $endpoint, $query_args ): string { |
| 228 |
$url = $this->base_url() . $endpoint; |
| 229 |
|
| 230 |
if ( empty( $query_args ) ) { |
| 231 |
return $url; |
| 232 |
} |
| 233 |
|
| 234 |
return add_query_arg( $query_args, $url ); |
| 235 |
} |
| 236 |
|
| 237 |
public function get_response() { |
| 238 |
$params = array( $this->get_url(), $this->get_request_args() ); |
| 239 |
|
| 240 |
return 'POST' === $this->get_method() |
| 241 |
? wp_remote_post( ...$params ) |
| 242 |
: wp_remote_get( ...$params ); |
| 243 |
} |
| 244 |
|
| 245 |
public function before_make_request() { |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Make a request |
| 250 |
*/ |
| 251 |
public function request(): self { |
| 252 |
$this->before_make_request(); |
| 253 |
|
| 254 |
$response = $this->get_response(); |
| 255 |
|
| 256 |
$this->set_response_body( wp_remote_retrieve_body( $response ) ); |
| 257 |
$this->set_response_code( (int) wp_remote_retrieve_response_code( $response ) ); |
| 258 |
$this->set_response_message( wp_remote_retrieve_response_message( $response ) ); |
| 259 |
$this->set_response_headers( wp_remote_retrieve_headers( $response ) ); |
| 260 |
|
| 261 |
return $this; |
| 262 |
} |
| 263 |
|
| 264 |
public function get_response_headers() { |
| 265 |
return $this->response_headers; |
| 266 |
} |
| 267 |
|
| 268 |
private function set_response_headers( $headers ): self { |
| 269 |
$this->response_headers = $headers; |
| 270 |
|
| 271 |
return $this; |
| 272 |
} |
| 273 |
|
| 274 |
private function set_response_message( $message ): self { |
| 275 |
$this->response_message = $message; |
| 276 |
|
| 277 |
return $this; |
| 278 |
} |
| 279 |
|
| 280 |
public function get_response_message() { |
| 281 |
return $this->response_message; |
| 282 |
} |
| 283 |
|
| 284 |
public function get_response_body() { |
| 285 |
return $this->response_body; |
| 286 |
} |
| 287 |
|
| 288 |
private function set_response_body( $body ): self { |
| 289 |
$this->response_body = $body; |
| 290 |
|
| 291 |
return $this; |
| 292 |
} |
| 293 |
|
| 294 |
public function get_response_code() { |
| 295 |
return $this->response_code; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
private function set_response_code( $code ): self { |
| 300 |
$this->response_code = $code; |
| 301 |
|
| 302 |
return $this; |
| 303 |
} |
| 304 |
|
| 305 |
public function response_message( $base_message ): string { |
| 306 |
return $base_message . "\r\n" . sprintf( |
| 307 |
'%d: %s', |
| 308 |
$this->get_response_code(), |
| 309 |
$this->get_response_message() |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* @throws Gateway_Exception |
| 315 |
*/ |
| 316 |
public function check_response_code(): self { |
| 317 |
if ( 200 <= $this->get_response_code() && $this->get_response_code() <= 299 ) { |
| 318 |
return $this; |
| 319 |
} |
| 320 |
|
| 321 |
$this->response_body_as_array(); |
| 322 |
|
| 323 |
throw new Gateway_Exception( |
| 324 |
esc_html( $this->response_message( 'Invalid HTTP code.' ) ), |
| 325 |
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 326 |
$this->get_response_body(), |
| 327 |
$this->get_body() |
| 328 |
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* @throws Gateway_Exception |
| 334 |
*/ |
| 335 |
public function response_body_as_array(): self { |
| 336 |
|
| 337 |
if ( is_array( $this->get_response_body() ) ) { |
| 338 |
return $this; |
| 339 |
} |
| 340 |
|
| 341 |
if ( empty( $this->get_response_body() ) ) { |
| 342 |
throw new Gateway_Exception( |
| 343 |
esc_html( $this->response_message( 'Empty response.' ) ) |
| 344 |
); |
| 345 |
} |
| 346 |
if ( is_wp_error( $this->get_response_body() ) ) { |
| 347 |
/** @var \WP_Error $response */ |
| 348 |
$response = $this->get_response_body(); |
| 349 |
|
| 350 |
throw new Gateway_Exception( |
| 351 |
esc_html( $this->response_message( 'Internal error.' ) ), |
| 352 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 353 |
$response->get_error_message( |
| 354 |
$response->get_error_code() |
| 355 |
) |
| 356 |
); |
| 357 |
} |
| 358 |
|
| 359 |
$parsed_response = Tools::decode_json( $this->get_response_body() ); |
| 360 |
|
| 361 |
if ( is_null( $parsed_response ) ) { |
| 362 |
throw new Gateway_Exception( |
| 363 |
esc_html( $this->response_message( 'Invalid JSON.' ) ), |
| 364 |
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 365 |
$this->get_response_body(), |
| 366 |
$this->get_request_args(), |
| 367 |
$this->get_url() |
| 368 |
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 369 |
); |
| 370 |
} |
| 371 |
|
| 372 |
$this->set_response_body( $parsed_response ); |
| 373 |
|
| 374 |
return $this; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* @throws Gateway_Exception |
| 379 |
*/ |
| 380 |
public function send_request() { |
| 381 |
$this->request(); |
| 382 |
|
| 383 |
$this->response_body_as_array(); |
| 384 |
|
| 385 |
return $this->get_response_body(); |
| 386 |
} |
| 387 |
|
| 388 |
} |
| 389 |
|