Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| RemoteRequest | |
0.00% |
0 / 56 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| get_handler | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
20 | |||
| post_handler | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| __callStatic | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| format_error_data | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Patterns\Api; |
| 4 | |
| 5 | /** |
| 6 | * Remote request class. |
| 7 | */ |
| 8 | class RemoteRequest { |
| 9 | |
| 10 | /** |
| 11 | * The API endpoint |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | public $base_url = ''; |
| 16 | |
| 17 | /** |
| 18 | * Request data sent to the server |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | public $data = array(); |
| 23 | |
| 24 | /** |
| 25 | * Request headers sent to the server |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | public $headers = array(); |
| 30 | |
| 31 | /** |
| 32 | * The class instance. |
| 33 | * |
| 34 | * @var $instance |
| 35 | */ |
| 36 | protected static $instance = null; |
| 37 | |
| 38 | /** |
| 39 | * Set up the base object to send with every request |
| 40 | * |
| 41 | * @param \WP_REST_Request $request - The request. |
| 42 | * @return void |
| 43 | */ |
| 44 | public function __construct( $request ) { |
| 45 | if ( ! \wp_verify_nonce( \sanitize_text_field( \wp_unslash( $request->get_header( 'x_wp_nonce' ) ) ), 'wp_rest' ) ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | $this->headers = array( |
| 50 | 'Accept' => 'application/json', |
| 51 | 'referer' => $request->get_header( 'referer' ), |
| 52 | 'user_agent' => $request->get_header( 'user_agent' ), |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Register dynamic routes |
| 58 | * |
| 59 | * @param string $endpoint - The endpoint. |
| 60 | * @param array $data - The data to include. |
| 61 | * @param array $headers - The headers to include. |
| 62 | * |
| 63 | * @return array |
| 64 | */ |
| 65 | public function get_handler( $endpoint, $data = array(), $headers = array() ) { |
| 66 | $url = \esc_url_raw( |
| 67 | \add_query_arg( |
| 68 | \urlencode_deep( \urldecode_deep( array_merge( $this->data, $data ) ) ), |
| 69 | $this->base_url . $endpoint |
| 70 | ) |
| 71 | ); |
| 72 | |
| 73 | $response = \wp_remote_get( |
| 74 | $url, |
| 75 | array( |
| 76 | 'headers' => array_merge( $this->headers, $headers ), |
| 77 | ) |
| 78 | ); |
| 79 | if ( \is_wp_error( $response ) ) { |
| 80 | return $response; |
| 81 | } |
| 82 | |
| 83 | // Check for other errors |
| 84 | $status_code = \wp_remote_retrieve_response_code( $response ); |
| 85 | |
| 86 | if ( $status_code < 200 || $status_code >= 300 ) { |
| 87 | return new \WP_Error( |
| 88 | 'remote_request_error', |
| 89 | \wp_remote_retrieve_response_message( $response ), |
| 90 | array( |
| 91 | 'status_code' => $status_code, |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | $response_body = \wp_remote_retrieve_body( $response ); |
| 97 | return json_decode( $response_body, true ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Register dynamic routes |
| 102 | * |
| 103 | * @param string $endpoint - The endpoint. |
| 104 | * @param array $data - The arguments to include. |
| 105 | * @param array $headers - The headers to include. |
| 106 | * |
| 107 | * @return array |
| 108 | */ |
| 109 | public function post_handler( $endpoint, $data = array(), $headers = array() ) { |
| 110 | $response = \wp_remote_post( |
| 111 | $this->base_url . $endpoint, |
| 112 | array( |
| 113 | 'headers' => array_merge( $this->headers, $headers ), |
| 114 | 'body' => array_merge( $this->data, $data ), |
| 115 | ) |
| 116 | ); |
| 117 | if ( \is_wp_error( $response ) ) { |
| 118 | return $response; |
| 119 | } |
| 120 | |
| 121 | $response_body = \wp_remote_retrieve_body( $response ); |
| 122 | return json_decode( $response_body, true ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * The caller |
| 127 | * |
| 128 | * @param string $name - The name of the method to call. |
| 129 | * @param array $arguments - The arguments to pass in. |
| 130 | * |
| 131 | * @return mixed |
| 132 | */ |
| 133 | public static function __callStatic( $name, array $arguments ) { |
| 134 | |
| 135 | if ( 'init' === $name ) { |
| 136 | self::$instance = new static( $arguments[0] ); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | $name = "{$name}_handler"; |
| 141 | $r = self::$instance; |
| 142 | |
| 143 | return $r->$name( ...$arguments ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Format error data |
| 148 | * |
| 149 | * @param \WP_Error $error - The error. |
| 150 | * @return array |
| 151 | */ |
| 152 | public static function format_error_data( \WP_Error $error ) { |
| 153 | return array( |
| 154 | 'code' => $error->get_error_code(), |
| 155 | 'message' => $error->get_error_message(), |
| 156 | 'data' => $error->get_error_data(), |
| 157 | ); |
| 158 | } |
| 159 | } |