PluginProbe
WPGet API – Connect to any external REST API / 1.8.9
WPGet API – Connect to any external REST API v1.8.9
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
← All changes | includes/class-api.php +31 -205 2.0.61.8.9 View file →
@@ -28,9 +28,8 @@
28 28 public $header_parameters = ''; // the header paramaters
29 29 public $body_parameters = ''; // the body paramaters
30 30 public $body_json_encode = ''; // body_json_encode body
31 31 public $body_url_encode = ''; // urlencode body
32 - public $body_base64_encode = ''; // base64 encode body
33 32 public $body_xml_format = ''; // xml format body
34 33 public $xml_root_node = 'root'; // root node for xml
35 34
36 35 public $response = ''; // the returned response
@@ -39,12 +38,10 @@
39 38 public $final_request_args = array(); // final request_args
40 39
41 40 public $results_format = 'json_string'; // results format
42 41
43 - public $response_code = ''; // the response code from the call
44 -
45 42 public $timeout = 10; // the timeout
46 - public $sslverify = 1; // ssl verify
43 + public $sslverify = 1; // the paramaters
47 44 public $debug = false;
48 45
49 46
50 47 /**
@@ -75,9 +72,9 @@
75 72 public function init() {
76 73
77 74 // if we are doing importer plugin,
78 75 // we need to intercept and modify most of our methods values
79 - if( $this->api_id === 'wpgetapi_importer' ) {
76 + if( $this->api_id == 'wpgetapi_importer' ) {
80 77
81 78 $data = apply_filters( 'wpgetapi_api_importer_init', $this );
82 79
83 80 if( $data ) {
@@ -125,38 +122,20 @@
125 122 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
126 123
127 124 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
128 125
129 - // how to encode the body
130 126 $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false;
131 127 $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false;
132 - $this->body_base64_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'base64' ? true : false;
133 128 $this->body_xml_format = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'xml' ? true : false;
134 -
135 129 $this->xml_root_node = isset( $endpoint['xml_root'] ) && ! empty( $endpoint['xml_root'] ) ? $endpoint['xml_root'] : $this->xml_root_node;
136 130
137 131 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
138 132
139 -
140 - if(
141 - // set the format to php array data if html format is set
142 - ( isset( $this->args['format'] ) && $this->args['format'] == 'html' && $this->results_format != 'xml_array' ) ||
143 - // set the format to php array data if it is set in the args (coming from the importer)
144 - ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' )
145 - ) {
133 + // set the format to php array data if html format is set
134 + if( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) {
146 135 $this->results_format = 'json_decoded';
147 136 }
148 137
149 - // set the format to JSON string if it is set in the args (coming from the wpdatatables integration)
150 - if( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_string' ) {
151 - $this->results_format = 'json_string';
152 - }
153 -
154 - // set the format to JSON string if it is set in the args (coming from the wpdatatables integration)
155 - if( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' ) {
156 - $this->results_format = 'json_decoded';
157 - }
158 -
159 138 }
160 139
161 140 }
162 141
@@ -191,9 +170,9 @@
191 170 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
192 171
193 172 // build the request args
194 173 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
195 -
174 +
196 175 /**
197 176 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
198 177 * @param bool $on Whether on is set or not.
199 178 */
@@ -209,12 +188,8 @@
209 188 // PUT request
210 189 if( $this->method == 'PUT' )
211 190 $this->response = $this->PUT_request();
212 191
213 - // PATCH request
214 - if( $this->method == 'PATCH' )
215 - $this->response = $this->PATCH_request();
216 -
217 192 // GET request
218 193 if( $this->method == 'GET' )
219 194 $this->response = $this->GET_request();
220 195
@@ -227,17 +202,17 @@
227 202 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
228 203 }
229 204
230 205 // get response_code
231 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
206 + $response_code = wp_remote_retrieve_response_code( $this->response );
232 207
233 208 // action to intercept call depending on response_code
234 - $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $this->response_code, $this );
209 + $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $response_code, $this );
235 210
236 211 // get body
237 212 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
238 213 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
239 -
214 +
240 215 // returning in JSON format
241 216 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
242 217 $data = $this->return_data( $data );
243 218
@@ -257,14 +232,8 @@
257 232 // first make sure our endpoint starts with a slash
258 233 if ( substr( $this->endpoint, 0, 1 ) != '/' )
259 234 $this->endpoint = '/' . $this->endpoint;
260 235
261 - // if we have item_key from api to posts plugin
262 - // replace our keyword with the actual item_key value
263 - if ( isset( $this->args['item_key'] ) && strpos( $this->endpoint, '(importer:item_key)' ) !== false ) {
264 - $this->endpoint = str_replace( '(importer:item_key)', $this->args['item_key'], $this->endpoint );
265 - }
266 -
267 236 $this->endpoint = apply_filters( 'wpgetapi_endpoint', $this->endpoint, $this );
268 237
269 238 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
270 239
@@ -278,12 +247,10 @@
278 247
279 248 // add empty array - was getting errors if it was just null or false
280 249 $params = empty( $params ) ? array(''=>'') : $params;
281 250
282 - $result = add_query_arg( $params, $url );
251 + return add_query_arg( $params, $url );
283 252
284 - return $result;
285 -
286 253 }
287 254
288 255
289 256 /**
@@ -297,11 +264,10 @@
297 264 ), $this );
298 265
299 266
300 267 // if doing POST request, include body paramters
301 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
268 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
302 269
303 - // tokens are sent through here
304 270 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
305 271
306 272 // do we json_encode the entire body
307 273 if( $this->body_json_encode == true ) {
@@ -308,25 +274,22 @@
308 274 $this->body_parameters = json_encode( $this->body_parameters );
309 275 }
310 276
311 277 // do we urlencode the entire body
312 - if( $this->body_parameters && $this->body_url_encode == true ) {
278 + if( $this->body_url_encode == true ) {
313 279
314 280 // checking if the parameters have a json string (raw)
315 - if (
316 - ( ! is_array( $this->body_parameters ) ) &&
317 - ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' )
318 - ) {
281 + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) {
319 282 $this->body_parameters = json_decode( $this->body_parameters, true );
320 283 }
321 -
322 - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
323 284
285 + $this->body_parameters = http_build_query( $this->body_parameters );
286 +
324 287 }
325 288
326 289 // do we xml the entire body
327 - if( $this->body_parameters && $this->body_xml_format == true ) {
328 -
290 + if( $this->body_xml_format == true ) {
291 +
329 292 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
330 293
331 294 foreach( $this->body_parameters as $key => $value) {
332 295 $xml->addChild( $key, $value );
@@ -335,16 +298,8 @@
335 298 $this->body_parameters = $xml->asXML();
336 299
337 300 }
338 301
339 - // do we base64 encode the entire body
340 - if( $this->body_parameters && $this->body_base64_encode == true ) {
341 -
342 - $this->body_parameters = is_array( $this->body_parameters ) ? json_encode( $this->body_parameters ) : $this->body_parameters;
343 - $this->body_parameters = base64_encode( $this->body_parameters );
344 -
345 - }
346 -
347 302 // add our body parameters
348 303 if( $this->body_parameters && ! empty( $this->body_parameters ) )
349 304 $headers['body'] = $this->body_parameters;
350 305
@@ -349,11 +304,8 @@
349 304 $headers['body'] = $this->body_parameters;
350 305
351 306 }
352 307
353 - // get our body paramters as they are formatted
354 - $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this );
355 -
356 308 // add headers to our final request args
357 309 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
358 310
359 311 // build the args taht are sent to the request
@@ -369,15 +321,12 @@
369 321
370 322 }
371 323
372 324
373 -
374 -
375 325 /**
376 326 * Do a GET request
377 327 */
378 328 public function GET_request() {
379 -
380 329 $response = '';
381 330 // so we can so something before the remote_get (caching)
382 331 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
383 332
@@ -382,11 +331,9 @@
382 331 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
383 332
384 333 if( empty( $response ) )
385 334 $response = wp_remote_get( $this->final_url, $this->final_request_args );
386 -
387 335 return $response;
388 -
389 336 }
390 337
391 338 /**
392 339 * Do a POST request
@@ -391,18 +338,10 @@
391 338 /**
392 339 * Do a POST request
393 340 */
394 341 public function POST_request() {
395 -
396 - $response = '';
397 - // so we can so something before the remote_post (caching)
398 - $response = apply_filters( 'wpgetapi_before_post_request', $response, $this );
399 -
400 342 $this->final_request_args['method'] = 'POST';
401 -
402 - if( empty( $response ) )
403 - $response = wp_remote_post( $this->final_url, $this->final_request_args );
404 -
343 + $response = wp_remote_post( $this->final_url, $this->final_request_args );
405 344 return $response;
406 345 }
407 346
408 347 /**
@@ -414,17 +353,8 @@
414 353 return $response;
415 354 }
416 355
417 356 /**
418 - * Do a PATCH request
419 - */
420 - public function PATCH_request() {
421 - $this->final_request_args['method'] = 'PATCH';
422 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
423 - return $response;
424 - }
425 -
426 - /**
427 357 * Do a DELETE request
428 358 */
429 359 public function DELETE_request() {
430 360 $this->final_request_args['method'] = 'DELETE';
@@ -438,9 +368,9 @@
438 368 */
439 369 public function return_data( $data ) {
440 370
441 371 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
442 -
372 +
443 373 // converts all data to arrays, removing objects
444 374 if( is_string( $data ) ) {
445 375 $decode = json_decode( $data, true );
446 376 if( json_last_error() ) {
@@ -448,13 +378,13 @@
448 378 } else {
449 379 $data = $decode;
450 380 }
451 381 }
452 -
382 +
453 383 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
454 384
455 385 if( $this->results_format == 'json_string' )
456 - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
386 + return trim( json_encode( $data ),'"');
457 387
458 388 if( $this->results_format == 'json_decoded' )
459 389 return $data;
460 390
@@ -463,9 +393,9 @@
463 393 /**
464 394 * Sort out the parameters if they exist
465 395 */
466 396 public function do_parameters( $parameters ) {
467 -
397 +
468 398 $new_array = array();
469 399 $raw = '';
470 400 $parameters = $this->decrypt( $parameters );
471 401
@@ -481,14 +411,8 @@
481 411 unset( $parameters[ $key ] );
482 412 continue;
483 413 }
484 414
485 - // if we have item_key from api to posts plugin
486 - // replace our keyword with the actual item_key value
487 - if ( isset( $this->args['item_key'] ) && strpos( $parameter['value'], '(importer:item_key)' ) !== false ) {
488 - $parameter['value'] = str_replace( '(importer:item_key)', $this->args['item_key'], $parameter['value'] );
489 - }
490 -
491 415 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
492 416
493 417 // if we are sending raw body
494 418 } else if( empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
@@ -499,20 +423,17 @@
499 423
500 424 }
501 425 }
502 426
503 - // now look for array or json string and sort these out - for using such things in shortcode
427 + // look for array or json string and sort these out - for using such things in shortcode
504 428 if( ! empty( $new_array ) ) {
505 429
506 430 foreach ( $new_array as $key => $value ) {
507 431
508 432 // checking if the parameters have a json string
509 - if ( strpos( $value, '{"' ) !== false && strpos( $value, '}' ) !== false ) {
510 -
511 - $tmp_val = $value;
512 - $tmp_val = json_decode( $tmp_val, true );
513 - $new_array[ $key ] = $tmp_val ? $tmp_val : $value;
514 -
433 + if ( strpos( $value, '{' ) !== false && strpos( $value, '}' ) !== false ) {
434 + //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
435 + $new_array[ $key ] = json_decode( $value, true );
515 436 } else
516 437
517 438 // checking if the parameters have an array within the string
518 439 // if they do, create them as an actual array
@@ -531,10 +452,8 @@
531 452 // set value as float
532 453 if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) {
533 454 preg_match("/\(([^\)]*)\)/", $value, $match );
534 455 $result = $match[1];
535 - if ( substr( $result, 0, 1 ) === '"' && substr( $result, -1 ) === '"' )
536 - $result = trim( $result,'""' );
537 456 $new_array[ $key ] = (float) $result;
538 457
539 458 }
540 459
@@ -591,69 +510,10 @@
591 510 ) {
592 511
593 512 $this->debug = true;
594 513
595 - // admin test endpoint button
596 514 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
597 515
598 - // get api status from response code
599 - $status = array(
600 - 'title' => 'Error in API response.',
601 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
602 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
603 - );
604 -
605 - if( $this->response_code >= 200 && $this->response_code <= 299 ) {
606 - $status = array(
607 - 'title' => 'Success!',
608 - 'desc' => 'Your API is returning a success message and is returning the data as shown in the Data Output section below. You can modify this by changing the Results Format option or see <a href="https://wpgetapi.com/docs/format-api-data-as-html/">here</a> for info on formatting this data.',
609 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
610 - );
611 - }
612 - if( $this->response_code >= 400 && $this->response_code <= 499 ) {
613 - $status = array(
614 - 'title' => 'Bad Request',
615 - 'desc' => 'This could indicate that the URL you are calling is incorrect or that some other data is missing or incorrect. Please check your Base URL and Endpoint are correct as well as the Request Method.',
616 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
617 - );
618 - }
619 - if( $this->response_code == 401 ) {
620 - $status = array(
621 - 'title' => 'Unauthorised',
622 - 'desc' => 'You are successfully connecting to your API. But your API key, login or API credentials are incorrect. See <a href="https://wpgetapi.com/docs/authentication-authorization/">here</a> for info on correctly authorizing your API requests.',
623 - 'type' => '<span class="dashicons dashicons-warning"></span>'
624 - );
625 - }
626 - if( $this->response_code == 403 ) {
627 - $status = array(
628 - 'title' => 'Forbidden',
629 - 'desc' => 'This could indicate that you may need to get your IP address whitelisted with your API provider or that you do not have access to this API.',
630 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
631 - );
632 - }
633 - if( $this->response_code == 405 ) {
634 - $status = array(
635 - 'title' => 'Method Not Allowed',
636 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
637 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
638 - );
639 - }
640 - if( $this->response_code == 415 ) {
641 - $status = array(
642 - 'title' => 'Unsupported Media Type',
643 - 'desc' => 'Try adding "Content-type" to the Headers with the Value of "json/application" as shown <a href="https://wpgetapi.com/docs/troubleshooting/">here</a>.',
644 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
645 - );
646 - }
647 - if( $this->response_code >= 500 && $this->response_code <= 599 ) {
648 - $status = array(
649 - 'title' => 'Internal Server Error',
650 - 'desc' => 'This indicates an issue with the API server. Contact your API provider.',
651 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
652 - );
653 - }
654 -
655 -
656 516 ob_start(); ?>
657 517
658 518 <style>
659 519
@@ -690,10 +550,10 @@
690 550 background: #fff;
691 551 padding: 15px 20px;
692 552 display: inline-block !important;
693 553 width: 100% !important;
694 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
695 - border: 1px solid #eee;
554 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
555 + border: 1px solid #efefef;
696 556 }
697 557 .wpgetapi-debug .debug-item h6 {
698 558 font-weight: bold;
699 559 font-size: 14px;
@@ -705,14 +565,15 @@
705 565 font-family: monospace !important;
706 566 font-size: 13px !important;
707 567 margin: 0 !important;
708 568 padding: 8px 12px;
709 - background: #f4f7f9;
569 + background: #f8f8f8;
710 570 }
711 571 .wpgetapi-debug .debug-item .debug-result pre {
712 572 border: none;
713 573 padding: 5px
714 574 }
575 +
715 576 .wpgetapi-debug.testing {
716 577 background: transparent;
717 578 padding: 0;
718 579 box-shadow: none;
@@ -719,32 +580,8 @@
719 580 }
720 581 .wpgetapi-debug.testing .debug-item {
721 582 margin: 5px 0;
722 583 }
723 -.wpgetapi-debug.testing .debug-item.status {
724 - border: none;
725 - box-shadow: none;
726 - margin-bottom: 15px;
727 -}
728 -.wpgetapi-debug .debug-item.status h5 {
729 - font-size: 15px;
730 -}
731 -.wpgetapi-debug .status .dashicons {
732 - margin: 0 5px 3px 0;
733 -}
734 -.wpgetapi-debug .status .dashicons-yes-alt {
735 - color: #5e9929;
736 -}
737 -.wpgetapi-debug .status .dashicons-dismiss {
738 - color: #cc4646;
739 -}
740 -.wpgetapi-debug .status .dashicons-warning {
741 - color: #dfa12e;
742 -}
743 -.wpgetapi-debug .status .desc {
744 - font-size: 14px;
745 - font-weight: 400 !important;
746 -}
747 584 </style>
748 585
749 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
750 587
@@ -749,15 +586,9 @@
749 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
750 587
751 588 <?php if( $testing ) { ?>
752 589
753 - <div class="debug-item status">
754 - <div class="">
755 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
756 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
757 - </div>
758 - </div>
759 - <div class="debug-item">
590 + <div class="debug-item">
760 591 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
761 592 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
762 593 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
763 594 </div>
@@ -764,15 +595,10 @@
764 595
765 596 <div class="debug-item">
766 597 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
767 598 <p class="small"><?php esc_html_e( 'The output that has been returned from the API. This is what the shortcode or template tag will display and can be formatted however you like.', 'wpgetapi' ); ?></p>
599 + <p class="small"><?php esc_html_e( 'See the ', 'wpgetapi' ); ?> <a target="_blank" href="https://wpgetapi.com/docs/?utm_campaign=Test Endpoint&utm_medium=Admin&utm_source=User"><?php esc_html_e( 'documentation', 'wpgetapi' ); ?></a> <?php esc_html_e( ' for help in formatting.', 'wpgetapi' ); ?></p>
768 600 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
769 - </div>
770 -
771 - <div class="debug-item">
772 - <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
773 - <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
774 - <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
775 601 </div>
776 602
777 603 <div class="debug-item">
778 604 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>