| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
/** |
| 7 |
* The main class |
| 8 |
* |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
class WpGetApi_Api { |
| 12 |
|
| 13 |
public $encryption = ''; // the encryption class |
| 14 |
|
| 15 |
public $setup = ''; // store the setup data from the admin page |
| 16 |
public $api = ''; // store the api's from the admin page |
| 17 |
|
| 18 |
public $api_id = ''; // the api_id that we are wanting to get |
| 19 |
public $endpoint_id = ''; // the endpoint_id that we are wanting to get |
| 20 |
public $args = ''; // the args |
| 21 |
public $keys = ''; // the keys we want to retrieve |
| 22 |
|
| 23 |
public $base_url = ''; // base url of the API |
| 24 |
public $method = ''; // the endpoint method GET POST etc |
| 25 |
public $cache_time = ''; // the time to cache |
| 26 |
public $endpoint = ''; // the endpoint that we will get |
| 27 |
public $query_parameters = ''; // the query paramaters appended to url |
| 28 |
public $header_parameters = ''; // the header paramaters |
| 29 |
public $body_parameters = ''; // the body paramaters |
| 30 |
|
| 31 |
public $response = ''; // the returned response |
| 32 |
|
| 33 |
public $final_url = ''; // final URL |
| 34 |
public $final_headers = ''; // final headers |
| 35 |
|
| 36 |
public $results_format = 'json'; // reseults format |
| 37 |
|
| 38 |
public $timeout = 10; // the timeout |
| 39 |
public $sslverify = 1; // the paramaters |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Main constructor |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* |
| 47 |
*/ |
| 48 |
public function __construct( $api_id = '', $endpoint_id = '', $args = array(), $keys = array() ) { |
| 49 |
|
| 50 |
$this->setup = get_option( 'wpgetapi_setup' ); |
| 51 |
$this->api = get_option( 'wpgetapi_' . $api_id ); |
| 52 |
|
| 53 |
// do some checks |
| 54 |
if( empty( $this->setup ) || ! isset( $this->setup['apis'] ) || empty( $this->setup['apis'] ) ) |
| 55 |
return 'No API found.'; |
| 56 |
|
| 57 |
// do some checks |
| 58 |
if( ! $this->api || ! isset( $this->api['endpoints'] ) || empty( $this->api['endpoints'] )) |
| 59 |
return 'API not found.'; |
| 60 |
|
| 61 |
$this->encryption = new WpGetApi_Encryption(); |
| 62 |
$this->api_id = $api_id; |
| 63 |
$this->endpoint_id = $endpoint_id; |
| 64 |
$this->keys = $keys; |
| 65 |
$this->args = $args; |
| 66 |
|
| 67 |
$this->init(); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Init our api data |
| 74 |
*/ |
| 75 |
public function init() { |
| 76 |
|
| 77 |
// loop through api's |
| 78 |
// set base_url |
| 79 |
foreach ( $this->setup['apis'] as $index => $api_item ) { |
| 80 |
if( $this->api_id == $api_item['id'] ) { |
| 81 |
$this->base_url = isset( $api_item['base_url'] ) && ! empty( $api_item['base_url'] ) ? sanitize_text_field( $api_item['base_url'] ) : ''; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// loop through endpoints |
| 86 |
foreach ( $this->api['endpoints'] as $index => $endpoint ) { |
| 87 |
if( $this->endpoint_id == $endpoint['id'] ) { |
| 88 |
|
| 89 |
$this->method = isset( $endpoint['method'] ) && ! empty( $endpoint['method'] ) ? sanitize_text_field( $endpoint['method'] ) : 'GET'; |
| 90 |
$this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : ''; |
| 91 |
$this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : ''; |
| 92 |
$this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array(); |
| 93 |
$this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array(); |
| 94 |
$this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array(); |
| 95 |
$this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$this->timeout = isset( $this->api['timeout'] ) && ! empty( $this->api['timeout'] ) ? $this->api['timeout'] : $this->timeout; |
| 100 |
$this->sslverify = isset( $this->api['sslverify'] ) ? absint( $this->api['sslverify'] ) : $this->sslverify; |
| 101 |
|
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* The main function to output the data |
| 108 |
* |
| 109 |
* @param string $field Field to retrieve |
| 110 |
*/ |
| 111 |
public function endpoint_output() { |
| 112 |
return $this->do_the_call(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Sort out the parameters if they exist |
| 118 |
*/ |
| 119 |
public function do_parameters( $parameters ) { |
| 120 |
|
| 121 |
$array = array(); |
| 122 |
$parameters = $this->decrypt($parameters); |
| 123 |
|
| 124 |
if( is_array( $parameters ) ) { |
| 125 |
foreach ( $parameters as $key => $parameter ) { |
| 126 |
if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) |
| 127 |
$array[ sanitize_text_field( $parameter['name'] ) ] = sanitize_text_field( $parameter['value'] ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $array; |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Send the request and return our data |
| 137 |
*/ |
| 138 |
public function do_the_call() { |
| 139 |
|
| 140 |
// build the URL |
| 141 |
$this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url() ); |
| 142 |
|
| 143 |
// build the headers |
| 144 |
$this->final_headers = apply_filters( 'wpgetapi_final_headers', $this->build_headers() ); |
| 145 |
|
| 146 |
// POST request |
| 147 |
if( $this->method == 'POST' ) |
| 148 |
$this->response = $this->POST_request(); |
| 149 |
|
| 150 |
// GET request |
| 151 |
if( $this->method == 'GET' ) |
| 152 |
$this->response = $this->GET_request(); |
| 153 |
|
| 154 |
// DEBUG |
| 155 |
$this->debug(); |
| 156 |
|
| 157 |
// wp error |
| 158 |
if( is_wp_error( $this->response ) ) |
| 159 |
return $this->response->get_error_message(); |
| 160 |
|
| 161 |
$data = wp_remote_retrieve_body( $this->response ); |
| 162 |
|
| 163 |
// returning in JSON format |
| 164 |
if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' ) |
| 165 |
return $this->output_json( $data ); |
| 166 |
|
| 167 |
return apply_filters( 'wpgetapi_raw_data', wpgetapi_sanitize_text_or_array( $data ), $this ); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* return JSON data |
| 174 |
*/ |
| 175 |
public function output_json( $data ) { |
| 176 |
|
| 177 |
// converts all data to arrays, removing objects |
| 178 |
$data = json_decode( $data, true ); |
| 179 |
$data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys ); |
| 180 |
|
| 181 |
if( $this->results_format == 'json_string' ) { |
| 182 |
return json_encode( wpgetapi_sanitize_text_or_array( $data ) ); |
| 183 |
} |
| 184 |
if( $this->results_format == 'json_decoded' ) { |
| 185 |
return wpgetapi_sanitize_text_or_array( $data ); |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* build the request url with the ability to add args |
| 193 |
*/ |
| 194 |
public function build_url() { |
| 195 |
// make sure our endpoint starts with a slash |
| 196 |
if ( substr( $this->endpoint, 0, 1 ) != '/' ) |
| 197 |
$this->endpoint = '/' . $this->endpoint; |
| 198 |
return add_query_arg( $this->query_parameters, untrailingslashit( $this->base_url ) . $this->endpoint ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* build the header with the ability to add args |
| 203 |
*/ |
| 204 |
public function build_headers() { |
| 205 |
|
| 206 |
if( $this->method == 'POST' ) { |
| 207 |
|
| 208 |
if( isset( $this->body_parameters ) && $this->body_parameters != '' ) { |
| 209 |
|
| 210 |
// checking if the body paramters have a json string |
| 211 |
foreach ($this->body_parameters as $key => $value ) { |
| 212 |
|
| 213 |
if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) { |
| 214 |
$this->body_parameters[ $key ] = json_decode( $value, true); |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
$body = apply_filters( 'wpgetapi_body_parameters', array( |
| 220 |
'body' => json_encode( $this->body_parameters ), |
| 221 |
) ); |
| 222 |
|
| 223 |
$this->header_parameters = wp_parse_args( $body, $this->header_parameters ); |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
// build the header |
| 229 |
$defaults = apply_filters( 'wpgetapi_default_header_parameters', array( |
| 230 |
'timeout' => $this->timeout, |
| 231 |
'sslverify' => $this->sslverify, |
| 232 |
'redirection' => 5, |
| 233 |
'blocking' => true, |
| 234 |
'cookies' => array(), |
| 235 |
) ); |
| 236 |
|
| 237 |
return wp_parse_args( $this->header_parameters, $defaults ); |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
/** |
| 243 |
* Do a GET request |
| 244 |
*/ |
| 245 |
public function GET_request() { |
| 246 |
$response = ''; |
| 247 |
// so we can so something before the remote_get (caching) |
| 248 |
$response = apply_filters( 'wpgetapi_before_get_request', $response, $this ); |
| 249 |
if( empty( $response ) ) |
| 250 |
$response = wp_remote_get( $this->final_url, $this->final_headers ); |
| 251 |
return $response; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
/** |
| 256 |
* Do a POST request |
| 257 |
*/ |
| 258 |
public function POST_request() { |
| 259 |
$response = wp_remote_post( $this->final_url, $this->final_headers ); |
| 260 |
return $response; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
/** |
| 267 |
* decrypt our parameters from the DB |
| 268 |
*/ |
| 269 |
public function decrypt( $array_or_string ) { |
| 270 |
|
| 271 |
if( is_string($array_or_string) ){ |
| 272 |
$array_or_string = $this->encryption->decrypt($array_or_string); |
| 273 |
}elseif( is_array($array_or_string) ){ |
| 274 |
foreach ( $array_or_string as $key => &$value ) { |
| 275 |
if( $key == 'name' ) |
| 276 |
continue; |
| 277 |
if ( is_array( $value ) ) { |
| 278 |
$value = $this->decrypt($value); |
| 279 |
} |
| 280 |
else { |
| 281 |
$value = $this->encryption->decrypt($value); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
return $array_or_string; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* debug |
| 290 |
*/ |
| 291 |
public function debug() { |
| 292 |
//var_dump($this->args); |
| 293 |
if( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) { |
| 294 |
echo '<strong>Final URL</strong>'; |
| 295 |
wpgetapi_pp( $this->final_url ); |
| 296 |
echo '<strong>Query Parameters</strong>'; |
| 297 |
wpgetapi_pp( $this->query_parameters ); |
| 298 |
echo '<strong>Headers</strong>'; |
| 299 |
wpgetapi_pp( $this->final_headers ); |
| 300 |
echo '<strong>Response</strong>'; |
| 301 |
wpgetapi_pp($this->response); |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
} |