PluginProbe
WPGet API – Connect to any external REST API / 1.4.0
WPGet API – Connect to any external REST API v1.4.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.4.0, at includes/class-api.php

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