Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| HiiveHelper | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| send_request | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Helpers; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Data\HiiveConnection; |
| 6 | |
| 7 | /** |
| 8 | * Thin Hiive HTTP helper (duplicated from wp-module-hosting for module independence). |
| 9 | */ |
| 10 | class HiiveHelper { |
| 11 | |
| 12 | /** |
| 13 | * Base URL for Hiive API requests. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | private $api_base_url; |
| 18 | |
| 19 | /** |
| 20 | * API path appended to the base URL. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private $endpoint; |
| 25 | |
| 26 | /** |
| 27 | * Request body for POST-like methods or query args for GET/DELETE. |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | private $body; |
| 32 | |
| 33 | /** |
| 34 | * HTTP method (GET, POST, PUT, PATCH, DELETE). |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | private $method; |
| 39 | |
| 40 | /** |
| 41 | * Constructor. |
| 42 | * |
| 43 | * @param string $endpoint API endpoint (relative to NFD_HIIVE_URL). |
| 44 | * @param array $body Request body / query args for GET/DELETE. |
| 45 | * @param string $method HTTP method. |
| 46 | */ |
| 47 | public function __construct( $endpoint, $body = array(), $method = 'POST' ) { |
| 48 | if ( ! defined( 'NFD_HIIVE_URL' ) ) { |
| 49 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Platform constant. |
| 50 | define( 'NFD_HIIVE_URL', 'https://hiive.cloud/api' ); |
| 51 | } |
| 52 | |
| 53 | $this->api_base_url = (string) apply_filters( 'newfold_performance_hiive_api_base_url', constant( 'NFD_HIIVE_URL' ) ); |
| 54 | $this->endpoint = (string) $endpoint; |
| 55 | $this->body = is_array( $body ) ? $body : array(); |
| 56 | $this->method = strtoupper( (string) $method ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Sends the request to Hiive. |
| 61 | * |
| 62 | * @return string|\WP_Error Response body string on success. |
| 63 | */ |
| 64 | public function send_request() { |
| 65 | if ( ! HiiveConnection::is_connected() ) { |
| 66 | return new \WP_Error( |
| 67 | 'nfd_hiive_error', |
| 68 | __( 'Could not enable object cache right now. Please try again later.', 'wp-module-performance' ) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | $url = untrailingslashit( $this->api_base_url ) . $this->endpoint; |
| 73 | |
| 74 | $args = array( |
| 75 | 'method' => $this->method, |
| 76 | 'headers' => array( |
| 77 | 'Content-Type' => 'application/json', |
| 78 | 'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(), |
| 79 | ), |
| 80 | 'timeout' => SiteApisConfig::hiive_request_timeout_seconds(), |
| 81 | ); |
| 82 | |
| 83 | if ( in_array( $this->method, array( 'POST', 'PUT', 'PATCH' ), true ) ) { |
| 84 | $args['body'] = wp_json_encode( $this->body ); |
| 85 | } |
| 86 | |
| 87 | if ( in_array( $this->method, array( 'GET', 'DELETE' ), true ) && ! empty( $this->body ) ) { |
| 88 | $url = add_query_arg( $this->body, $url ); |
| 89 | } |
| 90 | |
| 91 | $response = wp_remote_request( $url, $args ); |
| 92 | |
| 93 | if ( is_wp_error( $response ) ) { |
| 94 | return $response; |
| 95 | } |
| 96 | |
| 97 | $code = (int) wp_remote_retrieve_response_code( $response ); |
| 98 | if ( $code < 200 || $code >= 300 ) { |
| 99 | return new \WP_Error( |
| 100 | 'nfd_hiive_error', |
| 101 | __( 'Could not enable object cache right now. Please try again later.', 'wp-module-performance' ), |
| 102 | array( |
| 103 | 'status' => $code, |
| 104 | 'body' => wp_remote_retrieve_body( $response ), |
| 105 | ) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | return wp_remote_retrieve_body( $response ); |
| 110 | } |
| 111 | } |