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 +23 -164 2.0.21.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,10 +38,8 @@
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 43 public $sslverify = 1; // the paramaters
47 44 public $debug = false;
48 45
@@ -125,33 +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 138 }
155 139
156 140 }
157 141
@@ -186,9 +170,9 @@
186 170 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
187 171
188 172 // build the request args
189 173 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
190 -
174 +
191 175 /**
192 176 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
193 177 * @param bool $on Whether on is set or not.
194 178 */
@@ -204,12 +188,8 @@
204 188 // PUT request
205 189 if( $this->method == 'PUT' )
206 190 $this->response = $this->PUT_request();
207 191
208 - // PATCH request
209 - if( $this->method == 'PATCH' )
210 - $this->response = $this->PATCH_request();
211 -
212 192 // GET request
213 193 if( $this->method == 'GET' )
214 194 $this->response = $this->GET_request();
215 195
@@ -222,17 +202,17 @@
222 202 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
223 203 }
224 204
225 205 // get response_code
226 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
206 + $response_code = wp_remote_retrieve_response_code( $this->response );
227 207
228 208 // action to intercept call depending on response_code
229 - $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 );
230 210
231 211 // get body
232 212 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
233 213 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
234 -
214 +
235 215 // returning in JSON format
236 216 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
237 217 $data = $this->return_data( $data );
238 218
@@ -267,12 +247,10 @@
267 247
268 248 // add empty array - was getting errors if it was just null or false
269 249 $params = empty( $params ) ? array(''=>'') : $params;
270 250
271 - $result = add_query_arg( $params, $url );
251 + return add_query_arg( $params, $url );
272 252
273 - return $result;
274 -
275 253 }
276 254
277 255
278 256 /**
@@ -286,11 +264,10 @@
286 264 ), $this );
287 265
288 266
289 267 // if doing POST request, include body paramters
290 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
268 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
291 269
292 - // tokens are sent through here
293 270 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
294 271
295 272 // do we json_encode the entire body
296 273 if( $this->body_json_encode == true ) {
@@ -297,24 +274,21 @@
297 274 $this->body_parameters = json_encode( $this->body_parameters );
298 275 }
299 276
300 277 // do we urlencode the entire body
301 - if( $this->body_parameters && $this->body_url_encode == true ) {
278 + if( $this->body_url_encode == true ) {
302 279
303 280 // checking if the parameters have a json string (raw)
304 - if (
305 - ( ! is_array( $this->body_parameters ) ) &&
306 - ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' )
307 - ) {
281 + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) {
308 282 $this->body_parameters = json_decode( $this->body_parameters, true );
309 283 }
310 -
311 - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
312 284
285 + $this->body_parameters = http_build_query( $this->body_parameters );
286 +
313 287 }
314 288
315 289 // do we xml the entire body
316 - if( $this->body_parameters && $this->body_xml_format == true ) {
290 + if( $this->body_xml_format == true ) {
317 291
318 292 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
319 293
320 294 foreach( $this->body_parameters as $key => $value) {
@@ -324,16 +298,8 @@
324 298 $this->body_parameters = $xml->asXML();
325 299
326 300 }
327 301
328 - // do we base64 encode the entire body
329 - if( $this->body_parameters && $this->body_base64_encode == true ) {
330 -
331 - $this->body_parameters = is_array( $this->body_parameters ) ? json_encode( $this->body_parameters ) : $this->body_parameters;
332 - $this->body_parameters = base64_encode( $this->body_parameters );
333 -
334 - }
335 -
336 302 // add our body parameters
337 303 if( $this->body_parameters && ! empty( $this->body_parameters ) )
338 304 $headers['body'] = $this->body_parameters;
339 305
@@ -338,11 +304,8 @@
338 304 $headers['body'] = $this->body_parameters;
339 305
340 306 }
341 307
342 - // get our body paramters as they are formatted
343 - $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this );
344 -
345 308 // add headers to our final request args
346 309 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
347 310
348 311 // build the args taht are sent to the request
@@ -358,10 +321,8 @@
358 321
359 322 }
360 323
361 324
362 -
363 -
364 325 /**
365 326 * Do a GET request
366 327 */
367 328 public function GET_request() {
@@ -392,17 +353,8 @@
392 353 return $response;
393 354 }
394 355
395 356 /**
396 - * Do a PATCH request
397 - */
398 - public function PATCH_request() {
399 - $this->final_request_args['method'] = 'PATCH';
400 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
401 - return $response;
402 - }
403 -
404 - /**
405 357 * Do a DELETE request
406 358 */
407 359 public function DELETE_request() {
408 360 $this->final_request_args['method'] = 'DELETE';
@@ -416,9 +368,9 @@
416 368 */
417 369 public function return_data( $data ) {
418 370
419 371 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
420 -
372 +
421 373 // converts all data to arrays, removing objects
422 374 if( is_string( $data ) ) {
423 375 $decode = json_decode( $data, true );
424 376 if( json_last_error() ) {
@@ -426,13 +378,13 @@
426 378 } else {
427 379 $data = $decode;
428 380 }
429 381 }
430 -
382 +
431 383 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
432 384
433 385 if( $this->results_format == 'json_string' )
434 - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
386 + return trim( json_encode( $data ),'"');
435 387
436 388 if( $this->results_format == 'json_decoded' )
437 389 return $data;
438 390
@@ -441,9 +393,9 @@
441 393 /**
442 394 * Sort out the parameters if they exist
443 395 */
444 396 public function do_parameters( $parameters ) {
445 -
397 +
446 398 $new_array = array();
447 399 $raw = '';
448 400 $parameters = $this->decrypt( $parameters );
449 401
@@ -558,69 +510,10 @@
558 510 ) {
559 511
560 512 $this->debug = true;
561 513
562 - // admin test endpoint button
563 514 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
564 515
565 - // get api status from response code
566 - $status = array(
567 - 'title' => 'Error in API response.',
568 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
569 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
570 - );
571 -
572 - if( $this->response_code == 200 ) {
573 - $status = array(
574 - 'title' => 'Success!',
575 - '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.',
576 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
577 - );
578 - }
579 - if( $this->response_code == 400 || $this->response_code == 404 ) {
580 - $status = array(
581 - 'title' => 'Bad Request',
582 - '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.',
583 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
584 - );
585 - }
586 - if( $this->response_code == 401 ) {
587 - $status = array(
588 - 'title' => 'Unauthorised',
589 - '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.',
590 - 'type' => '<span class="dashicons dashicons-warning"></span>'
591 - );
592 - }
593 - if( $this->response_code == 403 ) {
594 - $status = array(
595 - 'title' => 'Forbidden',
596 - '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.',
597 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
598 - );
599 - }
600 - if( $this->response_code == 405 ) {
601 - $status = array(
602 - 'title' => 'Method Not Allowed',
603 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
604 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
605 - );
606 - }
607 - if( $this->response_code == 415 ) {
608 - $status = array(
609 - 'title' => 'Unsupported Media Type',
610 - '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>.',
611 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
612 - );
613 - }
614 - if( $this->response_code == 500 ) {
615 - $status = array(
616 - 'title' => 'Internal Server Error',
617 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
618 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
619 - );
620 - }
621 -
622 -
623 516 ob_start(); ?>
624 517
625 518 <style>
626 519
@@ -657,10 +550,10 @@
657 550 background: #fff;
658 551 padding: 15px 20px;
659 552 display: inline-block !important;
660 553 width: 100% !important;
661 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
662 - border: 1px solid #eee;
554 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
555 + border: 1px solid #efefef;
663 556 }
664 557 .wpgetapi-debug .debug-item h6 {
665 558 font-weight: bold;
666 559 font-size: 14px;
@@ -672,14 +565,15 @@
672 565 font-family: monospace !important;
673 566 font-size: 13px !important;
674 567 margin: 0 !important;
675 568 padding: 8px 12px;
676 - background: #f4f7f9;
569 + background: #f8f8f8;
677 570 }
678 571 .wpgetapi-debug .debug-item .debug-result pre {
679 572 border: none;
680 573 padding: 5px
681 574 }
575 +
682 576 .wpgetapi-debug.testing {
683 577 background: transparent;
684 578 padding: 0;
685 579 box-shadow: none;
@@ -686,32 +580,8 @@
686 580 }
687 581 .wpgetapi-debug.testing .debug-item {
688 582 margin: 5px 0;
689 583 }
690 -.wpgetapi-debug.testing .debug-item.status {
691 - border: none;
692 - box-shadow: none;
693 - margin-bottom: 15px;
694 -}
695 -.wpgetapi-debug .debug-item.status h5 {
696 - font-size: 15px;
697 -}
698 -.wpgetapi-debug .status .dashicons {
699 - margin: 0 5px 3px 0;
700 -}
701 -.wpgetapi-debug .status .dashicons-yes-alt {
702 - color: #5e9929;
703 -}
704 -.wpgetapi-debug .status .dashicons-dismiss {
705 - color: #cc4646;
706 -}
707 -.wpgetapi-debug .status .dashicons-warning {
708 - color: #dfa12e;
709 -}
710 -.wpgetapi-debug .status .desc {
711 - font-size: 14px;
712 - font-weight: 400 !important;
713 -}
714 584 </style>
715 585
716 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
717 587
@@ -716,15 +586,9 @@
716 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
717 587
718 588 <?php if( $testing ) { ?>
719 589
720 - <div class="debug-item status">
721 - <div class="">
722 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
723 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
724 - </div>
725 - </div>
726 - <div class="debug-item">
590 + <div class="debug-item">
727 591 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
728 592 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
729 593 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
730 594 </div>
@@ -731,15 +595,10 @@
731 595
732 596 <div class="debug-item">
733 597 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
734 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>
735 600 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
736 - </div>
737 -
738 - <div class="debug-item">
739 - <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
740 - <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
741 - <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
742 601 </div>
743 602
744 603 <div class="debug-item">
745 604 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>