setup = get_option( 'wpgetapi_setup' ); $this->api = get_option( 'wpgetapi_' . $api_id ); $this->encryption = new WpGetApi_Encryption(); $this->api_id = sanitize_text_field( $api_id ); $this->endpoint_id = sanitize_text_field( $endpoint_id ); $this->keys = $keys; $this->args = $args; add_filter( 'wpgetapi_raw_data', array( $this, 'maybe_add_debug_info' ), 5, 2 ); } /** * Init our api data */ public function init() { // do some checks if( empty( $this->setup ) || ! isset( $this->setup['apis'] ) || empty( $this->setup['apis'] ) ) return 'No API found.'; // do some checks if( ! $this->api || ! isset( $this->api['endpoints'] ) || empty( $this->api['endpoints'] )) return 'API not found.'; // loop through api's // set base_url foreach ( $this->setup['apis'] as $index => $api_item ) { if( $this->api_id == $api_item['id'] ) { $this->base_url = isset( $api_item['base_url'] ) && ! empty( $api_item['base_url'] ) ? esc_url_raw( $api_item['base_url'] ) : ''; } } // loop through endpoints foreach ( $this->api['endpoints'] as $index => $endpoint ) { if( $this->endpoint_id == $endpoint['id'] ) { $this->method = isset( $endpoint['method'] ) && $endpoint['method'] != '' ? sanitize_text_field( $endpoint['method'] ) : 'GET'; $this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : ''; $this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : ''; $this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array(); $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array(); $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array(); $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format; } } $this->timeout = isset( $this->api['timeout'] ) && ! empty( $this->api['timeout'] ) ? $this->api['timeout'] : $this->timeout; $this->sslverify = isset( $this->api['sslverify'] ) ? absint( $this->api['sslverify'] ) : $this->sslverify; } /** * The main function to output the data * * @param string $field Field to retrieve */ public function endpoint_output() { return $this->do_the_call(); } /** * Sort out the parameters if they exist */ public function do_parameters( $parameters ) { $array = array(); $parameters = $this->decrypt($parameters); if( is_array( $parameters ) ) { foreach ( $parameters as $key => $parameter ) { if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) $array[ sanitize_text_field( $parameter['name'] ) ] = sanitize_text_field( $parameter['value'] ); } } return $array; } /** * Send the request and return our data */ public function do_the_call() { $this->init(); // build the URL $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url() ); // build the headers $this->final_headers = apply_filters( 'wpgetapi_final_headers', $this->build_headers() ); // POST request if( $this->method == 'POST' ) $this->response = $this->POST_request(); // GET request if( $this->method == 'GET' ) $this->response = $this->GET_request(); // wp error if( is_wp_error( $this->response ) ) { return json_encode( $this->response ); } $data = wp_remote_retrieve_body( $this->response ); // returning in JSON format if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' ) $data = $this->output_json( $data ); return apply_filters( 'wpgetapi_raw_data', $data, $this ); } /** * return JSON data */ public function output_json( $data ) { // converts all data to arrays, removing objects $data = json_decode( $data, true ); $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys ); if( $this->results_format == 'json_string' ) return json_encode( $data ); if( $this->results_format == 'json_decoded' ) return $data; } /** * build the request url with the ability to add args */ public function build_url() { // make sure our endpoint starts with a slash if ( substr( $this->endpoint, 0, 1 ) != '/' ) $this->endpoint = '/' . $this->endpoint; $url = untrailingslashit( $this->base_url ) . $this->endpoint; $params = empty( $this->query_parameters ) ? array(''=>'') : $this->query_parameters; return add_query_arg( $params, $url ); } /** * build the header with the ability to add args */ public function build_headers() { if( $this->method == 'POST' ) { if( isset( $this->body_parameters ) && $this->body_parameters != '' ) { // checking if the body paramters have a json string foreach ($this->body_parameters as $key => $value ) { if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) { $this->body_parameters[ $key ] = json_decode( $value, true); } } $body = apply_filters( 'wpgetapi_body_parameters', array( 'body' => json_encode( $this->body_parameters ), ) ); $this->header_parameters = wp_parse_args( $body, $this->header_parameters ); } } // build the header $defaults = apply_filters( 'wpgetapi_default_header_parameters', array( 'timeout' => $this->timeout, 'sslverify' => $this->sslverify, 'redirection' => 5, 'blocking' => true, 'cookies' => array(), ) ); return wp_parse_args( $this->header_parameters, $defaults ); } /** * Do a GET request */ public function GET_request() { $response = ''; // so we can so something before the remote_get (caching) $response = apply_filters( 'wpgetapi_before_get_request', $response, $this ); if( empty( $response ) ) $response = wp_remote_get( $this->final_url, $this->final_headers ); return $response; } /** * Do a POST request */ public function POST_request() { $response = wp_remote_post( $this->final_url, $this->final_headers ); return $response; } /** * decrypt our parameters from the DB */ public function decrypt( $array_or_string ) { if( is_string($array_or_string) ){ $array_or_string = $this->encryption->decrypt($array_or_string); }elseif( is_array($array_or_string) ){ foreach ( $array_or_string as $key => &$value ) { if( $key == 'name' ) continue; if ( is_array( $value ) ) { $value = $this->decrypt($value); } else { $value = $this->encryption->decrypt($value); } } } return $array_or_string; } /** * debug */ public function maybe_add_debug_info( $data, $this_data ) { if( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) { ob_start(); ?>
WPGetAPI, Debug Mode - Plugin Version version ); ?>
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.
Final URL