| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Addons\Webhooks\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit(); |
| 7 |
} |
| 8 |
|
| 9 |
use StoreEngine\classes\Exceptions\StoreEngineException; |
| 10 |
use WP_Error; |
| 11 |
|
| 12 |
class Http { |
| 13 |
const METHOD_GET = 'GET'; |
| 14 |
const METHOD_POST = 'POST'; |
| 15 |
const METHOD_PUT = 'PUT'; |
| 16 |
const METHOD_PATCH = 'PATCH'; |
| 17 |
const METHOD_DELETE = 'DELETE'; |
| 18 |
|
| 19 |
protected array $allowed_http_verb = [ |
| 20 |
self::METHOD_GET, |
| 21 |
self::METHOD_POST, |
| 22 |
self::METHOD_PUT, |
| 23 |
self::METHOD_PATCH, |
| 24 |
self::METHOD_DELETE, |
| 25 |
]; |
| 26 |
|
| 27 |
protected string $url; |
| 28 |
protected string $method = self::METHOD_POST; |
| 29 |
protected bool $blocking = true; |
| 30 |
protected string $httpversion = '1.0'; |
| 31 |
protected array $payload = []; |
| 32 |
protected array $headers = []; |
| 33 |
protected array $cookies = []; |
| 34 |
protected ?string $user_agent = null; |
| 35 |
protected int $redirection = 0; |
| 36 |
protected int $timeout = MINUTE_IN_SECONDS; |
| 37 |
|
| 38 |
public function __construct( string $url = '' ) { |
| 39 |
$this->url = $url; |
| 40 |
} |
| 41 |
|
| 42 |
public static function request( string $url ): Http { |
| 43 |
return new self( $url ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @throws StoreEngineException |
| 48 |
*/ |
| 49 |
public function get( string $path = '' ) { |
| 50 |
return $this->make( $path, self::METHOD_GET ); |
| 51 |
} |
| 52 |
|
| 53 |
public function post( string $path = '' ) { |
| 54 |
return $this->make( $path, self::METHOD_POST ); |
| 55 |
} |
| 56 |
|
| 57 |
public function set_payload( array $data ): self { |
| 58 |
$this->payload = $data; |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
public function set_headers( array $headers ): self { |
| 64 |
$this->headers = array_merge( $this->headers, $headers ); |
| 65 |
|
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
public function set_user_agent( string $user_agent ): self { |
| 70 |
$this->user_agent = $user_agent; |
| 71 |
|
| 72 |
return $this; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @param string $path |
| 77 |
* @param string|null $method |
| 78 |
* |
| 79 |
* @return array|WP_Error |
| 80 |
*/ |
| 81 |
public function make( string $path = '', ?string $method = null ) { |
| 82 |
if ( empty( $this->url . $path ) ) { |
| 83 |
return new WP_Error( 'empty-webhook-url', __( 'Webhook URL is empty', 'storeengine' ) ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( $method && in_array( strtoupper( $method ), $this->allowed_http_verb, true ) ) { |
| 87 |
$this->method = strtoupper( $method ); |
| 88 |
} |
| 89 |
|
| 90 |
$args = [ |
| 91 |
'redirection' => $this->redirection, |
| 92 |
'method' => $this->method, |
| 93 |
'timeout' => $this->timeout, |
| 94 |
'blocking' => $this->blocking, |
| 95 |
'httpversion' => $this->httpversion, |
| 96 |
]; |
| 97 |
|
| 98 |
if ( ! empty( $this->user_agent ) ) { |
| 99 |
$args['user-agent'] = $this->user_agent; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! empty( $this->headers ) ) { |
| 103 |
$args['headers'] = $this->headers; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! empty( $this->cookies ) ) { |
| 107 |
$args['cookies'] = $this->cookies; |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! empty( $this->payload ) ) { |
| 111 |
$args['body'] = trim( wp_json_encode( $this->payload ) ); |
| 112 |
} |
| 113 |
|
| 114 |
$response = wp_safe_remote_request( $this->url . $path, $args ); |
| 115 |
|
| 116 |
if ( isset( $args['body'] ) ) { |
| 117 |
unset( $args['body'] ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( is_wp_error( $response ) ) { |
| 121 |
$response->add_data( $args, 'http_args' ); |
| 122 |
} else { |
| 123 |
$response['http_args'] = $args; |
| 124 |
} |
| 125 |
|
| 126 |
return $response; |
| 127 |
} |
| 128 |
} |
| 129 |
|