| 1 |
<?php |
| 2 |
/** |
| 3 |
* API class. |
| 4 |
* |
| 5 |
* @package WPZinc\Social |
| 6 |
* @author WP Zinc |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPZinc\Social; |
| 10 |
|
| 11 |
/** |
| 12 |
* Used by other classes which interact with APIs to perform POST and GET requests. |
| 13 |
* |
| 14 |
* @package WPZinc\Social |
| 15 |
* @author WP Zinc |
| 16 |
* @version 1.0.0 |
| 17 |
*/ |
| 18 |
class API { |
| 19 |
|
| 20 |
/** |
| 21 |
* Holds the base class object. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* |
| 25 |
* @var object |
| 26 |
*/ |
| 27 |
public $base; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the API endpoint. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public $api_endpoint = ''; |
| 37 |
|
| 38 |
/** |
| 39 |
* Sanitizes API arguments, by removing false or empty |
| 40 |
* arguments in the array. |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
* |
| 44 |
* @param array $args Arguments. |
| 45 |
* @return array Sanitized Arguments |
| 46 |
*/ |
| 47 |
public function sanitize_arguments( $args ) { |
| 48 |
|
| 49 |
foreach ( $args as $key => $value ) { |
| 50 |
if ( empty( $value ) ) { |
| 51 |
unset( $args[ $key ] ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return $args; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Private function to perform a GET request |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @param string $cmd Command (required). |
| 65 |
* @param array $params Params (optional). |
| 66 |
* @return mixed \WP_Error | object |
| 67 |
*/ |
| 68 |
public function get( $cmd, $params = array() ) { |
| 69 |
|
| 70 |
return $this->request( $cmd, 'get', $params ); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Private function to perform a POST request |
| 76 |
* |
| 77 |
* @since 1.0.0 |
| 78 |
* |
| 79 |
* @param string $cmd Command (required). |
| 80 |
* @param array $params Params (optional). |
| 81 |
* @return mixed \WP_Error | object |
| 82 |
*/ |
| 83 |
public function post( $cmd, $params = array() ) { |
| 84 |
|
| 85 |
return $this->request( $cmd, 'post', $params ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Main function which handles sending requests to an API |
| 91 |
* |
| 92 |
* @since 1.0.0 |
| 93 |
* |
| 94 |
* @param string $cmd Command. |
| 95 |
* @param string $method Method (get|post). |
| 96 |
* @param array $params Parameters (optional). |
| 97 |
* @return mixed \WP_Error | object |
| 98 |
*/ |
| 99 |
private function request( $cmd, $method = 'get', $params = array() ) { |
| 100 |
|
| 101 |
// Set timeout. |
| 102 |
$timeout = 20; |
| 103 |
|
| 104 |
/** |
| 105 |
* Defines the number of seconds before timing out a request to the remote API. |
| 106 |
* |
| 107 |
* @since 3.0.0 |
| 108 |
* |
| 109 |
* @param int $timeout Timeout, in seconds. |
| 110 |
*/ |
| 111 |
$timeout = apply_filters( $this->base->plugin->filter_name . '_api_request_timeout', $timeout ); |
| 112 |
|
| 113 |
// Send request. |
| 114 |
$result = $this->request_wordpress( $this->api_endpoint, $cmd, $method, $params, $timeout ); |
| 115 |
|
| 116 |
// Result will be WP_Error or the data we expect. |
| 117 |
return $result; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Performs POST and GET requests. |
| 123 |
* |
| 124 |
* @since 1.7.1 |
| 125 |
* |
| 126 |
* @param string $url URL. |
| 127 |
* @param string $cmd API Command. |
| 128 |
* @param string $method Method (post|get). |
| 129 |
* @param array $params Parameters. |
| 130 |
* @param int $timeout Timeout, in seconds (default: 10). |
| 131 |
* @return mixed \WP_Error | object |
| 132 |
*/ |
| 133 |
private function request_wordpress( $url, $cmd, $method, $params, $timeout = 20 ) { |
| 134 |
|
| 135 |
// Send request. |
| 136 |
$response = new \WP_Error( $this->base->plugin->filter_name . '_api_invalid_method', __( 'Invalid request method.', 'wp-to-buffer' ) ); |
| 137 |
switch ( $method ) { |
| 138 |
/** |
| 139 |
* GET |
| 140 |
*/ |
| 141 |
case 'get': |
| 142 |
case 'GET': |
| 143 |
$response = wp_remote_get( |
| 144 |
$url . '&' . http_build_query( |
| 145 |
array( |
| 146 |
'endpoint' => $cmd, |
| 147 |
'params' => $params, |
| 148 |
) |
| 149 |
), |
| 150 |
array( |
| 151 |
'timeout' => $timeout, |
| 152 |
) |
| 153 |
); |
| 154 |
break; |
| 155 |
|
| 156 |
/** |
| 157 |
* POST |
| 158 |
*/ |
| 159 |
case 'post': |
| 160 |
case 'POST': |
| 161 |
$response = wp_remote_post( |
| 162 |
$url, |
| 163 |
array( |
| 164 |
'body' => array( |
| 165 |
'endpoint' => $cmd, |
| 166 |
'params' => $params, |
| 167 |
), |
| 168 |
'timeout' => $timeout, |
| 169 |
) |
| 170 |
); |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
// If an error occured, return it now. |
| 175 |
if ( is_wp_error( $response ) ) { |
| 176 |
return $response; |
| 177 |
} |
| 178 |
|
| 179 |
// Fetch HTTP code and body. |
| 180 |
$http_code = wp_remote_retrieve_response_code( $response ); |
| 181 |
$response = wp_remote_retrieve_body( $response ); |
| 182 |
|
| 183 |
// Decode the result. |
| 184 |
$result = json_decode( $response ); |
| 185 |
|
| 186 |
// If the response is empty or missing the data payload, return a generic error. |
| 187 |
if ( is_null( $result ) || ! isset( $result->data ) ) { |
| 188 |
return new \WP_Error( |
| 189 |
$http_code, |
| 190 |
'API Error: HTTP Code ' . $http_code . '. Sorry, we don\'t have any more information about this error. Please try again.' |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
// If the response's success flag is false, return the data as an error. |
| 195 |
if ( ! $result->success ) { |
| 196 |
return new \WP_Error( $http_code, $result->data ); |
| 197 |
} |
| 198 |
|
| 199 |
// All OK - return the data. |
| 200 |
// This is from the originating API request, and we no longer need it. |
| 201 |
unset( $result->data->status ); |
| 202 |
|
| 203 |
// object comprising of data, links + meta. |
| 204 |
return $result->data; |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|