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 +21 -144 1.9.71.8.9 View file →
@@ -38,10 +38,8 @@
38 38 public $final_request_args = array(); // final request_args
39 39
40 40 public $results_format = 'json_string'; // results format
41 41
42 - public $response_code = ''; // the response code from the call
43 -
44 42 public $timeout = 10; // the timeout
45 43 public $sslverify = 1; // the paramaters
46 44 public $debug = false;
47 45
@@ -124,24 +122,17 @@
124 122 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
125 123
126 124 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
127 125
128 - // how to encode the body
129 126 $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false;
130 127 $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false;
131 128 $this->body_xml_format = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'xml' ? true : false;
132 -
133 129 $this->xml_root_node = isset( $endpoint['xml_root'] ) && ! empty( $endpoint['xml_root'] ) ? $endpoint['xml_root'] : $this->xml_root_node;
134 130
135 131 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
136 132
137 -
138 - if(
139 - // set the format to php array data if html format is set
140 - ( isset( $this->args['format'] ) && $this->args['format'] == 'html' && $this->results_format != 'xml_array' ) ||
141 - // set the format to php array data if it is set in the args (coming from the importer)
142 - ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' )
143 - ) {
133 + // set the format to php array data if html format is set
134 + if( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) {
144 135 $this->results_format = 'json_decoded';
145 136 }
146 137
147 138 }
@@ -179,9 +170,9 @@
179 170 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
180 171
181 172 // build the request args
182 173 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
183 -
174 +
184 175 /**
185 176 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
186 177 * @param bool $on Whether on is set or not.
187 178 */
@@ -197,12 +188,8 @@
197 188 // PUT request
198 189 if( $this->method == 'PUT' )
199 190 $this->response = $this->PUT_request();
200 191
201 - // PATCH request
202 - if( $this->method == 'PATCH' )
203 - $this->response = $this->PATCH_request();
204 -
205 192 // GET request
206 193 if( $this->method == 'GET' )
207 194 $this->response = $this->GET_request();
208 195
@@ -215,17 +202,17 @@
215 202 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
216 203 }
217 204
218 205 // get response_code
219 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
206 + $response_code = wp_remote_retrieve_response_code( $this->response );
220 207
221 208 // action to intercept call depending on response_code
222 - $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 );
223 210
224 211 // get body
225 212 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
226 213 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
227 -
214 +
228 215 // returning in JSON format
229 216 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
230 217 $data = $this->return_data( $data );
231 218
@@ -260,12 +247,10 @@
260 247
261 248 // add empty array - was getting errors if it was just null or false
262 249 $params = empty( $params ) ? array(''=>'') : $params;
263 250
264 - $result = add_query_arg( $params, $url );
251 + return add_query_arg( $params, $url );
265 252
266 - return $result;
267 -
268 253 }
269 254
270 255
271 256 /**
@@ -279,11 +264,10 @@
279 264 ), $this );
280 265
281 266
282 267 // if doing POST request, include body paramters
283 - //if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
268 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
284 269
285 - // tokens are sent through here
286 270 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
287 271
288 272 // do we json_encode the entire body
289 273 if( $this->body_json_encode == true ) {
@@ -290,24 +274,21 @@
290 274 $this->body_parameters = json_encode( $this->body_parameters );
291 275 }
292 276
293 277 // do we urlencode the entire body
294 - if( $this->body_parameters && $this->body_url_encode == true ) {
278 + if( $this->body_url_encode == true ) {
295 279
296 280 // checking if the parameters have a json string (raw)
297 - if (
298 - ( ! is_array( $this->body_parameters ) ) &&
299 - ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' )
300 - ) {
281 + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) {
301 282 $this->body_parameters = json_decode( $this->body_parameters, true );
302 283 }
303 -
304 - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
305 284
285 + $this->body_parameters = http_build_query( $this->body_parameters );
286 +
306 287 }
307 288
308 289 // do we xml the entire body
309 - if( $this->body_parameters && $this->body_xml_format == true ) {
290 + if( $this->body_xml_format == true ) {
310 291
311 292 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
312 293
313 294 foreach( $this->body_parameters as $key => $value) {
@@ -321,9 +302,9 @@
321 302 // add our body parameters
322 303 if( $this->body_parameters && ! empty( $this->body_parameters ) )
323 304 $headers['body'] = $this->body_parameters;
324 305
325 - //}
306 + }
326 307
327 308 // add headers to our final request args
328 309 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
329 310
@@ -340,10 +321,8 @@
340 321
341 322 }
342 323
343 324
344 -
345 -
346 325 /**
347 326 * Do a GET request
348 327 */
349 328 public function GET_request() {
@@ -374,17 +353,8 @@
374 353 return $response;
375 354 }
376 355
377 356 /**
378 - * Do a PATCH request
379 - */
380 - public function PATCH_request() {
381 - $this->final_request_args['method'] = 'PATCH';
382 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
383 - return $response;
384 - }
385 -
386 - /**
387 357 * Do a DELETE request
388 358 */
389 359 public function DELETE_request() {
390 360 $this->final_request_args['method'] = 'DELETE';
@@ -498,9 +468,9 @@
498 468
499 469 }
500 470
501 471 }
502 -
472 +
503 473 return $raw ? $raw : $new_array;
504 474
505 475 }
506 476
@@ -540,69 +510,10 @@
540 510 ) {
541 511
542 512 $this->debug = true;
543 513
544 - // admin test endpoint button
545 514 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
546 515
547 - // get api status from response code
548 - $status = array(
549 - 'title' => 'Error in API response.',
550 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
551 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
552 - );
553 -
554 - if( $this->response_code == 200 ) {
555 - $status = array(
556 - 'title' => 'Success!',
557 - '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.',
558 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
559 - );
560 - }
561 - if( $this->response_code == 400 || $this->response_code == 404 ) {
562 - $status = array(
563 - 'title' => 'Bad Request',
564 - '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.',
565 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
566 - );
567 - }
568 - if( $this->response_code == 401 ) {
569 - $status = array(
570 - 'title' => 'Unauthorised',
571 - '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.',
572 - 'type' => '<span class="dashicons dashicons-warning"></span>'
573 - );
574 - }
575 - if( $this->response_code == 403 ) {
576 - $status = array(
577 - 'title' => 'Forbidden',
578 - '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.',
579 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
580 - );
581 - }
582 - if( $this->response_code == 405 ) {
583 - $status = array(
584 - 'title' => 'Method Not Allowed',
585 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
586 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
587 - );
588 - }
589 - if( $this->response_code == 415 ) {
590 - $status = array(
591 - 'title' => 'Unsupported Media Type',
592 - '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>.',
593 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
594 - );
595 - }
596 - if( $this->response_code == 500 ) {
597 - $status = array(
598 - 'title' => 'Internal Server Error',
599 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
600 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
601 - );
602 - }
603 -
604 -
605 516 ob_start(); ?>
606 517
607 518 <style>
608 519
@@ -639,10 +550,10 @@
639 550 background: #fff;
640 551 padding: 15px 20px;
641 552 display: inline-block !important;
642 553 width: 100% !important;
643 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
644 - border: 1px solid #eee;
554 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
555 + border: 1px solid #efefef;
645 556 }
646 557 .wpgetapi-debug .debug-item h6 {
647 558 font-weight: bold;
648 559 font-size: 14px;
@@ -654,14 +565,15 @@
654 565 font-family: monospace !important;
655 566 font-size: 13px !important;
656 567 margin: 0 !important;
657 568 padding: 8px 12px;
658 - background: #f4f7f9;
569 + background: #f8f8f8;
659 570 }
660 571 .wpgetapi-debug .debug-item .debug-result pre {
661 572 border: none;
662 573 padding: 5px
663 574 }
575 +
664 576 .wpgetapi-debug.testing {
665 577 background: transparent;
666 578 padding: 0;
667 579 box-shadow: none;
@@ -668,32 +580,8 @@
668 580 }
669 581 .wpgetapi-debug.testing .debug-item {
670 582 margin: 5px 0;
671 583 }
672 -.wpgetapi-debug.testing .debug-item.status {
673 - border: none;
674 - box-shadow: none;
675 - margin-bottom: 15px;
676 -}
677 -.wpgetapi-debug .debug-item.status h5 {
678 - font-size: 15px;
679 -}
680 -.wpgetapi-debug .status .dashicons {
681 - margin: 0 5px 3px 0;
682 -}
683 -.wpgetapi-debug .status .dashicons-yes-alt {
684 - color: #5e9929;
685 -}
686 -.wpgetapi-debug .status .dashicons-dismiss {
687 - color: #cc4646;
688 -}
689 -.wpgetapi-debug .status .dashicons-warning {
690 - color: #dfa12e;
691 -}
692 -.wpgetapi-debug .status .desc {
693 - font-size: 14px;
694 - font-weight: 400 !important;
695 -}
696 584 </style>
697 585
698 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
699 587
@@ -698,15 +586,9 @@
698 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
699 587
700 588 <?php if( $testing ) { ?>
701 589
702 - <div class="debug-item status">
703 - <div class="">
704 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
705 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
706 - </div>
707 - </div>
708 - <div class="debug-item">
590 + <div class="debug-item">
709 591 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
710 592 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
711 593 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
712 594 </div>
@@ -713,15 +595,10 @@
713 595
714 596 <div class="debug-item">
715 597 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
716 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>
717 600 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
718 - </div>
719 -
720 - <div class="debug-item">
721 - <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
722 - <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
723 - <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
724 601 </div>
725 602
726 603 <div class="debug-item">
727 604 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>