PluginProbe
WPGet API – Connect to any external REST API / 1.6.0
WPGet API – Connect to any external REST API v1.6.0
1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.10 2.2.2 2.2.3 All 97 releases
wpgetapi / includes / class-api.php

class-api.php in WPGet API – Connect to any external REST API 1.6.0, at includes/class-api.php

503 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = 'GET'; // 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 public $body_json_encode = ''; // body_json_encode body
31 public $body_url_encode = ''; // urlencode body
32
33 public $response = ''; // the returned response
34
35 public $final_url = ''; // final URL
36 public $final_request_args = array(); // final request_args
37
38 public $results_format = 'json_string'; // reseults format
39
40 public $timeout = 10; // the timeout
41 public $sslverify = 1; // the paramaters
42
43
44 /**
45 * Main constructor
46 *
47 * @since 1.0.0
48 *
49 */
50 public function __construct( $api_id = '', $endpoint_id = '', $args = array(), $keys = array() ) {
51
52 $this->setup = get_option( 'wpgetapi_setup' );
53 $this->api = get_option( 'wpgetapi_' . $api_id );
54
55 $this->encryption = new WpGetApi_Encryption();
56 $this->api_id = sanitize_text_field( $api_id );
57 $this->endpoint_id = sanitize_text_field( $endpoint_id );
58 $this->keys = $keys;
59 $this->args = $args;
60
61 add_filter( 'wpgetapi_raw_data', array( $this, 'maybe_add_debug_info' ), 5, 2 );
62
63 }
64
65
66 /**
67 * Init our api data
68 */
69 public function init() {
70
71 // do some checks
72 if( empty( $this->setup ) || ! isset( $this->setup['apis'] ) || empty( $this->setup['apis'] ) )
73 return 'No API found.';
74
75 // do some checks
76 if( ! $this->api || ! isset( $this->api['endpoints'] ) || empty( $this->api['endpoints'] ))
77 return 'API not found.';
78
79 // loop through api's
80 // set base_url
81 foreach ( $this->setup['apis'] as $index => $api_item ) {
82 if( $this->api_id == $api_item['id'] ) {
83 $this->base_url = isset( $api_item['base_url'] ) && ! empty( $api_item['base_url'] ) ? esc_url_raw( $api_item['base_url'] ) : '';
84 }
85 }
86
87 // loop through endpoints
88 foreach ( $this->api['endpoints'] as $index => $endpoint ) {
89
90 if( $this->endpoint_id == $endpoint['id'] ) {
91
92 $this->method = isset( $endpoint['method'] ) && $endpoint['method'] != '' ? sanitize_text_field( $endpoint['method'] ) : 'GET';
93 $this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : '';
94 $this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : '';
95
96 $this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array();
97
98 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
99
100 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
101
102 $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false;
103 $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false;
104
105 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
106 }
107 }
108
109 $this->timeout = isset( $this->api['timeout'] ) && ! empty( $this->api['timeout'] ) ? $this->api['timeout'] : $this->timeout;
110 $this->sslverify = isset( $this->api['sslverify'] ) ? absint( $this->api['sslverify'] ) : $this->sslverify;
111
112 }
113
114
115 /**
116 * The main function to output the data
117 *
118 * @param string $field Field to retrieve
119 */
120 public function endpoint_output() {
121 return $this->do_the_call();
122 }
123
124
125 /**
126 * Sort out the parameters if they exist
127 */
128 public function do_parameters( $parameters ) {
129
130 $new_array = array();
131 $parameters = $this->decrypt($parameters);
132
133 if( is_array( $parameters ) ) {
134 foreach ( $parameters as $key => $parameter ) {
135 if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
136 $parameter['value'] = stripslashes( $parameter['value'] );
137 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
138 }
139 }
140 }
141
142 // look for array or json string and sort these out - for using such things in shortcode
143 if( is_array( $new_array ) ) {
144
145 foreach ( $new_array as $key => $value ) {
146
147 // checking if the parameters have a json string
148 if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
149 $new_array[ $key ] = json_decode( $value, true);
150 }
151
152 // checking if the parameters have an array within the string
153 // if they do, create them as an actual array
154 if ( substr( $value, 0, 1 ) === '[' && substr( $value, -1 ) === ']' ) {
155 $new_array[ $key ] = array( $value );
156 }
157
158 }
159
160 }
161
162 return $new_array;
163
164 }
165
166 /**
167 * Send the request and return our data
168 */
169 public function do_the_call() {
170
171 $this->init();
172
173 // build the URL
174 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
175
176 // build the request args
177 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
178
179 // POST request
180 if( $this->method == 'POST' )
181 $this->response = $this->POST_request();
182
183 // PUT request
184 if( $this->method == 'PUT' )
185 $this->response = $this->PUT_request();
186
187 // GET request
188 if( $this->method == 'GET' )
189 $this->response = $this->GET_request();
190
191 // wp error
192 if( is_wp_error( $this->response ) ) {
193 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
194 }
195
196 // get response_code
197 $response_code = wp_remote_retrieve_response_code( $this->response );
198
199 // action to intercept call depending on response_code
200 $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $response_code, $this );
201
202 // get body
203 $data = wp_remote_retrieve_body( $this->response );
204
205 // returning in JSON format
206 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
207 $data = $this->return_data( $data );
208
209 return apply_filters( 'wpgetapi_raw_data', $data, $this );
210
211 }
212
213
214 /**
215 * Build the request url with the ability to add args
216 */
217 public function build_url() {
218
219 // first make sure our endpoint starts with a slash
220 if ( substr( $this->endpoint, 0, 1 ) != '/' )
221 $this->endpoint = '/' . $this->endpoint;
222
223 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
224
225 // filter our params - added for use in Oauth 2.0 plugin
226 $params = apply_filters( 'wpgetapi_query_parameters', $this->query_parameters, $this );
227
228 // add empty array - was getting errors if it was just null or false
229 $params = empty( $params ) ? array(''=>'') : $params;
230
231 return add_query_arg( $params, $url );
232
233 }
234
235
236 /**
237 * build the header with the ability to add args
238 */
239 public function build_request_args() {
240
241 // add our header parameters to the headers
242 $headers = apply_filters( 'wpgetapi_header_parameters', array(
243 'headers' => $this->header_parameters,
244 ), $this );
245
246
247 // if doing POST request, include body paramters
248 if( $this->method == 'POST' || $this->method == 'PUT' ) {
249
250 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
251
252 // do we json_encode the entire body as a string
253 if( $this->body_json_encode == true )
254 $this->body_parameters = json_encode( $this->body_parameters );
255
256
257 // do we urlencode the entire body as a string
258 if( $this->body_url_encode == true ) {
259 $data_raw = '';
260 foreach ($this->body_parameters as $key => $value){
261 $data_raw = $data_raw . $key . "=" . urlencode($value) . "&";
262 }
263 $this->body_parameters = $data_raw;
264 }
265
266 // add our body parameters
267 if( $this->body_parameters && ! empty( $this->body_parameters ) )
268 $headers['body'] = $this->body_parameters;
269
270 }
271
272 // add headers to our final request args
273 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
274
275 // build the args taht are sent to the request
276 $default_args = apply_filters( 'wpgetapi_default_request_args_parameters', array(
277 'timeout' => $this->timeout,
278 'sslverify' => $this->sslverify,
279 'redirection' => 5,
280 'blocking' => true,
281 'cookies' => array(),
282 ) );
283
284 return wp_parse_args( $this->final_request_args, $default_args );
285
286 }
287
288
289 /**
290 * Do a GET request
291 */
292 public function GET_request() {
293 $response = '';
294 // so we can so something before the remote_get (caching)
295 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
296 if( empty( $response ) )
297 $response = wp_remote_get( $this->final_url, $this->final_request_args );
298
299 return $response;
300 }
301
302
303 /**
304 * Do a POST request
305 */
306 public function POST_request() {
307 $response = wp_remote_post( $this->final_url, $this->final_request_args );
308 return $response;
309 }
310
311 /**
312 * Do a PUT request
313 */
314 public function PUT_request() {
315 $this->final_request_args['method'] = 'PUT';
316 $response = wp_remote_request( $this->final_url, $this->final_request_args );
317 return $response;
318 }
319
320
321 /**
322 * return data
323 */
324 public function return_data( $data ) {
325
326 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
327
328 // converts all data to arrays, removing objects
329 $data = json_decode( $data, true );
330
331 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
332
333 if( $this->results_format == 'json_string' )
334 return trim( json_encode( $data ),'"');
335
336 if( $this->results_format == 'json_decoded' )
337 return $data;
338
339 }
340
341
342 /**
343 * decrypt our parameters from the DB
344 */
345 public function decrypt( $array_or_string ) {
346
347 if( is_string($array_or_string) ){
348 $array_or_string = $this->encryption->decrypt($array_or_string);
349 }elseif( is_array($array_or_string) ){
350 foreach ( $array_or_string as $key => &$value ) {
351 if( $key === 'name' )
352 continue;
353 if ( is_array( $value ) ) {
354 $value = $this->decrypt($value);
355 }
356 else {
357 $value = $this->encryption->decrypt($value);
358
359 }
360 }
361 }
362 return $array_or_string;
363 }
364
365
366 /**
367 * debug
368 */
369 public function maybe_add_debug_info( $data, $this_data ) {
370
371 if( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) {
372
373 ob_start(); ?>
374
375 <style>
376
377 .wpgetapi-debug {
378 background: #f4f4f4;
379 padding: 20px;
380 border: 1px solid #ccc;
381 margin: 20px;
382 font-size: 15px;
383 }
384 .wpgetapi-debug
385 .wpgetapi-debug p
386 .wpgetapi-debug h5
387 .wpgetapi-debug h6 {
388 color: #000;
389 line-height: 1 !important;
390 text-transform: none !important;
391 font-family: sans-serif !important;
392 letter-spacing: 0px !important;
393 }
394 .wpgetapi-debug p.small {
395 font-size: 85%;
396 color: #999;
397 margin: 0 0 12px !important;
398 }
399 .wpgetapi-debug h5 {
400 font-weight: bold;
401 font-size: 18px;
402 margin: 0 0 3px;
403 text-transform: none !important;
404 }
405 .wpgetapi-debug .debug-item {
406 margin: 10px 0;
407 background: #fff;
408 padding: 15px 20px;
409 display: inline-block !important;
410 width: 100% !important;
411 box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
412 border: 1px solid #efefef;
413 }
414 .wpgetapi-debug .debug-item h6 {
415 font-weight: bold;
416 font-size: 16px;
417 margin: 0;
418 text-transform: none !important;
419 }
420 .wpgetapi-debug .debug-item .debug-result,
421 .wpgetapi-debug .debug-item .debug-result pre {
422 font-family: monospace !important;
423 font-size: 15px !important;
424 margin: 0 !important;
425 padding: 10px 12px;
426 background: #f8f8f8;
427 }
428 .wpgetapi-debug .debug-item .debug-result pre {
429 border: none;
430 padding: 5px
431 }
432
433 </style>
434
435 <div class="wpgetapi-debug">
436
437 <h5><strong><?php esc_html_e( 'WPGetAPI, Debug Mode - Plugin Version', 'wpgetapi' ); ?> <?php esc_html_e( WpGetApi()->version ); ?></strong></h5>
438 <p class="small"><?php esc_html_e( 'Below you will find debug info for the current API call. To turn this off, set debug to false in your shortcode or template tag.', 'wpgetapi' ); ?><br><?php esc_html_e( 'Connecting to API\'s can be tricky - if you require help, please see the', 'wpgetapi' ); ?> <a target="_blank" href="https://wpgetapi.com/docs/"><?php esc_html_e( 'Documentation', 'wpgetapi' ); ?></a>.</p>
439
440 <div class="debug-item">
441 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
442 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
443 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
444 </div>
445
446 <div class="debug-item">
447 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
448 <p class="small"><?php esc_html_e( 'The resulting output that has been returned from the API.', 'wpgetapi' ); ?></p>
449 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
450 </div>
451
452 <div class="debug-item">
453 <h6><?php esc_html_e( 'Arguments', 'wpgetapi' ); ?></h6>
454 <p class="small"><?php esc_html_e( 'The arguments set within your shortcode or template tag.', 'wpgetapi' ); ?></p>
455 <div class="debug-result"><?php wpgetapi_pp( $this->args ); ?></div>
456 </div>
457
458 <div class="debug-item">
459 <h6><?php esc_html_e( 'Query String', 'wpgetapi' ); ?></h6>
460 <p class="small"><?php esc_html_e( 'The query string parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
461 <div class="debug-result"><?php wpgetapi_pp( $this->query_parameters ); ?></div>
462 </div>
463
464 <div class="debug-item">
465 <h6><?php esc_html_e( 'Headers', 'wpgetapi' ); ?></h6>
466 <p class="small"><?php esc_html_e( 'The header parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
467 <div class="debug-result"><?php wpgetapi_pp( $this->header_parameters ); ?></div>
468 </div>
469
470 <div class="debug-item">
471 <h6><?php esc_html_e( 'Body', 'wpgetapi' ); ?></h6>
472 <p class="small"><?php esc_html_e( 'The body parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
473 <div class="debug-result"><?php wpgetapi_pp( $this->body_parameters ); ?></div>
474 </div>
475
476 <div class="debug-item">
477 <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
478 <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
479 <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
480 </div>
481
482 <div class="debug-item">
483 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>
484 <p class="small"><?php esc_html_e( 'The full response from the API request.', 'wpgetapi' ); ?></p>
485 <div class="debug-result"><?php wpgetapi_pp( $this->response); ?></div>
486 </div>
487
488 </div>
489
490 <?php
491
492 return ob_get_clean();
493
494 } else {
495
496 return $data;
497
498 }
499
500 }
501
502
503 }