| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trait HttpTrait. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Framework; |
| 11 |
|
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Trait HttpTrait. |
| 16 |
* |
| 17 |
* @package Packetery |
| 18 |
*/ |
| 19 |
trait HttpTrait { |
| 20 |
/** |
| 21 |
* Performs an HTTP request using the GET method and returns its response. |
| 22 |
* |
| 23 |
* @param string $url URL to retrieve. |
| 24 |
* @param array $args Optional. Request arguments. Default empty array. See WP_Http::request() for information on accepted arguments. |
| 25 |
* |
| 26 |
* @return array|WP_Error The response or WP_Error on failure. |
| 27 |
*/ |
| 28 |
public function remoteGet( string $url, array $args = [] ) { |
| 29 |
return wp_remote_get( $url, $args ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Retrieves only the body from the raw response. |
| 34 |
* |
| 35 |
* @param array|WP_Error $response HTTP response. |
| 36 |
* @return string The body of the response. Empty string if no body or incorrect parameter given. |
| 37 |
*/ |
| 38 |
public function remoteRetrieveBody( $response ): string { |
| 39 |
return wp_remote_retrieve_body( $response ); |
| 40 |
} |
| 41 |
|
| 42 |
public function safeRedirect( string $location ): bool { |
| 43 |
return wp_safe_redirect( $location ); |
| 44 |
} |
| 45 |
} |
| 46 |
|